persistence 0.0.1.alpha → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (138) hide show
  1. data/CHANGELOG.md +4 -0
  2. data/README.md +260 -17
  3. data/lib/namespaces.rb +55 -0
  4. data/lib/persistence.rb +38 -3
  5. data/lib/persistence/adapter/abstract.rb +22 -0
  6. data/lib/persistence/adapter/abstract/enable_disable.rb +107 -0
  7. data/lib/persistence/adapter/abstract/primary_key/id_property_string.rb +33 -0
  8. data/lib/persistence/adapter/abstract/primary_key/simple.rb +29 -0
  9. data/lib/persistence/adapter/mock.rb +9 -0
  10. data/lib/persistence/adapter/mock/adapter_interface.rb +102 -0
  11. data/lib/persistence/adapter/mock/bucket.rb +9 -0
  12. data/lib/persistence/adapter/mock/bucket/bucket_interface.rb +260 -0
  13. data/lib/persistence/adapter/mock/bucket/index.rb +9 -0
  14. data/lib/persistence/adapter/mock/bucket/index/index_interface.rb +155 -0
  15. data/lib/persistence/adapter/mock/cursor.rb +9 -0
  16. data/lib/persistence/adapter/mock/cursor/cursor_interface.rb +238 -0
  17. data/lib/persistence/cursor.rb +11 -0
  18. data/lib/persistence/cursor/atomic.rb +110 -0
  19. data/lib/persistence/cursor/cursor_interface.rb +337 -0
  20. data/lib/persistence/exception/block_required.rb +7 -0
  21. data/lib/persistence/exception/conflicting_index_already_declared.rb +7 -0
  22. data/lib/persistence/exception/duplicate_violates_unique_index.rb +7 -0
  23. data/lib/persistence/exception/explicit_index_required.rb +7 -0
  24. data/lib/persistence/exception/indexing_block_failed_to_generate_keys.rb +7 -0
  25. data/lib/persistence/exception/indexing_object_requires_keys.rb +7 -0
  26. data/lib/persistence/exception/key_value_required.rb +7 -0
  27. data/lib/persistence/exception/no_port_enabled.rb +7 -0
  28. data/lib/persistence/object.rb +21 -0
  29. data/lib/persistence/object/autodetermine.rb +74 -0
  30. data/lib/persistence/object/class_instance.rb +1884 -0
  31. data/lib/persistence/object/complex.rb +17 -0
  32. data/lib/persistence/object/complex/array.rb +14 -0
  33. data/lib/persistence/object/complex/array/class_instance.rb +37 -0
  34. data/lib/persistence/object/complex/array/object_instance.rb +54 -0
  35. data/lib/persistence/object/complex/attributes.rb +1808 -0
  36. data/lib/persistence/object/complex/attributes/attributes_array.rb +32 -0
  37. data/lib/persistence/object/complex/attributes/attributes_hash.rb +187 -0
  38. data/lib/persistence/object/complex/attributes/default_atomic_non_atomic.rb +102 -0
  39. data/lib/persistence/object/complex/attributes/hash_to_port.rb +40 -0
  40. data/lib/persistence/object/complex/class_and_object_instance.rb +132 -0
  41. data/lib/persistence/object/complex/class_instance.rb +267 -0
  42. data/lib/persistence/object/complex/complex_object.rb +111 -0
  43. data/lib/persistence/object/complex/hash.rb +14 -0
  44. data/lib/persistence/object/complex/hash/class_instance.rb +40 -0
  45. data/lib/persistence/object/complex/hash/object_instance.rb +63 -0
  46. data/lib/persistence/object/complex/index/attribute_index.rb +10 -0
  47. data/lib/persistence/object/complex/index/attribute_index/attribute_index_interface.rb +43 -0
  48. data/lib/persistence/object/complex/object_instance.rb +469 -0
  49. data/lib/persistence/object/flat.rb +17 -0
  50. data/lib/persistence/object/flat/class_instance.rb +34 -0
  51. data/lib/persistence/object/flat/file.rb +14 -0
  52. data/lib/persistence/object/flat/file/class_instance.rb +122 -0
  53. data/lib/persistence/object/flat/file/contents.rb +7 -0
  54. data/lib/persistence/object/flat/file/file_persistence.rb +147 -0
  55. data/lib/persistence/object/flat/file/object_instance.rb +116 -0
  56. data/lib/persistence/object/flat/file/path.rb +9 -0
  57. data/lib/persistence/object/flat/object_instance.rb +24 -0
  58. data/lib/persistence/object/index.rb +479 -0
  59. data/lib/persistence/object/index/block_index.rb +10 -0
  60. data/lib/persistence/object/index/block_index/block_index_interface.rb +110 -0
  61. data/lib/persistence/object/index/explicit_index.rb +10 -0
  62. data/lib/persistence/object/index/explicit_index/explicit_index_interface.rb +57 -0
  63. data/lib/persistence/object/index_hash.rb +40 -0
  64. data/lib/persistence/object/object_instance.rb +322 -0
  65. data/lib/persistence/object/parse_persistence_args.rb +145 -0
  66. data/lib/persistence/port.rb +9 -0
  67. data/lib/persistence/port/bucket.rb +9 -0
  68. data/lib/persistence/port/bucket/bucket_index.rb +9 -0
  69. data/lib/persistence/port/bucket/bucket_interface.rb +685 -0
  70. data/lib/persistence/port/controller.rb +263 -0
  71. data/lib/persistence/port/port_interface.rb +417 -0
  72. data/lib/requires.rb +146 -0
  73. data/spec/Integration_spec.rb +53 -0
  74. data/spec/Persistence_spec.rb +175 -0
  75. data/spec/example_objects.rb +6 -0
  76. data/spec/example_objects/complex_object.rb +7 -0
  77. data/spec/example_objects/complex_object/array_object.rb +7 -0
  78. data/spec/example_objects/complex_object/hash_object.rb +7 -0
  79. data/spec/example_objects/flat_object.rb +7 -0
  80. data/spec/example_objects/flat_object/file_object.rb +7 -0
  81. data/spec/persistence/adapter/enable_disable_spec.rb +29 -0
  82. data/spec/persistence/adapter/mock/cursor_spec.rb +64 -0
  83. data/spec/persistence/adapter/mock_helpers.rb +27 -0
  84. data/spec/persistence/adapter/mock_helpers/bucket.rb +10 -0
  85. data/spec/persistence/adapter/mock_helpers/integration/dictionary_hash.rb +4 -0
  86. data/spec/persistence/adapter/mock_helpers/integration/note.rb +18 -0
  87. data/spec/persistence/adapter/mock_helpers/integration/notes_array.rb +4 -0
  88. data/spec/persistence/adapter/mock_helpers/integration/user.rb +44 -0
  89. data/spec/persistence/adapter/mock_helpers/integration/user/address.rb +18 -0
  90. data/spec/persistence/adapter/mock_helpers/integration/user/dictionary_entry.rb +12 -0
  91. data/spec/persistence/adapter/mock_helpers/integration/user/sub_account.rb +15 -0
  92. data/spec/persistence/adapter/mock_helpers/object.rb +87 -0
  93. data/spec/persistence/adapter/mock_helpers/port.rb +21 -0
  94. data/spec/persistence/adapter/mock_spec.rb +211 -0
  95. data/spec/persistence/adapter/primary_key/id_property_string_spec.rb +27 -0
  96. data/spec/persistence/adapter/primary_key/simple_spec.rb +19 -0
  97. data/spec/persistence/adapter/spec_abstract/adapter_spec.rb +223 -0
  98. data/spec/persistence/adapter/spec_abstract/cursor_spec.rb +116 -0
  99. data/spec/persistence/cursor/atomic_spec.rb +86 -0
  100. data/spec/persistence/cursor/object_and_class_instance_spec.rb +73 -0
  101. data/spec/persistence/cursor_spec.rb +128 -0
  102. data/spec/persistence/object/complex/attributes/persistence_hash/array_instance_spec.rb +51 -0
  103. data/spec/persistence/object/complex/attributes/persistence_hash/hash_instance_spec.rb +56 -0
  104. data/spec/persistence/object/complex/attributes_spec.rb +1717 -0
  105. data/spec/persistence/object/complex/complex_spec.rb +922 -0
  106. data/spec/persistence/object/complex/index/attribute_index_spec.rb +76 -0
  107. data/spec/persistence/object/flat/bignum_spec.rb +33 -0
  108. data/spec/persistence/object/flat/class_instance_spec.rb +30 -0
  109. data/spec/persistence/object/flat/class_spec.rb +38 -0
  110. data/spec/persistence/object/flat/complex_spec.rb +36 -0
  111. data/spec/persistence/object/flat/false_class_spec.rb +34 -0
  112. data/spec/persistence/object/flat/file/class_instance_spec.rb +54 -0
  113. data/spec/persistence/object/flat/file/object_instance_spec.rb +143 -0
  114. data/spec/persistence/object/flat/file_spec.rb +64 -0
  115. data/spec/persistence/object/flat/fixnum_spec.rb +32 -0
  116. data/spec/persistence/object/flat/float_spec.rb +32 -0
  117. data/spec/persistence/object/flat/indexing_spec.rb +38 -0
  118. data/spec/persistence/object/flat/rational_spec.rb +33 -0
  119. data/spec/persistence/object/flat/regexp_spec.rb +32 -0
  120. data/spec/persistence/object/flat/string_spec.rb +34 -0
  121. data/spec/persistence/object/flat/symbol_spec.rb +32 -0
  122. data/spec/persistence/object/flat/true_class_spec.rb +32 -0
  123. data/spec/persistence/object/indexes/block_index_spec.rb +119 -0
  124. data/spec/persistence/object/indexes/explicit_index_spec.rb +112 -0
  125. data/spec/persistence/object/parse_persistence_args_spec.rb +65 -0
  126. data/spec/persistence/object_spec.rb +310 -0
  127. data/spec/persistence/port/bucket/bucket_interface_spec.rb +146 -0
  128. data/spec/persistence/port/bucket/index/bucket_index_spec.rb +67 -0
  129. data/spec/persistence/port/bucket_spec.rb +20 -0
  130. data/spec/persistence/port/controller_spec.rb +60 -0
  131. data/spec/persistence/port/port_interface_spec.rb +105 -0
  132. metadata +178 -21
  133. data/.gitignore +0 -17
  134. data/Gemfile +0 -4
  135. data/LICENSE +0 -22
  136. data/Rakefile +0 -2
  137. data/lib/persistence/version.rb +0 -3
  138. data/persistence.gemspec +0 -17
