Class: Moneta::Adapters::LevelDB

Inherits:
Moneta::Adapter show all
Includes:
CreateSupport, EachKeySupport, HashAdapter, IncrementSupport
Defined in:
lib/moneta/adapters/leveldb.rb

Overview

LevelDB backend

Instance Attribute Summary

Attributes included from HashAdapter

#backend

Attributes inherited from Moneta::Adapter

#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 inherited from Moneta::Adapter

backend, backend_block, backend_required?

Methods included from Config

#config, included

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 = {}) ⇒ Object

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
# File 'lib/moneta/adapters/leveldb.rb', line 18

backend { |dir:| ::LevelDB::DB.new(dir) }

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

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


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

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

#closeObject

Explicitly close the store

Returns:

  • nil



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

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)


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

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)


21
22
23
# File 'lib/moneta/adapters/leveldb.rb', line 21

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)


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

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



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

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