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.



287
288
289
290
291
# File 'lib/lafcadio/objectStore.rb', line 287

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



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

def method_missing( meth, *args )
	simple_dispatch = [
		:flush, :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, :rollback, :transaction ].include?( meth )
		@db_bridge.send( meth, *args )
	else
		super
	end
end

Instance Attribute Details

#db_bridgeObject (readonly)

Returns the value of attribute db_bridge.



284
285
286
# File 'lib/lafcadio/objectStore.rb', line 284

def db_bridge
  @db_bridge
end

#domain_class_cachesObject

Returns the value of attribute domain_class_caches.



285
286
287
# File 'lib/lafcadio/objectStore.rb', line 285

def domain_class_caches
  @domain_class_caches
end

Instance Method Details

#cache(domain_class) ⇒ Object



310
311
312
313
314
315
316
317
# File 'lib/lafcadio/objectStore.rb', line 310

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



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/lafcadio/objectStore.rb', line 293

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



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/lafcadio/objectStore.rb', line 319

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



338
339
340
341
342
343
344
345
346
# File 'lib/lafcadio/objectStore.rb', line 338

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

#transactional_cloneObject



363
364
365
366
367
368
369
370
371
# File 'lib/lafcadio/objectStore.rb', line 363

def transactional_clone
	tc = Cache.new @db_bridge.transactional_clone
	dcc_clones = {}
	@domain_class_caches.each do |domain_class, dcc|
		dcc_clones[domain_class] = dcc.transactional_clone
	end
	tc.domain_class_caches = dcc_clones
	tc
end

#update_dependent_domain_class(db_object, aClass, field) ⇒ Object



373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/lafcadio/objectStore.rb', line 373

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



386
387
388
389
390
391
392
393
# File 'lib/lafcadio/objectStore.rb', line 386

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