Class: Moneta::Adapters::Sequel

Inherits:
Object
  • Object
show all
Includes:
Defaults
Defined in:
lib/moneta/adapters/sequel.rb,
lib/moneta/adapters/sequel/mysql.rb,
lib/moneta/adapters/sequel/sqlite.rb,
lib/moneta/adapters/sequel/postgres.rb,
lib/moneta/adapters/sequel/postgres_hstore.rb

Defined Under Namespace

Modules: MySQL, Postgres, PostgresHStore, SQLite Classes: IncrementError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Defaults

#[], #[]=, #decrement, #features, #fetch, included, #supports?, #update

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

#initialize(options = {}) ⇒ Sequel

Returns a new instance of Sequel.

Parameters:

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

Options Hash (options):

  • :db (String)

    Sequel database

  • :table (String, Symbol) — default: :moneta

    Table name

  • :extensions (Array) — default: []

    List of Sequel extensions

  • :connection_validation_timeout (Integer) — default: nil

    Sequel connection_validation_timeout

  • :backend (Sequel::Database)

    Use existing backend instance

  • :optimize (Boolean) — default: true

    Set to false to prevent database-specific optimisations

  • :create_table (Proc, Boolean)

    Provide a Proc for creating the table, or set to false to disable table creation all together. If a Proc is given, it will be called regardless of whether the table exists already.

  • :key_column (Symbol) — default: :k

    The name of the key column

  • :value_column (Symbol) — default: :v

    The name of the value column

  • :hstore (String)

    If using Postgres, keys and values are stored in a single row of the table in the value_column using the hstore format. The row to use is the one where the value_column is equal to the value of this option, and will be created if it doesn’t exist.

  • :each_key_server (Symbol)

    Some adapters are unable to do multiple operations with a single connection. For these, it is possible to specify a separate connection to use for ‘#each_key`. Use in conjunction with Sequel’s ‘:servers` option

  • All (Object)

    other options passed to ‘Sequel#connect`



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/moneta/adapters/sequel.rb', line 39

def initialize(options = {})
  extensions = options.delete(:extensions)
  connection_validation_timeout = options.delete(:connection_validation_timeout)
  optimize = options.delete(:optimize)
  @backend = options.delete(:backend) ||
    connect(extensions: extensions, connection_validation_timeout: connection_validation_timeout, **options)

  if hstore = options.delete(:hstore)
    @row = hstore.to_s
    extend Sequel::PostgresHStore
  elsif optimize == nil || optimize
    add_optimizations
  end

  @table_name = (options.delete(:table) || :moneta).to_sym
  @key_column = options.delete(:key_column) || :k
  @value_column = options.delete(:value_column) || :v
  @each_key_server = options.delete(:each_key_server)

  create_proc = options.delete(:create_table)
  if create_proc == nil
    create_table
  elsif create_proc
    create_proc.call(@backend)
  end

  @table = @backend[@table_name]
  prepare_statements
end

Instance Attribute Details

#backendObject (readonly)



16
17
18
# File 'lib/moneta/adapters/sequel.rb', line 16

def backend
  @backend
end

#key_columnObject (readonly)



16
17
18
# File 'lib/moneta/adapters/sequel.rb', line 16

def key_column
  @key_column
end

#value_columnObject (readonly)



16
17
18
# File 'lib/moneta/adapters/sequel.rb', line 16

def value_column
  @value_column
end

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

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


131
132
133
134
# File 'lib/moneta/adapters/sequel.rb', line 131

def clear(options = {})
  @table.delete
  self
end

#closeObject

Explicitly close the store

Returns:

  • nil



137
138
139
140
# File 'lib/moneta/adapters/sequel.rb', line 137

def close
  @backend.disconnect
  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



94
95
96
97
98
99
# File 'lib/moneta/adapters/sequel.rb', line 94

def create(key, value, options = {})
  @create.call(key: key, value: blob(value))
  true
rescue ::Sequel::ConstraintViolation
  false
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



124
125
126
127
128
# File 'lib/moneta/adapters/sequel.rb', line 124

def delete(key, options = {})
  value = load(key, options)
  @delete.call(key: key)
  value
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)


188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/moneta/adapters/sequel.rb', line 188

