lib/dynamoid/adapter.rb in dynamoid-2.2.0 vs lib/dynamoid/adapter.rb in dynamoid-3.0.0

- old
+ new

@@ -1,12 +1,13 @@ +# frozen_string_literal: true + # require only 'concurrent/atom' once this issue is resolved: # https://github.com/ruby-concurrency/concurrent-ruby/pull/377 require 'concurrent' # encoding: utf-8 module Dynamoid - # Adapter's value-add: # 1) For the rest of Dynamoid, the gateway to DynamoDB. # 2) Allows switching `config.adapter` to ease development of a new adapter. # 3) Caches the list of tables Dynamoid knows about. class Adapter @@ -14,31 +15,31 @@ @adapter_ = Concurrent::Atom.new(nil) @tables_ = Concurrent::Atom.new(nil) end def tables - if !@tables_.value - @tables_.swap{|value, args| benchmark('Cache Tables') { list_tables || [] } } + unless @tables_.value + @tables_.swap { |_value, _args| benchmark('Cache Tables') { list_tables || [] } } end @tables_.value end # The actual adapter currently in use. # # @since 0.2.0 def adapter - if !@adapter_.value + unless @adapter_.value adapter = self.class.adapter_plugin_class.new adapter.connect! if adapter.respond_to?(:connect!) @adapter_.compare_and_set(nil, adapter) clear_cache! end @adapter_.value end def clear_cache! - @tables_.swap{|value, args| nil} + @tables_.swap { |_value, _args| nil } end # Shows how long it takes a method to run on the adapter. Useful for generating logged output. # # @param [Symbol] method the name of the method to appear in the log @@ -49,12 +50,12 @@ # # @since 0.2.0 def benchmark(method, *args) start = Time.now result = yield - Dynamoid.logger.debug "(#{((Time.now - start) * 1000.0).round(2)} ms) #{method.to_s.split('_').collect(&:upcase).join(' ')}#{ " - #{args.inspect}" unless args.nil? || args.empty? }" - return result + Dynamoid.logger.debug "(#{((Time.now - start) * 1000.0).round(2)} ms) #{method.to_s.split('_').collect(&:upcase).join(' ')}#{" - #{args.inspect}" unless args.nil? || args.empty?}" + result end # Write an object to the adapter. # # @param [String] table the name of the table to write the object to @@ -79,17 +80,13 @@ # @param [Hash] options: Passed to the underlying query. The :range_key option is required whenever the table has a range key, # unless multiple ids are passed in. # # @since 0.2.0 def read(table, ids, options = {}, &blk) - range_key = options.delete(:range_key) - if ids.respond_to?(:each) - ids = ids.collect{|id| range_key ? [id, range_key] : id} - batch_get_item({table => ids}, options, &blk) + batch_get_item({ table => ids }, options, &blk) else - options[:range_key] = range_key if range_key get_item(table, ids, options) end end # Delete an item from a table. @@ -99,16 +96,16 @@ # @param [Array] range_key of the record to delete, can also be a string of just one range_key # def delete(table, ids, options = {}) range_key = options[:range_key] # array of range keys that matches the ids passed in if ids.respond_to?(:each) - if range_key.respond_to?(:each) - # turn ids into array of arrays each element being hash_key, range_key - ids = ids.each_with_index.map{|id, i| [id, range_key[i]]} - else - ids = range_key ? ids.map { |id| [id, range_key] } : ids - end + ids = if range_key.respond_to?(:each) + # turn ids into array of arrays each element being hash_key, range_key + ids.each_with_index.map { |id, i| [id, range_key[i]] } + else + range_key ? ids.map { |id| [id, range_key] } : ids + end batch_delete_item(table => ids) else delete_item(table, ids, options) end @@ -119,15 +116,15 @@ # @param [String] table the name of the table to write the object to # @param [Hash] scan_hash a hash of attributes: matching records will be returned by the scan # # @since 0.2.0 def scan(table, query = {}, opts = {}) - benchmark('Scan', table, query) {adapter.scan(table, query, opts)} + benchmark('Scan', table, query) { adapter.scan(table, query, opts) } end def create_table(table_name, key, options = {}) - if !tables.include?(table_name) + unless tables.include?(table_name) benchmark('Create Table') { adapter.create_table(table_name, key, options) } tables << table_name end end @@ -138,28 +135,28 @@ idx = tables.index(table_name) tables.delete_at(idx) end end - [:batch_get_item, :delete_item, :get_item, :list_tables, :put_item, :truncate, :batch_write_item, :batch_delete_item].each do |m| + %i[batch_get_item delete_item get_item list_tables put_item truncate batch_write_item batch_delete_item].each do |m| # Method delegation with benchmark to the underlying adapter. Faster than relying on method_missing. # # @since 0.2.0 define_method(m) do |*args, &blk| if blk.present? - benchmark("#{m.to_s}", *args) { adapter.send(m, *args, &blk) } + benchmark(m.to_s, *args) { adapter.send(m, *args, &blk) } else - benchmark("#{m.to_s}", *args) { adapter.send(m, *args) } + benchmark(m.to_s, *args) { adapter.send(m, *args) } end end end # Delegate all methods that aren't defind here to the underlying adapter. # # @since 0.2.0 def method_missing(method, *args, &block) - return benchmark(method, *args) {adapter.send(method, *args, &block)} if adapter.respond_to?(method) + return benchmark(method, *args) { adapter.send(method, *args, &block) } if adapter.respond_to?(method) super end # Query the DynamoDB table. This employs DynamoDB's indexes so is generally faster than scanning, but is # only really useful for range queries, since it can only find by one hash key at once. Only provide @@ -178,17 +175,14 @@ # def query(table_name, opts = {}) adapter.query(table_name, opts) end - private - def self.adapter_plugin_class unless Dynamoid.const_defined?(:AdapterPlugin) && Dynamoid::AdapterPlugin.const_defined?(Dynamoid::Config.adapter.camelcase) require "dynamoid/adapter_plugin/#{Dynamoid::Config.adapter}" end Dynamoid::AdapterPlugin.const_get(Dynamoid::Config.adapter.camelcase) end - end end