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.
- data/CHANGELOG.md +4 -0
- data/README.md +260 -17
- data/lib/namespaces.rb +55 -0
- data/lib/persistence.rb +38 -3
- data/lib/persistence/adapter/abstract.rb +22 -0
- data/lib/persistence/adapter/abstract/enable_disable.rb +107 -0
- data/lib/persistence/adapter/abstract/primary_key/id_property_string.rb +33 -0
- data/lib/persistence/adapter/abstract/primary_key/simple.rb +29 -0
- data/lib/persistence/adapter/mock.rb +9 -0
- data/lib/persistence/adapter/mock/adapter_interface.rb +102 -0
- data/lib/persistence/adapter/mock/bucket.rb +9 -0
- data/lib/persistence/adapter/mock/bucket/bucket_interface.rb +260 -0
- data/lib/persistence/adapter/mock/bucket/index.rb +9 -0
- data/lib/persistence/adapter/mock/bucket/index/index_interface.rb +155 -0
- data/lib/persistence/adapter/mock/cursor.rb +9 -0
- data/lib/persistence/adapter/mock/cursor/cursor_interface.rb +238 -0
- data/lib/persistence/cursor.rb +11 -0
- data/lib/persistence/cursor/atomic.rb +110 -0
- data/lib/persistence/cursor/cursor_interface.rb +337 -0
- data/lib/persistence/exception/block_required.rb +7 -0
- data/lib/persistence/exception/conflicting_index_already_declared.rb +7 -0
- data/lib/persistence/exception/duplicate_violates_unique_index.rb +7 -0
- data/lib/persistence/exception/explicit_index_required.rb +7 -0
- data/lib/persistence/exception/indexing_block_failed_to_generate_keys.rb +7 -0
- data/lib/persistence/exception/indexing_object_requires_keys.rb +7 -0
- data/lib/persistence/exception/key_value_required.rb +7 -0
- data/lib/persistence/exception/no_port_enabled.rb +7 -0
- data/lib/persistence/object.rb +21 -0
- data/lib/persistence/object/autodetermine.rb +74 -0
- data/lib/persistence/object/class_instance.rb +1884 -0
- data/lib/persistence/object/complex.rb +17 -0
- data/lib/persistence/object/complex/array.rb +14 -0
- data/lib/persistence/object/complex/array/class_instance.rb +37 -0
- data/lib/persistence/object/complex/array/object_instance.rb +54 -0
- data/lib/persistence/object/complex/attributes.rb +1808 -0
- data/lib/persistence/object/complex/attributes/attributes_array.rb +32 -0
- data/lib/persistence/object/complex/attributes/attributes_hash.rb +187 -0
- data/lib/persistence/object/complex/attributes/default_atomic_non_atomic.rb +102 -0
- data/lib/persistence/object/complex/attributes/hash_to_port.rb +40 -0
- data/lib/persistence/object/complex/class_and_object_instance.rb +132 -0
- data/lib/persistence/object/complex/class_instance.rb +267 -0
- data/lib/persistence/object/complex/complex_object.rb +111 -0
- data/lib/persistence/object/complex/hash.rb +14 -0
- data/lib/persistence/object/complex/hash/class_instance.rb +40 -0
- data/lib/persistence/object/complex/hash/object_instance.rb +63 -0
- data/lib/persistence/object/complex/index/attribute_index.rb +10 -0
- data/lib/persistence/object/complex/index/attribute_index/attribute_index_interface.rb +43 -0
- data/lib/persistence/object/complex/object_instance.rb +469 -0
- data/lib/persistence/object/flat.rb +17 -0
- data/lib/persistence/object/flat/class_instance.rb +34 -0
- data/lib/persistence/object/flat/file.rb +14 -0
- data/lib/persistence/object/flat/file/class_instance.rb +122 -0
- data/lib/persistence/object/flat/file/contents.rb +7 -0
- data/lib/persistence/object/flat/file/file_persistence.rb +147 -0
- data/lib/persistence/object/flat/file/object_instance.rb +116 -0
- data/lib/persistence/object/flat/file/path.rb +9 -0
- data/lib/persistence/object/flat/object_instance.rb +24 -0
- data/lib/persistence/object/index.rb +479 -0
- data/lib/persistence/object/index/block_index.rb +10 -0
- data/lib/persistence/object/index/block_index/block_index_interface.rb +110 -0
- data/lib/persistence/object/index/explicit_index.rb +10 -0
- data/lib/persistence/object/index/explicit_index/explicit_index_interface.rb +57 -0
- data/lib/persistence/object/index_hash.rb +40 -0
- data/lib/persistence/object/object_instance.rb +322 -0
- data/lib/persistence/object/parse_persistence_args.rb +145 -0
- data/lib/persistence/port.rb +9 -0
- data/lib/persistence/port/bucket.rb +9 -0
- data/lib/persistence/port/bucket/bucket_index.rb +9 -0
- data/lib/persistence/port/bucket/bucket_interface.rb +685 -0
- data/lib/persistence/port/controller.rb +263 -0
- data/lib/persistence/port/port_interface.rb +417 -0
- data/lib/requires.rb +146 -0
- data/spec/Integration_spec.rb +53 -0
- data/spec/Persistence_spec.rb +175 -0
- data/spec/example_objects.rb +6 -0
- data/spec/example_objects/complex_object.rb +7 -0
- data/spec/example_objects/complex_object/array_object.rb +7 -0
- data/spec/example_objects/complex_object/hash_object.rb +7 -0
- data/spec/example_objects/flat_object.rb +7 -0
- data/spec/example_objects/flat_object/file_object.rb +7 -0
- data/spec/persistence/adapter/enable_disable_spec.rb +29 -0
- data/spec/persistence/adapter/mock/cursor_spec.rb +64 -0
- data/spec/persistence/adapter/mock_helpers.rb +27 -0
- data/spec/persistence/adapter/mock_helpers/bucket.rb +10 -0
- data/spec/persistence/adapter/mock_helpers/integration/dictionary_hash.rb +4 -0
- data/spec/persistence/adapter/mock_helpers/integration/note.rb +18 -0
- data/spec/persistence/adapter/mock_helpers/integration/notes_array.rb +4 -0
- data/spec/persistence/adapter/mock_helpers/integration/user.rb +44 -0
- data/spec/persistence/adapter/mock_helpers/integration/user/address.rb +18 -0
- data/spec/persistence/adapter/mock_helpers/integration/user/dictionary_entry.rb +12 -0
- data/spec/persistence/adapter/mock_helpers/integration/user/sub_account.rb +15 -0
- data/spec/persistence/adapter/mock_helpers/object.rb +87 -0
- data/spec/persistence/adapter/mock_helpers/port.rb +21 -0
- data/spec/persistence/adapter/mock_spec.rb +211 -0
- data/spec/persistence/adapter/primary_key/id_property_string_spec.rb +27 -0
- data/spec/persistence/adapter/primary_key/simple_spec.rb +19 -0
- data/spec/persistence/adapter/spec_abstract/adapter_spec.rb +223 -0
- data/spec/persistence/adapter/spec_abstract/cursor_spec.rb +116 -0
- data/spec/persistence/cursor/atomic_spec.rb +86 -0
- data/spec/persistence/cursor/object_and_class_instance_spec.rb +73 -0
- data/spec/persistence/cursor_spec.rb +128 -0
- data/spec/persistence/object/complex/attributes/persistence_hash/array_instance_spec.rb +51 -0
- data/spec/persistence/object/complex/attributes/persistence_hash/hash_instance_spec.rb +56 -0
- data/spec/persistence/object/complex/attributes_spec.rb +1717 -0
- data/spec/persistence/object/complex/complex_spec.rb +922 -0
- data/spec/persistence/object/complex/index/attribute_index_spec.rb +76 -0
- data/spec/persistence/object/flat/bignum_spec.rb +33 -0
- data/spec/persistence/object/flat/class_instance_spec.rb +30 -0
- data/spec/persistence/object/flat/class_spec.rb +38 -0
- data/spec/persistence/object/flat/complex_spec.rb +36 -0
- data/spec/persistence/object/flat/false_class_spec.rb +34 -0
- data/spec/persistence/object/flat/file/class_instance_spec.rb +54 -0
- data/spec/persistence/object/flat/file/object_instance_spec.rb +143 -0
- data/spec/persistence/object/flat/file_spec.rb +64 -0
- data/spec/persistence/object/flat/fixnum_spec.rb +32 -0
- data/spec/persistence/object/flat/float_spec.rb +32 -0
- data/spec/persistence/object/flat/indexing_spec.rb +38 -0
- data/spec/persistence/object/flat/rational_spec.rb +33 -0
- data/spec/persistence/object/flat/regexp_spec.rb +32 -0
- data/spec/persistence/object/flat/string_spec.rb +34 -0
- data/spec/persistence/object/flat/symbol_spec.rb +32 -0
- data/spec/persistence/object/flat/true_class_spec.rb +32 -0
- data/spec/persistence/object/indexes/block_index_spec.rb +119 -0
- data/spec/persistence/object/indexes/explicit_index_spec.rb +112 -0
- data/spec/persistence/object/parse_persistence_args_spec.rb +65 -0
- data/spec/persistence/object_spec.rb +310 -0
- data/spec/persistence/port/bucket/bucket_interface_spec.rb +146 -0
- data/spec/persistence/port/bucket/index/bucket_index_spec.rb +67 -0
- data/spec/persistence/port/bucket_spec.rb +20 -0
- data/spec/persistence/port/controller_spec.rb +60 -0
- data/spec/persistence/port/port_interface_spec.rb +105 -0
- metadata +178 -21
- data/.gitignore +0 -17
- data/Gemfile +0 -4
- data/LICENSE +0 -22
- data/Rakefile +0 -2
- data/lib/persistence/version.rb +0 -3
- data/persistence.gemspec +0 -17
@@ -0,0 +1,10 @@
|
|
1
|
+
|
2
|
+
###
|
3
|
+
# Class used for block index instances, which index keys provided by running a Proc on an object instance.
|
4
|
+
# Interface provided in module so interface can be easily overridden.
|
5
|
+
#
|
6
|
+
class ::Persistence::Object::Index::BlockIndex
|
7
|
+
|
8
|
+
include ::Persistence::Object::Index::BlockIndex::BlockIndexInterface
|
9
|
+
|
10
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
|
2
|
+
###
|
3
|
+
# Interface for block index instances, which index keys provided by running a Proc on an object instance.
|
4
|
+
#
|
5
|
+
module ::Persistence::Object::Index::BlockIndex::BlockIndexInterface
|
6
|
+
|
7
|
+
include ::Persistence::Object::Index
|
8
|
+
|
9
|
+
include ::CascadingConfiguration::Setting
|
10
|
+
include ::CascadingConfiguration::Array
|
11
|
+
|
12
|
+
###
|
13
|
+
#
|
14
|
+
# @yield [object] Block to create index keys on object.
|
15
|
+
# @yieldparam object [Object] Object to index.
|
16
|
+
#
|
17
|
+
def initialize( index_name,
|
18
|
+
parent_bucket,
|
19
|
+
permits_duplicates = nil,
|
20
|
+
sorting_proc_or_sort_names = nil,
|
21
|
+
duplicates_sorting_proc_or_sort_names = nil,
|
22
|
+
ancestor_index_instance = nil,
|
23
|
+
& indexing_block )
|
24
|
+
|
25
|
+
super
|
26
|
+
|
27
|
+
if block_given?
|
28
|
+
indexing_procs.push( indexing_block )
|
29
|
+
elsif indexing_procs.empty?
|
30
|
+
raise ::Persistence::Exception::BlockRequired, 'Block required for index.'
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
###################
|
37
|
+
# requires_keys #
|
38
|
+
###################
|
39
|
+
|
40
|
+
###
|
41
|
+
# Whether index requires keys be generated when block is run.
|
42
|
+
#
|
43
|
+
# @return [true,false] Whether keys must be generated.
|
44
|
+
#
|
45
|
+
attr_setting :requires_keys
|
46
|
+
|
47
|
+
#######################
|
48
|
+
# permits_nil_keys? #
|
49
|
+
#######################
|
50
|
+
|
51
|
+
###
|
52
|
+
# Whether index permits nil keys be generated when block is run.
|
53
|
+
#
|
54
|
+
# @return [true,false] Whether keys must be non-nil.
|
55
|
+
#
|
56
|
+
attr_setting :permits_nil_keys?
|
57
|
+
|
58
|
+
####################
|
59
|
+
# indexing_procs #
|
60
|
+
####################
|
61
|
+
|
62
|
+
###
|
63
|
+
# Procs used to generate keys.
|
64
|
+
#
|
65
|
+
# @return [CompositingArray<Proc>] Procs for key generation.
|
66
|
+
#
|
67
|
+
attr_array :indexing_procs
|
68
|
+
|
69
|
+
##################
|
70
|
+
# index_object #
|
71
|
+
##################
|
72
|
+
|
73
|
+
###
|
74
|
+
# Index keys for object instance.
|
75
|
+
#
|
76
|
+
# @param object [Object] Object to index.
|
77
|
+
#
|
78
|
+
def index_object( object )
|
79
|
+
|
80
|
+
# get key(s) from block(s)
|
81
|
+
keys = [ ]
|
82
|
+
indexing_procs.each do |this_indexing_proc|
|
83
|
+
|
84
|
+
if block_return = object.instance_eval( & this_indexing_proc ) or
|
85
|
+
permits_nil_keys?
|
86
|
+
|
87
|
+
keys.push( block_return )
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
if keys.empty?
|
94
|
+
|
95
|
+
if requires_keys
|
96
|
+
raise ::Persistence::Exception::IndexingBlockFailedToGenerateKeys.new,
|
97
|
+
'Index block failed to generate keys, which were required.'
|
98
|
+
end
|
99
|
+
|
100
|
+
else
|
101
|
+
|
102
|
+
super( object, *keys )
|
103
|
+
|
104
|
+
end
|
105
|
+
|
106
|
+
return self
|
107
|
+
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
|
2
|
+
###
|
3
|
+
# Class used for explicit index instances, which index keys that are explicitly provided.
|
4
|
+
# Interface provided in module so interface can be easily overridden.
|
5
|
+
#
|
6
|
+
class ::Persistence::Object::Index::ExplicitIndex
|
7
|
+
|
8
|
+
include ::Persistence::Object::Index::ExplicitIndex::ExplicitIndexInterface
|
9
|
+
|
10
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
|
2
|
+
###
|
3
|
+
# Interface for explicit index instances, which index keys that are explicitly provided.
|
4
|
+
#
|
5
|
+
module ::Persistence::Object::Index::ExplicitIndex::ExplicitIndexInterface
|
6
|
+
|
7
|
+
include ::Persistence::Object::Index
|
8
|
+
|
9
|
+
###
|
10
|
+
#
|
11
|
+
# @method index_existing_objects
|
12
|
+
#
|
13
|
+
# We undefine :index_existing_objects because it makes no sense on an index requiring explicit keys.
|
14
|
+
#
|
15
|
+
undef_method( :index_existing_objects )
|
16
|
+
|
17
|
+
##################
|
18
|
+
# index_object #
|
19
|
+
##################
|
20
|
+
|
21
|
+
###
|
22
|
+
# Index keys for object instance.
|
23
|
+
#
|
24
|
+
# @param object [Object] Object to index.
|
25
|
+
#
|
26
|
+
# @param keys [Array<Object>] Keys to use for index entries.
|
27
|
+
#
|
28
|
+
def index_object( object, *keys )
|
29
|
+
|
30
|
+
if keys.empty?
|
31
|
+
raise ::Persistence::Exception::IndexingObjectRequiresKeys.new,
|
32
|
+
'No keys provided for index.'
|
33
|
+
end
|
34
|
+
|
35
|
+
# check for existing index on keys if we don't permit duplicates
|
36
|
+
unless permits_duplicates?
|
37
|
+
|
38
|
+
keys.each do |this_key|
|
39
|
+
if global_id = adapter_index.get_object_id( this_key ) and
|
40
|
+
global_id != object.persistence_id
|
41
|
+
raise ::Persistence::Exception::DuplicateViolatesUniqueIndex.new,
|
42
|
+
'Attempt to create index for key ' + this_key.to_s +
|
43
|
+
' would create duplicates in unique index :' + @name.to_s + '.'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
keys.each do |this_key|
|
50
|
+
adapter_index.index_object_id( object.persistence_id, this_key )
|
51
|
+
end
|
52
|
+
|
53
|
+
return self
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
|
2
|
+
###
|
3
|
+
# @private
|
4
|
+
#
|
5
|
+
# Internal helper module with common features for hashes that store indexes.
|
6
|
+
#
|
7
|
+
module ::Persistence::Object::IndexHash
|
8
|
+
|
9
|
+
###################
|
10
|
+
# post_set_hook #
|
11
|
+
###################
|
12
|
+
|
13
|
+
def post_set_hook( index_name, index_instance )
|
14
|
+
|
15
|
+
configuration_instance.indexes[ index_name ] = index_instance
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
########################
|
20
|
+
# child_pre_set_hook #
|
21
|
+
########################
|
22
|
+
|
23
|
+
def child_pre_set_hook( index_name, index_instance )
|
24
|
+
|
25
|
+
parent_bucket = nil
|
26
|
+
|
27
|
+
case configuration_instance
|
28
|
+
when ::Module
|
29
|
+
parent_bucket = configuration_instance.instance_persistence_bucket
|
30
|
+
else
|
31
|
+
parent_bucket = configuration_instance.persistence_bucket
|
32
|
+
end
|
33
|
+
|
34
|
+
child_index_instance = index_instance.class.new( nil, parent_bucket, nil, nil, nil, index_instance )
|
35
|
+
|
36
|
+
return child_index_instance
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
@@ -0,0 +1,322 @@
|
|
1
|
+
|
2
|
+
###
|
3
|
+
# Instance methods for any objects enabled with persistence capabilities.
|
4
|
+
#
|
5
|
+
module ::Persistence::Object::ObjectInstance
|
6
|
+
|
7
|
+
include ::Persistence::Object::ParsePersistenceArgs
|
8
|
+
|
9
|
+
include ::CascadingConfiguration::Setting
|
10
|
+
include ::CascadingConfiguration::Hash
|
11
|
+
|
12
|
+
#######################
|
13
|
+
# persistence_port= #
|
14
|
+
#######################
|
15
|
+
|
16
|
+
attr_local_configuration :persistence_port
|
17
|
+
|
18
|
+
###
|
19
|
+
# Assign a persistence port to be used with this object.
|
20
|
+
#
|
21
|
+
# @overload persistence_port=( port_name )
|
22
|
+
#
|
23
|
+
# @param port_name Name of port to be used. Expects port by name to be available in Persistence controller.
|
24
|
+
#
|
25
|
+
# @overload persistence_port=( port_instance )
|
26
|
+
#
|
27
|
+
# @param port_instance Port instance to use.
|
28
|
+
#
|
29
|
+
def persistence_port=( persistence_port_class_or_name )
|
30
|
+
|
31
|
+
case persistence_port_class_or_name
|
32
|
+
|
33
|
+
when ::Symbol, ::String
|
34
|
+
|
35
|
+
super( ::Persistence.port_for_name_or_port( persistence_port_class_or_name, true ) )
|
36
|
+
|
37
|
+
else
|
38
|
+
|
39
|
+
if persistence_port_class_or_name.respond_to?( :persistence_port )
|
40
|
+
super( persistence_port_class_or_name.persistence_port )
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
return self
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
######################
|
51
|
+
# persistence_port #
|
52
|
+
######################
|
53
|
+
|
54
|
+
###
|
55
|
+
# Get persistence port that will be used with this object. Will use instance persistence port if no port is assigned,
|
56
|
+
# which will result in using the current port if no instance persistence port is specified.
|
57
|
+
#
|
58
|
+
# @return [Persistence::Port,nil] Persistence port instance.
|
59
|
+
#
|
60
|
+
def persistence_port
|
61
|
+
|
62
|
+
# if specified at instance level, use specified value
|
63
|
+
# otherwise, use value stored in class
|
64
|
+
return super || self.class.instance_persistence_port
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
#########################
|
69
|
+
# persistence_bucket= #
|
70
|
+
#########################
|
71
|
+
|
72
|
+
attr_instance_setting :persistence_bucket
|
73
|
+
|
74
|
+
###
|
75
|
+
# Assign a persistence bucket to be used with this object.
|
76
|
+
#
|
77
|
+
# @overload instance_persistence_bucket=( bucket_name )
|
78
|
+
#
|
79
|
+
# @param port_name [Symbol,String] Name of port to be used. Expects port by name to be available
|
80
|
+
# in Persistence controller.
|
81
|
+
#
|
82
|
+
# @overload instance_persistence_bucket=( bucket_instance )
|
83
|
+
#
|
84
|
+
# @param port_instance [Persistence::Port::Bucket,nil] Persistence::Port::Bucket instance to use.
|
85
|
+
#
|
86
|
+
def persistence_bucket=( persistence_bucket_class_or_name )
|
87
|
+
|
88
|
+
if persistence_bucket_class_or_name.nil?
|
89
|
+
|
90
|
+
super( nil )
|
91
|
+
|
92
|
+
elsif persistence_bucket_class_or_name.is_a?( ::Persistence::Port::Bucket )
|
93
|
+
|
94
|
+
super( persistence_bucket_class_or_name )
|
95
|
+
|
96
|
+
elsif persistence_bucket_class_or_name.respond_to?( :instance_persistence_bucket )
|
97
|
+
|
98
|
+
# if arg responds to :instance_persistence_bucket we use arg's bucket
|
99
|
+
super( persistence_bucket_class_or_name.instance_persistence_bucket )
|
100
|
+
|
101
|
+
elsif ! ( persistence_bucket_class_or_name.is_a?( String ) or
|
102
|
+
persistence_bucket_class_or_name.is_a?( Symbol ) ) and
|
103
|
+
persistence_bucket_class_or_name.respond_to?( :persistence_bucket )
|
104
|
+
|
105
|
+
# if arg responds to :persistence_bucket we use arg's bucket
|
106
|
+
super( persistence_bucket_class_or_name.persistence_bucket )
|
107
|
+
|
108
|
+
else
|
109
|
+
|
110
|
+
# otherwise we use arg as a symbol
|
111
|
+
# this means classnames are ok (which are default)
|
112
|
+
self.persistence_bucket = persistence_port.persistence_bucket( persistence_bucket_class_or_name.to_sym )
|
113
|
+
|
114
|
+
end
|
115
|
+
|
116
|
+
return self
|
117
|
+
|
118
|
+
end
|
119
|
+
|
120
|
+
########################
|
121
|
+
# persistence_bucket #
|
122
|
+
########################
|
123
|
+
|
124
|
+
###
|
125
|
+
# Get persistence bucket that will be used with this object. Will use name of class if bucket
|
126
|
+
# does not already exist.
|
127
|
+
#
|
128
|
+
# @return [Persistence::Port,nil] Persistence port instance.
|
129
|
+
#
|
130
|
+
def persistence_bucket
|
131
|
+
|
132
|
+
# if specified at instance level, use specified value
|
133
|
+
# otherwise, use value stored in class
|
134
|
+
return super || self.class.instance_persistence_bucket
|
135
|
+
|
136
|
+
end
|
137
|
+
|
138
|
+
####################
|
139
|
+
# persistence_id #
|
140
|
+
####################
|
141
|
+
|
142
|
+
###
|
143
|
+
# Get persistence ID, used to identify unique objects.
|
144
|
+
#
|
145
|
+
attr_instance_setting :persistence_id
|
146
|
+
|
147
|
+
##############
|
148
|
+
# persist! #
|
149
|
+
##############
|
150
|
+
|
151
|
+
###
|
152
|
+
# Store object to persistence port. Will cause all non-atomic properties to be updated if object has already
|
153
|
+
# been persisted.
|
154
|
+
#
|
155
|
+
# @overload persist!
|
156
|
+
#
|
157
|
+
# @overload persist!( index_name, key )
|
158
|
+
#
|
159
|
+
# @param index_name [Symbol,String] Name of index for explicit key-based indexing.
|
160
|
+
# @param key [Object] Key for explicit indexing.
|
161
|
+
#
|
162
|
+
# @overload persist!( index_name_key_hash )
|
163
|
+
#
|
164
|
+
# @param index_name_key_hash [Hash{Symbol,String=>Object}] Name of index for explicit key-based indexing,
|
165
|
+
# pointing to index value.
|
166
|
+
#
|
167
|
+
# @overload persist!( index_instance, key )
|
168
|
+
#
|
169
|
+
# @param index_instance [Symbol,String] Name of index for explicit key-based indexing.
|
170
|
+
# @param key [Object] Key for explicit indexing.
|
171
|
+
#
|
172
|
+
# @overload persist!( index_instance_key_hash )
|
173
|
+
#
|
174
|
+
# @param index_instance_key_hash [Hash{Persistence::Object::Index=>Object}] Name of index
|
175
|
+
# for explicit key-based indexing, pointing to index value.
|
176
|
+
#
|
177
|
+
# @return self
|
178
|
+
#
|
179
|
+
def persist!( *args )
|
180
|
+
|
181
|
+
index_instance, key, no_key = parse_args_for_index_value_no_value( args, false )
|
182
|
+
|
183
|
+
# call super to ensure object is persisted
|
184
|
+
unless persistence_port
|
185
|
+
raise ::Persistence::Exception::NoPortEnabled, 'No persistence port currently enabled.'
|
186
|
+
end
|
187
|
+
|
188
|
+
self.persistence_id = persistence_bucket.put_object!( self )
|
189
|
+
|
190
|
+
if index_instance
|
191
|
+
|
192
|
+
# if we have an index make sure that we have a key
|
193
|
+
if no_key
|
194
|
+
raise ::Persistence::Exception::KeyValueRequired,
|
195
|
+
'Key required when specifying index for :persist!'
|
196
|
+
end
|
197
|
+
|
198
|
+
# and make sure we have an index that permits arbitrary keys
|
199
|
+
unless explicit_indexes[ index_instance.name ] == index_instance
|
200
|
+
raise ::Persistence::Exception::ExplicitIndexRequired,
|
201
|
+
'Index ' + index_instance.name.to_s + ' was not declared as an explicit index '
|
202
|
+
'and thus does not permit arbitrary keys.'
|
203
|
+
end
|
204
|
+
|
205
|
+
index_instance.index_object( self, key )
|
206
|
+
|
207
|
+
end
|
208
|
+
|
209
|
+
unless block_indexes.empty?
|
210
|
+
block_indexes.each do |this_index_name, this_block_index|
|
211
|
+
this_block_index.index_object( self )
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
return self
|
216
|
+
|
217
|
+
end
|
218
|
+
|
219
|
+
################
|
220
|
+
# persisted? #
|
221
|
+
################
|
222
|
+
|
223
|
+
###
|
224
|
+
# Query whether object is persisted in port.
|
225
|
+
#
|
226
|
+
# @return [true,false] Whether object is persisted.
|
227
|
+
#
|
228
|
+
def persisted?
|
229
|
+
|
230
|
+
bucket_name = persistence_port.get_bucket_name_for_object_id( persistence_id )
|
231
|
+
|
232
|
+
return bucket_name ? true : false
|
233
|
+
|
234
|
+
end
|
235
|
+
|
236
|
+
############
|
237
|
+
# cease! #
|
238
|
+
############
|
239
|
+
|
240
|
+
###
|
241
|
+
# Remove object properties stored for object from persistence bucket and indexes.
|
242
|
+
#
|
243
|
+
def cease!
|
244
|
+
|
245
|
+
indexes.each do |this_index_name, this_index|
|
246
|
+
this_index.delete_keys_for_object_id!( persistence_id )
|
247
|
+
end
|
248
|
+
|
249
|
+
persistence_hash_from_port = persistence_bucket.delete_object!( persistence_id )
|
250
|
+
|
251
|
+
self.persistence_id = nil
|
252
|
+
|
253
|
+
return persistence_hash_from_port
|
254
|
+
|
255
|
+
end
|
256
|
+
|
257
|
+
######################
|
258
|
+
# explicit_indexes #
|
259
|
+
######################
|
260
|
+
|
261
|
+
###
|
262
|
+
#
|
263
|
+
# @method explicit_indexes
|
264
|
+
#
|
265
|
+
# Hash holding explicit indexes: index_name => index.
|
266
|
+
#
|
267
|
+
# @return [CompositingHash{Symbol,String=>Persistence::Object::Index::ExplicitIndex}]
|
268
|
+
#
|
269
|
+
attr_hash :explicit_indexes, ::Persistence::Object::IndexHash
|
270
|
+
|
271
|
+
###################
|
272
|
+
# block_indexes #
|
273
|
+
###################
|
274
|
+
|
275
|
+
###
|
276
|
+
#
|
277
|
+
# @method block_indexes
|
278
|
+
#
|
279
|
+
# Hash holding block indexes: index_name => index.
|
280
|
+
#
|
281
|
+
# @return [CompositingHash{Symbol,String=>Persistence::Object::Index::BlockIndex}]
|
282
|
+
#
|
283
|
+
attr_hash :block_indexes, ::Persistence::Object::IndexHash
|
284
|
+
|
285
|
+
#############
|
286
|
+
# indexes #
|
287
|
+
#############
|
288
|
+
|
289
|
+
###
|
290
|
+
#
|
291
|
+
# @method indexes
|
292
|
+
#
|
293
|
+
# Hash holding indexes: index_name => index.
|
294
|
+
#
|
295
|
+
# @return [CompositingHash{Symbol,String=>Persistence::Object::Index}]
|
296
|
+
#
|
297
|
+
attr_hash :indexes do
|
298
|
+
|
299
|
+
#======================#
|
300
|
+
# child_pre_set_hook #
|
301
|
+
#======================#
|
302
|
+
|
303
|
+
def child_pre_set_hook( index_name, index_instance )
|
304
|
+
|
305
|
+
child_instance = nil
|
306
|
+
|
307
|
+
case index_instance
|
308
|
+
when ::Persistence::Object::Index::ExplicitIndex
|
309
|
+
child_instance = configuration_instance.explicit_indexes[ index_name ]
|
310
|
+
when ::Persistence::Object::Index::BlockIndex
|
311
|
+
child_instance = configuration_instance.block_indexes[ index_name ]
|
312
|
+
when ::Persistence::Object::Complex::Index::AttributeIndex
|
313
|
+
child_instance = configuration_instance.attribute_indexes[ index_name ]
|
314
|
+
end
|
315
|
+
|
316
|
+
return child_instance
|
317
|
+
|
318
|
+
end
|
319
|
+
|
320
|
+
end
|
321
|
+
|
322
|
+
end
|