ocean-dynamo 1.2.1 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9093b06c18bfab10ad838fabcfa6b193cbd8bfe7
4
- data.tar.gz: 8343febaa0690023516a27585d2d1857710b94ff
3
+ metadata.gz: d3c55ae49ff1ad852246e6af0b6727f284a10acc
4
+ data.tar.gz: 17c013a3f22302b9a48a117a83e800e35297fb35
5
5
  SHA512:
6
- metadata.gz: 1cc8858fec48e9796f12d60120989f0fad06a04cbcc15805b8af58551efb2d673fc56360efea679d1ebd4453d32aa417c18a30a1a40a2fbc17ee0573db3dd8be
7
- data.tar.gz: f1246850490184ce41e24044207924ec2ecc24aeb1dd180e1f3c8c42c671ca446161d2ed67ed93c154ad98d3b18f288b17fb08eb89fddee83b09153831f746fb
6
+ metadata.gz: 16351feeb2ff2d90ea76d87c9c352c752fc0d29eab5c77d8dc522d39779c69835ce90b88599823ffed8a638c01d26c1556807f91533c0a71337d0600bbc1a93a
7
+ data.tar.gz: b9cdc4ea2f37f4dabd048264e61bc96f79b504b4d9fc6f89e94519e6f9c5f66a6bd097f614f35a2698296ba72ff55b756395752dac95b3bed0c0823f2fe1edd7
@@ -216,12 +216,10 @@ the possibility to define both local and secondary indices for Tables.
216
216
 
217
217
  == Secondary Indices
218
218
 
219
- We now have basic support for secondary indices.
220
-
221
- We will add high-level support for specifying that a secondary index is to be used
222
- in +in_batches+ and +find_each+ etc, but for now you will have to specify that a
223
- secondary index is to be used explicitly in the options passed to +in_batches+,
224
- +query+, and +scan+.
219
+ We now have basic support for secondary indices. High-level support for global secondary indices
220
+ is also available; we will add high-level support for local secondary indices very soon.
221
+ For now you will have to specify that a secondary index is to be used explicitly in the options
222
+ passed to +in_batches+, +query+, and +scan+.
225
223
  * +query+: http://docs.aws.amazon.com/sdkforruby/api/Aws/DynamoDB/Table.html#query-instance_method
226
224
  * +scan+: http://docs.aws.amazon.com/sdkforruby/api/Aws/DynamoDB/Table.html#scan-instance_method
227
225
  * +in_batches+, +find_each+: lib/ocean-dynamo/queries.rb
