Module: Toy::Dynamo::Querying::ClassMethods
- Defined in:
- lib/toy/dynamo/querying.rb
Instance Method Summary collapse
- #count_range(hash_value, options = {}) ⇒ Object
-
#read_first(hash_value, options = {}) ⇒ Object
scan.
- #read_multiple(keys, options = nil) ⇒ Object
-
#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).
-
#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 ).
Instance Method Details
#count_range(hash_value, options = {}) ⇒ Object
68 69 70 71 72 |
# File 'lib/toy/dynamo/querying.rb', line 68 def count_range(hash_value, ={}) raise ArgumentError, "no range_key specified for this table" if dynamo_table.range_keys.blank? results = dynamo_table.query(hash_value, .merge(:select => :count)) Response.new(results).count end |
#read_first(hash_value, options = {}) ⇒ Object
scan
140 141 142 |
# File 'lib/toy/dynamo/querying.rb', line 140 def read_first(hash_value, ={}) self.read_range(hash_value, ).first end |
#read_multiple(keys, options = nil) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/toy/dynamo/querying.rb', line 74 def read_multiple(keys, =nil) results_map = {} results = adapter.batch_read(keys, ) 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)
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 58 59 60 61 62 63 64 65 66 |
# File 'lib/toy/dynamo/querying.rb', line 16 def read_range(hash_value, ={}) raise ArgumentError, "no range_key specified for this table" if dynamo_table.range_keys.blank? && global_secondary_indexes.blank? aggregated_results = [] # Useful if doing pagination where you would need the last key evaluated return_last_evaluated_key = .delete(:return_last_evaluated_key) batch_size = .delete(:batch) || DEFAULT_BATCH_SIZE max_results_limit = [:limit] if [:limit] && [:limit] > batch_size .merge!(:limit => batch_size) end results = dynamo_table.query(hash_value, ) 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 .merge!(:limit => delta_results_limit) else .merge!(:limit => batch_size) end results = dynamo_table.query(hash_value, .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 return_last_evaluated_key { last_evaluated_key: response.last_evaluated_key, members: aggregated_results } else aggregated_results end 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 )
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 130 131 132 133 134 135 136 137 138 |
# File 'lib/toy/dynamo/querying.rb', line 91 def scan(={}) aggregated_results = [] batch_size = .delete(:batch) || DEFAULT_BATCH_SIZE max_results_limit = [:limit] [:limit] = batch_size results = dynamo_table.scan() 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? && ![: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 .merge!(:limit => delta_results_limit) else .merge!(:limit => batch_size) end results = dynamo_table.scan(.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 [: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 |