Module: Toy::Dynamo::Querying::ClassMethods

Defined in:
lib/toy/dynamo/querying.rb

Instance Method Summary collapse

Instance Method Details

#count_range(hash_value, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


59
60
61
62
63
# File 'lib/toy/dynamo/querying.rb', line 59

def count_range(hash_value, options={})
  raise ArgumentError, "no range_key specified for this table" if dynamo_table.range_keys.blank?
  results = dynamo_table.query(hash_value, options.merge(:select => :count))
  Response.new(results).count
end

#read_first(hash_value, options = {}) ⇒ Object

scan



131
132
133
# File 'lib/toy/dynamo/querying.rb', line 131

def read_first(hash_value, options={})
  self.read_range(hash_value, options).first
end

#read_multiple(keys, options = nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/toy/dynamo/querying.rb', line 65

def read_multiple(keys, options=nil)
  results_map = {}
  results = adapter.batch_read(keys, options)
  results[:responses][dynamo_table.table_schema[:table_name]].each do |result|
    attrs = Response.strip_attr_types(result)
    if dynamo_table.range_keys.present? && primary_range_key = dynamo_table.range_keys.find{|rk| rk[:primary_range_key] }
      (results_map[attrs[dynamo_table.hash_key[:attribute_name]]] ||= {})[attrs[primary_range_key[:attribute_name]]] = load(attrs[dynamo_table.hash_key[:attribute_name]], attrs)
    else
      results_map[attrs[dynamo_table.hash_key[:attribute_name]]] = load(attrs[dynamo_table.hash_key[:attribute_name]], attrs)
    end
  end
  results_map
end

#read_range(hash_value, options = {}) ⇒ Object

Read results up to the limit

read_range("1", :range => { :varname.gte => "2"}, :limit => 10)

Loop results in given batch size until limit is hit or no more results

read_range("1", :range => { :varname.eq => "2"}, :batch => 10, :limit => 1000)

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/toy/dynamo/querying.rb', line 16

def read_range(hash_value, options={})
  raise ArgumentError, "no range_key specified for this table" if dynamo_table.range_keys.blank? && global_secondary_indexes.blank?
  aggregated_results = []

  batch_size = options.delete(:batch) || DEFAULT_BATCH_SIZE
  max_results_limit = options[:limit]
  if options[:limit] && options[:limit] > batch_size
    options.merge!(:limit => batch_size)
  end

  results = dynamo_table.query(hash_value, options)
  response = Response.new(results)

  results[:member].each do |result|
    attrs = Response.strip_attr_types(result)
    aggregated_results << load(attrs[dynamo_table.hash_key[:attribute_name]], attrs)
  end

  if response.more_results?
    results_returned = response.count
    batch_iteration = 0
    while response.more_results? && batch_iteration < MAX_BATCH_ITERATIONS
      if max_results_limit && (delta_results_limit = (max_results_limit-results_returned)) < batch_size
        break if delta_results_limit == 0
        options.merge!(:limit => delta_results_limit)
      else
        options.merge!(:limit => batch_size)
      end

      results = dynamo_table.query(hash_value, options.merge(:exclusive_start_key => response.last_evaluated_key))
      response = Response.new(results)
      results[:member].each do |result|
        attrs = Response.strip_attr_types(result)
        aggregated_results << load(attrs[dynamo_table.hash_key[:attribute_name]], attrs)
      end
      results_returned += response.count
      batch_iteration += 1
    end
  end

  aggregated_results
end

#scan(options = {}) ⇒ Object

:count=>10, :scanned_count=>10, :last_evaluated_key=>“guid”=>{:s=>“11f82550-5c5d-11e3-9b55-d311a43114ca”}} :manual_batching => true|false

return results with last_evaluated_key instead of automatically looping through (useful to throttle or )


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/toy/dynamo/querying.rb', line 82

def scan(options={})
  aggregated_results = []

  batch_size = options.delete(:batch) || DEFAULT_BATCH_SIZE
  max_results_limit = options[:limit]
  options[:limit] = batch_size

  results = dynamo_table.scan(options)
  response = Response.new(results)

  results[:member].each do |result|
    attrs = Response.strip_attr_types(result)
    aggregated_results << load(attrs[dynamo_table.hash_key[:attribute_name]], attrs)
  end

  if response.more_results? && !options[:manual_batching]
    results_returned = response.count
    batch_iteration = 0
    while response.more_results? && batch_iteration < MAX_BATCH_ITERATIONS
      if max_results_limit && (delta_results_limit = (max_results_limit-results_returned)) < batch_size
        break if delta_results_limit == 0
        options.merge!(:limit => delta_results_limit)
      else
        options.merge!(:limit => batch_size)
      end

      results = dynamo_table.scan(options.merge(:exclusive_start_key => response.last_evaluated_key))
      response = Response.new(results)
      results[:member].each do |result|
        attrs = Response.strip_attr_types(result)
        aggregated_results << load(attrs[dynamo_table.hash_key[:attribute_name]], attrs)
      end
      results_returned += response.count
      batch_iteration += 1
    end
  end

  if options[:manual_batching]
    response_hash = {
      :results => aggregated_results,
      :last_evaluated_key => results[:last_evaluated_key]
    }
    response_hash.merge!(:consumed_capacity => results[:consumed_capacity]) if results[:consumed_capacity]
    response_hash
  else
    aggregated_results
  end
end