Class: Moneta::Adapters::LMDB
- Inherits:
-
Moneta::Adapter
- Object
- Moneta::Adapter
- Moneta::Adapters::LMDB
- Defined in:
- lib/moneta/adapters/lmdb.rb
Overview
LMDB backend
Constant Summary collapse
- PUT_FLAGS =
%i[nooverwrite nodupdata current append appenddup].freeze
Instance Attribute Summary
Attributes inherited from Moneta::Adapter
Instance Method Summary collapse
-
#clear(options = {}) ⇒ void
Clear all keys in this store.
-
#close ⇒ Object
Explicitly close the store.
-
#create(key, value, options = {}) ⇒ Boolean
Atomically sets a key to value if it’s not set.
-
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value.
-
#each_key ⇒ Object
Calls block once for each key in store, passing the key as a parameter.
-
#increment(key, amount = 1, options = {}) ⇒ Object
Atomically increment integer value with key.
-
#initialize(options = {}) ⇒ LMDB
constructor
A new instance of LMDB.
-
#key?(key, options = {}) ⇒ Boolean
Exists the value with key.
-
#load(key, options = {}) ⇒ Object
Fetch value with key.
-
#merge!(pairs, options = {}) ⇒ Object
Stores the pairs in the key-value store, and returns itself.
-
#slice(*keys, **options) ⇒ <(Object, Object)>
Returns a collection of key-value pairs corresponding to those supplied keys which are present in the key-value store, and their associated values.
-
#store(key, value, options = {}) ⇒ Object
Store value with key.
-
#values_at(*keys, **options) ⇒ Array<Object, nil>
Returns an array containing the values associated with the given keys, in the same order as the supplied keys.
Methods inherited from Moneta::Adapter
backend, backend_block, backend_required?
Methods included from Config
Methods included from Defaults
#[], #[]=, #decrement, #features, #fetch, #fetch_values, included, #supports?, #update
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options = {}) ⇒ LMDB
Returns a new instance of LMDB.
24 25 26 27 |
# File 'lib/moneta/adapters/lmdb.rb', line 24 def initialize( = {}) super @db = backend.database(config.db, create: true) end |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
56 57 58 59 |
# File 'lib/moneta/adapters/lmdb.rb', line 56 def clear( = {}) @db.clear self end |
#close ⇒ Object
Explicitly close the store
83 84 85 86 |
# File 'lib/moneta/adapters/lmdb.rb', line 83 def close backend.close nil end |
#create(key, value, options = {}) ⇒ Boolean
Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.
Atomically sets a key to value if it’s not set.
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/moneta/adapters/lmdb.rb', line 71 def create(key, value, = {}) backend.transaction do if @db.get(key) false else @db.put(key, value, Utils.only(, *PUT_FLAGS)) true end end end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
46 47 48 49 50 51 52 53 |
# File 'lib/moneta/adapters/lmdb.rb', line 46 def delete(key, = {}) backend.transaction do if value = @db.get(key) @db.delete(key) value end end end |
#each_key ⇒ Enumerator #each_key {|key| ... } ⇒ self
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.
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/moneta/adapters/lmdb.rb', line 89 def each_key return enum_for(:each_key) { @db.size } unless block_given? @db.cursor do |cursor| while record = cursor.next yield record[0] end end self end |
#increment(key, amount = 1, options = {}) ⇒ Object
Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.
Atomically increment integer value with key
This method also accepts negative amounts.
62 63 64 65 66 67 68 |
# File 'lib/moneta/adapters/lmdb.rb', line 62 def increment(key, amount = 1, = {}) backend.transaction do value = Integer(@db.get(key) || 0) + amount @db.put(key, value.to_s, Utils.only(, *PUT_FLAGS)) value end end |
#key?(key, options = {}) ⇒ Boolean
Exists the value with key
30 31 32 |
# File 'lib/moneta/adapters/lmdb.rb', line 30 def key?(key, = {}) @db.get(key) != nil end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
35 36 37 |
# File 'lib/moneta/adapters/lmdb.rb', line 35 def load(key, = {}) @db.get(key) end |
#merge!(pairs, options = {}) ⇒ self #merge!(pairs, options = {}) {|key, old_value, new_value| ... } ⇒ self
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.
112 113 114 |
# File 'lib/moneta/adapters/lmdb.rb', line 112 def merge!(pairs, = {}) backend.transaction { super } end |
#slice(*keys, **options) ⇒ <(Object, Object)>
The keys in the return value may be the same objects that were supplied (i.e. Object#equal?), or may simply be equal (i.e. Object#==).
Some adapters may implement this method atomically. The default implmentation uses #values_at.
Returns a collection of key-value pairs corresponding to those supplied keys which are present in the key-value store, and their associated values. Only those keys present in the store will have pairs in the return value. The return value can be any enumerable object that yields pairs, so it could be a hash, but needn’t be.
107 108 109 |
# File 'lib/moneta/adapters/lmdb.rb', line 107 def slice(*keys, **) backend.transaction { super } end |
#store(key, value, options = {}) ⇒ Object
Store value with key
40 41 42 43 |
# File 'lib/moneta/adapters/lmdb.rb', line 40 def store(key, value, = {}) @db.put(key, value, Utils.only(, *PUT_FLAGS)) value end |
#values_at(*keys, **options) ⇒ Array<Object, nil>
Some adapters may implement this method atomically, but the default implementation simply makes repeated calls to #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.
102 103 104 |
# File 'lib/moneta/adapters/lmdb.rb', line 102 def values_at(*keys, **) backend.transaction { super } end |