class SqliteStore

The SqliteStore class manages access to all object storage.

db

is a handle to the database.

dbtop

stores the highest id used in the database.

cache

is a handle to the cache

Public Class Methods

new(dbfile) click to toggle source
# File lib/storage/sqlitestore.rb, line 31
  def initialize(dbfile)
    super()
    @dbfile = "#{dbfile}.sqlite"

    # check if database exists and build it if not
    build_database
    log.info "Loading world..."

    # open database and sets @dbtop to highest object id
    @db = SQLite::Database.open(@dbfile)
    @db.cache_size = 1000;     # default is 2000 1.5K pages
    @db.synchronous = 0;       # dangerous if OS crash, but fast
    @db.type_translation = true;
    @db.execute("select id from tmud;") do |i|
      @dbtop = i.first if i.first > @dbtop
    end

    @cache = CacheManager.new(@db)
    log.info "Database '#{@dbfile}' loaded...highest id = #{@dbtop}."
#    log.debug @db.inspect
  rescue
    log.fatal $!
    raise
  end

Public Instance Methods

check(oid) click to toggle source

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/sqlitestore.rb, line 99
def check(oid)
  @db.has_key? oid.to_s
end
close() click to toggle source

Close the database

return

Undefined.

# File lib/storage/sqlitestore.rb, line 64
def close
  @db.close
end
delete(oid) click to toggle source

Deletes an object from the database.

oid

is the id to to be deleted.

return

Undefined.

# File lib/storage/sqlitestore.rb, line 85
def delete(oid)
  @cache.delete(oid)
end
each() { |get| ... } click to toggle source

Iterate through all objects This needs to have all the possible keys first. So we need to fetch them from the cache and from the database and and produce a list of unique keys.

yield

Each object in database to block of caller.

# File lib/storage/sqlitestore.rb, line 129
def each
  kys = @cache.keys
  @db.execute("select id from tmud;") do |k|
    kys << k.to_i
  end
  kys.uniq!
  kys.each {|k| yield @cache.get(k)}
end
get(oid) click to toggle source

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/sqlitestore.rb, line 92
def get(oid)
  @cache.get(oid)
end
inspect() click to toggle source

inspect the store cache (only for caches)

return

string

# File lib/storage/sqlitestore.rb, line 70
def inspect
  @cache.inspect
end
makenoswap(oid) click to toggle source

Marks an object nonswappable

oid

is the object id

return

undefined

# File lib/storage/sqlitestore.rb, line 113
def makenoswap(oid)
  @cache.makenoswap(oid)
end
makeswap(oid) click to toggle source

Marks an object swappable

oid

is the object id

return

undefined

# File lib/storage/sqlitestore.rb, line 120
def makeswap(oid)
  @cache.makeswap(oid)
end
mark(oid) click to toggle source

Marks an object dirty

oid

is the id to use in the search.

return

undefined

# File lib/storage/sqlitestore.rb, line 106
def mark(oid)
  @cache.mark(oid)
end
put(obj) click to toggle source

Adds a new object to the database.

obj

is a reference to object to be added

return

Undefined.

# File lib/storage/sqlitestore.rb, line 77
def put(obj)
  @cache.put(obj)
  obj # return really should not be checked
end
save() click to toggle source

Save the world

return

Undefined.

# File lib/storage/sqlitestore.rb, line 58
def save
  @cache.sync
end
stats() click to toggle source

produces a statistical report of the database

return

a string containing the report

# File lib/storage/sqlitestore.rb, line 140
def stats
  stats = super
  stats << @cache.stats
end