Difference between revisions of "Persistence code samples"

(BINARY_FORMAT)
Line 1: Line 1:
 
Here the main classes of the framework are sketched.  
 
Here the main classes of the framework are sketched.  
 +
 +
For an open discussion on the design choices have a look at the [[persistence framework design discussion]]. 
  
 
You can download the source code here (TODO: provide a link to the sources)
 
You can download the source code here (TODO: provide a link to the sources)

Revision as of 00:45, 19 August 2007

Here the main classes of the framework are sketched.

For an open discussion on the design choices have a look at the persistence framework design discussion.

You can download the source code here (TODO: provide a link to the sources)

Simplified BON diagram

PERSISTENCE_MANAGER

class
	PERSISTENCE_MANAGER
 
feature -- Access
 
	medium: PERSISTENCE_MEDIUM -- the chosen medium
 
	format: PERSISTENCE_FORMAT	-- the chosen format
 
feature -- Basic operations
 
	store (an_object:ANY)
			-- persists an_object using the format and medium stored by current object
		require
			an_object_exists:an_object /= Void
			medium_exists: medium /= Void
		do
			format.store(an_object,medium)
		end
 
	retrieve: ANY
			-- retrieves an_object using the medium and format stored by current object
		require
			medium_exists: medium /= Void
		do
			Result:=format.retrieve(medium)
		end
end

BINARY_SERIALIZATION_MANAGER

class
	BINARY_SERIALIZATION_MANAGER
 
inherit
	PERSISTENCE_MANAGER
 
create
	make
 
feature -- Creation
 
	make (file_name: STRING)
			-- Creation procedure
		require
			file_name_exists: file_name /= Void
		do
			create {FILE_MEDIUM} medium.make (file_name)
			create {INDEPENDENT_BINARY_FORMAT} format
		ensure
			medium_exists: medium /= Void
			format_exists: format /= Void
		end
 
end

PERSISTENCE_FORMAT

deferred class
	PERSISTENCE_FORMAT
 
feature -- Basic operations
 
	store (object:ANY; medium:PERSISTENCE_MEDIUM)
			-- stores object using medium
		require
			object_exists: object /= Void
			medium_exists: medium /= Void
		deferred
		end
 
	retrieve (medium: PERSISTENCE_MEDIUM):ANY
		require
			medium_exists: medium /= Void
		deferred
		end
end

BINARY_FORMAT

deferred class
	BINARY_FORMAT
 
inherit
	PERSISTENCE_FORMAT
 
	redefine
		store,
		retrieve
	end
 
feature -- Access
 
	store_handler:SED_STORABLE_FACILITIES
	serializer:SED_MEDIUM_READER_WRITER
 
 
feature -- Basic operations
 
	store (object:ANY; data_file:FILE_MEDIUM)
			-- stores object using a FILE_MEDIUM
		do
			create store_handler
			create serializer.make (data_file.raw_file)
			if data_file.exists then
				data_file.reopen_write (data_file.file_name)
			else
				data_file.open_write
			end
			serializer.set_for_writing
			store_now (object)
		end
 
	store_now (object_to_store:ANY)
			--proper store operation depends on specific descendant
	require
		object_to_store_exists:object_to_store/=Void
		deferred
	end
 
	retrieve (data_file:FILE_MEDIUM):ANY
			-- retrieves object using a FILE_MEDIUM
		do
			create store_handler
			create serializer.make (data_file.raw_file)
			if data_file.exists then
				data_file.open_read
				serializer.set_for_reading
				Result:= store_handler.retrieved (serializer,true)
			else
				print ("%NData file does not exist! ")
			end
		end
end

INDEPENDENT_BINARY_FORMAT

class
	INDEPENDENT_BINARY_FORMAT
 
inherit
	BINARY_FORMAT
 
feature -- Access
 
	optimized_for_retrieval: BOOLEAN
 
feature -- Status setting
 
	set_optimized_for_retrieval (is_optimized_for_retrieval:BOOLEAN)
	do
		optimized_for_retrieval:= is_optimized_for_retrieval
	ensure
		optimized_for_retrieval_set:optimized_for_retrieval = is_optimized_for_retrieval
	end
 
feature -- Basic operations
 
	store_now (object_to_store:ANY)
	do
		store_handler.independent_store(object_to_store,serializer,optimized_for_retrieval)
	end
 
end

FILE_MEDIUM

class
	FILE_MEDIUM
 
inherit
	PERSISTENCE_MEDIUM
 
create
	make
 
feature --Creation
 
	make (a_file_name:STRING)
	require
		a_file_name_exists:a_file_name /= Void
	do
		create raw_file.make (a_file_name)
		file_name:=a_file_name
	ensure
		raw_file_exists: raw_file /=Void
		file_name_set: file_name = a_file_name
	end
 
feature -- Access
 
	raw_file: RAW_FILE 
	file_name: STRING
 
feature -- Status report
 
	exists: BOOLEAN
	do
		Result:= raw_file.exists
	end
 
feature -- Basic operations
 
	reopen_write (a_file_name:STRING)
			-- reopens the file named 'a_file_name' for writing
	require
		a_file_name_exists:a_file_name /= Void
	do
		raw_file.reopen_write (a_file_name)
	end
 
	open_write
			-- opens the current file for writing
	require
		a_file_to_open_exists:raw_file /= Void
	do
		raw_file.open_write
	end
 
	open_read
			-- opens the current file for reading
	do
		raw_file.open_read
	end
 
end