def each_key
  return enum_for(:each_key) { @table.count } unless block_given?
  if @each_key_server
    @table.server(@each_key_server).order(key_column).select(key_column).paged_each do |row|
      yield row[key_column]
    end
  else
    @table.select(key_column).order(key_column).paged_each(stream: false) do |row|
      yield row[key_column]
    end
  end
  self
end

#fetch_values(*keys, **options) ⇒ Object #fetch_values(*keys, **options) {|key| ... } ⇒ Array<Object, nil>

Note:

Some adapters may implement this method atomically. The default implmentation uses #values_at.

Behaves identically to #values_at except that it accepts an optional block. When supplied, the block will be called successively with each supplied key that is not present in the store. The return value of the block call will be used in place of nil in returned the array of values.

Overloads:

  • #fetch_values(*keys, **options) {|key| ... } ⇒ Array<Object, nil>

    Returns Array containing the values requested, or where keys are missing, the return values from the corresponding block calls.

    Yield Parameters:

    • key (Object)

      Each key that is not found in the store

    Yield Returns:

    • (Object, nil)

      The value to substitute for the missing one

    Returns:

    • (Array<Object, nil>)

      Array containing the values requested, or where keys are missing, the return values from the corresponding block calls



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/moneta/adapters/sequel.rb', line 154

def fetch_values(*keys, **options)
  return values_at(*keys, **options) unless block_given?
  existing = Hash[slice(*keys, **options)]
  keys.map do |key|
    if existing.key? key
      existing[key]
    else
      yield key
    end
  end
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



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/moneta/adapters/sequel.rb', line 102

def increment(key, amount = 1, options = {})
  @backend.transaction do
    if existing = @load_for_update.call(key: key)
      existing_value = existing[value_column]
      amount += Integer(existing_value)
      raise IncrementError, "no update" unless @increment_update.call(
        key: key,
        value: existing_value,
        new_value: blob(amount.to_s)
      ) == 1
    else
      @create.call(key: key, value: blob(amount.to_s))
    end
    amount
  end
rescue ::Sequel::DatabaseError
  # Concurrent modification might throw a bunch of different errors
  tries ||= 0
  (tries += 1) < 10 ? retry : raise
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)


70
71
72
# File 'lib/moneta/adapters/sequel.rb', line 70

def key?(key, options = {})
  @key.call(key: key) != nil
end

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

Fetch value with key. Return nil if the key doesn’t exist

Parameters:

  • key (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)

  • :sync (Boolean)

    Synchronized load (Cache reloads from adapter, Daybreak syncs with file)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value



75
76
77
78
79
# File 'lib/moneta/adapters/sequel.rb', line 75

def load(key, options = {})
  if row = @load.call(key: key)
    row[value_column]
  end
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?, #load and #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)


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/moneta/adapters/sequel.rb', line 167

def merge!(pairs, options = {})
  @backend.transaction do
    existing = Hash[slice_for_update(pairs)]
    update_pairs, insert_pairs = pairs.partition { |k, _| existing.key?(k) }
    @table.import([key_column, value_column], blob_pairs(insert_pairs))

    if block_given?
      update_pairs.map! do |key, new_value|
        [key, yield(key, existing[key], new_value)]
      end
    end

    update_pairs.each do |key, value|
      @store_update.call(key: key, value: blob(value))
    end
  end

  self
end

#slice(*keys, **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



143
144
145
# File 'lib/moneta/adapters/sequel.rb', line 143

def slice(*keys, **options)
  @slice.all(keys).map! { |row| [row[key_column], row[value_column]] }
end

#store(key, value, options = {}) ⇒ Object

Store value with key

Parameters:

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

Options Hash (options):

  • :expires (Integer)

    Set expiration time (See Expires)

  • :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:

  • value



82
83
84
85
86
87
88
89
90
91
# File 'lib/moneta/adapters/sequel.rb', line 82

def store(key, value, options = {})
  blob_value = blob(value)
  unless @store_update.call(key: key, value: blob_value) == 1
    @create.call(key: key, value: blob_value)
  end
  value
rescue ::Sequel::DatabaseError
  tries ||= 0
  (tries += 1) < 10 ? retry : raise
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 #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



148
149
150
151
# File 'lib/moneta/adapters/sequel.rb', line 148

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