Class: Lafcadio::ObjectStore::Cache

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/lafcadio/objectStore.rb

Overview

:nodoc:

Defined Under Namespace

Classes: DomainClassCache

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db_bridge = DbBridge.new) ⇒ Cache

Returns a new instance of Cache.



275
276
277
278
279
# File 'lib/lafcadio/objectStore.rb', line 275

def initialize( db_bridge = DbBridge.new )
	super()
	@db_bridge = db_bridge
	@domain_class_caches = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/lafcadio/objectStore.rb', line 334

def method_missing( meth, *args )
	simple_dispatch = [
		:queries, :save, :set_commit_time, :update_after_commit
	]
	if simple_dispatch.include?( meth )
		cache( args.first.domain_class ).send( meth, *args )
	elsif [ :[], :last_commit_time ].include?( meth )
		cache( args.first ).send( meth, *args[1..-1] )
	elsif [ :group_query, :transaction ].include?( meth )
		@db_bridge.send( meth, *args )
	end
end

Instance Attribute Details

#db_bridgeObject (readonly)

Returns the value of attribute db_bridge.



273
274
275
# File 'lib/lafcadio/objectStore.rb', line 273

def db_bridge
  @db_bridge
end

Instance Method Details

#cache(domain_class) ⇒ Object



298
299
300
301
302
303
304
305
# File 'lib/lafcadio/objectStore.rb', line 298

def cache( domain_class )
	unless @domain_class_caches[domain_class]
		@domain_class_caches[domain_class] = DomainClassCache.new(
			domain_class, @db_bridge
		)
	end
	@domain_class_caches[domain_class]
end

#commit(db_object) ⇒ Object



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/lafcadio/objectStore.rb', line 281

def commit( db_object )
	db_object.verify if LafcadioConfig.new()['checkFields'] == 'onCommit'
	db_object.last_commit_type = get_last_commit db_object
	db_object.pre_commit_trigger
	update_dependent_domain_objects( db_object ) if db_object.delete
	synchronize do
		@db_bridge.commit db_object
		unless db_object.pk_id
			db_object.pk_id = @db_bridge.last_pk_id_inserted
		end
	end
	update_after_commit db_object
	db_object.post_commit_trigger
	db_object.reset_original_values_hash
	db_object
end

#get_by_query(query) ⇒ Object



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/lafcadio/objectStore.rb', line 307

def get_by_query( query )
	main_cache = cache query.domain_class
	unless main_cache.queries[query]
		if query.one_pk_id?
			collected = false
		else
			collected = main_cache.collect_from_superset query
		end
		if !collected and main_cache.queries.values
			newObjects = @db_bridge.select_dobjs query
			newObjects.each { |dbObj| main_cache.save dbObj }
			main_cache.queries[query] = newObjects.collect { |dobj| dobj.pk_id }
		end
	end
	main_cache.queries[query].map { |pk_id| main_cache[pk_id] }.compact
end

#get_last_commit(db_object) ⇒ Object



324
325
326
327
328
329
330
331
332
# File 'lib/lafcadio/objectStore.rb', line 324

def get_last_commit( db_object )
	if db_object.delete
		:delete
	elsif db_object.pk_id
		:update
	else
		:insert
	end
end

#update_dependent_domain_class(db_object, aClass, field) ⇒ Object



347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/lafcadio/objectStore.rb', line 347

def update_dependent_domain_class( db_object, aClass, field )
	object_store = ObjectStore.get_object_store
	collection = aClass.get( db_object, field.name )
	collection.each { |dependentObject|
		if field.delete_cascade
			dependentObject.delete = true
		else
			dependentObject.send( field.name + '=', nil )
		end
		object_store.commit dependentObject
	}
end

#update_dependent_domain_objects(db_object) ⇒ Object



360
361
362
363
364
365
366
367
# File 'lib/lafcadio/objectStore.rb', line 360

def update_dependent_domain_objects( db_object )
	dependent_classes = db_object.domain_class.dependent_classes
	dependent_classes.keys.each { |aClass|
		update_dependent_domain_class(
			db_object, aClass, dependent_classes[aClass]
		)
	}
end