Class: Xampl::RedisPersister
Constant Summary
collapse
- REPOSITORIES_KEY =
"XAMPL::REPOSITORIES"
- @@default_redis_options =
{
:repo_properties => {
:mentions => true,
},
:thread_safe => true, :redis_server => "redis://127.0.0.1:6379/0", :clobbering_allowed => false,
:allow_connections => true,
:connect_to_known => true, :connect_to_unknown => true, :testing => false
}
Instance Attribute Summary collapse
Attributes inherited from Persister
#automatic, #block_changes, #cache_hits, #expunged, #format, #last_write_count, #name, #read_count, #rolled_back, #slow_sync, #syncing, #total_cache_hits, #total_read_count, #total_rollback_count, #total_sync_count, #total_write_count, #write_count
Class Method Summary
collapse
Instance Method Summary
collapse
-
#cache(xampl) ⇒ Object
-
#clear_cache ⇒ Object
(also: #fresh_cache)
-
#clobber ⇒ Object
-
#close ⇒ Object
-
#common_key_prefix ⇒ Object
-
#ensure_connected ⇒ Object
-
#ensure_disconnected ⇒ Object
-
#in_any_cache?(klass, index) ⇒ Boolean
-
#in_new_cache?(klass, index) ⇒ Boolean
-
#in_perm_cache?(klass, index) ⇒ Boolean
(also: #in_cache?)
-
#initialize(name = nil, format = nil, options = {}) ⇒ RedisPersister
constructor
A new instance of RedisPersister.
-
#key_for_class(klass, index) ⇒ Object
-
#key_for_xampl(xampl) ⇒ Object
-
#kind ⇒ Object
-
#known_repos ⇒ Object
-
#load_repo_properties ⇒ Object
-
#perm_cache(xampl) ⇒ Object
-
#perm_uncache(xampl) ⇒ Object
-
#read(klass, pid, target = nil) ⇒ Object
-
#read_from_cache(klass, index, target = nil) ⇒ Object
-
#repo_properties_key ⇒ Object
-
#rollback_cleanup ⇒ Object
-
#sync_done ⇒ Object
-
#uncache(xampl) ⇒ Object
-
#write(xampl) ⇒ Object
Methods inherited from Persister
#busy, #count_changed, #do_sync_write, #done_sync_write, #expunge, #find_known, #find_xampl, #has_changed, #has_not_changed, #introduce, #is_busy, #lazy_load, #lookup, #optimise, #print_stats, #put_changed, #query_implemented, #realise, replace, #represent, #rollback, #shutdown, #start_sync_write, #sync
Constructor Details
#initialize(name = nil, format = nil, options = {}) ⇒ RedisPersister
Returns a new instance of RedisPersister.
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/xamplr/persisters/redis.rb', line 29
def initialize(name=nil, format=nil, options={})
super(name, format)
@repo_name = name
@instance_options = {}
@instance_options = @instance_options.merge(@@default_redis_options)
@instance_options = @instance_options.merge(Xampl.raw_persister_options)
@instance_options = @instance_options.merge(options) if options
@suggested_repo_properties = @instance_options.delete(:repo_properties)
clear_cache
ensure_connected
end
|
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
5
6
7
|
# File 'lib/xamplr/persisters/redis.rb', line 5
def client
@client
end
|
#instance_options ⇒ Object
Returns the value of attribute instance_options.
5
6
7
|
# File 'lib/xamplr/persisters/redis.rb', line 5
def instance_options
@instance_options
end
|
#repo_name ⇒ Object
Returns the value of attribute repo_name.
5
6
7
|
# File 'lib/xamplr/persisters/redis.rb', line 5
def repo_name
@repo_name
end
|
#repo_properties ⇒ Object
Returns the value of attribute repo_properties.
5
6
7
|
# File 'lib/xamplr/persisters/redis.rb', line 5
def repo_properties
@repo_properties
end
|
#suggested_repo_properties ⇒ Object
Returns the value of attribute suggested_repo_properties.
5
6
7
|
# File 'lib/xamplr/persisters/redis.rb', line 5
def suggested_repo_properties
@suggested_repo_properties
end
|
Class Method Details
.kind ⇒ Object
48
49
50
|
# File 'lib/xamplr/persisters/redis.rb', line 48
def RedisPersister.kind
:redis
end
|
Instance Method Details
#cache(xampl) ⇒ Object
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
# File 'lib/xamplr/persisters/redis.rb', line 194
def cache(xampl)
raise NotXamplPersistedObject.new(xampl) unless xampl.kind_of?(XamplPersistedObject)
key = key_for_xampl(xampl)
existing = @new_cache[key]
raise DuplicateXamplInPersister.new(existing, xampl, self) if existing && (existing != xampl)
existing = @cache[key]
begin
raise DuplicateXamplInPersister.new(existing, xampl, self) if existing && (existing.__getobj__ != xampl)
rescue WeakRef::RefError => e
end
@new_cache[key] = xampl
end
|
#clear_cache ⇒ Object
Also known as:
fresh_cache
141
142
143
144
145
|
# File 'lib/xamplr/persisters/redis.rb', line 141
def clear_cache
@cache_hits = 0
@cache = {}
@new_cache = {}
end
|
#clobber ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/xamplr/persisters/redis.rb', line 109
def clobber
unless @instance_options[:clobbering_allowed] then
raise IncompatiblePersisterConfiguration.new('redis', "clobbering is not enabled for this connection to repo: '#{ repo_name }' in #{ @instance_options[:redis_server] }")
end
keys = @client.keys("#{ common_key_prefix }*")
@client.multi do
@client.del(repo_properties_key)
@client.srem(REPOSITORIES_KEY, repo_name)
keys.each do |key|
@client.del(key)
end
end
ensure_disconnected
end
|
#close ⇒ Object
149
150
151
152
|
# File 'lib/xamplr/persisters/redis.rb', line 149
def close
ensure_disconnected
clear_cache
end
|
#common_key_prefix ⇒ Object
154
155
156
|
# File 'lib/xamplr/persisters/redis.rb', line 154
def common_key_prefix
"XAMPL::#{ @repo_name }::"
end
|
#ensure_connected ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/xamplr/persisters/redis.rb', line 56
def ensure_connected
return if @client
return unless instance_options[:allow_connections]
server = @instance_options[:redis_server]
thread_safe = @instance_options[:thread_safe]
@client = Redis.connect(:url => server, :thread_safe => thread_safe)
@client.ping
load_repo_properties
if @repo_properties && !@instance_options[:connect_to_known] then
ensure_disconnected
raise IncompatiblePersisterConfiguration.new('redis', "prevent connections to existing repos named: '#{ repo_name }' in #{ server }")
end
if @repo_properties.nil? && !@instance_options[:connect_to_unknown] then
ensure_disconnected
raise IncompatiblePersisterConfiguration.new('redis', "prevent connections to unknown repos named: '#{ repo_name }' in #{ server }")
end
return if @repo_properties
@repo_properties = {}
suggested_repo_properties.each do |k, v|
@repo_properties[k] = v
end
@repo_properties['name'] = repo_name
@repo_properties['created_at'] = DateTime.now.to_s
@client.multi do
@client.mapped_hmset(repo_properties_key, @repo_properties)
@client.sadd(REPOSITORIES_KEY, repo_name)
end
load_repo_properties
end
|
#ensure_disconnected ⇒ Object
99
100
101
102
103
104
105
106
107
|
# File 'lib/xamplr/persisters/redis.rb', line 99
def ensure_disconnected
return unless client
return unless instance_options[:allow_connections]
@client.quit
ensure
@repo_properties = nil
@client = nil
end
|
#in_any_cache?(klass, index) ⇒ Boolean
236
237
238
|
# File 'lib/xamplr/persisters/redis.rb', line 236
def in_any_cache?(klass, index)
in_new_cache?(klass, index) || in_cache?(klass, index)
end
|
#in_new_cache?(klass, index) ⇒ Boolean
231
232
233
234
|
# File 'lib/xamplr/persisters/redis.rb', line 231
def in_new_cache?(klass, index)
key = key_for_class(klass, index)
@new_cache.include?(key)
end
|
#in_perm_cache?(klass, index) ⇒ Boolean
Also known as:
in_cache?
222
223
224
225
226
227
|
# File 'lib/xamplr/persisters/redis.rb', line 222
def in_perm_cache?(klass, index)
key = key_for_class(klass, index)
xampl = @cache[key]
(xampl && xampl.weakref_alive?) ? true : false
end
|
#key_for_class(klass, index) ⇒ Object
158
159
160
161
|
# File 'lib/xamplr/persisters/redis.rb', line 158
def key_for_class(klass, index)
"#{ common_key_prefix }#{ klass.persistence_class.name }[#{ index }]"
end
|
#key_for_xampl(xampl) ⇒ Object
163
164
165
|
# File 'lib/xamplr/persisters/redis.rb', line 163
def key_for_xampl(xampl)
key_for_class(xampl.class, xampl.get_the_index)
end
|
#kind ⇒ Object
52
53
54
|
# File 'lib/xamplr/persisters/redis.rb', line 52
def kind
RedisPersister.kind
end
|
#known_repos ⇒ Object
167
168
169
|
# File 'lib/xamplr/persisters/redis.rb', line 167
def known_repos
return @client.smembers(REPOSITORIES_KEY)
end
|
#load_repo_properties ⇒ Object
135
136
137
138
139
|
# File 'lib/xamplr/persisters/redis.rb', line 135
def load_repo_properties
key = repo_properties_key()
@repo_properties = @client.hgetall(key)
@repo_properties = nil if @repo_properties.empty?
end
|
#perm_cache(xampl) ⇒ Object
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
# File 'lib/xamplr/persisters/redis.rb', line 171
def perm_cache(xampl)
raise NotXamplPersistedObject.new(xampl) unless xampl.kind_of?(XamplPersistedObject)
key = key_for_xampl(xampl)
existing = @cache[key]
begin
raise DuplicateXamplInPersister.new(existing, xampl, self) if existing && (existing.__getobj__ != xampl)
rescue WeakRef::RefError => e
end
@cache[key] = WeakRef.new(xampl)
end
|
#perm_uncache(xampl) ⇒ Object
187
188
189
190
191
192
|
# File 'lib/xamplr/persisters/redis.rb', line 187
def perm_uncache(xampl)
raise NotXamplPersistedObject.new(xampl) unless xampl.kind_of?(XamplPersistedObject)
key = key_for_xampl(xampl)
@cache.delete(key).__getobj__
end
|
#read(klass, pid, target = nil) ⇒ Object
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
# File 'lib/xamplr/persisters/redis.rb', line 309
def read(klass, pid, target=nil)
xampl, target = read_from_cache(klass, pid, target)
return xampl if xampl and !target
key = key_for_class(klass, pid)
xml = client.get(key)
unless xml
puts "#{File.basename(__FILE__)}:#{__LINE__} [#{ __method__ }] TEST ME"
return nil
end
xampl = realise(xml, target)
Xampl.store_in_cache(@cache, xampl, self) { xampl }
xampl.introduce_persister(self)
@read_count = @read_count + 1
xampl.changes_accepted
@changed.delete(xampl)
return xampl
end
|
#read_from_cache(klass, index, target = nil) ⇒ Object
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
# File 'lib/xamplr/persisters/redis.rb', line 240
def read_from_cache(klass, index, target=nil)
key = key_for_class(klass, index)
xampl = @cache[key]
begin
xampl = xampl.__getobj__ if xampl
rescue WeakRef::RefError => e
xampl = nil
end
unless xampl then
xampl = @new_cache[key]
end
return nil, target unless xampl
if target and target != xampl then
puts "#{File.basename(__FILE__)}:#{__LINE__} [#{ __method__ }] TEST ME"
target.invalidate
raise XamplException.new(:cache_conflict)
end
unless xampl.load_needed then
@cache_hits = @cache_hits + 1
return xampl, target
end
return xampl, xampl
end
|
#repo_properties_key ⇒ Object
130
131
132
133
|
# File 'lib/xamplr/persisters/redis.rb', line 130
def repo_properties_key
key = "XAMPL::PROPERTIES::#{ @repo_name }"
return key
end
|
#rollback_cleanup ⇒ Object
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
# File 'lib/xamplr/persisters/redis.rb', line 330
def rollback_cleanup
@new_cache.each { |name, xampl|
if xampl then
@changed.delete(xampl)
xampl.invalidate
end
}
@changed.each { |xampl, ignore|
xampl.force_load
}
@new_cache = {}
super
end
|
#sync_done ⇒ Object
271
272
273
274
275
276
277
|
# File 'lib/xamplr/persisters/redis.rb', line 271
def sync_done
(@new_cache || {}).each do |key, xampl|
@cache[key] = WeakRef.new(xampl)
end
@new_cache = {}
end
|
#uncache(xampl) ⇒ Object
214
215
216
217
218
219
220
|
# File 'lib/xamplr/persisters/redis.rb', line 214
def uncache(xampl)
raise NotXamplPersistedObject.new(xampl) unless xampl.kind_of?(XamplPersistedObject)
key = key_for_xampl(xampl)
@new_cache.delete(key)
end
|
#write(xampl) ⇒ Object
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
# File 'lib/xamplr/persisters/redis.rb', line 279
def write(xampl)
unless xampl.get_the_index
puts "#{File.basename(__FILE__)}:#{__LINE__} [#{ __method__ }] TEST ME"
raise NotXamplPersistedObject.new(xampl)
end
mentions = []
xml = represent(xampl, mentions)
key = key_for_xampl(xampl)
client.set(key, xml)
@write_count = @write_count + 1
xampl.changes_accepted
return true
rescue NotXamplPersistedObject => nxpo
raise nxpo
rescue => e
puts "#{File.basename(__FILE__)}:#{__LINE__} [#{ __method__ }] #{ e }"
puts e.backtrace
return false
end
|