Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby18/lib64/ruby/1.8
File: pstore.rb
# = PStore -- Transactional File Storage for Ruby Objects
[0] Fix | Delete
#
[1] Fix | Delete
# pstore.rb -
[2] Fix | Delete
# originally by matz
[3] Fix | Delete
# documentation by Kev Jackson and James Edward Gray II
[4] Fix | Delete
#
[5] Fix | Delete
# See PStore for documentation.
[6] Fix | Delete
[7] Fix | Delete
[8] Fix | Delete
require "fileutils"
[9] Fix | Delete
require "digest/md5"
[10] Fix | Delete
[11] Fix | Delete
#
[12] Fix | Delete
# PStore implements a file based persistence mechanism based on a Hash. User
[13] Fix | Delete
# code can store hierarchies of Ruby objects (values) into the data store file
[14] Fix | Delete
# by name (keys). An object hierarchy may be just a single object. User code
[15] Fix | Delete
# may later read values back from the data store or even update data, as needed.
[16] Fix | Delete
#
[17] Fix | Delete
# The transactional behavior ensures that any changes succeed or fail together.
[18] Fix | Delete
# This can be used to ensure that the data store is not left in a transitory
[19] Fix | Delete
# state, where some values were updated but others were not.
[20] Fix | Delete
#
[21] Fix | Delete
# Behind the scenes, Ruby objects are stored to the data store file with
[22] Fix | Delete
# Marshal. That carries the usual limitations. Proc objects cannot be
[23] Fix | Delete
# marshalled, for example.
[24] Fix | Delete
#
[25] Fix | Delete
# == Usage example:
[26] Fix | Delete
#
[27] Fix | Delete
# require "pstore"
[28] Fix | Delete
#
[29] Fix | Delete
# # a mock wiki object...
[30] Fix | Delete
# class WikiPage
[31] Fix | Delete
# def initialize( page_name, author, contents )
[32] Fix | Delete
# @page_name = page_name
[33] Fix | Delete
# @revisions = Array.new
[34] Fix | Delete
#
[35] Fix | Delete
# add_revision(author, contents)
[36] Fix | Delete
# end
[37] Fix | Delete
#
[38] Fix | Delete
# attr_reader :page_name
[39] Fix | Delete
#
[40] Fix | Delete
# def add_revision( author, contents )
[41] Fix | Delete
# @revisions << { :created => Time.now,
[42] Fix | Delete
# :author => author,
[43] Fix | Delete
# :contents => contents }
[44] Fix | Delete
# end
[45] Fix | Delete
#
[46] Fix | Delete
# def wiki_page_references
[47] Fix | Delete
# [@page_name] + @revisions.last[:contents].scan(/\b(?:[A-Z]+[a-z]+){2,}/)
[48] Fix | Delete
# end
[49] Fix | Delete
#
[50] Fix | Delete
# # ...
[51] Fix | Delete
# end
[52] Fix | Delete
#
[53] Fix | Delete
# # create a new page...
[54] Fix | Delete
# home_page = WikiPage.new( "HomePage", "James Edward Gray II",
[55] Fix | Delete
# "A page about the JoysOfDocumentation..." )
[56] Fix | Delete
#
[57] Fix | Delete
# # then we want to update page data and the index together, or not at all...
[58] Fix | Delete
# wiki = PStore.new("wiki_pages.pstore")
[59] Fix | Delete
# wiki.transaction do # begin transaction; do all of this or none of it
[60] Fix | Delete
# # store page...
[61] Fix | Delete
# wiki[home_page.page_name] = home_page
[62] Fix | Delete
# # ensure that an index has been created...
[63] Fix | Delete
# wiki[:wiki_index] ||= Array.new
[64] Fix | Delete
# # update wiki index...
[65] Fix | Delete
# wiki[:wiki_index].push(*home_page.wiki_page_references)
[66] Fix | Delete
# end # commit changes to wiki data store file
[67] Fix | Delete
#
[68] Fix | Delete
# ### Some time later... ###
[69] Fix | Delete
#
[70] Fix | Delete
# # read wiki data...
[71] Fix | Delete
# wiki.transaction(true) do # begin read-only transaction, no changes allowed
[72] Fix | Delete
# wiki.roots.each do |data_root_name|
[73] Fix | Delete
# p data_root_name
[74] Fix | Delete
# p wiki[data_root_name]
[75] Fix | Delete
# end
[76] Fix | Delete
# end
[77] Fix | Delete
#
[78] Fix | Delete
class PStore
[79] Fix | Delete
binmode = defined?(File::BINARY) ? File::BINARY : 0
[80] Fix | Delete
RDWR_ACCESS = File::RDWR | File::CREAT | binmode
[81] Fix | Delete
RD_ACCESS = File::RDONLY | binmode
[82] Fix | Delete
WR_ACCESS = File::WRONLY | File::CREAT | File::TRUNC | binmode
[83] Fix | Delete
[84] Fix | Delete
# The error type thrown by all PStore methods.
[85] Fix | Delete
class Error < StandardError
[86] Fix | Delete
end
[87] Fix | Delete
[88] Fix | Delete
#
[89] Fix | Delete
# To construct a PStore object, pass in the _file_ path where you would like
[90] Fix | Delete
# the data to be stored.
[91] Fix | Delete
#
[92] Fix | Delete
def initialize(file)
[93] Fix | Delete
dir = File::dirname(file)
[94] Fix | Delete
unless File::directory? dir
[95] Fix | Delete
raise PStore::Error, format("directory %s does not exist", dir)
[96] Fix | Delete
end
[97] Fix | Delete
if File::exist? file and not File::readable? file
[98] Fix | Delete
raise PStore::Error, format("file %s not readable", file)
[99] Fix | Delete
end
[100] Fix | Delete
@transaction = false
[101] Fix | Delete
@filename = file
[102] Fix | Delete
@abort = false
[103] Fix | Delete
end
[104] Fix | Delete
[105] Fix | Delete
# Raises PStore::Error if the calling code is not in a PStore#transaction.
[106] Fix | Delete
def in_transaction
[107] Fix | Delete
raise PStore::Error, "not in transaction" unless @transaction
[108] Fix | Delete
end
[109] Fix | Delete
#
[110] Fix | Delete
# Raises PStore::Error if the calling code is not in a PStore#transaction or
[111] Fix | Delete
# if the code is in a read-only PStore#transaction.
[112] Fix | Delete
#
[113] Fix | Delete
def in_transaction_wr()
[114] Fix | Delete
in_transaction()
[115] Fix | Delete
raise PStore::Error, "in read-only transaction" if @rdonly
[116] Fix | Delete
end
[117] Fix | Delete
private :in_transaction, :in_transaction_wr
[118] Fix | Delete
[119] Fix | Delete
#
[120] Fix | Delete
# Retrieves a value from the PStore file data, by _name_. The hierarchy of
[121] Fix | Delete
# Ruby objects stored under that root _name_ will be returned.
[122] Fix | Delete
#
[123] Fix | Delete
# *WARNING*: This method is only valid in a PStore#transaction. It will
[124] Fix | Delete
# raise PStore::Error if called at any other time.
[125] Fix | Delete
#
[126] Fix | Delete
def [](name)
[127] Fix | Delete
in_transaction
[128] Fix | Delete
@table[name]
[129] Fix | Delete
end
[130] Fix | Delete
#
[131] Fix | Delete
# This method is just like PStore#[], save that you may also provide a
[132] Fix | Delete
# _default_ value for the object. In the event the specified _name_ is not
[133] Fix | Delete
# found in the data store, your _default_ will be returned instead. If you do
[134] Fix | Delete
# not specify a default, PStore::Error will be raised if the object is not
[135] Fix | Delete
# found.
[136] Fix | Delete
#
[137] Fix | Delete
# *WARNING*: This method is only valid in a PStore#transaction. It will
[138] Fix | Delete
# raise PStore::Error if called at any other time.
[139] Fix | Delete
#
[140] Fix | Delete
def fetch(name, default=PStore::Error)
[141] Fix | Delete
in_transaction
[142] Fix | Delete
unless @table.key? name
[143] Fix | Delete
if default==PStore::Error
[144] Fix | Delete
raise PStore::Error, format("undefined root name `%s'", name)
[145] Fix | Delete
else
[146] Fix | Delete
return default
[147] Fix | Delete
end
[148] Fix | Delete
end
[149] Fix | Delete
@table[name]
[150] Fix | Delete
end
[151] Fix | Delete
#
[152] Fix | Delete
# Stores an individual Ruby object or a hierarchy of Ruby objects in the data
[153] Fix | Delete
# store file under the root _name_. Assigning to a _name_ already in the data
[154] Fix | Delete
# store clobbers the old data.
[155] Fix | Delete
#
[156] Fix | Delete
# == Example:
[157] Fix | Delete
#
[158] Fix | Delete
# require "pstore"
[159] Fix | Delete
#
[160] Fix | Delete
# store = PStore.new("data_file.pstore")
[161] Fix | Delete
# store.transaction do # begin transaction
[162] Fix | Delete
# # load some data into the store...
[163] Fix | Delete
# store[:single_object] = "My data..."
[164] Fix | Delete
# store[:obj_heirarchy] = { "Kev Jackson" => ["rational.rb", "pstore.rb"],
[165] Fix | Delete
# "James Gray" => ["erb.rb", "pstore.rb"] }
[166] Fix | Delete
# end # commit changes to data store file
[167] Fix | Delete
#
[168] Fix | Delete
# *WARNING*: This method is only valid in a PStore#transaction and it cannot
[169] Fix | Delete
# be read-only. It will raise PStore::Error if called at any other time.
[170] Fix | Delete
#
[171] Fix | Delete
def []=(name, value)
[172] Fix | Delete
in_transaction_wr()
[173] Fix | Delete
@table[name] = value
[174] Fix | Delete
end
[175] Fix | Delete
#
[176] Fix | Delete
# Removes an object hierarchy from the data store, by _name_.
[177] Fix | Delete
#
[178] Fix | Delete
# *WARNING*: This method is only valid in a PStore#transaction and it cannot
[179] Fix | Delete
# be read-only. It will raise PStore::Error if called at any other time.
[180] Fix | Delete
#
[181] Fix | Delete
def delete(name)
[182] Fix | Delete
in_transaction_wr()
[183] Fix | Delete
@table.delete name
[184] Fix | Delete
end
[185] Fix | Delete
[186] Fix | Delete
#
[187] Fix | Delete
# Returns the names of all object hierarchies currently in the store.
[188] Fix | Delete
#
[189] Fix | Delete
# *WARNING*: This method is only valid in a PStore#transaction. It will
[190] Fix | Delete
# raise PStore::Error if called at any other time.
[191] Fix | Delete
#
[192] Fix | Delete
def roots
[193] Fix | Delete
in_transaction
[194] Fix | Delete
@table.keys
[195] Fix | Delete
end
[196] Fix | Delete
#
[197] Fix | Delete
# Returns true if the supplied _name_ is currently in the data store.
[198] Fix | Delete
#
[199] Fix | Delete
# *WARNING*: This method is only valid in a PStore#transaction. It will
[200] Fix | Delete
# raise PStore::Error if called at any other time.
[201] Fix | Delete
#
[202] Fix | Delete
def root?(name)
[203] Fix | Delete
in_transaction
[204] Fix | Delete
@table.key? name
[205] Fix | Delete
end
[206] Fix | Delete
# Returns the path to the data store file.
[207] Fix | Delete
def path
[208] Fix | Delete
@filename
[209] Fix | Delete
end
[210] Fix | Delete
[211] Fix | Delete
#
[212] Fix | Delete
# Ends the current PStore#transaction, committing any changes to the data
[213] Fix | Delete
# store immediately.
[214] Fix | Delete
#
[215] Fix | Delete
# == Example:
[216] Fix | Delete
#
[217] Fix | Delete
# require "pstore"
[218] Fix | Delete
#
[219] Fix | Delete
# store = PStore.new("data_file.pstore")
[220] Fix | Delete
# store.transaction do # begin transaction
[221] Fix | Delete
# # load some data into the store...
[222] Fix | Delete
# store[:one] = 1
[223] Fix | Delete
# store[:two] = 2
[224] Fix | Delete
#
[225] Fix | Delete
# store.commit # end transaction here, committing changes
[226] Fix | Delete
#
[227] Fix | Delete
# store[:three] = 3 # this change is never reached
[228] Fix | Delete
# end
[229] Fix | Delete
#
[230] Fix | Delete
# *WARNING*: This method is only valid in a PStore#transaction. It will
[231] Fix | Delete
# raise PStore::Error if called at any other time.
[232] Fix | Delete
#
[233] Fix | Delete
def commit
[234] Fix | Delete
in_transaction
[235] Fix | Delete
@abort = false
[236] Fix | Delete
throw :pstore_abort_transaction
[237] Fix | Delete
end
[238] Fix | Delete
#
[239] Fix | Delete
# Ends the current PStore#transaction, discarding any changes to the data
[240] Fix | Delete
# store.
[241] Fix | Delete
#
[242] Fix | Delete
# == Example:
[243] Fix | Delete
#
[244] Fix | Delete
# require "pstore"
[245] Fix | Delete
#
[246] Fix | Delete
# store = PStore.new("data_file.pstore")
[247] Fix | Delete
# store.transaction do # begin transaction
[248] Fix | Delete
# store[:one] = 1 # this change is not applied, see below...
[249] Fix | Delete
# store[:two] = 2 # this change is not applied, see below...
[250] Fix | Delete
#
[251] Fix | Delete
# store.abort # end transaction here, discard all changes
[252] Fix | Delete
#
[253] Fix | Delete
# store[:three] = 3 # this change is never reached
[254] Fix | Delete
# end
[255] Fix | Delete
#
[256] Fix | Delete
# *WARNING*: This method is only valid in a PStore#transaction. It will
[257] Fix | Delete
# raise PStore::Error if called at any other time.
[258] Fix | Delete
#
[259] Fix | Delete
def abort
[260] Fix | Delete
in_transaction
[261] Fix | Delete
@abort = true
[262] Fix | Delete
throw :pstore_abort_transaction
[263] Fix | Delete
end
[264] Fix | Delete
[265] Fix | Delete
#
[266] Fix | Delete
# Opens a new transaction for the data store. Code executed inside a block
[267] Fix | Delete
# passed to this method may read and write data to and from the data store
[268] Fix | Delete
# file.
[269] Fix | Delete
#
[270] Fix | Delete
# At the end of the block, changes are committed to the data store
[271] Fix | Delete
# automatically. You may exit the transaction early with a call to either
[272] Fix | Delete
# PStore#commit or PStore#abort. See those methods for details about how
[273] Fix | Delete
# changes are handled. Raising an uncaught Exception in the block is
[274] Fix | Delete
# equivalent to calling PStore#abort.
[275] Fix | Delete
#
[276] Fix | Delete
# If _read_only_ is set to +true+, you will only be allowed to read from the
[277] Fix | Delete
# data store during the transaction and any attempts to change the data will
[278] Fix | Delete
# raise a PStore::Error.
[279] Fix | Delete
#
[280] Fix | Delete
# Note that PStore does not support nested transactions.
[281] Fix | Delete
#
[282] Fix | Delete
def transaction(read_only=false) # :yields: pstore
[283] Fix | Delete
raise PStore::Error, "nested transaction" if @transaction
[284] Fix | Delete
begin
[285] Fix | Delete
@rdonly = read_only
[286] Fix | Delete
@abort = false
[287] Fix | Delete
@transaction = true
[288] Fix | Delete
value = nil
[289] Fix | Delete
new_file = @filename + ".new"
[290] Fix | Delete
[291] Fix | Delete
content = nil
[292] Fix | Delete
unless read_only
[293] Fix | Delete
file = File.open(@filename, RDWR_ACCESS)
[294] Fix | Delete
file.flock(File::LOCK_EX)
[295] Fix | Delete
commit_new(file) if FileTest.exist?(new_file)
[296] Fix | Delete
content = file.read()
[297] Fix | Delete
else
[298] Fix | Delete
begin
[299] Fix | Delete
file = File.open(@filename, RD_ACCESS)
[300] Fix | Delete
file.flock(File::LOCK_SH)
[301] Fix | Delete
content = (File.open(new_file, RD_ACCESS) {|n| n.read} rescue file.read())
[302] Fix | Delete
rescue Errno::ENOENT
[303] Fix | Delete
content = ""
[304] Fix | Delete
end
[305] Fix | Delete
end
[306] Fix | Delete
[307] Fix | Delete
if content != ""
[308] Fix | Delete
@table = load(content)
[309] Fix | Delete
if !read_only
[310] Fix | Delete
size = content.size
[311] Fix | Delete
md5 = Digest::MD5.digest(content)
[312] Fix | Delete
end
[313] Fix | Delete
else
[314] Fix | Delete
@table = {}
[315] Fix | Delete
end
[316] Fix | Delete
content = nil # unreference huge data
[317] Fix | Delete
[318] Fix | Delete
begin
[319] Fix | Delete
catch(:pstore_abort_transaction) do
[320] Fix | Delete
value = yield(self)
[321] Fix | Delete
end
[322] Fix | Delete
rescue Exception
[323] Fix | Delete
@abort = true
[324] Fix | Delete
raise
[325] Fix | Delete
ensure
[326] Fix | Delete
if !read_only and !@abort
[327] Fix | Delete
tmp_file = @filename + ".tmp"
[328] Fix | Delete
content = dump(@table)
[329] Fix | Delete
if !md5 || size != content.size || md5 != Digest::MD5.digest(content)
[330] Fix | Delete
File.open(tmp_file, WR_ACCESS) {|t| t.write(content)}
[331] Fix | Delete
File.rename(tmp_file, new_file)
[332] Fix | Delete
commit_new(file)
[333] Fix | Delete
end
[334] Fix | Delete
content = nil # unreference huge data
[335] Fix | Delete
end
[336] Fix | Delete
end
[337] Fix | Delete
ensure
[338] Fix | Delete
@table = nil
[339] Fix | Delete
@transaction = false
[340] Fix | Delete
file.close if file
[341] Fix | Delete
end
[342] Fix | Delete
value
[343] Fix | Delete
end
[344] Fix | Delete
[345] Fix | Delete
# This method is just a wrapped around Marshal.dump.
[346] Fix | Delete
def dump(table) # :nodoc:
[347] Fix | Delete
Marshal::dump(table)
[348] Fix | Delete
end
[349] Fix | Delete
[350] Fix | Delete
# This method is just a wrapped around Marshal.load.
[351] Fix | Delete
def load(content) # :nodoc:
[352] Fix | Delete
Marshal::load(content)
[353] Fix | Delete
end
[354] Fix | Delete
[355] Fix | Delete
# This method is just a wrapped around Marshal.load.
[356] Fix | Delete
def load_file(file) # :nodoc:
[357] Fix | Delete
Marshal::load(file)
[358] Fix | Delete
end
[359] Fix | Delete
[360] Fix | Delete
private
[361] Fix | Delete
# Commits changes to the data store file.
[362] Fix | Delete
def commit_new(f)
[363] Fix | Delete
f.truncate(0)
[364] Fix | Delete
f.rewind
[365] Fix | Delete
new_file = @filename + ".new"
[366] Fix | Delete
File.open(new_file, RD_ACCESS) do |nf|
[367] Fix | Delete
FileUtils.copy_stream(nf, f)
[368] Fix | Delete
end
[369] Fix | Delete
File.unlink(new_file)
[370] Fix | Delete
end
[371] Fix | Delete
end
[372] Fix | Delete
[373] Fix | Delete
# :enddoc:
[374] Fix | Delete
[375] Fix | Delete
if __FILE__ == $0
[376] Fix | Delete
db = PStore.new("/tmp/foo")
[377] Fix | Delete
db.transaction do
[378] Fix | Delete
p db.roots
[379] Fix | Delete
ary = db["root"] = [1,2,3,4]
[380] Fix | Delete
ary[1] = [1,1.5]
[381] Fix | Delete
end
[382] Fix | Delete
[383] Fix | Delete
1000.times do
[384] Fix | Delete
db.transaction do
[385] Fix | Delete
db["root"][0] += 1
[386] Fix | Delete
p db["root"][0]
[387] Fix | Delete
end
[388] Fix | Delete
end
[389] Fix | Delete
[390] Fix | Delete
db.transaction(true) do
[391] Fix | Delete
p db["root"]
[392] Fix | Delete
end
[393] Fix | Delete
end
[394] Fix | Delete
[395] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function