The XmlStore class manages access to all object storage.
db
is a handle to the database implementation (in this iteration a hash).
dbtop
stores the highest id used in the database.
# File lib/storage/xmlstore.rb, line 28 def initialize(dbfile) super() @dbfile = "#{dbfile}.xml" # check if database exists and build it if not build_database log.info "Loading world..." @db = {} # load the yaml database and sets @dbtop to highest object id File.open(@dbfile) do |f| XMLRPC::Marshal.load(f.read).each do |o| @dbtop = o.id if o.id > @dbtop @db[o.id]=o end end log.info "Database '#{@dbfile}' loaded...highest id = #{@dbtop}." # log.debug @db.inspect rescue log.fatal "Error loading database" log.fatal $! raise end
Check if an object is in the database by its id.
oid
is the id to use in the search.
return
true or false
# File lib/storage/xmlstore.rb, line 86 def check(oid) @db.has_key? oid end
Deletes an object from the database.
oid
is the id to to be deleted.
return
Undefined.
# File lib/storage/xmlstore.rb, line 72 def delete(oid) @db.delete(oid) end
Iterate through all objects
yield
Each object in database to block of caller.
# File lib/storage/xmlstore.rb, line 92 def each(&blk) @db.each_value &blk end
Finds an object in the database by its id.
oid
is the id to use in the search.
return
Handle to the object or nil.
# File lib/storage/xmlstore.rb, line 79 def get(oid) @db[oid] end
Adds a new object to the database.
obj
is a reference to object to be added
return
Undefined.
# File lib/storage/xmlstore.rb, line 64 def put(obj) @db[obj.id] = obj obj # return really ought not be checked end
Save the world
return
Undefined.
# File lib/storage/xmlstore.rb, line 55 def save File.open(@dbfile,'w') do |f| f.write(XMLRPC::Marshal.dump(@db.values)) end end