Class: Moneta::Adapters::KyotoCabinet
- Inherits:
-
Moneta::Adapter
- Object
- Moneta::Adapter
- Moneta::Adapters::KyotoCabinet
- Includes:
- HashAdapter
- Defined in:
- lib/moneta/adapters/kyotocabinet.rb
Overview
KyotoCabinet backend
Instance Attribute Summary
Attributes included from HashAdapter
Attributes inherited from Moneta::Adapter
Instance Method Summary collapse
-
#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 = {}) ⇒ Object constructor
-
#key?(key, options = {}) ⇒ Boolean
Exists the value with key.
-
#merge!(pairs, options = {}) ⇒ Object
Stores the pairs in the key-value store, and returns itself.
-
#slice(*keys, atomic: true, **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.
-
#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 included from HashAdapter
#clear, #fetch_values, #load, #store
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 = {}) ⇒ Object
16 17 18 19 20 |
# File 'lib/moneta/adapters/kyotocabinet.rb', line 16 backend do |file:| backend = ::KyotoCabinet::DB.new raise backend.error.to_s unless backend.open(file, ::KyotoCabinet::DB::OWRITER | ::KyotoCabinet::DB::OCREATE) backend end |
Instance Method Details
#close ⇒ Object
Explicitly close the store
38 39 40 41 |
# File 'lib/moneta/adapters/kyotocabinet.rb', line 38 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.
33 34 35 |
# File 'lib/moneta/adapters/kyotocabinet.rb', line 33 def create(key, value, = {}) backend.add(key, value) end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
28 29 30 |
# File 'lib/moneta/adapters/kyotocabinet.rb', line 28 def delete(key, = {}) backend.seize(key) 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.
44 45 46 47 48 |
# File 'lib/moneta/adapters/kyotocabinet.rb', line 44 def each_key return enum_for(:each_key) { backend.count } unless block_given? backend.each_key { |arr| yield arr[0] } 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.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/moneta/adapters/kyotocabinet.rb', line 51 def increment(key, amount = 1, = {}) ret = nil success = backend.accept(key) do |_, value| ret = if value Integer(value) + amount else amount end ret.to_s end raise backend.error unless success ret end |
#key?(key, options = {}) ⇒ Boolean
Exists the value with key
23 24 25 |
# File 'lib/moneta/adapters/kyotocabinet.rb', line 23 def key?(key, = {}) backend.check(key) >= 0 end |
#merge!(pairs, options = {}) ⇒ self #merge!(pairs, options = {}) {|key, old_value, new_value| ... } ⇒ self
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.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/moneta/adapters/kyotocabinet.rb', line 79 def merge!(pairs, = {}) hard = .key?(:hard) ? [:hard] : false atomic = .key?(:atomic) ? [:atomic] : true success = if block_given? backend.transaction(hard) do existing = slice(*pairs.map { |k, _| k }, **) pairs = pairs.map do |key, new_value| if existing.key?(key) [key, yield(key, existing[key], new_value)] else [key, new_value] end end backend.set_bulk(pairs.to_h, atomic) >= 0 end else backend.set_bulk(pairs.to_h, atomic) >= 0 end raise backend.error unless success self end |
#slice(*keys, atomic: true, **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.
68 69 70 |
# File 'lib/moneta/adapters/kyotocabinet.rb', line 68 def slice(*keys, atomic: true, **) backend.get_bulk(keys, atomic) end |
#values_at(*keys, **options) ⇒ Array<Object, nil>
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.
73 74 75 76 |
# File 'lib/moneta/adapters/kyotocabinet.rb', line 73 def values_at(*keys, **) hash = slice(*keys, **) keys.map { |key| hash[key] } end |