Class: Moneta::Adapters::KyotoCabinet

Inherits:
Object
  • Object
show all
Includes:
Defaults, HashAdapter
Defined in:
lib/moneta/adapters/kyotocabinet.rb

Overview

KyotoCabinet backend

Instance Attribute Summary

Attributes included from HashAdapter

#backend

Instance Method Summary collapse

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

Parameters:

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

Options Hash (options):

  • :file (String)

    Database file

  • :backend (::KyotoCabinet::DB)

    Use existing backend instance



16
17
18
19
20
21
22
23
24
25
# File 'lib/moneta/adapters/kyotocabinet.rb', line 16

def initialize(options = {})
  if options[:backend]
    @backend = options[:backend]
  else
    raise ArgumentError, 'Option :file is required' unless options[:file]
    @backend = ::KyotoCabinet::DB.new
    raise @backend.error.to_s unless @backend.open(options[:file],
                                                   ::KyotoCabinet::DB::OWRITER | ::KyotoCabinet::DB::OCREATE)
  end
end

Instance Method Details

#closeObject

Explicitly close the store

Returns:

  • nil



43
44
45
46
# File 'lib/moneta/adapters/kyotocabinet.rb', line 43

def close
  @backend.close
  nil
end

#create(key, value, options = {}) ⇒ Boolean

Note:

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.

Parameters:

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

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)

Returns:

  • (Boolean)

    key was set



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

def create(key, value, options = {})
  @backend.add(key, value)
end

#delete(key, options = {}) ⇒ Object

Delete the key from the store and return the current value

Parameters:

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

Options Hash (options):

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    current value



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

def delete(key, options = {})
  @backend.seize(key)
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)


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

Note:

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.

Parameters:

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

Options Hash (options):

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value from store



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/moneta/adapters/kyotocabinet.rb', line 56

def increment(key, amount = 1, options = {})
  ret = nil
  success = @backend.accept(key) do |key, value|
    if value
      ret = Integer(value) + amount
    else
      ret = amount
    end
    ret.to_s
  end

  raise @backend.error unless success
  ret
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)


28
29
30
# File 'lib/moneta/adapters/kyotocabinet.rb', line 28

def key?(key, options = {})
  @backend.check(key) >= 0
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)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/moneta/adapters/kyotocabinet.rb', line 83

def merge!(pairs, options = {})
  hard = options.key?(:hard) ? options[:hard] : false
  atomic = options.key?(:atomic) ? options[:atomic] : true

  success =
    if block_given?
      @backend.transaction(hard) do
        existing = slice(*pairs.map { |k, _| k }, **options)
        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)>

Note:

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#==).

Note:

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.

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:

  • (<(Object, Object)>)

    A collection of key-value pairs



72
73
74
# File 'lib/moneta/adapters/kyotocabinet.rb', line 72

def slice(*keys, atomic: true, **options)
  @backend.get_bulk(keys, atomic)
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



77
78
79
80
# File 'lib/moneta/adapters/kyotocabinet.rb', line 77

def values_at(*keys, **options)
  hash = slice(*keys, **options)
  keys.map { |key| hash[key] }
end