Class: Moneta::Adapters::KyotoCabinet
- Inherits:
-
Object
- Object
- Moneta::Adapters::KyotoCabinet
- Includes:
- Defaults, HashAdapter
- Defined in:
- lib/moneta/adapters/kyotocabinet.rb
Overview
KyotoCabinet backend
Instance Attribute Summary
Attributes included from HashAdapter
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 = {}) ⇒ KyotoCabinet
constructor
A new instance of KyotoCabinet.
-
#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 included from Defaults
#[], #[]=, #decrement, #features, #fetch, #fetch_values, included, #supports?, #update
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options = {}) ⇒ KyotoCabinet
Returns a new instance of KyotoCabinet.
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/moneta/adapters/kyotocabinet.rb', line 16 def initialize( = {}) if [:backend] @backend = [:backend] else raise ArgumentError, 'Option :file is required' unless [:file] @backend = ::KyotoCabinet::DB.new raise @backend.error.to_s unless @backend.open([:file], ::KyotoCabinet::DB::OWRITER | ::KyotoCabinet::DB::OCREATE) end end |
Instance Method Details
#close ⇒ Object
Explicitly close the store
43 44 45 46 |
# File 'lib/moneta/adapters/kyotocabinet.rb', line 43 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.
38 39 40 |
# File 'lib/moneta/adapters/kyotocabinet.rb', line 38 def create(key, value, = {}) @backend.add(key, value) end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
33 34 35 |
# File 'lib/moneta/adapters/kyotocabinet.rb', line 33 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.
49 50 51 52 53 |
# File 'lib/moneta/adapters/kyotocabinet.rb', line 49 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.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/moneta/adapters/kyotocabinet.rb', line 56 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
28 29 30 |
# File 'lib/moneta/adapters/kyotocabinet.rb', line 28 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.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/moneta/adapters/kyotocabinet.rb', line 84 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.
73 74 75 |
# File 'lib/moneta/adapters/kyotocabinet.rb', line 73 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.
78 79 80 81 |
# File 'lib/moneta/adapters/kyotocabinet.rb', line 78 def values_at(*keys, **) hash = slice(*keys, **) keys.map { |key| hash[key] } end |