Module: DynaModel::Query::ClassMethods
- Defined in:
- lib/dyna_model/query.rb
Instance Method Summary collapse
- #count_range(hash_value, options = {}) ⇒ Object
- #read(hash_value, range_value_or_options = nil, options = nil) ⇒ Object
- #read_first(hash_value, options = {}) ⇒ Object
- #read_guid(guid, options = {}) ⇒ Object
- #read_multiple(keys, options = {}) ⇒ 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
113 114 115 116 117 |
# File 'lib/dyna_model/query.rb', line 113 def count_range(hash_value, ={}) raise ArgumentError, "no range_key specified for this table" if self.dynamo_db_table.range_keys.blank? results = self.dynamo_db_table.query(hash_value, .merge(select: :count)) Response.new(results).count end |
#read(hash_value, range_value_or_options = nil, options = nil) ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/dyna_model/query.rb', line 21 def read(hash_value, =nil, =nil) if self.range_key.nil? item_attrs = self.dynamo_db_table.get_item(hash_value, || {})[:item] return nil if item_attrs.nil? self.obj_from_attrs(Table.values_from_response_hash(item_attrs), ( || {})) else raise ArgumentError, "This table requires a range_key_value" if .nil? self.read_range(hash_value, ( || {}).merge(range: { self.range_key[:attribute_name].to_sym.eq => })).first end end |
#read_first(hash_value, options = {}) ⇒ Object
119 120 121 122 |
# File 'lib/dyna_model/query.rb', line 119 def read_first(hash_value, ={}) [:limit] = 1 self.read_range(hash_value, ).first end |
#read_guid(guid, options = {}) ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/dyna_model/query.rb', line 11 def read_guid(guid, ={}) return nil if guid.blank? if self.range_key hash_value, range_value = guid.split(self.guid_delimiter) self.read(hash_value, range_value, ) else self.read(guid, ) end end |
#read_multiple(keys, options = {}) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/dyna_model/query.rb', line 32 def read_multiple(keys, ={}) [:format] = ([:format] && [:format] == :array) ? :array : :hash results_map = {} results_arr = [] if keys.present? results = self.dynamo_db_table.batch_get_item(keys, ) results[:responses][self.dynamo_db_table_name([:shard_name])].each do |result| attrs = Response.strip_attr_types(result) obj = self.obj_from_attrs(attrs, ) if [:format] == :array results_arr << obj else if self.dynamo_db_table.range_keys.present? && primary_range_key = self.dynamo_db_table.range_keys.find{|rk| rk[:primary_range_key] } (results_map[attrs[self.dynamo_db_table.hash_key[:attribute_name]]] ||= {})[attrs[primary_range_key[:attribute_name]]] = obj else results_map[attrs[self.dynamo_db_table.hash_key[:attribute_name]]] = obj end end end end [:format] == :array ? results_arr : 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)
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 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/dyna_model/query.rb', line 59 def read_range(hash_value, ={}) raise ArgumentError, "no range_key specified for this table" if self.dynamo_db_table.range_keys.blank? && self.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 = self.dynamo_db_table.query(hash_value, ) response = Response.new(results) results[:member].each do |result| attrs = Response.strip_attr_types(result) aggregated_results << self.obj_from_attrs(attrs, ) end if response.more_results? results_returned = response.count batch_iteration = 0 Timeout::timeout(QUERY_TIMEOUT) do while response.more_results? 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 = self.dynamo_db_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 << self.obj_from_attrs(attrs, ) end results_returned += response.count batch_iteration += 1 end 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 )
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/dyna_model/query.rb', line 127 def scan(={}) aggregated_results = [] batch_size = .delete(:batch) || DEFAULT_BATCH_SIZE max_results_limit = [:limit] [:limit] = batch_size results = self.dynamo_db_table.scan() response = Response.new(results) results[:member].each do |result| attrs = Response.strip_attr_types(result) aggregated_results << self.obj_from_attrs(attrs, ) end if response.more_results? && ![:manual_batching] results_returned = response.count batch_iteration = 0 Timeout::timeout(QUERY_TIMEOUT) do while response.more_results? 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 << self.obj_from_attrs(attrs, ) end results_returned += response.count batch_iteration += 1 end 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 |