Class: Archipelago::Hashish::BerkeleyHashish
- Inherits:
-
Object
- Object
- Archipelago::Hashish::BerkeleyHashish
- Includes:
- Current::Synchronized
- Defined in:
- lib/archipelago/hashish.rb
Overview
In essence a Berkeley Database backed Hash.
Will cache all values having been written or read in a normal Hash cache for fast access.
Will save the last update timestamp for all keys in a separate Hash cache AND a separate Berkeley Database.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Simply get the value for the
key. -
#[]=(key, value) ⇒ Object
Insert
valueunderkey. -
#close! ⇒ Object
Close the @content_db and @timestamps_db behind this BerkeleyHashish.
-
#delete(key) ⇒ Object
Delete
keyand its value and timestamp. -
#each(callable) ⇒ Object
Will do
callable.call(key, value) for each key-and-value pair in this Hashish. -
#get_deep_clone(key) ⇒ Object
Returns a deep ( Marshal.load(Marshal.dump(o)) ) clone of the object represented by
key. -
#include?(key) ⇒ Boolean
Returns true if this BerkeleyHashish include
key. -
#initialize(name, env) ⇒ BerkeleyHashish
constructor
Initialize an instance with the
nameand BDB::Envenv. -
#store_if_changed(key) ⇒ Object
Stores whatever is under
keyif it is not the same as whats in the persistent db - if thekeyactually has a representation in the db. -
#timestamp(key) ⇒ Object
Returns the last time the value under
keywas changed.
Methods included from Current::Synchronized
#lock_on, #mon_check_owner, #synchronize_on, #unlock_on
Constructor Details
#initialize(name, env) ⇒ BerkeleyHashish
Initialize an instance with the name and BDB::Env env.
42 43 44 45 46 47 48 49 |
# File 'lib/archipelago/hashish.rb', line 42 def initialize(name, env) super() @content_db = env.open_db(BDB::HASH, name, "content", BDB::CREATE) @content = {} @timestamps_db = env.open_db(BDB::HASH, name, "timestamps", BDB::CREATE | BDB::NOMMAP) @timestamps = {} @lock = Archipelago::Current::Lock.new end |
Instance Method Details
#[](key) ⇒ Object
Simply get the value for the key.
Will call value.load_hook and send it a block that does the actuall insertion of the value into the live hash if value.respond_to?(:load_hook) if the value didnt exist in the live hash yet.
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/archipelago/hashish.rb', line 78 def [](key) @lock.synchronize_on(key) do value = @content[key] return value if value return get_from_db(key) end end |
#[]=(key, value) ⇒ Object
Insert value under key.
Will call value.save_hook(old_value) and send it a block that does the actual saving if value.respond_to?(:save_hook).
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/archipelago/hashish.rb', line 95 def []=(key, value) @lock.synchronize_on(key) do @content[key] = value write_to_db(key, Marshal.dump(key), Marshal.dump(value), value) return value end end |
#close! ⇒ Object
Close the @content_db and @timestamps_db behind this BerkeleyHashish
53 54 55 56 |
# File 'lib/archipelago/hashish.rb', line 53 def close! @content_db.close @timestamps_db.close end |
#delete(key) ⇒ Object
Delete key and its value and timestamp.
177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/archipelago/hashish.rb', line 177 def delete(key) @lock.synchronize_on(key) do serialized_key = Marshal.dump(key) @content.delete(key) @content_db[serialized_key] = nil @timestamps.delete(key) @timestamps_db[serialized_key] = nil end end |
#each(callable) ⇒ Object
Will do callable.call(key, value) for each key-and-value pair in this Hashish.
NB: This is totaly thread-unsafe, only do this for management or rescue!
168 169 170 171 172 173 |
# File 'lib/archipelago/hashish.rb', line 168 def each(callable) @content_db.each do |serialized_key, serialized_value| key = Marshal.load(serialized_key) callable.call(key, self.[](key)) end end |
#get_deep_clone(key) ⇒ Object
Returns a deep ( Marshal.load(Marshal.dump(o)) ) clone of the object represented by key.
61 62 63 |
# File 'lib/archipelago/hashish.rb', line 61 def get_deep_clone(key) return Marshal.load(@content_db[Marshal.dump(key)]) end |
#include?(key) ⇒ Boolean
Returns true if this BerkeleyHashish include key.
67 68 69 |
# File 'lib/archipelago/hashish.rb', line 67 def include?(key) @content.include?(key) || !@content_db[Marshal.dump(key)].nil? end |
#store_if_changed(key) ⇒ Object
Stores whatever is under key if it is not the same as whats in the persistent db - if the key actually has a representation in the db.
Will call value.save_hook(old_value) and send it a block that does the actual saving if value.respond_to?(:save_hook) and a save is actually performed.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/archipelago/hashish.rb', line 116 def store_if_changed(key) @lock.synchronize_on(key) do value = @content[key] if value.respond_to?(:dirty?) if value.dirty? serialized_value = Marshal.dump(value) serialized_key = Marshal.dump(key) write_to_db(key, serialized_key, serialized_value, value) value.is_clean! if value.respond_to?(:is_clean!) end else serialized_value = Marshal.dump(value) serialized_key = Marshal.dump(key) old_serialized_value = @content_db[serialized_key] write_to_db(key, serialized_key, serialized_value, value) if old_serialized_value && old_serialized_value != serialized_value end end end |
#timestamp(key) ⇒ Object
Returns the last time the value under key was changed.
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/archipelago/hashish.rb', line 145 def (key) @lock.synchronize_on(key) do = @timestamps[key] return if serialized_key = Marshal.dump(key) = @timestamps_db[serialized_key] return nil unless = Marshal.load() @timestamps[key] = return end end |