class ObStore::FileStore

Attributes

_store[RW]

Public Class Methods

new(opts={}) click to toggle source
# File lib/obstore/filestore.rb, line 16
def initialize(opts={})
  opts[:database] ||= "./tmp/obstore.db"
  opts[:threadsafe] ||= true
  opts[:atomic_writes] ||= false
  @_store = PStore.new(opts[:database], opts[:threadsafe])
  @_store.ultra_safe = opts[:atomic_writes]
end

Public Instance Methods

atomic_writes() click to toggle source

returns boolean if atomic writes is active

# File lib/obstore/filestore.rb, line 73
def atomic_writes
  @_store.ultra_safe
end
atomic_writes=(bool) click to toggle source

sets atomic writes

# File lib/obstore/filestore.rb, line 78
def atomic_writes=(bool)
  @_store.ultra_safe = bool
end
compact!() click to toggle source

removes stale records from the pstore db

# File lib/obstore/filestore.rb, line 25
def compact!
  keys = []
  @_store.transaction do
    keys = @_store.roots
  end
  keys.each do |key|
    fetch_data_by_key key.to_sym # just fetching the stale items deletes them
  end
  return true
end
fetch(key) click to toggle source

fetches saved object for the given key

# File lib/obstore/filestore.rb, line 53
def fetch(key)
  if key.class != Symbol
    raise TypeError "key must be of type symbol"
  end
  data = fetch_data_by_key(key)
  if data.nil?
    return nil
  else
    return data.fetch
  end
end
keys() click to toggle source

lists all the keys that are currently in the DBs

# File lib/obstore/filestore.rb, line 66
def keys
  @_store.transaction do
    @_store.roots
  end
end
store(key, value, opts={}) click to toggle source

stores data to pstore db

# File lib/obstore/filestore.rb, line 37
def store(key, value, opts={})
  if key.class != Symbol
    raise TypeError "key must be of type symbol"
  end
  store_data_by_key key, value, opts
end
store!(key, value, opts={}) click to toggle source

stores data to pstore db

# File lib/obstore/filestore.rb, line 45
def store!(key, value, opts={})
  if key.class != Symbol
    raise TypeError "key must be of type symbol"
  end
  return true if store_data_by_key key, value, opts
end

Private Instance Methods

fetch_data_by_key(key) click to toggle source

method used by method_missing to fetch data

# File lib/obstore/filestore.rb, line 129
def fetch_data_by_key(key)
  @_store.transaction do
    data = unmarshal(@_store[key.to_sym])
    if data.nil?
      return data
    end
    if data.stale?
      data = nil
      @_store.delete key.to_sym
      @_store.commit
    end
    return data
  end
end
marshal(obj) click to toggle source

marshals the data object to a string

# File lib/obstore/filestore.rb, line 85
def marshal(obj)
  YAML.dump obj
end
method_missing(meth, *args, &block) click to toggle source
Calls superclass method
# File lib/obstore/filestore.rb, line 144
def method_missing(meth, *args, &block)
  if meth.to_s =~ /^(.+)=$/
    store_obj_by_key($1, *args)
  elsif meth.to_s =~ /^(.+)$/
    fetch_data_by_key($1)
  else
    super
  end
end
store_data_by_key(key, *args) click to toggle source

internal method used for storing data by key

# File lib/obstore/filestore.rb, line 98
def store_data_by_key(key, *args)
  options = {}
  if args.class == Array
    value = args.shift
    options = args.shift
  else
    value = args
  end
  @_store.transaction do
    if value.class == ObStore::Data
      @_store[key.to_sym] = marshal value
    elsif value.nil?
      @_store.delete key.to_sym
    else
      @_store[key.to_sym] = marshal ObStore::Data.new(value, options)
    end
    @_store.commit
  end
  return value
end
store_obj_by_key(key, args) click to toggle source
# File lib/obstore/filestore.rb, line 119
def store_obj_by_key(key, args)
  if args.class != ObStore::Data
    unless args.class == NilClass
      raise TypeError "data must be of type ObStore::Data"
    end
  end
  store_data_by_key key, args
end
unmarshal(str) click to toggle source

un-marshals the passed string into an object

# File lib/obstore/filestore.rb, line 90
def unmarshal(str)
  if str.nil?
    return nil
  end
  YAML.load str
end