Class: Xampl::RedisPersister

Inherits:
Persister show all
Defined in:
lib/xamplr/persisters/redis.rb

Constant Summary collapse

REPOSITORIES_KEY =
"XAMPL::REPOSITORIES"
@@default_redis_options =
{
        :repo_properties => {
                # DB Properties, maybe only set when the DB is created for the first time.
                :mentions => true,
                },
         # Connect Properties
        :thread_safe => true, # redis connections will be thread safe
        :redis_server => "redis://127.0.0.1:6379/0", #This is the format expected by redis-rb, just use it
        :clobbering_allowed => false,
        :allow_connections => true,
        :connect_to_known => true, # will connect to repos already in the redis db
        :connect_to_unknown => true, # will connect to repose not in the redis db
        :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

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
#      puts "#{File.basename(__FILE__)}:#{__LINE__} [#{ __method__ }] @@default_redis_options: #{ @@default_redis_options.inspect }"
#      puts "#{File.basename(__FILE__)}:#{__LINE__} [#{ __method__ }] Xampl.raw_persister_options: #{ Xampl.raw_persister_options.inspect }"
#      puts "#{File.basename(__FILE__)}:#{__LINE__} [#{ __method__ }] options: #{ options.inspect }"

  @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

#clientObject (readonly)

Returns the value of attribute client.



5
6
7
# File 'lib/xamplr/persisters/redis.rb', line 5

def client
  @client
end

#instance_optionsObject (readonly)

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_nameObject (readonly)

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_propertiesObject (readonly)

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_propertiesObject (readonly)

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

.kindObject



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)
  # this is called by xampl for the temporary new_cache
  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.weakref_alive?) && (existing.__getobj__ != xampl)
    raise DuplicateXamplInPersister.new(existing, xampl, self) if existing && (existing.__getobj__ != xampl)
  rescue WeakRef::RefError => e
    # key is there but the original object isn't...
  end

  @new_cache[key] = xampl
end

#clear_cacheObject 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

#clobberObject



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
  # TODO -- this is a BAD idea if there are any other connections to this repo (i.e. in different processes)

  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

  #TODO -- getting the keys outside the multi might allow some new key to sneak in there
  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

#closeObject



149
150
151
152
# File 'lib/xamplr/persisters/redis.rb', line 149

def close
  ensure_disconnected
  clear_cache
end

#common_key_prefixObject



154
155
156
# File 'lib/xamplr/persisters/redis.rb', line 154

def common_key_prefix
  "XAMPL::#{ @repo_name }::"
end

#ensure_connectedObject



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)

  # This check is necessary to make sure that the connection is made, otherwise the exception will be sometime in the future
  # the redis exceptions are good enough, so don't bother trapping right here
  @client.ping

  # check to see if the repository with this name is known (in redis) already
  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

  # if it is unknown, then set it up

  @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_disconnectedObject



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

Returns:

  • (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

Returns:

  • (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?

Returns:

  • (Boolean)


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)
  #NOTE -- the XAMPL::#{ @repo_name }:: is a prefix common to all keys specific to this repository
  "#{ 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

#kindObject



52
53
54
# File 'lib/xamplr/persisters/redis.rb', line 52

def kind
  RedisPersister.kind
end

#known_reposObject



167
168
169
# File 'lib/xamplr/persisters/redis.rb', line 167

def known_repos
  return @client.smembers(REPOSITORIES_KEY)
end

#load_repo_propertiesObject



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.weakref_alive?) && (existing.__getobj__ != xampl)
    raise DuplicateXamplInPersister.new(existing, xampl, self) if existing && (existing.__getobj__ != xampl)
  rescue WeakRef::RefError => e
    # key is there but the original object isn't...
  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
    #not there
    xampl = nil
  end
#      xampl = (xampl && xampl.weakref_alive?) ? xampl.__getobj__ : nil

  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_keyObject



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_cleanupObject



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

#          map.each { |name2, map2|
#            if map2 then
#              map2.each { |pid, xampl|
#                @changed.delete(xampl)
#                xampl.invalidate
#              }
#            end
#          }
    end
  }
  @changed.each { |xampl, ignore|
    xampl.force_load
  }
  @new_cache = {}

  super
end

#sync_doneObject



271
272
273
274
275
276
277
# File 'lib/xamplr/persisters/redis.rb', line 271

def sync_done
  # simply moves the new_cache to the permanent cache
  (@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)
  # this is called by xampl for the temporary new_cache
  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

  #TODO -- honour the mentions config information (FROM THE DB not the configuration!!)
  mentions = []
  xml = represent(xampl, mentions)
  key = key_for_xampl(xampl)

  #TODO save the modified-time-like value to support multi processing

#      puts "#{File.basename(__FILE__)}:#{__LINE__} [#{ __method__ }] redis #{ self }"
#      puts "#{File.basename(__FILE__)}:#{__LINE__} [#{ __method__ }] write: #{ xampl } --> #{ xml }"
#      puts "#{File.basename(__FILE__)}:#{__LINE__} [#{ __method__ }] key: #{ key }"

  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