Class: Daybreak::DB

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/daybreak/db.rb

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

Instance Method Summary collapse

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.

Parameters:

  • file (String)

    the path to the db file

  • options (Hash) (defaults to: {})

    a hash that contains the options for creating a new database

Options Hash (options):

  • :serializer (Class)

    Serializer class

  • :format (Class)

    Format class

  • :default (Object)

    Default value

Yields:

  • (key)

    a block that will return the default value to store.

Yield Parameters:

  • key (String)

    the key to be stored.



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, options = {}, &block)
  @serializer = (options[:serializer] || Serializer::Default).new
  @table = Hash.new(&method(:hash_default))
  @journal = Journal.new(file, (options[: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 : options[: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

Parameters:

  • key (Object) (defaults to: nil)

    the default value to retrieve.

Returns:

  • (Object)

    value the default value



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.

Parameters:

  • key (Object)

    the value to retrieve from the database.

Returns:

  • (Object)

    the value



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).

Parameters:

  • key (Object)

    the key of the storage slot in the database

  • value (Object)

    the value to store

Returns:

  • (Object)

    the value



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

#bytesizeFixnum

Utility method that will return the size of the database in bytes, useful for determining when to compact

Returns:

  • (Fixnum)


150
151
152
# File 'lib/daybreak/db.rb', line 150

def bytesize
  @journal.bytesize
end

#clearDB

Remove all keys and values from the database.

Returns:

  • (DB)

    self



220
221
222
223
224
# File 'lib/daybreak/db.rb', line 220

def clear
  @table.clear
  @journal.clear
  self
end

#closeObject

Close the database for reading and writing.

Returns:

  • nil



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.

Returns:

  • (Boolean)


243
244
245
# File 'lib/daybreak/db.rb', line 243

def closed?
  @journal.closed?
end

#compactDB

Compact the database to remove stale commits and reduce the file size.

Returns:

  • (DB)

    self



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

Parameters:

  • key (Object)

    the key of the storage slot in the database

Returns:

  • (Object)

    the value



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.

Parameters:

  • key (Object)

    the key of the storage slot in the database

Returns:

  • (Object)

    the value



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.

Yields:

  • (key, value)

    blk the iterator for each key value pair.

Yield Parameters:

  • key

    the key.

  • value

    the value from 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.

Returns:

  • (Boolean)


162
163
164
# File 'lib/daybreak/db.rb', line 162

def empty?
  @table.empty?
end

#fileString

Database file name

Returns:

  • (String)

    database file name



40
41
42
# File 'lib/daybreak/db.rb', line 40

def file
  @journal.file
end

#flushDB

Flush all changes to disk.

Returns:

  • (DB)

    self



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?

Parameters:

  • key (Object)

    the key to check if the DB has it

Returns:

  • (Boolean)


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?

Parameters:

  • value (Object)

    the value to check if the DB has it

Returns:

  • (Boolean)


135
136
137
# File 'lib/daybreak/db.rb', line 135

def has_value?(value)
  @table.has_value?(value)
end

#keysArray<String>

Return the keys in the db.

Returns:

  • (Array<String>)


176
177
178
# File 'lib/daybreak/db.rb', line 176

def keys
  @table.keys
end

#loadDB Also known as: sunrise

Sync the database with what is on disk, by first flushing changes, and then loading the new records if necessary.

Returns:

  • (DB)

    self



190
191
192
193
# File 'lib/daybreak/db.rb', line 190

def load
  @journal.load
  self
end

#lock {|db| ... } ⇒ Object

Note:

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

Yields:

  • a block where every change to the database is synced

Yield Parameters:

Returns:

  • result of the block

See Also:



203
204
205
# File 'lib/daybreak/db.rb', line 203

def lock
  synchronize { @journal.lock { yield self } }
end

#logsizeFixnum

Counter of how many records are in the journal

Returns:

  • (Fixnum)


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.

Parameters:

  • key (Object)

    the key of the storage slot in the database

  • value (Object)

    the value to store

Returns:

  • (Object)

    the value



77
78
79
80
81
# File 'lib/daybreak/db.rb', line 77

def set!(key, value)
  set(key, value)
  flush
  value
end

#sizeFixnum Also known as: length

Return the number of stored items.

Returns:

  • (Fixnum)


142
143
144
# File 'lib/daybreak/db.rb', line 142

def size
  @table.size
end

#synchronize {|db| ... } ⇒ Object

Note:

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

Yields:

  • a block where every change to the database is synced

Yield Parameters:

Returns:

  • result of the block

See Also:



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)

Parameters:

  • hash (Hash)

    the key/value hash

Returns:

  • (DB)

    self



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.

Parameters:

  • hash (Hash)

    the key/value hash

Returns:

  • (DB)

    self



117
118
119
120
# File 'lib/daybreak/db.rb', line 117

def update!(hash)
  update(hash)
  @journal.flush
end