Class: ObStore::FileStore

Inherits:
Object
  • Object
show all
Defined in:
lib/obstore/filestore.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ FileStore

Returns a new instance of FileStore.



16
17
18
19
20
21
22
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object (private)



144
145
146
147
148
149
150
151
152
# 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

Instance Attribute Details

#_storeObject

Returns the value of attribute _store.



14
15
16
# File 'lib/obstore/filestore.rb', line 14

def _store
  @_store
end

Instance Method Details

#atomic_writesObject

returns boolean if atomic writes is active



73
74
75
# File 'lib/obstore/filestore.rb', line 73

def atomic_writes
  @_store.ultra_safe
end

#atomic_writes=(bool) ⇒ Object

sets atomic writes



78
79
80
# File 'lib/obstore/filestore.rb', line 78

def atomic_writes=(bool)
  @_store.ultra_safe = bool
end

#compact!Object

removes stale records from the pstore db



25
26
27
28
29
30
31
32
33
34
# 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) ⇒ Object

fetches saved object for the given key



53
54
55
56
57
58
59
60
61
62
63
# 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

#keysObject

lists all the keys that are currently in the DBs



66
67
68
69
70
# File 'lib/obstore/filestore.rb', line 66

def keys
  @_store.transaction do
    @_store.roots
  end
end

#store(key, value, opts = {}) ⇒ Object

stores data to pstore db



37
38
39
40
41
42
# 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 = {}) ⇒ Object

stores data to pstore db



45
46
47
48
49
50
# 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