@@ -0,0 +1,1884 @@
1
+
2
+ ###
3
+ # Class methods for any objects enabled with persistence capabilities.
4
+ #
5
+ module ::Persistence::Object::ClassInstance
6
+
7
+ include ::Persistence::Object::ParsePersistenceArgs
8
+
9
+ include ::CascadingConfiguration::Setting
10
+ include ::CascadingConfiguration::Hash
11
+
12
+ include ::Enumerable
13
+
14
+ ################################
15
+ # instance_persistence_port= #
16
+ # store_using #
17
+ # persists_using #
18
+ ################################
19
+
20
+ attr_setting :instance_persistence_port
21
+
22
+ ###
23
+ # Assign a persistence port to be used with instances of this object.
24
+ #
25
+ # @overload instance_persistence_port=( port_name )
26
+ #
27
+ # @param port_name Name of port to be used. Expects port by name to be available in Persistence controller.
28
+ #
29
+ # @overload instance_persistence_port=( port_instance )
30
+ #
31
+ # @param port_instance Port instance to use.
32
+ #
33
+ def instance_persistence_port=( port_object_port_or_port_name )
34
+
35
+ port = nil
36
+
37
+ case port_object_port_or_port_name
38
+
39
+ when nil
40
+
41
+ port = super( nil )
42
+
43
+ when ::Persistence::Port
44
+
45
+ port = super( port_object_port_or_port_name )
46
+
47
+ when ::Symbol, ::String
48
+
49
+ port = super( ::Persistence.port_for_name_or_port( port_object_port_or_port_name, true ) )
50
+
51
+ else
52
+
53
+ if port_object_port_or_port_name.respond_to?( :instance_persistence_port )
54
+
55
+ # if arg responds to :instance_persistence_port we use arg's instance port
56
+ port = super( port_object_port_or_port_name.instance_persistence_port )
57
+
58
+ elsif port_object_port_or_port_name.respond_to?( :persistence_port )
59
+
60
+ # if arg responds to :persistence_port we use arg's port
61
+ port = super( port_object_port_or_port_name.persistence_port )
62
+
63
+ end
64
+
65
+ end
66
+
67
+ if port
68
+ # check encapsulation for instance persistence bucket - that way we avoid creating a loop
69
+ encapsulation = ::CascadingConfiguration::Core::Encapsulation.encapsulation( :default )
70
+ if bucket = encapsulation.get_configuration( self, :instance_persistence_bucket )
71
+ if port.enabled?
72
+ bucket.initialize_for_port( port )
73
+ else
74
+ bucket.disable
75
+ end
76
+ end
77
+ port.register_instance( self )
78
+ end
79
+
80
+ return port
81
+
82
+ end
83
+
84
+ alias_method :store_using, :instance_persistence_port=
85
+
86
+ alias_method :persists_using, :instance_persistence_port=
87
+
88
+ ###############################
89
+ # instance_persistence_port #
90
+ ###############################
91
+
92
+ ###
93
+ # Get persistence port that will be used with instances of this object. Will use current port if available and
94
+ # no port is assigned.
95
+ #
96
+ # @return [Persistence::Port,nil] Persistence port instance.
97
+ #
98
+ def instance_persistence_port
99
+
100
+ return super || ( self.instance_persistence_port = ::Persistence.current_port )
101
+
102
+ end
103
+
104
+ ##################################
105
+ # instance_persistence_bucket= #
106
+ # store_as #
107
+ # persists_in #
108
+ ##################################
109
+
110
+ attr_setting :instance_persistence_bucket
111
+
112
+ ###
113
+ # Assign a persistence bucket to be used with instances of this object.
114
+ #
115
+ # @overload instance_persistence_bucket=( bucket_name )
116
+ #
117
+ # @param port_name [Symbol,String] Name of port to be used. Expects port by name to be available
118
+ # in Persistence controller.
119
+ #
120
+ # @overload instance_persistence_bucket=( bucket_instance )
121
+ #
122
+ # @param port_instance [Persistence::Port::Bucket,nil] Persistence::Port::Bucket instance to use.
123
+ #
124
+ def instance_persistence_bucket=( persistence_bucket_class_or_name )
125
+
126
+ bucket = nil
127
+
128
+ case persistence_bucket_class_or_name
129
+
130
+ when nil
131
+
132
+ bucket = super( nil )
133
+
134
+ when ::String, ::Symbol
135
+
136
+ if port = instance_persistence_port
137
+ bucket = super( port.persistence_bucket( persistence_bucket_class_or_name.to_s ) )
138
+ else
139
+ bucket = super( ::Persistence.pending_bucket( self, persistence_bucket_class_or_name.to_s ) )
140
+ end
141
+
142
+ when ::Persistence::Bucket
143
+
144
+ bucket = super( persistence_bucket_class_or_name )
145
+
146
+ else
147
+
148
+ if persistence_bucket_class_or_name.respond_to?( :persistence_bucket )
149
+ bucket = super( persistence_bucket_class_or_name.persistence_bucket )
150
+ end
151
+
152
+ end
153
+
154
+ return bucket
155
+
156
+ end
157
+
158
+ alias_method :store_as, :instance_persistence_bucket=
159
+
160
+ alias_method :persists_in, :instance_persistence_bucket=
161
+
162
+ #################################
163
+ # instance_persistence_bucket #
164
+ #################################
165
+
166
+ ###
167
+ # Get persistence bucket that will be used with instances of this object. Will use name of class if bucket
168
+ # does not already exist.
169
+ #
170
+ # @return [Persistence::Port,nil] Persistence port instance.
171
+ #
172
+ def instance_persistence_bucket
173
+
174
+ bucket_instance = nil
175
+
176
+ encapsulation = ::CascadingConfiguration::Core::Encapsulation.encapsulation( :default )
177
+
178
+ unless bucket_instance = encapsulation.get_configuration( self, :instance_persistence_bucket )
179
+ self.instance_persistence_bucket = to_s
180
+ bucket_instance = super
181
+ end
182
+
183
+ return bucket_instance
184
+
185
+ end
186
+
187
+ #############
188
+ # persist #
189
+ #############
190
+
191
+ ###
192
+ # Retrieve object from persistence port.
193
+ #
194
+ # @overload persist( global_id )
195
+ #
196
+ # @param global_id [Object] Object persistence ID.
197
+ #
198
+ # @overload persist( index_name, key )
199
+ #
200
+ # @param index_name [Symbol,String] Name of index for key-based retrieval.
201
+ # @param key [Object] Key for retrieving object ID.
202
+ #
203
+ # @overload persist( index_name_key_hash )
204
+ #
205
+ # @param index_name_key_hash [Hash{Symbol,String=>Object}] Name of index for key-based retrieval.
206
+ #
207
+ # @overload persist( index_instance, key )
208
+ #
209
+ # @param index_instance [Symbol,String] Name of index for key-based retrieval.
210
+ # @param key [Object] Key for retrieving object ID.
211
+ #
212
+ # @overload persist( index_instance_key_hash )
213
+ #
214
+ # @param index_instance_key_hash [Hash{Persistence::Object::Index=>Object}] Name of index
215
+ # for key-based retrieval.
216
+ #
217
+ # @return [Object,nil] Persisted object.
218
+ #
219
+ def persist( *args )
220
+
221
+ persistence_value = nil
222
+
223
+ index_instance, key, no_key = parse_args_for_index_value_no_value( args )
224
+
225
+ # if no key, open a cursor for a list
226
+ if no_key
227
+
228
+ persistence_value = ::Persistence::Cursor.new( instance_persistence_bucket, index_instance )
229
+
230
+ else
231
+
232
+ global_id = index_instance ? index_instance.get_object_id( key ) : key
233
+
234
+ persistence_value = instance_persistence_port.get_object( global_id )
235
+
236
+ end
237
+
238
+ return persistence_value
239
+
240
+ end
241
+
242
+ ################
243
+ # persisted? #
244
+ ################
245
+
246
+ ###
247
+ # Query whether object is persisted in port.
248
+ #
249
+ # @overload persisted?( global_id )
250
+ #
251
+ # @param global_id [Object] Object persistence ID.
252
+ #
253
+ # @overload persisted?( index_name, key )
254
+ #
255
+ # @param index_name [Symbol,String] Name of index for key-based retrieval.
256
+ # @param key [Object] Key for retrieving object ID.
257
+ #
258
+ # @overload persisted?( index_name_key_hash )
259
+ #
260
+ # @param index_name_key_hash [Hash{Symbol,String=>Object}] Name of index for key-based retrieval.
261
+ #
262
+ # @overload persisted?( index_instance, key )
263
+ #
264
+ # @param index_instance [Symbol,String] Name of index for key-based retrieval.
265
+ # @param key [Object] Key for retrieving object ID.
266
+ #
267
+ # @overload persisted?( index_instance_key_hash )
268
+ #
269
+ # @param index_instance_key_hash [Hash{Persistence::Object::Index=>Object}] Name of index
270
+ # for key-based retrieval.
271
+ #
272
+ # @return [true,false] Whether object is persisted.
273
+ #
274
+ def persisted?( *args )
275
+
276
+ index, key, no_key = parse_args_for_index_value_no_value( args, true )
277
+
278
+ global_id = index ? index.get_object_id( key ) : key
279
+
280
+ return instance_persistence_port.get_bucket_name_for_object_id( global_id ) ? true : false
281
+
282
+ end
283
+
284
+ ############
285
+ # cease! #
286
+ ############
287
+
288
+ ###
289
+ # Remove object properties stored for object ID from persistence bucket and indexes.
290
+ #
291
+ # @overload cease!( global_id )
292
+ #
293
+ # @param global_id [Object] Object persistence ID.
294
+ #
295
+ # @overload cease!( index_name, key )
296
+ #
297
+ # @param index_name [Symbol,String] Name of index for key-based retrieval.
298
+ #
299
+ # @param key [Object] Key for retrieving object ID.
300
+ #
301
+ # @overload cease!( index_name_key_hash )
302
+ #
303
+ # @param index_name_key_hash [Hash{Symbol,String=>Object}] Name of index for key-based retrieval.
304
+ #
305
+ # @overload cease!( index_instance, key )
306
+ #
307
+ # @param index_instance [Symbol,String] Name of index for key-based retrieval.
308
+ #
309
+ # @param key [Object] Key for retrieving object ID.
310
+ #
311
+ # @overload cease!( index_instance_key_hash )
312
+ #
313
+ # @param index_instance_key_hash [Hash{Persistence::Object::Index=>Object}] Name of index
314
+ # for key-based retrieval.
315
+ #
316
+ # @return [Object,nil] Persisted object.
317
+ #
318
+ def cease!( *args )
319
+
320
+ # FIX - future: archive if appropriate (distinct from delete/etc. see draft spec)
321
+
322
+ index, key, no_key = parse_args_for_index_value_no_value( args, true )
323
+
324
+ global_id = index ? index.get_object_id( key ) : key
325
+
326
+ indexes.each do |this_index_name, this_index|
327
+ this_index.delete_keys_for_object_id!( global_id )
328
+ end
329
+
330
+ hash_in_port = instance_persistence_bucket.delete_object!( global_id )
331
+
332
+ return hash_in_port
333
+
334
+ end
335
+
336
+ ###########
337
+ # index #
338
+ ###########
339
+
340
+ ###
341
+ # Get index with given name.
342
+ #
343
+ # @param index_name Name of requested index.
344
+ #
345
+ # @param ensure_exists Throw exception if index does not exist.
346
+ #
347
+ # @return [Persistence::Object::Index,nil] Index instance.
348
+ #
349
+ def index( index_name, ensure_exists = false )
350
+
351
+ index_instance = nil
352
+
353
+ unless index_instance = indexes[ index_name ]
354
+ if ensure_exists
355
+ raise ::ArgumentError, 'No index found by name ' << index_name.to_s + '.'
356
+ end
357
+ end
358
+
359
+ return indexes[ index_name ]
360
+
361
+ end
362
+
363
+ ################
364
+ # has_index? #
365
+ ################
366
+
367
+ ###
368
+ # Query whether index(es) exist for object.
369
+ #
370
+ # @overload has_index?( index_name, ... )
371
+ #
372
+ # @param index_name Name of requested index.
373
+ #
374
+ # @return [true,false] Whether index(es) exist.
375
+ #
376
+ def has_index?( *index_names )
377
+
378
+ has_index = false
379
+
380
+ index_names.each do |this_index_name|
381
+ break unless has_index = indexes.has_key?( this_index_name )
382
+ end
383
+
384
+ return has_index
385
+
386
+ end
387
+
388
+ #################
389
+ # block_index #
390
+ #################
391
+
392
+ ###
393
+ # Create a block index.
394
+ #
395
+ # @overload block_index( index_name, ... )
396
+ #
397
+ # @param index_name Name of index.
398
+ #
399
+ # @yield [object] Block to create index keys on object.
400
+ # @yieldparam object [Object] Object to index.
401
+ #
402
+ # @return [Persistence::Object::Index::BlockIndex] Index instance.
403
+ #
404
+ def block_index( *index_names, & indexing_block )
405
+
406
+ index_names.each do |this_index_name|
407
+ instance = create_block_index( this_index_name, false, & indexing_block )
408
+ end
409
+
410
+ return self
411
+
412
+ end
413
+
414
+ #########################
415
+ # block_index_ordered #
416
+ #########################
417
+
418
+ ###
419
+ # Create an ordered block index. PENDING.
420
+ #
421
+ # @param index_name Name of index.
422
+ #
423
+ # @param ordering_proc Proc for determining sort order. See {::Array#sort_by}.
424
+ #
425
+ # @yield [object] Block to create index keys on object.
426
+ # @yieldparam object [Object] Object to index.
427
+ #
428
+ # @return [Persistence::Object::Index::BlockIndex] Index instance.
429
+ #
430
+ def block_index_ordered( index_name, ordering_proc, & indexing_block )
431
+
432
+ instance = create_block_index( index_name, true, ordering_proc, & indexing_block )
433
+
434
+ return self
435
+
436
+ end
437
+
438
+ #################################
439
+ # block_index_with_duplicates #
440
+ #################################
441
+
442
+ ###
443
+ # Create a block index that permits duplicates.
444
+ #
445
+ # @overload block_index( index_name, ... )
446
+ #
447
+ # @param index_name Name of index.
448
+ #
449
+ # @yield [object] Block to create index keys on object.
450
+ # @yieldparam object [Object] Object to index.
451
+ #
452
+ # @return [Persistence::Object::Index::BlockIndex] Index instance.
453
+ #
454
+ def block_index_with_duplicates( *index_names, & indexing_block )
455
+
456
+ index_names.each do |this_index_name|
457
+ this_instance = create_block_index( this_index_name, true, & indexing_block )
458
+ indexes[ this_index_name ] = block_indexes[ this_index_name ] = this_instance
459
+ end
460
+
461
+ return self
462
+
463
+ end
464
+
465
+ #########################################
466
+ # block_index_ordered_with_duplicates #
467
+ #########################################
468
+
469
+ ###
470
+ # Create an ordered block index that permits duplicates. PENDING.
471
+ #
472
+ # @param index_name Name of index.
473
+ #
474
+ # @param ordering_proc Proc for determining sort order. See {::Array#sort_by}.
475
+ #
476
+ # @param duplicates_ordering_proc Proc for determining sort order of duplicates. See {::Array#sort_by}.
477
+ #
478
+ # @yield [object] Block to create index keys on object.
479
+ # @yieldparam object [Object] Object to index.
480
+ #
481
+ # @return [Persistence::Object::Index::BlockIndex] Index instance.
482
+ #
483
+ def block_index_ordered_with_duplicates( index_name, ordering_proc, duplicates_ordering_proc = nil, & indexing_block )
484
+
485
+ raise 'Pending.'
486
+
487
+ instance = create_block_index( index_name, true, ordering_proc, duplicates_ordering_proc, & indexing_block )
488
+
489
+ indexes[ index_name ] = block_indexes[ index_name ] = instance
490
+
491
+ return self
492
+
493
+ end
494
+
495
+ ######################
496
+ # has_block_index? #
497
+ ######################
498
+
499
+ ###
500
+ # Query whether block index(es) exist for object.
501
+ #
502
+ # overload( index_name, ... )
503
+ #
504
+ # @param index_name Name of requested index.
505
+ #
506
+ # @return [true,false] Whether index(es) exist.
507
+ #
508
+ def has_block_index?( *index_names )
509
+
510
+ has_index = false
511
+
512
+ index_names.each do |this_index_name|
513
+ break unless has_index = block_indexes.has_key?( this_index_name )
514
+ end
515
+
516
+ return has_index
517
+
518
+ end
519
+
520
+ ####################
521
+ # explicit_index #
522
+ ####################
523
+
524
+ ###
525
+ # Create a explicit index.
526
+ #
527
+ # @overload explicit_index( index_name, ... )
528
+ #
529
+ # @param index_name Name of index.
530
+ #
531
+ # @yield [object] Block to create index keys on object.
532
+ # @yieldparam object [Object] Object to index.
533
+ #
534
+ # @return [Persistence::Object::Index::BlockIndex] Index instance.
535
+ #
536
+ def explicit_index( *index_names )
537
+
538
+ index_names.each do |this_index_name|
539
+ instance = create_explicit_index( this_index_name, false )
540
+ indexes[ this_index_name ] = explicit_indexes[ this_index_name ] = instance
541
+ end
542
+
543
+ return self
544
+
545
+ end
546
+
547
+ ############################
548
+ # explicit_index_ordered #
549
+ ############################
550
+
551
+ ###
552
+ # Create an ordered explicit index. PENDING.
553
+ #
554
+ # @overload explicit_index_ordered( index_name, ..., & ordering_block )
555
+ #
556
+ # @param index_name Name of index.
557
+ #
558
+ # @yield [object] Block for determining sort order. See {::Array#sort_by}.
559
+ # @yieldparam object [Object] Object to index.
560
+ #
561
+ # @return [Persistence::Object::Index::BlockIndex] Index instance.
562
+ #
563
+ def explicit_index_ordered( *index_names, & ordering_block )
564
+
565
+ raise 'Pending.'
566
+
567
+ index_names.each do |this_index_name|
568
+ instance = create_explicit_index( this_index_name, false, ordering_block )
569
+ indexes[ this_index_name ] = explicit_indexes[ this_index_name ] = instance
570
+ end
571
+
572
+ return self
573
+
574
+ end
575
+
576
+ ####################################
577
+ # explicit_index_with_duplicates #
578
+ ####################################
579
+
580
+ ###
581
+ # Create a explicit index that permits duplicates.
582
+ #
583
+ # @overload explicit_index( index_name, ... )
584
+ #
585
+ # @param index_name Name of index.
586
+ #
587
+ # @yield [object] Block to create index keys on object.
588
+ # @yieldparam object [Object] Object to index.
589
+ #
590
+ # @return [Persistence::Object::Index::BlockIndex] Index instance.
591
+ #
592
+ def explicit_index_with_duplicates( *index_names )
593
+
594
+ index_names.each do |this_index_name|
595
+ instance = create_explicit_index( this_index_name, true )
596
+ indexes[ this_index_name ] = explicit_indexes[ this_index_name ] = instance
597
+ end
598
+
599
+ return self
600
+
601
+ end
602
+
603
+ ############################################
604
+ # explicit_index_ordered_with_duplicates #
605
+ ############################################
606
+
607
+ ###
608
+ # Create an ordered explicit index that permits duplicates. PENDING.
609
+ #
610
+ # @param index_name Name of index.
611
+ #
612
+ # @param duplicates_ordering_proc Proc for determining sort order of duplicates. See {::Array#sort_by}.
613
+ #
614
+ # @yield [object] Block for determining sort order. See {::Array#sort_by}.
615
+ # @yieldparam object [Object] Object to index.
616
+ #
617
+ # @return [Persistence::Object::Index::BlockIndex] Index instance.
618
+ #
619
+ def explicit_index_ordered_with_duplicates( index_name, duplicates_ordering_proc = nil, & ordering_block )
620
+
621
+ raise 'Pending.'
622
+
623
+ instance = create_explicit_index( this_index_name, true, ordering_block, duplicates_ordering_proc )
624
+ indexes[ index_name ] = explicit_indexes[ index_name ] = instance
625
+
626
+ return self
627
+
628
+ end
629
+
630
+ #########################
631
+ # has_explicit_index? #
632
+ #########################
633
+
634
+ ###
635
+ # Query whether explicit index(es) exist for object.
636
+ #
637
+ # overload( index_name, ... )
638
+ #
639
+ # @param index_name Name of requested index.
640
+ #
641
+ # @return [true,false] Whether index(es) exist.
642
+ #
643
+ def has_explicit_index?( *index_names )
644
+
645
+ has_index = false
646
+
647
+ index_names.each do |index_name|
648
+ break unless has_index = explicit_indexes.has_key?( index_name )
649
+ end
650
+
651
+ return has_index
652
+
653
+ end
654
+
655
+ ##################
656
+ # delete_index #
657
+ ##################
658
+
659
+ ###
660
+ # Delete index(es).
661
+ #
662
+ # @overload delete_index( index_name, ... )
663
+ #
664
+ # @param index_name Name of index to delete.
665
+ #
666
+ # @return self
667
+ #
668
+ def delete_index( *index_names )
669
+
670
+ index_names.each do |this_index_name|
671
+
672
+ this_index = indexes.delete( this_index_name )
673
+ persistence_port.delete_index( self, this_index )
674
+
675
+ case this_index
676
+ when ::Persistence::Object::Index::Explicit
677
+ explicit_indexes.delete( this_index_name )
678
+ when ::Persistence::Object::Index::Block
679
+ block_indexes.delete( this_index_name )
680
+ when ::Persistence::Object::Index::Attribute
681
+ attribute_indexes.delete( this_index_name )
682
+ end
683
+
684
+ end
685
+
686
+ return self
687
+
688
+ end
689
+
690
+ ############
691
+ # cursor #
692
+ ############
693
+
694
+ ###
695
+ # Create and return cursor instance for this bucket.
696
+ #
697
+ # @overload cursor( global_id )
698
+ #
699
+ # @param global_id Object persistence ID for retrieval.
700
+ #
701
+ # @overload cursor( index_name, key )
702
+ #
703
+ # @param index_name Name of index for lookup of object persistence ID.
704
+ #
705
+ # @param key Key to look up in index.
706
+ #
707
+ # @overload cursor( index, key )
708
+ #
709
+ # @param index Index instance for lookup of object persistence ID.
710
+ #
711
+ # @param key Key to look up in index.
712
+ #
713
+ # @return [Persistence::Adapter::Mock::Cursor] New cursor instance.
714
+ #
715
+ def cursor( *args, & block )
716
+
717
+ cursor_instance = nil
718
+
719
+ index_instance, key, no_key = parse_args_for_index_value_no_value( args )
720
+
721
+ if index_instance
722
+
723
+ if no_key
724
+ cursor_instance = index_instance.cursor( & block )
725
+ else
726
+ cursor_instance = index_instance.cursor( key, & block )
727
+ end
728
+
729
+ else
730
+
731
+ if no_key
732
+ instance_persistence_bucket.cursor( & block )
733
+ else
734
+ instance_persistence_bucket.cursor( key, & block )
735
+ end
736
+
737
+ end
738
+
739
+ return cursor_instance
740
+
741
+ end
742
+
743
+ ##########
744
+ # all? #
745
+ ##########
746
+
747
+ ###
748
+ # See Enumerable.
749
+ #
750
+ def all?( index_name = nil, & block )
751
+
752
+ return_value = nil
753
+
754
+ if index_name
755
+ return_value = index( index_name ).all?( & block )
756
+ else
757
+ return_value = super( & block )
758
+ end
759
+
760
+ return return_value
761
+
762
+ end
763
+
764
+ ##########
765
+ # any? #
766
+ ##########
767
+
768
+ ###
769
+ # See Enumerable.
770
+ #
771
+ def any?( index_name = nil, & block )
772
+
773
+ return_value = nil
774
+
775
+ if index_name
776
+ return_value = index( index_name ).any?( & block )
777
+ else
778
+ return_value = super( & block )
779
+ end
780
+
781
+ return return_value
782
+
783
+ end
784
+
785
+ ###########
786
+ # chunk #
787
+ ###########
788
+
789
+ ###
790
+ # See Enumerable.
791
+ #
792
+ def chunk( index_name = nil, & block )
793
+
794
+ return_value = nil
795
+
796
+ if index_name
797
+ return_value = index( index_name ).chunk( & block )
798
+ else
799
+ return_value = super( & block )
800
+ end
801
+
802
+ return return_value
803
+
804
+ end
805
+
806
+ #############
807
+ # collect #
808
+ #############
809
+
810
+ ###
811
+ # See Enumerable.
812
+ #
813
+ def collect( index_name = nil, & block )
814
+
815
+ return_value = nil
816
+
817
+ if index_name
818
+ return_value = index( index_name ).collect( & block )
819
+ else
820
+ return_value = super( & block )
821
+ end
822
+
823
+ return return_value
824
+
825
+ end
826
+
827
+ ####################
828
+ # flat_map #
829
+ # collect_concat #
830
+ ####################
831
+
832
+ ###
833
+ # See Enumerable.
834
+ #
835
+ def flat_map( index_name = nil, & block )
836
+
837
+ return_value = nil
838
+
839
+ if index_name
840
+ return_value = index( index_name ).flat_map( & block )
841
+ else
842
+ return_value = super( & block )
843
+ end
844
+
845
+ return return_value
846
+
847
+ end
848
+ alias_method :collect_concat, :flat_map
849
+
850
+ ###########
851
+ # cycle #
852
+ ###########
853
+
854
+ ###
855
+ # See Enumerable.
856
+ #
857
+ def cycle( index_name = nil, item = nil, & block )
858
+
859
+ return_value = nil
860
+
861
+ if index_name
862
+ return_value = index( index_name ).cycle( item, & block )
863
+ else
864
+ return_value = super( item, & block )
865
+ end
866
+
867
+ return return_value
868
+
869
+ end
870
+
871
+ ############
872
+ # detect #
873
+ ############
874
+
875
+ ###
876
+ # See Enumerable.
877
+ #
878
+ def detect( index_name = nil, if_none = nil, & block )
879
+
880
+ return_value = nil
881
+
882
+ if index_name
883
+ return_value = index( index_name ).detect( if_none, & block )
884
+ else
885
+ return_value = super( if_none, & block )
886
+ end
887
+
888
+ return return_value
889
+
890
+ end
891
+
892
+ ##########
893
+ # drop #
894
+ ##########
895
+
896
+ ###
897
+ # See Enumerable.
898
+ #
899
+ def drop( index_name = nil, number = nil, & block )
900
+
901
+ return_value = nil
902
+
903
+ if index_name
904
+ return_value = index( index_name ).drop( number, & block )
905
+ else
906
+ return_value = super( number, & block )
907
+ end
908
+
909
+ return return_value
910
+
911
+ end
912
+
913
+ ################
914
+ # drop_while #
915
+ ################
916
+
917
+ ###
918
+ # See Enumerable.
919
+ #
920
+ def drop_while( index_name = nil, & block )
921
+
922
+ return_value = nil
923
+
924
+ if index_name
925
+ return_value = index( index_name ).drop_while( & block )
926
+ else
927
+ return_value = super( & block )
928
+ end
929
+
930
+ return return_value
931
+
932
+ end
933
+
934
+ ##########
935
+ # each #
936
+ ##########
937
+
938
+ ###
939
+ # See Enumerable.
940
+ #
941
+ def each( index_name = nil, & block )
942
+
943
+ return_value = nil
944
+
945
+ if index_name
946
+ return_value = index( index_name ).each( & block )
947
+ else
948
+ return_value = instance_persistence_bucket.each( & block )
949
+ end
950
+
951
+ return return_value
952
+
953
+ end
954
+
955
+ ###############
956
+ # each_cons #
957
+ ###############
958
+
959
+ ###
960
+ # See Enumerable.
961
+ #
962
+ def each_cons( index_name = nil, number = nil, & block )
963
+
964
+ return_value = nil
965
+
966
+ if index_name
967
+ return_value = index( index_name ).each_cons( number, & block )
968
+ else
969
+ return_value = super( number, & block )
970
+ end
971
+
972
+ return return_value
973
+
974
+ end
975
+
976
+ ################
977
+ # each_slice #
978
+ ################
979
+
980
+ ###
981
+ # See Enumerable.
982
+ #
983
+ def each_slice( index_name = nil, slice_size = nil, & block )
984
+
985
+ return_value = nil
986
+
987
+ if index_name
988
+ return_value = index( index_name ).each_cons( slice_size, & block )
989
+ else
990
+ return_value = super( slice_size, & block )
991
+ end
992
+
993
+ return return_value
994
+
995
+ end
996
+
997
+ #####################
998
+ # each_with_index #
999
+ #####################
1000
+
1001
+ ###
1002
+ # See Enumerable.
1003
+ #
1004
+ def each_with_index( index_name = nil, *args, & block )
1005
+
1006
+ return_value = nil
1007
+
1008
+ if index_name
1009
+ return_value = index( index_name ).each_with_index( *args, & block )
1010
+ else
1011
+ return_value = super( *args, & block )
1012
+ end
1013
+
1014
+ return return_value
1015
+
1016
+ end
1017
+
1018
+ ######################
1019
+ # each_with_object #
1020
+ ######################
1021
+
1022
+ ###
1023
+ # See Enumerable.
1024
+ #
1025
+ def each_with_object( index_name = nil, object = nil, & block )
1026
+
1027
+ return_value = nil
1028
+
1029
+ if index_name
1030
+ return_value = index( index_name ).each_with_object( object, & block )
1031
+ else
1032
+ return_value = super( object, & block )
1033
+ end
1034
+
1035
+ return return_value
1036
+
1037
+ end
1038
+
1039
+ #############
1040
+ # entries #
1041
+ #############
1042
+
1043
+ ###
1044
+ # See Enumerable.
1045
+ #
1046
+ def entries( index_name = nil, & block )
1047
+
1048
+ return_value = nil
1049
+
1050
+ if index_name
1051
+ return_value = index( index_name ).entries( & block )
1052
+ else
1053
+ return_value = super( & block )
1054
+ end
1055
+
1056
+ return return_value
1057
+
1058
+ end
1059
+
1060
+ ##########
1061
+ # find #
1062
+ ##########
1063
+
1064
+ ###
1065
+ # See Enumerable.
1066
+ #
1067
+ def find( index_name = nil, if_none = nil, & block )
1068
+
1069
+ return_value = nil
1070
+
1071
+ if index_name
1072
+ return_value = index( index_name ).find( if_none, & block )
1073
+ else
1074
+ return_value = super( if_none, & block )
1075
+ end
1076
+
1077
+ return return_value
1078
+
1079
+ end
1080
+
1081
+ ##############
1082
+ # find_all #
1083
+ ##############
1084
+
1085
+ ###
1086
+ # See Enumerable.
1087
+ #
1088
+ def find_all( index_name = nil, & block )
1089
+
1090
+ return_value = nil
1091
+
1092
+ if index_name
1093
+ return_value = index( index_name ).find_all( & block )
1094
+ else
1095
+ return_value = super( & block )
1096
+ end
1097
+
1098
+ return return_value
1099
+
1100
+ end
1101
+
1102
+ ############
1103
+ # select #
1104
+ ############
1105
+
1106
+ ###
1107
+ # See Enumerable.
1108
+ #
1109
+ def select( index_name = nil, & block )
1110
+
1111
+ return_value = nil
1112
+
1113
+ if index_name
1114
+ return_value = index( index_name ).select( & block )
1115
+ else
1116
+ return_value = super( & block )
1117
+ end
1118
+
1119
+ return return_value
1120
+
1121
+ end
1122
+
1123
+ ################
1124
+ # find_index #
1125
+ ################
1126
+
1127
+ ###
1128
+ # See Enumerable.
1129
+ #
1130
+ def find_index( index_name = nil, value = nil, & block )
1131
+
1132
+ return_value = nil
1133
+
1134
+ if index_name
1135
+ return_value = index( index_name ).find_index( value, & block )
1136
+ else
1137
+ return_value = super( value, & block )
1138
+ end
1139
+
1140
+ return return_value
1141
+
1142
+ end
1143
+
1144
+ ###########
1145
+ # first #
1146
+ ###########
1147
+
1148
+ ###
1149
+ # See Enumerable.
1150
+ #
1151
+ def first( index_name = nil, number = nil, & block )
1152
+
1153
+ return_value = nil
1154
+
1155
+ if index_name
1156
+ return_value = index( index_name ).first( number, & block )
1157
+ else
1158
+ return_value = super( number, & block )
1159
+ end
1160
+
1161
+ return return_value
1162
+
1163
+ end
1164
+
1165
+ ##########
1166
+ # grep #
1167
+ ##########
1168
+
1169
+ ###
1170
+ # See Enumerable.
1171
+ #
1172
+ def grep( index_name = nil, pattern = nil, & block )
1173
+
1174
+ return_value = nil
1175
+
1176
+ if index_name
1177
+ return_value = index( index_name ).grep( pattern, & block )
1178
+ else
1179
+ return_value = super( pattern, & block )
1180
+ end
1181
+
1182
+ return return_value
1183
+
1184
+ end
1185
+
1186
+ ##############
1187
+ # group_by #
1188
+ ##############
1189
+
1190
+ ###
1191
+ # See Enumerable.
1192
+ #
1193
+ def group_by( index_name = nil, & block )
1194
+
1195
+ return_value = nil
1196
+
1197
+ if index_name
1198
+ return_value = index( index_name ).group_by( & block )
1199
+ else
1200
+ return_value = super( & block )
1201
+ end
1202
+
1203
+ return return_value
1204
+
1205
+ end
1206
+
1207
+ ##############
1208
+ # include? #
1209
+ # member? #
1210
+ ##############
1211
+
1212
+ ###
1213
+ # See Enumerable.
1214
+ #
1215
+ def include?( index_name = nil, object = nil, & block )
1216
+
1217
+ return_value = nil
1218
+
1219
+ if index_name
1220
+ return_value = index( index_name ).include?( object, & block )
1221
+ else
1222
+ return_value = super( object, & block )
1223
+ end
1224
+
1225
+ return return_value
1226
+
1227
+ end
1228
+ alias_method :member?, :include?
1229
+
1230
+ ############
1231
+ # inject #
1232
+ # reduce #
1233
+ ############
1234
+
1235
+ ###
1236
+ # See Enumerable.
1237
+ #
1238
+ def inject( index_name = nil, initial = nil, sym = nil, & block )
1239
+
1240
+ return_value = nil
1241
+
1242
+ if index_name
1243
+ return_value = index( index_name ).inject( initial, sym, & block )
1244
+ else
1245
+ return_value = super( initial, sym, & block )
1246
+ end
1247
+
1248
+ return return_value
1249
+
1250
+ end
1251
+ alias_method :reduce, :inject
1252
+
1253
+ #########
1254
+ # map #
1255
+ #########
1256
+
1257
+ ###
1258
+ # See Enumerable.
1259
+ #
1260
+ def map( index_name = nil, & block )
1261
+
1262
+ return_value = nil
1263
+
1264
+ if index_name
1265
+ return_value = index( index_name ).map( & block )
1266
+ else
1267
+ return_value = super( & block )
1268
+ end
1269
+
1270
+ return return_value
1271
+
1272
+ end
1273
+
1274
+ #########
1275
+ # max #
1276
+ #########
1277
+
1278
+ ###
1279
+ # See Enumerable.
1280
+ #
1281
+ def max( index_name = nil, & block )
1282
+
1283
+ return_value = nil
1284
+
1285
+ if index_name
1286
+ return_value = index( index_name ).max( & block )
1287
+ else
1288
+ return_value = super( & block )
1289
+ end
1290
+
1291
+ return return_value
1292
+
1293
+ end
1294
+
1295
+ ############
1296
+ # max_by #
1297
+ ############
1298
+
1299
+ ###
1300
+ # See Enumerable.
1301
+ #
1302
+ def max_by( index_name = nil, & block )
1303
+
1304
+ return_value = nil
1305
+
1306
+ if index_name
1307
+ return_value = index( index_name ).max_by( & block )
1308
+ else
1309
+ return_value = super( & block )
1310
+ end
1311
+
1312
+ return return_value
1313
+
1314
+ end
1315
+
1316
+ ############
1317
+ # min_by #
1318
+ ############
1319
+
1320
+ ###
1321
+ # See Enumerable.
1322
+ #
1323
+ def min_by( index_name = nil, & block )
1324
+
1325
+ return_value = nil
1326
+
1327
+ if index_name
1328
+ return_value = index( index_name ).min_by( & block )
1329
+ else
1330
+ return_value = super( & block )
1331
+ end
1332
+
1333
+ return return_value
1334
+
1335
+ end
1336
+
1337
+ ############
1338
+ # minmax #
1339
+ ############
1340
+
1341
+ ###
1342
+ # See Enumerable.
1343
+ #
1344
+ def minmax( index_name = nil, & block )
1345
+
1346
+ return_value = nil
1347
+
1348
+ if index_name
1349
+ return_value = index( index_name ).minmax( & block )
1350
+ else
1351
+ return_value = super( & block )
1352
+ end
1353
+
1354
+ return return_value
1355
+
1356
+ end
1357
+
1358
+ ###############
1359
+ # minmax_by #
1360
+ ###############
1361
+
1362
+ ###
1363
+ # See Enumerable.
1364
+ #
1365
+ def minmax_by( index_name = nil, & block )
1366
+
1367
+ return_value = nil
1368
+
1369
+ if index_name
1370
+ return_value = index( index_name ).minmax_by( & block )
1371
+ else
1372
+ return_value = super( & block )
1373
+ end
1374
+
1375
+ return return_value
1376
+
1377
+ end
1378
+
1379
+ ###########
1380
+ # none? #
1381
+ ###########
1382
+
1383
+ ###
1384
+ # See Enumerable.
1385
+ #
1386
+ def none?( index_name = nil, & block )
1387
+
1388
+ return_value = nil
1389
+
1390
+ if index_name
1391
+ return_value = index( index_name ).none?( & block )
1392
+ else
1393
+ return_value = super( & block )
1394
+ end
1395
+
1396
+ return return_value
1397
+
1398
+ end
1399
+
1400
+ ##########
1401
+ # one? #
1402
+ ##########
1403
+
1404
+ ###
1405
+ # See Enumerable.
1406
+ #
1407
+ def one?( index_name = nil, & block )
1408
+
1409
+ return_value = nil
1410
+
1411
+ if index_name
1412
+ return_value = index( index_name ).one?( & block )
1413
+ else
1414
+ return_value = super( & block )
1415
+ end
1416
+
1417
+ return return_value
1418
+
1419
+ end
1420
+
1421
+ ###############
1422
+ # partition #
1423
+ ###############
1424
+
1425
+ ###
1426
+ # See Enumerable.
1427
+ #
1428
+ def partition( index_name = nil, & block )
1429
+
1430
+ return_value = nil
1431
+
1432
+ if index_name
1433
+ return_value = index( index_name ).partition( & block )
1434
+ else
1435
+ return_value = super( & block )
1436
+ end
1437
+
1438
+ return return_value
1439
+
1440
+ end
1441
+
1442
+ ############
1443
+ # reject #
1444
+ ############
1445
+
1446
+ ###
1447
+ # See Enumerable.
1448
+ #
1449
+ def reject( index_name = nil, & block )
1450
+
1451
+ return_value = nil
1452
+
1453
+ if index_name
1454
+ return_value = index( index_name ).reject( & block )
1455
+ else
1456
+ return_value = super( & block )
1457
+ end
1458
+
1459
+ return return_value
1460
+
1461
+ end
1462
+
1463
+ ##################
1464
+ # reverse_each #
1465
+ ##################
1466
+
1467
+ ###
1468
+ # See Enumerable.
1469
+ #
1470
+ def reverse_each( index_name = nil, *args, & block )
1471
+
1472
+ return_value = nil
1473
+
1474
+ if index_name
1475
+ return_value = index( index_name ).reverse_each( *args, & block )
1476
+ else
1477
+ return_value = super( *args, & block )
1478
+ end
1479
+
1480
+ return return_value
1481
+
1482
+ end
1483
+
1484
+ ##################
1485
+ # slice_before #
1486
+ ##################
1487
+
1488
+ ###
1489
+ # See Enumerable.
1490
+ #
1491
+ def slice_before( index_name = nil, pattern = nil, & block )
1492
+
1493
+ return_value = nil
1494
+
1495
+ if index_name
1496
+ return_value = index( index_name ).slice_before( pattern, & block )
1497
+ else
1498
+ return_value = super( pattern, & block )
1499
+ end
1500
+
1501
+ return return_value
1502
+
1503
+ end
1504
+
1505
+ ##########
1506
+ # sort #
1507
+ ##########
1508
+
1509
+ ###
1510
+ # See Enumerable.
1511
+ #
1512
+ def sort( index_name = nil, & block )
1513
+
1514
+ return_value = nil
1515
+
1516
+ if index_name
1517
+ return_value = index( index_name ).sort( & block )
1518
+ else
1519
+ return_value = super( & block )
1520
+ end
1521
+
1522
+ return return_value
1523
+
1524
+ end
1525
+
1526
+ #############
1527
+ # sort_by #
1528
+ #############
1529
+
1530
+ ###
1531
+ # See Enumerable.
1532
+ #
1533
+ def sort_by( index_name = nil, & block )
1534
+
1535
+ return_value = nil
1536
+
1537
+ if index_name
1538
+ return_value = index( index_name ).sort_by( & block )
1539
+ else
1540
+ return_value = super( & block )
1541
+ end
1542
+
1543
+ return return_value
1544
+
1545
+ end
1546
+
1547
+ ################
1548
+ # take_while #
1549
+ ################
1550
+
1551
+ ###
1552
+ # See Enumerable.
1553
+ #
1554
+ def take_while( index_name = nil, & block )
1555
+
1556
+ return_value = nil
1557
+
1558
+ if index_name
1559
+ return_value = index( index_name ).take_while( & block )
1560
+ else
1561
+ return_value = super( & block )
1562
+ end
1563
+
1564
+ return return_value
1565
+
1566
+ end
1567
+
1568
+ ##########
1569
+ # to_a #
1570
+ ##########
1571
+
1572
+ ###
1573
+ # See Enumerable.
1574
+ #
1575
+ def to_a( index_name = nil, & block )
1576
+
1577
+ return_value = nil
1578
+
1579
+ if index_name
1580
+ return_value = index( index_name ).to_a( & block )
1581
+ else
1582
+ return_value = super( & block )
1583
+ end
1584
+
1585
+ return return_value
1586
+
1587
+ end
1588
+
1589
+ ##########
1590
+ # take #
1591
+ ##########
1592
+
1593
+ ###
1594
+ # See Enumerable.
1595
+ #
1596
+ def take( index_name = nil, number = nil, & block )
1597
+
1598
+ return_value = nil
1599
+
1600
+ if index_name
1601
+ return_value = index( index_name ).take( number, & block )
1602
+ else
1603
+ return_value = super( number, & block )
1604
+ end
1605
+
1606
+ return return_value
1607
+
1608
+ end
1609
+
1610
+ #########
1611
+ # zip #
1612
+ #########
1613
+
1614
+ ###
1615
+ # See Enumerable.
1616
+ #
1617
+ def zip( index_name = nil, *args, & block )
1618
+
1619
+ return_value = nil
1620
+
1621
+ if index_name
1622
+ return_value = index( index_name ).zip( *args, & block )
1623
+ else
1624
+ return_value = super( *args, & block )
1625
+ end
1626
+
1627
+ return return_value
1628
+
1629
+ end
1630
+
1631
+ ###########
1632
+ # count #
1633
+ ###########
1634
+
1635
+ ###
1636
+ # See Enumerable.
1637
+ #
1638
+ def count( index_name = nil, *args, & block )
1639
+
1640
+ return_value = 0
1641
+
1642
+ if index_name
1643
+ return_value = index( index_name, true ).count( *args, & block )
1644
+ else
1645
+ if block_given?
1646
+ return_value = super( & block )
1647
+ elsif args.empty?
1648
+ return_value = instance_persistence_bucket.count
1649
+ else
1650
+ return_value = super( *args )
1651
+ end
1652
+ end
1653
+
1654
+ return return_value
1655
+
1656
+ end
1657
+
1658
+ ###################
1659
+ # persist_first #
1660
+ ###################
1661
+
1662
+ ###
1663
+ # Persist first object in cursor context.
1664
+ #
1665
+ # @overload persist_first( count )
1666
+ #
1667
+ # @param [Integer] count How many objects to persist from start of cursor context.
1668
+ #
1669
+ # @overload persist_first( :index, count )
1670
+ #
1671
+ # @return [Object,Array<Object>] Object or objects requested.
1672
+ #
1673
+ def persist_first( *index_name_and_or_count )
1674
+
1675
+ objects = nil
1676
+ index_name = nil
1677
+ count = 1
1678
+
1679
+ case index_name_or_count = index_name_and_or_count[ 0 ]
1680
+ when Symbol, String
1681
+ index_name = index_name_or_count
1682
+ count_or_nil = index_name_and_or_count[ 1 ]
1683
+ case count_or_nil
1684
+ when Integer
1685
+ count = count_or_nil
1686
+ end
1687
+ when Integer
1688
+ count = index_name_or_count
1689
+ end
1690
+
1691
+ if index_name
1692
+ objects = index( index_name ).cursor.first( count )
1693
+ else
1694
+ objects = instance_persistence_bucket.cursor.first( count )
1695
+ end
1696
+
1697
+ return objects
1698
+
1699
+ end
1700
+
1701
+ ##################
1702
+ # persist_last #
1703
+ ##################
1704
+
1705
+ ###
1706
+ # Persist last object in cursor context.
1707
+ #
1708
+ # @overload persist_last( count )
1709
+ #
1710
+ # @param [Integer] count How many objects to persist from end of cursor context.
1711
+ #
1712
+ # @overload persist_last( :index, count )
1713
+ #
1714
+ # @return [Object,Array<Object>] Object or objects requested.
1715
+ #
1716
+ def persist_last( *index_name_and_or_count )
1717
+
1718
+ objects = nil
1719
+ index_name = nil
1720
+ count = 1
1721
+
1722
+ case index_name_and_or_count[ 0 ]
1723
+ when Symbol, String
1724
+ index_name = index_name_or_count
1725
+ count_or_nil = index_name_and_or_count[ 1 ]
1726
+ case count_or_nil
1727
+ when Integer
1728
+ count = count_or_nil
1729
+ end
1730
+ when Integer
1731
+ count = index_name_or_count
1732
+ end
1733
+
1734
+ if index_name
1735
+ objects = index( index_name ).cursor.last( count )
1736
+ else
1737
+ objects = instance_persistence_bucket.cursor.last( count )
1738
+ end
1739
+
1740
+ return objects
1741
+
1742
+ end
1743
+
1744
+ #################
1745
+ # persist_any #
1746
+ #################
1747
+
1748
+ ###
1749
+ # Persist any object in cursor context.
1750
+ #
1751
+ # @overload persist_any( count )
1752
+ #
1753
+ # @param [Integer] count How many objects to persist from cursor context.
1754
+ #
1755
+ # @overload persist_any( :index, count )
1756
+ #
1757
+ # @return [Object,Array<Object>] Object or objects requested.
1758
+ #
1759
+ def persist_any( *index_name_and_or_count )
1760
+
1761
+ objects = nil
1762
+ index_name = nil
1763
+ count = 1
1764
+
1765
+ case index_name_and_or_count[ 0 ]
1766
+ when Symbol, String
1767
+ index_name = index_name_or_count
1768
+ count_or_nil = index_name_and_or_count[ 1 ]
1769
+ case count_or_nil
1770
+ when Integer
1771
+ count = count_or_nil
1772
+ end
1773
+ when Integer
1774
+ count = index_name_or_count
1775
+ end
1776
+
1777
+ if index_name
1778
+ objects = index( index_name ).cursor.any( count )
1779
+ else
1780
+ objects = instance_persistence_bucket.cursor.any( count )
1781
+ end
1782
+
1783
+ return objects
1784
+
1785
+ end
1786
+
1787
+ ##################################################################################################
1788
+ private ######################################################################################
1789
+ ##################################################################################################
1790
+
1791
+ ###########################
1792
+ # create_explicit_index #
1793
+ ###########################
1794
+
1795
+ ###
1796
+ # Internal helper method for common code creating explicit index.
1797
+ #
1798
+ # @param index_name [Symbol,String] Name of index to create.
1799
+ #
1800
+ # @param permits_duplicates [true,false] Whether index permits duplicates.
1801
+ #
1802
+ # @param sort_by_proc [Proc] Proc to use for sorting objects.
1803
+ #
1804
+ # @param sort_duplicates_by_proc [Proc] Proc to use for sorting duplicate objects.
1805
+ #
1806
+ # @return [Persistence::Object::Internal::ExplicitIndex]
1807
+ #
1808
+ def create_explicit_index( index_name, permits_duplicates, sort_by_proc = nil, sort_duplicates_by_proc = nil )
1809
+
1810
+ ensure_index_does_not_exist( index_name, permits_duplicates )
1811
+
1812
+ index_instance = ::Persistence::Object::Index::ExplicitIndex.new( index_name,
1813
+ instance_persistence_bucket,
1814
+ permits_duplicates,
1815
+ sort_duplicates_by_proc,
1816
+ & sort_by_proc )
1817
+
1818
+ indexes[ index_name ] = explicit_indexes[ index_name ] = index_instance
1819
+
1820
+ return index_instance
1821
+
1822
+ end
1823
+
1824
+ ########################
1825
+ # create_block_index #
1826
+ ########################
1827
+
1828
+ ###
1829
+ # Internal helper method for common code creating explicit index.
1830
+ #
1831
+ # @param index_name [Symbol,String] Name of index to create.
1832
+ #
1833
+ # @param permits_duplicates [true,false] Whether index permits duplicates.
1834
+ #
1835
+ # @param sort_by_proc [Proc] Proc to use for sorting objects.
1836
+ #
1837
+ # @param sort_duplicates_by_proc [Proc] Proc to use for sorting duplicate objects.
1838
+ #
1839
+ # @yield [object] Block to create index keys on object.
1840
+ # @yieldparam object [Object] Object to index.
1841
+ #
1842
+ # @return [Persistence::Object::Internal::ExplicitIndex]
1843
+ #
1844
+ def create_block_index( index_name,
1845
+ permits_duplicates,
1846
+ sort_by_proc = nil,
1847
+ sort_duplicates_by_proc = nil,
1848
+ & indexing_block )
1849
+
1850
+ ensure_index_does_not_exist( index_name, permits_duplicates )
1851
+
1852
+ index_instance = ::Persistence::Object::Index::BlockIndex.new( index_name,
1853
+ instance_persistence_bucket,
1854
+ permits_duplicates,
1855
+ sort_by_proc,
1856
+ sort_duplicates_by_proc,
1857
+ & indexing_block )
1858
+
1859
+ indexes[ index_name ] = block_indexes[ index_name ] = index_instance
1860
+
1861
+ return index_instance
1862
+
1863
+ end
1864
+
1865
+ #################################
1866
+ # ensure_index_does_not_exist #
1867
+ #################################
1868
+
1869
+ ###
1870
+ # Helper that throws exception if index name already exists.
1871
+ #
1872
+ def ensure_index_does_not_exist( index_name, permits_duplicates )
1873
+
1874
+ if index_instance = indexes[ index_name ] and
1875
+ index_instance.permits_duplicates? != permits_duplicates
1876
+
1877
+ raise 'Index ' << index_name.to_s + ' has already been declared, ' <<
1878
+ 'and new duplicates declaration does not match existing declaration.'
1879
+
1880
+ end
1881
+
1882
+ end
1883
+
1884
+ end