Class: ODB::DataStore

Inherits:
Object show all
Defined in:
lib/odb.rb

Direct Known Subclasses

FileStore, HashStore

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ DataStore

Returns a new instance of DataStore.



34
35
36
37
38
# File 'lib/odb.rb', line 34

def initialize(name)
  @name = name
  @key_cache = {}
  @object_map = {}
end

Instance Attribute Details

#dbObject

Returns the value of attribute db.



32
33
34
# File 'lib/odb.rb', line 32

def db
  @db
end

#key_cacheObject (readonly)

Returns the value of attribute key_cache.



31
32
33
# File 'lib/odb.rb', line 31

def key_cache
  @key_cache
end

#nameObject (readonly)

Returns the value of attribute name.



31
32
33
# File 'lib/odb.rb', line 31

def name
  @name
end

Instance Method Details

#[](key) ⇒ Object



61
# File 'lib/odb.rb', line 61

def [](key) read(key) end

#[]=(key, value) ⇒ Object



62
# File 'lib/odb.rb', line 62

def []=(key, value) write(key, value) end

#after_commitObject



64
# File 'lib/odb.rb', line 64

def after_commit; end

#read(key) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/odb.rb', line 40

def read(key)
  key = key_cache[key] if Symbol === key
  if oid = @object_map[key]
    ObjectSpace._id2ref(oid)
  else
    obj = read_object(key)
    @object_map[key] = obj.object_id
    obj
  end
end

#write(key, value) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/odb.rb', line 51

def write(key, value)
  if Symbol === key
    key = (key_cache[key] = value.object_id) 
    @object_map[key] = value.object_id
    return
  end
  @object_map[key] = value.object_id
  write_object(key, value)
end