Class: Moneta::Adapters::LevelDB

Inherits:
Object
  • Object
show all
Includes:
CreateSupport, Defaults, EachKeySupport, HashAdapter, IncrementSupport
Defined in:
lib/moneta/adapters/leveldb.rb

Overview

LevelDB backend

Instance Attribute Summary

Attributes included from HashAdapter

#backend

Instance Method Summary collapse

Methods included from EachKeySupport

included

Methods included from CreateSupport

#create, included

Methods included from IncrementSupport

included, #increment

Methods included from HashAdapter

#delete, #fetch_values, #load, #slice, #store

Methods included from Defaults

#[], #[]=, #create, #decrement, #features, #fetch, #fetch_values, included, #increment, #slice, #supports?, #update

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

#initialize(options = {}) ⇒ LevelDB

Returns a new instance of LevelDB

Parameters:

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

Options Hash (options):

  • :dir (String)
    • Database path

  • All (Object)

    other options passed to `LevelDB::DB#new`

  • :backend (::LevelDB::DB)

    Use existing backend instance


18
19
20
21
22
23
24
# File 'lib/moneta/adapters/leveldb.rb', line 18

def initialize(options = {})
  @backend = options[:backend] ||
    begin
      raise ArgumentError, 'Option :dir is required' unless options[:dir]
      ::LevelDB::DB.new(options[:dir])
    end
end

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

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

32
33
34
35
# File 'lib/moneta/adapters/leveldb.rb', line 32

def clear(options = {})
  @backend.each {|k,v| delete(k, options) }
  self
end

#closeObject

Explicitly close the store

Returns:

  • nil


38
39
40
41
# File 'lib/moneta/adapters/leveldb.rb', line 38

def close
  @backend.close
  nil
end

#each_keyEnumerator #each_key {|key| ... } ⇒ self

Note:

Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.

Calls block once for each key in store, passing the key as a parameter. If no block is given, an enumerator is returned instead.

Overloads:

  • #each_keyEnumerator

    Returns An all-the-keys enumerator

    Returns:

    • (Enumerator)

      An all-the-keys enumerator

  • #each_key {|key| ... } ⇒ self

    Yield Parameters:

    • key (Object)

      Each key is yielded to the supplied block

    Returns:

    • (self)

44
45
46
47
48
# File 'lib/moneta/adapters/leveldb.rb', line 44

def each_key
  return enum_for(:each_key) { @backend.size } unless block_given?
  @backend.each { |key, _| yield key }
  self
end

#key?(key, options = {}) ⇒ Boolean

Exists the value with key

Parameters:

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

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Boolean)

27
28
29
# File 'lib/moneta/adapters/leveldb.rb', line 27

def key?(key, options = {})
  @backend.includes?(key)
end

#merge!(pairs, options = {}) ⇒ self #merge!(pairs, options = {}) {|key, old_value, new_value| ... } ⇒ self

Note:

Some adapters may implement this method atomically, or in two passes when a block is provided. The default implmentation uses #key?, HashAdapter#load and HashAdapter#store.

Stores the pairs in the key-value store, and returns itself. When a block is provided, it will be called before overwriting any existing values with the key, old value and supplied value, and the return value of the block will be used in place of the supplied value.

Overloads:

  • #merge!(pairs, options = {}) ⇒ self

    Parameters:

    • pairs (<(Object, Object)>)

      A collection of key-value pairs to store

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

    Returns:

    • (self)
  • #merge!(pairs, options = {}) {|key, old_value, new_value| ... } ⇒ self

    Parameters:

    • pairs (<(Object, Object)>)

      A collection of key-value pairs to store

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

    Yield Parameters:

    • key (Object)

      A key that whose value is being overwritten

    • old_value (Object)

      The existing value which is being overwritten

    • new_value (Object)

      The value supplied in the method call

    Yield Returns:

    • (Object)

      The value to use for overwriting

    Returns:

    • (self)

58
59
60
61
# File 'lib/moneta/adapters/leveldb.rb', line 58

def merge!(*keys, **options)
  @backend.batch { super }
  self
end

#values_at(*keys, **options) ⇒ Array<Object, nil>

Note:

Some adapters may implement this method atomically, but the default implementation simply makes repeated calls to HashAdapter#load.

Returns an array containing the values associated with the given keys, in the same order as the supplied keys. If a key is not present in the key-value-store, nil is returned in its place.

Parameters:

  • keys (<Object>)

    The keys for the values to fetch

  • options (Hash)

Options Hash (**options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • :sync (Boolean)

    Synchronized load (Cache reloads from adapter, Daybreak syncs with file)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Array<Object, nil>)

    Array containing the values requested, with nil for missing values


51
52
53
54
55
# File 'lib/moneta/adapters/leveldb.rb', line 51

def values_at(*keys, **options)
  ret = nil
  @backend.batch { ret = super }
  ret
end