@@ -277,6 +275,30 @@ Each +global_secondary_index+ clause takes the following arguments:
277
275
  * +:read_capacity_units+ (defaults to the table's read capacity, normally 10)
278
276
  * +:write_capacity_units+ (default to the table's write capacity, normally 5)
279
277
 
278
+ High-level support for global secondary indices is now available through
279
+ +find_global_each+ and +find_global+. They take the same arguments; the former
280
+ yields to a block for each item, the other returns all items in an array.
281
+
282
+ The following finds all Authentications whose +:token+ is "quux"
283
+
284
+ Authentication.find_global(:token, "quux")
285
+
286
+ This retrieves all Authentications belonging to the user with the ID "dfstw-ruyhdf-ewijf",
287
+ sorted in ascending order of the +:expires_at+ attribute:
288
+
289
+ Authentication.find_global(:api_user_id, "dfstw-ruyhdf-ewijf",
290
+ :expires_at, ">=", 0)
291
+
292
+ To get the highest +:expires_at+ record, execute the following:
293
+
294
+ Authentication.find_global(:api_user_id, "dfstw-ruyhdf-ewijf",
295
+ :expires_at, ">=", 0,
296
+ scan_index_forward: false, limit: 1)
297
+
298
+ The combination of hash and range key must have been explicitly declared using
299
+ +global_secondary_index+. For more information, see the documentation of
300
+ +find_global_each+ and +find_global+.
301
+
280
302
 
281
303
  == Installation
282
304
 
@@ -113,10 +113,7 @@ module OceanDynamo
113
113
  def condition_options(child_class)
114
114
  hash_key = child_class.table_hash_key
115
115
  range_key = child_class.table_range_key
116
- { expression_attribute_names: { "#H" => hash_key, "#R" => range_key },
117
- key_condition_expression: "#H = :hashval AND #R >= :rangeval",
118
- expression_attribute_values: { ":hashval" => id, ":rangeval" => "0" }
119
- }
116
+ child_class.condition_builder(hash_key, id, range_key, ">=", "0", consistent: true)
120
117
  end
121
118
 
122
119
 
@@ -111,5 +111,89 @@ module OceanDynamo
111
111
  # def find_in_batches(start: nil, batch_size: 1000)
112
112
  # []
113
113
  # end
114
+
115
+
116
+ def condition_builder(hash_key, hash_value,
117
+ range_key=nil, comparator=nil, range_value=nil,
118
+ limit: nil, consistent: false, scan_index_forward: true)
119
+ if range_key
120
+ options = {
121
+ expression_attribute_names: { "#H" => hash_key, "#R" => range_key },
122
+ key_condition_expression: "#H = :hashval AND #R #{comparator} :rangeval",
123
+ expression_attribute_values: { ":hashval" => hash_value, ":rangeval" => range_value }
124
+ }
125
+ else
126
+ options = {
127
+ expression_attribute_names: { "#H" => hash_key },
128
+ key_condition_expression: "#H = :hashval",
129
+ expression_attribute_values: { ":hashval" => hash_value }
130
+ }
131
+ end
132
+ options[:limit] = limit if limit
133
+ options[:consistent_read] = consistent if consistent
134
+ options[:scan_index_forward] = scan_index_forward if !scan_index_forward
135
+ options
136
+ end
137
+
138
+
139
+ #
140
+ # This method finds each item of a global secondary index, sequentially yielding each item
141
+ # to the given block (required). The parameters are as follows:
142
+ #
143
+ # +hash_key+ The name of the hash key to use (required).
144
+ # +hash_value+ The value of the hash key to match (required).
145
+ # +range_key+ The name of the range key to use (optional).
146
+ # +comparator+ The comparator to use. "=", "<", ">", "<=", ">=". (optional).
147
+ # +range-value+ The value of the range key to match (optional).
148
+ #
149
+ # Note that +range_key+ is optional, but if it's present, then the +comparator+ and
150
+ # the +range_value+ must also be given. They must either all be present or absent.
151
+ #
152
+ # The following keyword arguments are accepted:
153
+ #
154
+ # +:limit+ The maximum number of items to read.
155
+ # +:scan_index_forward+ If false, items will be in reverse order.
156
+ #
157
+ # If the index contains all attributes, no extra read will be performed. If it doesn't,
158
+ # the entire item will be fetched using an extra read operation.
159
+ #
160
+ def find_global_each(hash_key, hash_value,
161
+ range_key=nil, comparator=nil, range_value=nil,
162
+ limit: nil, scan_index_forward: true,
163
+ &block)
164
+ hash_value = hash_value.to_i if hash_value.is_a?(Time)
165
+ range_value = range_value.to_i if range_value.is_a?(Time)
166
+ options = condition_builder(hash_key, hash_value, range_key, comparator, range_value,
167
+ limit: limit, scan_index_forward: scan_index_forward)
168
+ index_name = (range_key ? "#{hash_key}_#{range_key}" : hash_key.to_s) + "_global"
169
+ options[:index_name] = index_name
170
+ raise "Undefined global index: #{index_name}" unless global_secondary_indexes[index_name]
171
+ all_projected = global_secondary_indexes[index_name]["projection_type"] == "ALL"
172
+ in_batches :query, options do |attrs|
173
+ if limit
174
+ return if limit <= 0
175
+ limit = limit - 1
176
+ end
177
+ if all_projected
178
+ yield new._setup_from_dynamo(attrs)
179
+ else
180
+ yield find(attrs[table_hash_key.to_s], table_range_key && attrs[table_range_key.to_s])
181
+ end
182
+ end
183
+ end
184
+
185
+
186
+ #
187
+ # This method takes the same args as +find_global_each+ but returns all found items as
188
+ # an array.
189
+ #
190
+ def find_global(*args)
191
+ result = []
192
+ find_global_each(*args) do |item|
193
+ result << item
194
+ end
195
+ result
196
+ end
197
+
114
198
  end
115
199
  end
@@ -44,10 +44,10 @@ module OceanDynamo
44
44
  read_capacity_units: table_read_capacity_units,
45
45
  write_capacity_units: table_write_capacity_units)
46
46
  if range_key
47
- name = "#{hash_key}_#{range_key}"
47
+ name = "#{hash_key}_#{range_key}_global"
48
48
  keys = [hash_key.to_s, range_key.to_s]
49
49
  else
50
- name = "#{hash_key}"
50
+ name = "#{hash_key}_global"
51
51
  keys = [hash_key.to_s]
52
52
  end
53
53
  self.global_secondary_indexes[name] = {
@@ -1,3 +1,3 @@
1
1
  module OceanDynamo
2
- VERSION = "1.2.1"
2
+ VERSION = "1.2.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ocean-dynamo
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Bengtson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-18 00:00:00.000000000 Z
11
+ date: 2015-09-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk