Class: Moneta::Adapters::ActiveRecord
- Inherits:
-
Object
- Object
- Moneta::Adapters::ActiveRecord
- Includes:
- Defaults
- Defined in:
- lib/moneta/adapters/activerecord.rb
Overview
ActiveRecord as key/value stores
Class Attribute Summary collapse
- .connection_lock ⇒ Object readonly
Instance Attribute Summary collapse
- #key_column ⇒ Object readonly
- #table ⇒ Object readonly
- #value_column ⇒ Object readonly
Class Method Summary collapse
- .establish_connection(spec_name) ⇒ Object
- .retrieve_connection_pool(spec_name) ⇒ Object
- .retrieve_or_establish_connection_pool(spec_name) ⇒ Object
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(&block) ⇒ 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 = {}) ⇒ ActiveRecord
constructor
A new instance of ActiveRecord.
-
#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, lock: false, **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 = {}) ⇒ ActiveRecord
Returns a new instance of ActiveRecord.
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/moneta/adapters/activerecord.rb', line 49 def initialize( = {}) @key_column = .delete(:key_column) || :k @value_column = .delete(:value_column) || :v if backend = .delete(:backend) @spec = backend.connection_pool.spec @table = ::Arel::Table.new(backend.table_name.to_sym) else # Feed the connection info into ActiveRecord and get back a name to use for getting the # connection pool connection = .delete(:connection) @spec = case connection when Symbol connection when Hash, String # Normalize the connection specification to a hash resolver = ::ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new \ 'dummy' => connection # Turn the config into a standardised hash, sans a couple of bits hash = resolver.resolve(:dummy) hash.delete('name') hash.delete(:password) # For security # Make a name unique to this config name = 'moneta?' + URI.encode_www_form(hash.to_a.sort) # Add into configurations unless its already there (initially done without locking for # speed) unless self.class.configurations.key? name self.class.connection_lock.synchronize do self.class.configurations[name] = connection \ unless self.class.configurations.key? name end end name.to_sym else ::ActiveRecord::Base.connection_pool.spec.name.to_s end table_name = (.delete(:table) || :moneta).to_sym create_table_proc = .delete(:create_table) if create_table_proc == nil create_table(table_name) elsif create_table_proc with_connection(&create_table_proc) end @table = ::Arel::Table.new(table_name) end end |
Class Attribute Details
.connection_lock ⇒ Object (readonly)
18 19 20 |
# File 'lib/moneta/adapters/activerecord.rb', line 18 def connection_lock @connection_lock end |
Instance Attribute Details
#key_column ⇒ Object (readonly)
13 14 15 |
# File 'lib/moneta/adapters/activerecord.rb', line 13 def key_column @key_column end |
#table ⇒ Object (readonly)
13 14 15 |
# File 'lib/moneta/adapters/activerecord.rb', line 13 def table @table end |
#value_column ⇒ Object (readonly)
13 14 15 |
# File 'lib/moneta/adapters/activerecord.rb', line 13 def value_column @value_column end |
Class Method Details
.establish_connection(spec_name) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/moneta/adapters/activerecord.rb', line 25 def establish_connection(spec_name) connection_lock.synchronize do if connection_pool = retrieve_connection_pool(spec_name) connection_pool else connection_handler.establish_connection(spec_name.to_sym) end end end |
.retrieve_connection_pool(spec_name) ⇒ Object
21 22 23 |
# File 'lib/moneta/adapters/activerecord.rb', line 21 def retrieve_connection_pool(spec_name) connection_handler.retrieve_connection_pool(spec_name.to_s) end |
.retrieve_or_establish_connection_pool(spec_name) ⇒ Object
35 36 37 |
# File 'lib/moneta/adapters/activerecord.rb', line 35 def retrieve_or_establish_connection_pool(spec_name) retrieve_connection_pool(spec_name) || establish_connection(spec_name) end |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
186 187 188 189 190 191 |
# File 'lib/moneta/adapters/activerecord.rb', line 186 def clear( = {}) with_connection do |conn| conn.delete(arel_del) end self end |
#close ⇒ Object
Explicitly close the store
194 195 196 197 |
# File 'lib/moneta/adapters/activerecord.rb', line 194 def close @table = nil @spec = 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.
176 177 178 179 180 181 182 183 |
# File 'lib/moneta/adapters/activerecord.rb', line 176 def create(key, value, = {}) with_connection do |conn| conn_ins(conn, key, value) true end rescue ::ActiveRecord::RecordNotUnique false end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/moneta/adapters/activerecord.rb', line 136 def delete(key, = {}) with_connection do |conn| conn.transaction do sel = arel_sel_key(key).project(table[value_column]).lock value = decode(conn, conn.select_value(sel)) del = arel_del.where(table[key_column].eq(key)) conn.delete(del) value end end 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.
111 112 113 114 115 116 117 |
# File 'lib/moneta/adapters/activerecord.rb', line 111 def each_key(&block) with_connection do |conn| return enum_for(:each_key) { conn.select_value(arel_sel.project(table[key_column].count)) } unless block_given? conn.select_values(arel_sel.project(table[key_column])).each { |k| yield(k) } 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.
239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/moneta/adapters/activerecord.rb', line 239 def fetch_values(*keys, **) return values_at(*keys, **) unless block_given? hash = Hash[slice(*keys, **)] keys.map do |key| if hash.key?(key) hash[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.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/moneta/adapters/activerecord.rb', line 151 def increment(key, amount = 1, = {}) with_connection do |conn| begin conn_ins(conn, key, amount.to_s) amount rescue ::ActiveRecord::RecordNotUnique conn.transaction do sel = arel_sel_key(key).project(table[value_column]).lock value = decode(conn, conn.select_value(sel)) value = (value ? Integer(value) : 0) + amount # Re-raise if the upate affects no rows (i.e. row deleted after attempted insert, # before select for update) raise unless conn_upd(conn, key, value.to_s) == 1 value end end end rescue ::ActiveRecord::RecordNotUnique, ::ActiveRecord::Deadlocked # This handles the "no row updated" issue, above, as well as deadlocks # which may occur on some adapters tries ||= 0 (tries += 1) <= 3 ? retry : raise end |
#key?(key, options = {}) ⇒ Boolean
Exists the value with key
102 103 104 105 106 107 108 |
# File 'lib/moneta/adapters/activerecord.rb', line 102 def key?(key, = {}) with_connection do |conn| sel = arel_sel_key(key).project(::Arel.sql('1')) result = conn.select_all(sel) !result.empty? end end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn’t exist
120 121 122 123 124 |
# File 'lib/moneta/adapters/activerecord.rb', line 120 def load(key, = {}) with_connection do |conn| conn_sel_value(conn, key) 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.
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/moneta/adapters/activerecord.rb', line 252 def merge!(pairs, = {}) with_connection do |conn| conn.transaction do existing = Hash[slice(*pairs.map { |k, _| k }, lock: true, **)] update_pairs, insert_pairs = pairs.partition { |k, _| existing.key?(k) } insert_pairs.each { |key, value| conn_ins(conn, key, encode(conn, value)) } if block_given? update_pairs.map! do |key, new_value| [key, yield(key, existing[key], new_value)] end end update_pairs.each { |key, value| conn_upd(conn, key, encode(conn, value)) } end end self end |
#slice(*keys, lock: false, **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.
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/moneta/adapters/activerecord.rb', line 200 def slice(*keys, lock: false, **) with_connection do |conn| conn.create_table(:slice_keys, temporary: true) do |t| t.string :key, null: false end begin temp_table = ::Arel::Table.new(:slice_keys) keys.each do |key| conn.insert ::Arel::InsertManager.new .into(temp_table) .insert([[temp_table[:key], key]]) end sel = arel_sel .join(temp_table) .on(table[key_column].eq(temp_table[:key])) .project(table[key_column], table[value_column]) sel = sel.lock if lock result = conn.select_all(sel) k = key_column.to_s v = value_column.to_s result.map do |row| [row[k], decode(conn, row[v])] end ensure conn.drop_table(:slice_keys) end end end |
#store(key, value, options = {}) ⇒ Object
Store value with key
127 128 129 130 131 132 133 |
# File 'lib/moneta/adapters/activerecord.rb', line 127 def store(key, value, = {}) with_connection do |conn| encoded = encode(conn, value) conn_ins(conn, key, encoded) unless conn_upd(conn, key, encoded) == 1 end value 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.
233 234 235 236 |
# File 'lib/moneta/adapters/activerecord.rb', line 233 def values_at(*keys, **) hash = Hash[slice(*keys, **)] keys.map { |key| hash[key] } end |