ocean-dynamo 1.2.3 → 1.2.4

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: 577f687042b84996c0c8b6490f970a3827f90b4f
4
- data.tar.gz: 8632b8095f13fa066e3ee63831e723b5a9398c47
3
+ metadata.gz: eeab3b16bea7186a7a996113e1de39a7f9d21651
4
+ data.tar.gz: 9bf14c5d5521673acb6637bd03885f02a7ec05e6
5
5
  SHA512:
6
- metadata.gz: ec0edf51b620ed1082879309efd47d53e5bcbd97bb4689f8c294c74f930cace03389afbc28479d81f080ace84aceeb4590bda4dd57a9b6a78c9ce39ae3ee28d1
7
- data.tar.gz: fda47b459b7c0e71977dce1490bdea63d71668dfb38cf6bc568a589c5d625411b0f7a129cfc44f4820847e0f5787db7ecbef068b5c205edb1d113e7eb7891d14
6
+ metadata.gz: 71dd4bed657bb9fe67ec15f422f3007bbee26ed88bf5880bc2e569dafdf0a1726260b7d73eb0ec9598a3d7bc2565e9a9c0940aa875fbe268afa247d1a8df2de5
7
+ data.tar.gz: 2558c9434f37ce7af79a34ced28eaf87f1257e6c0dbe7a0b8d2be6cab1d6966c8d345341b9ef0dbd2875b860d00cb85a3a55f47436882177fdb2177bb0f930a0
@@ -33,9 +33,8 @@ with FactoryGirl.
33
33
 
34
34
  === Current State
35
35
 
36
- * Secondary indices are now supported! See below for more information.
37
- * Version 2 of the AWS Ruby SDK is now used. This required an internal reorganisation,
38
- but it also gives us access to local and global secondary indices.
36
+ * Secondary indices are now fully supported! See below for more information.
37
+ * Version 2 of the AWS Ruby SDK is now used.
39
38
  * Work begun on association proxies, etc.
40
39
 
41
40
 
@@ -216,13 +215,7 @@ the possibility to define both local and secondary indices for Tables.
216
215
 
217
216
  == Secondary Indices
218
217
 
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+.
223
- * +query+: http://docs.aws.amazon.com/sdkforruby/api/Aws/DynamoDB/Table.html#query-instance_method
224
- * +scan+: http://docs.aws.amazon.com/sdkforruby/api/Aws/DynamoDB/Table.html#scan-instance_method
225
- * +in_batches+, +find_each+: lib/ocean-dynamo/queries.rb
218
+ We now have support for secondary indices.
226
219
 
227
220
  === Local Secondary Indices
228
221
 
@@ -249,6 +242,27 @@ combination of keys. Secondary indices don't require the range key to be unique
249
242
  the same hash key. This means that secondary index searches always will return a
250
243
  collection.
251
244
 
245
+ High-level support for local secondary indices is now available through
246
+ +find_local_each+ and +find_local+. They take the same arguments; the former
247
+ yields to a block for each item, the other returns all items in an array.
248
+
249
+ The following finds all Authentications where +:username+ is "joe" and +:token+ is "quux":
250
+
251
+ Authentication.find_local(:username, "joe", :token, "=", "quux")
252
+
253
+ This retrieves all Authentications belonging to Joe, sorted on +:token+:
254
+
255
+ Authentication.find_local(:username, "joe", :token, ">=", "0")
256
+
257
+ The same thing but with the only the item with the highest token value:
258
+
259
+ Authentication.find_local(:username, "joe", :token, ">=", "0",
260
+ scan_index_forward: false, limit: 1)
261
+
262
+ For more information, see the documentation for
263
+ +find_local_each+ and +find_local+.
264
+
265
+
252
266
  === Global Secondary Indices
253
267
 
254
268
  Global secondary indices are declared after all attributes, but still within the +do+
@@ -296,7 +310,7 @@ To get the highest +:expires_at+ record, execute the following:
296
310
  scan_index_forward: false, limit: 1)
297
311
 
298
312
  The combination of hash and range key must have been explicitly declared using
