Class: Daybreak::DB
Overview
Daybreak::DB contains the public api for Daybreak. It includes Enumerable for functional goodies like map, each, reduce and friends.
Instance Attribute Summary collapse
-
#default(key = nil) ⇒ Object
Return default value belonging to key.
Instance Method Summary collapse
-
#[](key) ⇒ Object
(also: #get)
Retrieve a value at key from the database.
-
#[]=(key, value) ⇒ Object
(also: #set)
Set a key in the database to be written at some future date.
-
#bytesize ⇒ Fixnum
Utility method that will return the size of the database in bytes, useful for determining when to compact.
-
#clear ⇒ DB
Remove all keys and values from the database.
-
#close ⇒ Object
Close the database for reading and writing.
-
#closed? ⇒ Boolean
Check to see if we’ve already closed the database.
-
#compact ⇒ DB
Compact the database to remove stale commits and reduce the file size.
-
#delete(key) ⇒ Object
Delete a key from the database.
-
#delete!(key) ⇒ Object
Immediately delete the key on disk.
-
#each {|key, value| ... } ⇒ Object
Iterate over the key, value pairs in the database.
-
#empty? ⇒ Boolean
Return true if database is empty.
-
#file ⇒ String
Database file name.
-
#flush ⇒ DB
Flush all changes to disk.
-
#has_key?(key) ⇒ Boolean
(also: #key?, #include?, #member?)
Does this db have this key?.
-
#has_value?(value) ⇒ Boolean
(also: #value?)
Does this db have this value?.
-
#initialize(file, options = {}) {|key| ... } ⇒ DB
constructor
Create a new Daybreak::DB.
-
#keys ⇒ Array<String>
Return the keys in the db.
-
#load ⇒ DB
(also: #sunrise)
Sync the database with what is on disk, by first flushing changes, and then loading the new records if necessary.
-
#lock {|db| ... } ⇒ Object
Lock the database for an exclusive commit across processes and threads.
-
#logsize ⇒ Fixnum
Counter of how many records are in the journal.
-
#set!(key, value) ⇒ Object
set! flushes data immediately to disk.
-
#size ⇒ Fixnum
(also: #length)
Return the number of stored items.
-
#synchronize {|db| ... } ⇒ Object
Synchronize access to the database from multiple threads.
-
#update(hash) ⇒ DB
Update database with hash (Fast batch update).
-
#update!(hash) ⇒ DB
Updata database and flush data to disk.
Constructor Details
#initialize(file, options = {}) {|key| ... } ⇒ DB
Create a new Daybreak::DB. The second argument is the default value to store when accessing a previously unset key, this follows the Hash standard.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/daybreak/db.rb', line 21 def initialize(file, = {}, &block) @serializer = ([:serializer] || Serializer::Default).new @table = Hash.new &method(:hash_default) @journal = Journal.new(file, ([:format] || Format).new, @serializer) do |record| if !record @table.clear elsif record.size == 1 @table.delete(record.first) else @table[record.first] = @serializer.load(record.last) end end @default = block ? block : [:default] @mutex = Mutex.new # Mutex used by #synchronize and #lock @@databases_mutex.synchronize { @@databases << self } end |
Instance Attribute Details
#default(key = nil) ⇒ Object
Return default value belonging to key
47 48 49 |
# File 'lib/daybreak/db.rb', line 47 def default(key = nil) @table.default(@serializer.key_for(key)) end |
Instance Method Details
#[](key) ⇒ Object Also known as: get
Retrieve a value at key from the database. If the default value was specified when this database was created, that value will be set and returned. Aliased as get
.
56 57 58 |
# File 'lib/daybreak/db.rb', line 56 def [](key) @table[@serializer.key_for(key)] end |
#[]=(key, value) ⇒ Object Also known as: set
Set a key in the database to be written at some future date. If the data needs to be persisted immediately, call db.set(key, value, true)
.
66 67 68 69 70 |
# File 'lib/daybreak/db.rb', line 66 def []=(key, value) key = @serializer.key_for(key) @journal << [key, value] @table[key] = value end |
#bytesize ⇒ Fixnum
Utility method that will return the size of the database in bytes, useful for determining when to compact
150 151 152 |
# File 'lib/daybreak/db.rb', line 150 def bytesize @journal.bytesize end |
#clear ⇒ DB
Remove all keys and values from the database.
220 221 222 223 224 |
# File 'lib/daybreak/db.rb', line 220 def clear @table.clear @journal.clear self end |
#close ⇒ Object
Close the database for reading and writing.
235 236 237 238 239 |
# File 'lib/daybreak/db.rb', line 235 def close @journal.close @@databases_mutex.synchronize { @@databases.delete(self) } nil end |
#closed? ⇒ Boolean
Check to see if we’ve already closed the database.
243 244 245 |
# File 'lib/daybreak/db.rb', line 243 def closed? @journal.closed? end |
#compact ⇒ DB
Compact the database to remove stale commits and reduce the file size.
228 229 230 231 |
# File 'lib/daybreak/db.rb', line 228 def compact @journal.compact { @table } self end |
#delete(key) ⇒ Object
Delete a key from the database
86 87 88 89 90 |
# File 'lib/daybreak/db.rb', line 86 def delete(key) key = @serializer.key_for(key) @journal << [key] @table.delete(key) end |
#delete!(key) ⇒ Object
Immediately delete the key on disk.
95 96 97 98 99 |
# File 'lib/daybreak/db.rb', line 95 def delete!(key) value = delete(key) flush value end |
#each {|key, value| ... } ⇒ Object
Iterate over the key, value pairs in the database.
170 171 172 |
# File 'lib/daybreak/db.rb', line 170 def each(&block) @table.each(&block) end |
#empty? ⇒ Boolean
Return true if database is empty.
162 163 164 |
# File 'lib/daybreak/db.rb', line 162 def empty? @table.empty? end |
#file ⇒ String
Database file name
40 41 42 |
# File 'lib/daybreak/db.rb', line 40 def file @journal.file end |
#flush ⇒ DB
Flush all changes to disk.
182 183 184 185 |
# File 'lib/daybreak/db.rb', line 182 def flush @journal.flush self end |
#has_key?(key) ⇒ Boolean Also known as: key?, include?, member?
Does this db have this key?
125 126 127 |
# File 'lib/daybreak/db.rb', line 125 def has_key?(key) @table.has_key?(@serializer.key_for(key)) end |
#has_value?(value) ⇒ Boolean Also known as: value?
Does this db have this value?
135 136 137 |
# File 'lib/daybreak/db.rb', line 135 def has_value?(value) @table.has_value?(value) end |
#keys ⇒ Array<String>
Return the keys in the db.
176 177 178 |
# File 'lib/daybreak/db.rb', line 176 def keys @table.keys end |
#load ⇒ DB Also known as: sunrise
Sync the database with what is on disk, by first flushing changes, and then loading the new records if necessary.
190 191 192 193 |
# File 'lib/daybreak/db.rb', line 190 def load @journal.load self end |
#lock {|db| ... } ⇒ Object
This method performs an expensive locking over process boundaries. If you want to synchronize only between threads, use #synchronize.
Lock the database for an exclusive commit across processes and threads
203 204 205 |
# File 'lib/daybreak/db.rb', line 203 def lock @mutex.synchronize { @journal.lock { yield self } } end |
#logsize ⇒ Fixnum
Counter of how many records are in the journal
156 157 158 |
# File 'lib/daybreak/db.rb', line 156 def logsize @journal.size end |
#set!(key, value) ⇒ Object
set! flushes data immediately to disk.
77 78 79 80 81 |
# File 'lib/daybreak/db.rb', line 77 def set!(key, value) set(key, value) flush value end |
#size ⇒ Fixnum Also known as: length
Return the number of stored items.
142 143 144 |
# File 'lib/daybreak/db.rb', line 142 def size @table.size end |
#synchronize {|db| ... } ⇒ Object
Daybreak is not thread safe, if you want to access it from multiple threads, all accesses have to be in the #synchronize block.
Synchronize access to the database from multiple threads
214 215 216 |
# File 'lib/daybreak/db.rb', line 214 def synchronize @mutex.synchronize { yield self } end |
#update(hash) ⇒ DB
Update database with hash (Fast batch update)
104 105 106 107 108 109 110 111 112 |
# File 'lib/daybreak/db.rb', line 104 def update(hash) shash = {} hash.each do |key, value| shash[@serializer.key_for(key)] = value end @journal << shash @table.update(shash) self end |
#update!(hash) ⇒ DB
Updata database and flush data to disk.
117 118 119 120 |
# File 'lib/daybreak/db.rb', line 117 def update!(hash) update(hash) @journal.flush end |