Class: Dynamoid::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamoid/adapter.rb

Overview

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 Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAdapter

Returns a new instance of Adapter.



15
16
17
18
# File 'lib/dynamoid/adapter.rb', line 15

def initialize
  @adapter_ = Concurrent::Atom.new(nil)
  @tables_ = Concurrent::Atom.new(nil)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Delegate all methods that aren’t defind here to the underlying adapter.

Since:

  • 0.2.0



153
154
155
156
157
# File 'lib/dynamoid/adapter.rb', line 153

def method_missing(method, *args, &block)
  return benchmark(method, *args) { adapter.send(method, *args, &block) } if adapter.respond_to?(method)

  super
end

Class Method Details

.adapter_plugin_classObject



178
179
180
# File 'lib/dynamoid/adapter.rb', line 178

def self.adapter_plugin_class
  Dynamoid::AdapterPlugin.const_get(Dynamoid::Config.adapter.camelcase)
end

Instance Method Details

#adapterObject

The actual adapter currently in use.

Since:

  • 0.2.0



30
31
32
33
34
35
36
37
38
# File 'lib/dynamoid/adapter.rb', line 30

def adapter
  unless @adapter_.value
    adapter = self.class.adapter_plugin_class.new
    adapter.connect!
    @adapter_.compare_and_set(nil, adapter)
    clear_cache!
  end
  @adapter_.value
end

#benchmark(method, *args) { ... } ⇒ Object

Shows how long it takes a method to run on the adapter. Useful for generating logged output.

Parameters:

  • method (Symbol)

    the name of the method to appear in the log

  • args (Array)

    the arguments to the method to appear in the log

Yields:

  • the actual code to benchmark

Returns:

  • the result of the yield

Since:

  • 0.2.0



53
54
55
56
57
58
# File 'lib/dynamoid/adapter.rb', line 53

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?}"
  result
end

#clear_cache!Object



40
41
42
# File 'lib/dynamoid/adapter.rb', line 40

def clear_cache!
  @tables_.swap { |_value, _args| nil }
end

#create_table(table_name, key, options = {}) ⇒ Object



125
126
127
128
129
130
# File 'lib/dynamoid/adapter.rb', line 125

def create_table(table_name, key, options = {})
  unless tables.include?(table_name)
    benchmark('Create Table') { adapter.create_table(table_name, key, options) }
    tables << table_name
  end
end

#delete(table, ids, options = {}) ⇒ Object

Delete an item from a table.

Parameters:

  • table (String)

    the name of the table to write the object to

  • ids (Array)

    to delete, can also be a string of just one id

  • range_key (Array)

    of the record to delete, can also be a string of just one range_key



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/dynamoid/adapter.rb', line 99

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)
    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
end

#delete_table(table_name, options = {}) ⇒ Object

Since:

  • 0.2.0



133
134
135
136
137
138
139
# File 'lib/dynamoid/adapter.rb', line 133

def delete_table(table_name, options = {})
  if tables.include?(table_name)
    benchmark('Delete Table') { adapter.delete_table(table_name, options) }
    idx = tables.index(table_name)
    tables.delete_at(idx)
  end
end

#query(table_name, opts = {}) ⇒ Array

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 one range key to the hash.

Parameters:

  • table_name (String)

    the name of the table

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

    the options to query the table with

Options Hash (opts):

  • :hash_value (String)

    the value of the hash key to find

  • :range_value (Range)

    find the range key within this range

  • :range_greater_than (Number)

    find range keys greater than this

  • :range_less_than (Number)

    find range keys less than this

  • :range_gte (Number)

    find range keys greater than or equal to this

  • :range_lte (Number)

    find range keys less than or equal to this

Returns:

  • (Array)

    an array of all matching items



174
175
176
# File 'lib/dynamoid/adapter.rb', line 174

def query(table_name, opts = {})
  adapter.query(table_name, opts)
end

#read(table, ids, options = {}, &blk) ⇒ Object

Read one or many keys from the selected table. This method intelligently calls batch_get or get on the underlying adapter depending on whether ids is a range or a single key. If a range key is present, it will also interpolate that into the ids so that the batch get will acquire the correct record.

Parameters:

  • table (String)

    the name of the table to write the object to

  • ids (Array)

    to fetch, can also be a string of just one id

  • options: (Hash)

    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



85
86
87
88
89
90
91
# File 'lib/dynamoid/adapter.rb', line 85

def read(table, ids, options = {}, &blk)
  if ids.respond_to?(:each)
    batch_get_item({ table => ids }, options, &blk)
  else
    get_item(table, ids, options)
  end
end

#scan(table, query = {}, opts = {}) ⇒ Object

Scans a table. Generally quite slow; try to avoid using scan if at all possible.

Parameters:

  • table (String)

    the name of the table to write the object to

  • scan_hash (Hash)

    a hash of attributes: matching records will be returned by the scan

Since:

  • 0.2.0



121
122
123
# File 'lib/dynamoid/adapter.rb', line 121

def scan(table, query = {}, opts = {})
  benchmark('Scan', table, query) { adapter.scan(table, query, opts) }
end

#tablesObject



20
21
22
23
24
25
# File 'lib/dynamoid/adapter.rb', line 20

def tables
  unless @tables_.value
    @tables_.swap { |_value, _args| benchmark('Cache Tables') { list_tables || [] } }
  end
  @tables_.value
end

#write(table, object, options = nil) ⇒ Object

Write an object to the adapter.

Parameters:

  • table (String)

    the name of the table to write the object to

  • object (Object)

    the object itself

  • options (Hash) (defaults to: nil)

    Options that are passed to the put_item call

Returns:

  • (Object)

    the persisted object

Since:

  • 0.2.0



69
70
71
# File 'lib/dynamoid/adapter.rb', line 69

def write(table, object, options = nil)
  put_item(table, object, options)
end