Class: LanGrove::Plugin::Persistor

Inherits:
DataSource show all
Defined in:
lib/langrove/plugin/persistor.rb

Direct Known Subclasses

BufferedPersistor, YamlFile

Instance Attribute Summary

Attributes inherited from Base

#logger

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#config_exception, #type

Constructor Details

#initialize(root, config, name = nil) ⇒ Persistor

Returns a new instance of Persistor.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/langrove/plugin/persistor.rb', line 101

def initialize( root, config, name = nil ) 
   
  super( root, config, name )
  
  @allow_learn = @config[:allow_learn]
  #
  # If enabled, this config key enables allowing
  # a fetch() that failed to return anything to
  # process as if there was nothing to fetch,
  # 
  # ie. 
  #    It is new, not yet persisted.
  #
  # 
  
  validate_config
  
  @provides = {
    
    :persistor => {
      
      :version => PERSISTOR_VERSION
      
    }
    
  }

end

Class Method Details

.typeObject



20
21
22
23
24
# File 'lib/langrove/plugin/persistor.rb', line 20

def self.type
  
  :persistor
  
end

Instance Method Details

#all_capsules(&block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/langrove/plugin/persistor.rb', line 26

def all_capsules( &block )
  
  #
  # OVERRIDE
  # 
  # Should collect from storage
  # ALL the persisted casules,
  # 
  # each a Hash
  #
  # and yield the key and the capsule
  # 
  # OR raise exception
  #
   
  raise LanGrove::PluginException.new(

    "#{self.class}.all_capsules() not implemented."
    
  )
  
end

#fetch(handler) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/langrove/plugin/persistor.rb', line 70

def fetch( handler )

  #
  # OVERRIDE
  # 
  # To populate the capsule from Persistance
  # 
  # Return true or THROW EXCEPTION.
  # 
  
  raise LanGrove::PluginException.new(

    "#{self.class}.fetch() not implemented."
  
  )
  
end

#fetch_all(multiplexer = nil, &block) ⇒ Object



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
270
271
272
273
274
# File 'lib/langrove/plugin/persistor.rb', line 243

def fetch_all( multiplexer = nil, &block )
  
  config_exception(

    "#{self.class}.fetch_all() only acts on multiplexer"

  ) if ( 
  
    block.nil? and 
    not multiplexer.respond_to?( :create_handler )
  
  )

  count = 0

  all_capsules do |key, capsule|
    
    count += 1
  
    handler = multiplexer.create_handler( 
  
      key, capsule, capsule
  
    ) unless multiplexer.nil?
    
    yield capsule if block
  
  end
  
  @logger.warn "#{self.class} preloaded #{count} handlers" 

end

#fetch_all_handler(multiplexer) ⇒ Object



277
278
279
280
281
# File 'lib/langrove/plugin/persistor.rb', line 277

def fetch_all_handler( multiplexer )
  
  fetch_all( multiplexer )
  
end

#fetch_handler(handler) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/langrove/plugin/persistor.rb', line 152

def fetch_handler( handler )
  
  @root.trigger( handler, :handler, :fetch )
  
  #
  # Plugin implementable fetch
  #
  if fetch( handler )
  
    @root.trigger( handler, :handler, :after_fetch )
    
  end
 
end

#gen_key(handler) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/langrove/plugin/persistor.rb', line 168

def gen_key( handler )
  
  #
  # Use the pending capsule to assemble the
  # persistance key.
  #
  data_hash = handler.pending_capsule if handler.respond_to?( :pending_capsule )
  
  #
  # Fall back to using the actual capsule
  #
  if data_hash.nil?
    
    data_hash = handler.capsule
    
  end

  data_hash = transform( data_hash, :store )

  storage_address = {
    
    'table' => @table,
    'key' => {}
  
  }

  @key.each do |key_name, key_map|
    
    storage_address['key'][key_name] = data_hash[key_map]

    unless handler.capsule.nil?
      
      #
      # For mongo, put _id into capsule
      # 
      handler.capsule[key_name] = data_hash[key_map]
    
    end
    
    if storage_address['key'][key_name].nil?
      
      @logger.error "Missing key data in #{data_hash.inspect}"
      
      #
      # This exception brings the whole server down
      # 
      # Which is good
      # 
      # But when it fires inside a deferred proc it
      # only takes out the proc...
      # 
      # Then when a callback fires the proc is gone
      # and EventMachine::ConnectionNotBound 
      # exception is raised
      # 
      # It still takes down the Daemon, but the original 
      # exception is lost.
      # 
      
      behaviour_exception( 
      
        'Missing key data.'
      
      )
      
    end
    
  end
  
  handler.key = storage_address
  
  return true
  
end

#next_seqObject



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/langrove/plugin/persistor.rb', line 339

def next_seq
  
  #
  # Unlikely to reach MAX_INT before a daemon 
  # is restarted for what ever reason,
  # 
  # But just incase
  # 
  
  @seq = 0 if @seq.nil?
  
  @seq = 0 if @seq == MAX_INT
  
  @seq += 1
  
end

#store(handler) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/langrove/plugin/persistor.rb', line 49

def store( handler )
  
  #
  # OVERRIDE
  # 
  # To 'save' the capsule.
  # 
  # Persist the handler.capsule
  # 
  # Return true or false.
  # 

  raise LanGrove::PluginException.new(

    "#{self.class}.store() not implemented."
    
  )

end

#store_handler(handler) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/langrove/plugin/persistor.rb', line 131

def store_handler( handler )
  
  @root.trigger( handler, :handler, :store )
  
  #
  # Insert sequence number into capsule
  #
  handler.capsule['persisted_seq'] = next_seq
  
  #
  # Plugin implementable store
  #
  if store( handler )
  
    @root.trigger( handler, :handler, :after_store )
  
  end
  
end

#transform(capsule, action = nil) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/langrove/plugin/persistor.rb', line 88

def transform( capsule, action = nil )

  #
  # OVERRIDE 
  # 
  # To perform any necessary transforms
  # on the capsule just ahead of storage
  #

  return capsule

end

#validate_configObject



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/langrove/plugin/persistor.rb', line 284

def validate_config
  
  #
  # Defaults:
  #
  # DatabaseName as configured :system: name
  # TableName as configured :applicaiton: name
  #
  @database = @root.config.system
  @table = @root.config.application
  
  @logger.warn( 
  
    "#{self.class} defaulting :database: to '#{@database}'"
    
  ) if ( 
  
    @config[ :database ].nil? or
    not @database = @config[ :database ]
    
  )
  
  @logger.warn( 
  
    "#{self.class} defaulting :table: to '#{@table}'"
    
  ) if ( 
  
    @config[ :table ].nil? or
    not @table = @config[ :table ]
    
  )
  
  config_exception(
  
    "#{self.class} requires :key:"
  
  ) unless @key = @config[:key]
  
  config_exception(
  
    "#{self.class} requires at least one :key_name: defined in :key:"
  
  ) unless @key.size > 0
  
  config_exception(
  
    "#{self.class} requires a value for :key:#{@key.first[0]}:"
  
  ) if @key.first[1] == nil or @key.first[1] == ''

  @key = transform( @key, :key )
  
end