299
- +global_secondary_index+. For more information, see the documentation of
313
+ +global_secondary_index+. For more information, see the documentation for
300
314
  +find_global_each+ and +find_global+.
301
315
 
302
316
 
@@ -113,9 +113,15 @@ module OceanDynamo
113
113
  # end
114
114
 
115
115
 
116
+ #
117
+ # This helper constructs the +options+ hash for a subsequent call to +in_batches+ and friends.
118
+ # It takes care of creating expression attributes names and values for all data and takes
119
+ # parameters to control scan direction and limits, etc.
120
+ #
116
121
  def condition_builder(hash_key, hash_value,
117
122
  range_key=nil, comparator=nil, range_value=nil,
118
- limit: nil, consistent: false, scan_index_forward: true)
123
+ limit: nil, consistent: false, scan_index_forward: true,
124
+ select: nil)
119
125
  if range_key
120
126
  options = {
121
127
  expression_attribute_names: { "#H" => hash_key, "#R" => range_key },
@@ -132,6 +138,7 @@ module OceanDynamo
132
138
  options[:limit] = limit if limit
133
139
  options[:consistent_read] = consistent if consistent
134
140
  options[:scan_index_forward] = scan_index_forward if !scan_index_forward
141
+ options[:select] = select.to_s.upcase if select
135
142
  options
136
143
  end
137
144
 
@@ -195,5 +202,61 @@ module OceanDynamo
195
202
  result
196
203
  end
197
204
 
205
+
206
+ #
207
+ # This method finds each item of a local secondary index, sequentially yielding each item
208
+ # to the given block (required). The parameters are as follows:
209
+ #
210
+ # +hash_key+ The name of the hash key to use (required, must be the table's hash_key).
211
+ # +hash_value+ The value of the hash key to match (required).
212
+ # +range_key+ The name of the range key to use (required).
213
+ # +comparator+ The comparator to use. "=", "<", ">", "<=", ">=". (required).
214
+ # +range-value+ The value of the range key to match (required).
215
+ #
216
+ # Note that +range_key+ must all be present.
217
+ #
218
+ # The following keyword arguments are accepted:
219
+ #
220
+ # +:limit+ The maximum number of items to read.
221
+ # +:scan_index_forward+ If false, items will be in reverse order.
222
+ # +:consistent+ If true, consistent reads will be used. Default false.
223
+ #
224
+ def find_local_each(hash_key, hash_value,
225
+ range_key, comparator, range_value,
226
+ limit: nil, scan_index_forward: true, consistent: false,
227
+ &block)
228
+ raise "The hash_key is #{hash_key.inspect} but must be #{table_hash_key.inspect}" unless hash_key == table_hash_key
229
+ hash_value = hash_value.to_i if hash_value.is_a?(Time)
230
+ range_value = range_value.to_i if range_value.is_a?(Time)
231
+ options = condition_builder(hash_key, hash_value, range_key, comparator, range_value,
232
+ select: :all_attributes,
233
+ limit: limit, scan_index_forward: scan_index_forward,
234
+ consistent: consistent)
235
+ index_name = range_key.to_s
236
+ options[:index_name] = index_name
237
+ raise "Undefined local index: #{index_name}" unless local_secondary_indexes.include?(index_name)
238
+ in_batches :query, options do |attrs|
239
+ if limit
240
+ return if limit <= 0
241
+ limit = limit - 1
242
+ end
243
+ yield new._setup_from_dynamo(attrs)
244
+ end
245
+ end
246
+
247
+
248
+ #
249
+ # This method takes the same args as +find_local_each+ but returns all found items as
250
+ # an array.
251
+ #
252
+ def find_local(*args)
253
+ result = []
254
+ find_local_each(*args) do |item|
255
+ result << item
256
+ end
257
+ result
258
+ end
259
+
260
+
198
261
  end
199
262
  end
@@ -1,3 +1,3 @@
1
1
  module OceanDynamo
2
- VERSION = "1.2.3"
2
+ VERSION = "1.2.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ocean-dynamo
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.3
4
+ version: 1.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Bengtson