diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-03-06 03:56:38 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-03-06 03:56:38 +0000 |
commit | 287a34ae0dfc23e4158f67cb7783d239f202c368 (patch) | |
tree | 5e35d5b41aae961b37cf6632f60c42f51c7aa775 /lib/pstore.rb | |
parent | 9b52ae2e6491bb5d6c59e1799449f6268baf6f89 (diff) | |
download | ruby-287a34ae0dfc23e4158f67cb7783d239f202c368.tar.gz |
* {ext,lib,test}/**/*.rb: removed trailing spaces.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22784 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/pstore.rb')
-rw-r--r-- | lib/pstore.rb | 134 |
1 files changed, 67 insertions, 67 deletions
diff --git a/lib/pstore.rb b/lib/pstore.rb index fdc518eaec..7b7080338e 100644 --- a/lib/pstore.rb +++ b/lib/pstore.rb @@ -15,49 +15,49 @@ require "thread" # # PStore implements a file based persistence mechanism based on a Hash. User # code can store hierarchies of Ruby objects (values) into the data store file -# by name (keys). An object hierarchy may be just a single object. User code +# by name (keys). An object hierarchy may be just a single object. User code # may later read values back from the data store or even update data, as needed. -# +# # The transactional behavior ensures that any changes succeed or fail together. # This can be used to ensure that the data store is not left in a transitory # state, where some values were updated but others were not. -# -# Behind the scenes, Ruby objects are stored to the data store file with -# Marshal. That carries the usual limitations. Proc objects cannot be +# +# Behind the scenes, Ruby objects are stored to the data store file with +# Marshal. That carries the usual limitations. Proc objects cannot be # marshalled, for example. # # == Usage example: -# +# # require "pstore" -# +# # # a mock wiki object... # class WikiPage # def initialize( page_name, author, contents ) # @page_name = page_name # @revisions = Array.new -# +# # add_revision(author, contents) # end -# +# # attr_reader :page_name -# +# # def add_revision( author, contents ) # @revisions << { :created => Time.now, # :author => author, # :contents => contents } # end -# +# # def wiki_page_references # [@page_name] + @revisions.last[:contents].scan(/\b(?:[A-Z]+[a-z]+){2,}/) # end -# +# # # ... # end -# +# # # create a new page... # home_page = WikiPage.new( "HomePage", "James Edward Gray II", # "A page about the JoysOfDocumentation..." ) -# +# # # then we want to update page data and the index together, or not at all... # wiki = PStore.new("wiki_pages.pstore") # wiki.transaction do # begin transaction; do all of this or none of it @@ -68,9 +68,9 @@ require "thread" # # update wiki index... # wiki[:wiki_index].push(*home_page.wiki_page_references) # end # commit changes to wiki data store file -# +# # ### Some time later... ### -# +# # # read wiki data... # wiki.transaction(true) do # begin read-only transaction, no changes allowed # wiki.roots.each do |data_root_name| @@ -102,7 +102,7 @@ class PStore # The error type thrown by all PStore methods. class Error < StandardError end - + # Whether PStore should do its best to prevent file corruptions, even when under # unlikely-to-occur error conditions such as out-of-space conditions and other # unusual OS filesystem errors. Setting this flag comes at the price in the form @@ -112,13 +112,13 @@ class PStore # all POSIX platforms: Linux, MacOS X, FreeBSD, etc). The default value is false. attr_accessor :ultra_safe - # - # To construct a PStore object, pass in the _file_ path where you would like + # + # To construct a PStore object, pass in the _file_ path where you would like # the data to be stored. # # PStore objects are always reentrant. But if _thread_safe_ is set to true, # then it will become thread-safe at the cost of a minor performance hit. - # + # def initialize(file, thread_safe = false) dir = File::dirname(file) unless File::directory? dir @@ -142,10 +142,10 @@ class PStore def in_transaction raise PStore::Error, "not in transaction" unless @transaction end - # + # # Raises PStore::Error if the calling code is not in a PStore#transaction or # if the code is in a read-only PStore#transaction. - # + # def in_transaction_wr() in_transaction() raise PStore::Error, "in read-only transaction" if @rdonly @@ -153,9 +153,9 @@ class PStore private :in_transaction, :in_transaction_wr # - # Retrieves a value from the PStore file data, by _name_. The hierarchy of + # Retrieves a value from the PStore file data, by _name_. The hierarchy of # Ruby objects stored under that root _name_ will be returned. - # + # # *WARNING*: This method is only valid in a PStore#transaction. It will # raise PStore::Error if called at any other time. # @@ -164,12 +164,12 @@ class PStore @table[name] end # - # This method is just like PStore#[], save that you may also provide a - # _default_ value for the object. In the event the specified _name_ is not - # found in the data store, your _default_ will be returned instead. If you do - # not specify a default, PStore::Error will be raised if the object is not + # This method is just like PStore#[], save that you may also provide a + # _default_ value for the object. In the event the specified _name_ is not + # found in the data store, your _default_ will be returned instead. If you do + # not specify a default, PStore::Error will be raised if the object is not # found. - # + # # *WARNING*: This method is only valid in a PStore#transaction. It will # raise PStore::Error if called at any other time. # @@ -188,11 +188,11 @@ class PStore # Stores an individual Ruby object or a hierarchy of Ruby objects in the data # store file under the root _name_. Assigning to a _name_ already in the data # store clobbers the old data. - # + # # == Example: - # + # # require "pstore" - # + # # store = PStore.new("data_file.pstore") # store.transaction do # begin transaction # # load some data into the store... @@ -200,7 +200,7 @@ class PStore # store[:obj_heirarchy] = { "Kev Jackson" => ["rational.rb", "pstore.rb"], # "James Gray" => ["erb.rb", "pstore.rb"] } # end # commit changes to data store file - # + # # *WARNING*: This method is only valid in a PStore#transaction and it cannot # be read-only. It will raise PStore::Error if called at any other time. # @@ -210,7 +210,7 @@ class PStore end # # Removes an object hierarchy from the data store, by _name_. - # + # # *WARNING*: This method is only valid in a PStore#transaction and it cannot # be read-only. It will raise PStore::Error if called at any other time. # @@ -221,7 +221,7 @@ class PStore # # Returns the names of all object hierarchies currently in the store. - # + # # *WARNING*: This method is only valid in a PStore#transaction. It will # raise PStore::Error if called at any other time. # @@ -231,7 +231,7 @@ class PStore end # # Returns true if the supplied _name_ is currently in the data store. - # + # # *WARNING*: This method is only valid in a PStore#transaction. It will # raise PStore::Error if called at any other time. # @@ -247,22 +247,22 @@ class PStore # # Ends the current PStore#transaction, committing any changes to the data # store immediately. - # + # # == Example: - # + # # require "pstore" - # + # # store = PStore.new("data_file.pstore") # store.transaction do # begin transaction # # load some data into the store... # store[:one] = 1 # store[:two] = 2 - # + # # store.commit # end transaction here, committing changes - # + # # store[:three] = 3 # this change is never reached # end - # + # # *WARNING*: This method is only valid in a PStore#transaction. It will # raise PStore::Error if called at any other time. # @@ -274,21 +274,21 @@ class PStore # # Ends the current PStore#transaction, discarding any changes to the data # store. - # + # # == Example: - # + # # require "pstore" - # + # # store = PStore.new("data_file.pstore") # store.transaction do # begin transaction # store[:one] = 1 # this change is not applied, see below... # store[:two] = 2 # this change is not applied, see below... - # + # # store.abort # end transaction here, discard all changes - # + # # store[:three] = 3 # this change is never reached # end - # + # # *WARNING*: This method is only valid in a PStore#transaction. It will # raise PStore::Error if called at any other time. # @@ -300,19 +300,19 @@ class PStore # # Opens a new transaction for the data store. Code executed inside a block - # passed to this method may read and write data to and from the data store + # passed to this method may read and write data to and from the data store # file. - # + # # At the end of the block, changes are committed to the data store - # automatically. You may exit the transaction early with a call to either + # automatically. You may exit the transaction early with a call to either # PStore#commit or PStore#abort. See those methods for details about how - # changes are handled. Raising an uncaught Exception in the block is + # changes are handled. Raising an uncaught Exception in the block is # equivalent to calling PStore#abort. - # + # # If _read_only_ is set to +true+, you will only be allowed to read from the # data store during the transaction and any attempts to change the data will # raise a PStore::Error. - # + # # Note that PStore does not support nested transactions. # def transaction(read_only = false, &block) # :yields: pstore @@ -326,11 +326,11 @@ class PStore if file begin @table, checksum, original_data_size = load_data(file, read_only) - + catch(:pstore_abort_transaction) do value = yield(self) end - + if !@abort && !read_only save_data(checksum, original_data_size, file) end @@ -349,19 +349,19 @@ class PStore ensure @transaction = false end - + private # Constant for relieving Ruby's garbage collector. EMPTY_STRING = "" EMPTY_MARSHAL_DATA = Marshal.dump({}) EMPTY_MARSHAL_CHECKSUM = Digest::MD5.digest(EMPTY_MARSHAL_DATA) - + class DummyMutex def synchronize yield end end - + # # Open the specified filename (either in read-only mode or in # read-write mode) and lock it for reading or writing. @@ -391,7 +391,7 @@ class PStore return file end end - + # Load the given PStore file. # If +read_only+ is true, the unmarshalled Hash will be returned. # If +read_only+ is false, a 3-tuple will be returned: the unmarshalled @@ -427,7 +427,7 @@ class PStore [table, checksum, size] end end - + def on_windows? is_windows = RUBY_PLATFORM =~ /mswin/ || RUBY_PLATFORM =~ /mingw/ || @@ -438,7 +438,7 @@ class PStore end is_windows end - + # Check whether Marshal.dump supports the 'canonical' option. This option # makes sure that Marshal.dump always dumps data structures in the same order. # This is important because otherwise, the checksums that we generate may differ. @@ -454,7 +454,7 @@ class PStore end result end - + def save_data(original_checksum, original_file_size, file) # We only want to save the new data if the size or checksum has changed. # This results in less filesystem calls, which is good for performance. @@ -464,7 +464,7 @@ class PStore new_data = dump(@table) end new_checksum = Digest::MD5.digest(new_data) - + if new_data.size != original_file_size || new_checksum != original_checksum if @ultra_safe && !on_windows? # Windows doesn't support atomic file renames. @@ -473,10 +473,10 @@ class PStore save_data_with_fast_strategy(new_data, file) end end - + new_data.replace(EMPTY_STRING) end - + def save_data_with_atomic_file_rename_strategy(data, file) temp_filename = "#{@filename}.tmp.#{Process.pid}.#{rand 1000000}" temp_file = File.new(temp_filename, WR_ACCESS) @@ -492,7 +492,7 @@ class PStore temp_file.close end end - + def save_data_with_fast_strategy(data, file) file.rewind file.truncate(0) |