Class: Moneta::Adapters::Sequel
- Inherits:
-
Object
- Object
- Moneta::Adapters::Sequel
- Includes:
- Defaults
- Defined in:
- lib/moneta/adapters/sequel.rb
Overview
Sequel backend
Direct Known Subclasses
Defined Under Namespace
Classes: IncrementError, MySQL, Postgres, PostgresHStore, SQLite
Constant Summary collapse
- UniqueConstraintViolation =
Sequel::UniqueConstraintViolation is defined since sequel 3.44.0 older versions raise a Sequel::DatabaseError.
defined?(::Sequel::UniqueConstraintViolation) ? ::Sequel::UniqueConstraintViolation : ::Sequel::DatabaseError
Instance Attribute Summary collapse
- #backend ⇒ Object readonly
- #key_column ⇒ Object readonly
- #value_column ⇒ Object readonly
Class Method Summary collapse
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.
-
#fetch_values(*keys, **options) ⇒ Object
Behaves identically to #values_at except that it accepts an optional block.
-
#increment(key, amount = 1, options = {}) ⇒ Object
Atomically increment integer value with key.
-
#initialize(options) ⇒ Sequel
constructor
private
A new instance of Sequel.
-
#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 included from Defaults
#[], #[]=, #decrement, #features, #fetch, included, #supports?, #update
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options) ⇒ Sequel
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Sequel.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/moneta/adapters/sequel.rb', line 82 def initialize() @table_name = (.delete(:table) || :moneta).to_sym @key_column = .delete(:key_column) || :k @value_column = .delete(:value_column) || :v @each_key_server = .delete(:each_key_server) create_proc = .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
#backend ⇒ Object (readonly)
15 16 17 |
# File 'lib/moneta/adapters/sequel.rb', line 15 def backend @backend end |
#key_column ⇒ Object (readonly)
15 16 17 |
# File 'lib/moneta/adapters/sequel.rb', line 15 def key_column @key_column end |
#value_column ⇒ Object (readonly)
15 16 17 |
# File 'lib/moneta/adapters/sequel.rb', line 15 def value_column @value_column end |
Class Method Details
.new(options = {}) ⇒ Object
38 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 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/moneta/adapters/sequel.rb', line 38 def self.new( = {}) extensions = .delete(:extensions) connection_validation_timeout = .delete(:connection_validation_timeout) optimize = .delete(:optimize) backend = .delete(:backend) || begin raise ArgumentError, 'Option :db is required' unless db = .delete(:db) other_cols = [:table, :create_table, :key_column, :value_column, :hstore] ::Sequel.connect(db, .reject { |k,_| other_cols.member?(k) }).tap do |backend| if extensions raise ArgumentError, 'Option :extensions must be an Array' unless extensions.is_a?(Array) extensions.map(&:to_sym).each(&backend.method(:extension)) end if connection_validation_timeout backend.pool.connection_validation_timeout = connection_validation_timeout end end end instance = if optimize.nil? || optimize case backend.database_type when :mysql MySQL.allocate when :postgres if [:hstore] PostgresHStore.allocate elsif matches = backend.get(::Sequel[:version].function).match(/PostgreSQL (\d+)\.(\d+)/) # Our optimisations only work on Postgres 9.5+ major, minor = matches[1..2].map(&:to_i) Postgres.allocate if major > 9 || (major == 9 && minor >= 5) end when :sqlite SQLite.allocate end end || allocate instance.instance_variable_set(:@backend, backend) instance.send(:initialize, ) instance end |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
161 162 163 164 |
# File 'lib/moneta/adapters/sequel.rb', line 161 def clear( = {}) @table.delete self end |
#close ⇒ Object
Explicitly close the store
167 168 169 170 |
# File 'lib/moneta/adapters/sequel.rb', line 167 def close @backend.disconnect 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.
124 125 126 127 128 129 |
# File 'lib/moneta/adapters/sequel.rb', line 124 def create(key, value, = {}) @create.call(key: key, value: blob(value)) true rescue UniqueConstraintViolation false end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
154 155 156 157 158 |
# File 'lib/moneta/adapters/sequel.rb', line 154 def delete(key, = {}) value = load(key, ) @delete.call(key: key) value 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.
218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/moneta/adapters/sequel.rb', line 218 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>
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.
184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/moneta/adapters/sequel.rb', line 184 def fetch_values(*keys, **) return values_at(*keys, **) unless block_given? existing = Hash[slice(*keys, **)] keys.map do |key| if existing.key? key existing[key] else yield key end end 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.
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/moneta/adapters/sequel.rb', line 132 def increment(key, amount = 1, = {}) @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
100 101 102 |
# File 'lib/moneta/adapters/sequel.rb', line 100 def key?(key, = {}) @key.call(key: key) != nil end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
105 106 107 108 109 |
# File 'lib/moneta/adapters/sequel.rb', line 105 def load(key, = {}) if row = @load.call(key: key) row[value_column] end 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.
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/moneta/adapters/sequel.rb', line 197 def merge!(pairs, = {}) @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)>
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.
173 174 175 |
# File 'lib/moneta/adapters/sequel.rb', line 173 def slice(*keys, **) @slice.all(keys).map! { |row| [row[key_column], row[value_column]] } end |
#store(key, value, options = {}) ⇒ Object
Store value with key
112 113 114 115 116 117 118 119 120 121 |
# File 'lib/moneta/adapters/sequel.rb', line 112 def store(key, value, = {}) 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>
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.
178 179 180 181 |
# File 'lib/moneta/adapters/sequel.rb', line 178 def values_at(*keys, **) pairs = Hash[slice(*keys, **)] keys.map { |key| pairs[key] } end |