Difference between revisions of "Persistence code samples"

m (apply void-safety and style guide)
 
(19 intermediate revisions by 3 users not shown)
Line 1: Line 1:
Here the main classes of the framework are sketched.  
+
[[Category:Persistence]]
 +
Below are sketches of essential classes of the PERSIST framework.  
  
For an open discussion on the design choices have a look at the [[persistence framework design discussion]].   
+
For an open discussion on the design choices please 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)
 +
 +
An example of an actually implemented library built according to the PERSIST framework is [http://dadle.origo.ethz.ch/wiki/documentation_of_the_library dadle].
  
 
You can also go back to the [[persistence unified]] page.
 
You can also go back to the [[persistence unified]] page.
 
==Simplified BON diagram==
 
==Simplified BON diagram==
 +
 +
[[Image:Abel_class_diagram.png]]
 +
 
==PERSISTENCE_MANAGER==
 
==PERSISTENCE_MANAGER==
<code>
+
<e>
 
class
 
class
 
PERSISTENCE_MANAGER
 
PERSISTENCE_MANAGER
Line 14: Line 20:
 
feature -- Access
 
feature -- Access
  
medium: PERSISTENCE_MEDIUM -- the chosen medium
+
medium: PERSISTENCE_MEDIUM
 +
-- Chosen medium
  
format: PERSISTENCE_FORMAT -- the chosen format
+
format: PERSISTENCE_FORMAT
 +
-- Chosen format
  
 
feature -- Basic operations
 
feature -- Basic operations
  
store (an_object:ANY)
+
store (an_object: ANY)
-- persists an_object using the format and medium stored by current object
+
-- Persists an_object using the format and medium stored by current object.
require
+
an_object_exists:an_object /= Void
+
medium_exists: medium /= Void
+
 
do
 
do
format.store(an_object,medium)
+
format.store (an_object, medium)
 
end
 
end
  
 
retrieve: ANY
 
retrieve: ANY
-- retrieves an_object using the medium and format stored by current object
+
-- Retrieves an_object using the medium and format stored by current object.
require
+
medium_exists: medium /= Void
+
 
do
 
do
Result:=format.retrieve(medium)
+
Result := format.retrieve (medium)
 
end
 
end
 +
 
end
 
end
</code>
+
</e>
  
==BINARY_SERIALIZATION_MANAGER==
+
==BINARY_PERSISTENCE_MANAGER==
<code>
+
<e>
 
class
 
class
BINARY_SERIALIZATION_MANAGER
+
BINARY_PERSISTENCE_MANAGER
  
 
inherit
 
inherit
Line 50: Line 54:
 
make
 
make
  
feature -- Creation
+
feature -- Initialization
  
 
make (file_name: STRING)
 
make (file_name: STRING)
-- Creation procedure
+
-- Initialization procedure
require
+
file_name_exists: file_name /= Void
+
 
do
 
do
 
create {FILE_MEDIUM} medium.make (file_name)
 
create {FILE_MEDIUM} medium.make (file_name)
 
create {INDEPENDENT_BINARY_FORMAT} format
 
create {INDEPENDENT_BINARY_FORMAT} format
ensure
 
medium_exists: medium /= Void
 
format_exists: format /= Void
 
 
end
 
end
  
 
end
 
end
</code>
+
</e>
  
 
==PERSISTENCE_FORMAT==
 
==PERSISTENCE_FORMAT==
<code>
+
<e>
 
deferred class
 
deferred class
 
PERSISTENCE_FORMAT
 
PERSISTENCE_FORMAT
Line 74: Line 73:
 
feature -- Basic operations
 
feature -- Basic operations
  
store (object:ANY; medium:PERSISTENCE_MEDIUM)
+
      serialized_form: SERIALIZED_FORM
-- stores object using medium
+
-- Desired serialized form. If exists, this is what will be serialized.
require
+
     
object_exists: object /= Void
+
      set_serialized_form (custom_serialized: SERIALIZED_FORM)
medium_exists: medium /= Void
+
-- Set desired custom serialized form. This is what will be serialized.
 +
do
 +
serialized_form := custom_serialized
 +
ensure
 +
object_effect: custom_ser = serialized_form
 +
end
 +
 
 +
store (object: ANY; medium: PERSISTENCE_MEDIUM)
 +
-- Store 'object' using medium
 
deferred
 
deferred
 
end
 
end
  
 
retrieve (medium: PERSISTENCE_MEDIUM):ANY
 
retrieve (medium: PERSISTENCE_MEDIUM):ANY
require
 
medium_exists: medium /= Void
 
 
deferred
 
deferred
 
end
 
end
 
end
 
end
</code>
+
</e>
  
 
==BINARY_FORMAT==
 
==BINARY_FORMAT==
  
<code>
+
<e>
 
deferred class
 
deferred class
 
BINARY_FORMAT
 
BINARY_FORMAT
Line 98: Line 103:
 
inherit
 
inherit
 
PERSISTENCE_FORMAT
 
PERSISTENCE_FORMAT
 
+
redefine
redefine
+
store,
store,
+
retrieve
retrieve
+
end
end
+
  
 
feature -- Access
 
feature -- Access
  
store_handler:SED_STORABLE_FACILITIES
+
store_handler: SED_STORABLE_FACILITIES
serializer:SED_MEDIUM_READER_WRITER
+
  
 +
serializer: SED_MEDIUM_READER_WRITER
  
 
feature -- Basic operations
 
feature -- Basic operations
  
store (object:ANY; data_file:FILE_MEDIUM)
+
store (object: ANY; data_file: FILE_MEDIUM)
-- stores object using a FILE_MEDIUM
+
-- stores 'object' using a FILE_MEDIUM
 
do
 
do
 
create store_handler
 
create store_handler
Line 126: Line 130:
 
end
 
end
  
store_now (object_to_store:ANY)
+
store_now (object_to_store: ANY)
--proper store operation depends on specific descendant
+
-- Proper store operation depends on specific descendant.
require
+
object_to_store_exists:object_to_store/=Void
+
 
deferred
 
deferred
end
+
end
  
retrieve (data_file:FILE_MEDIUM):ANY
+
retrieve (data_file: FILE_MEDIUM): ANY
-- retrieves object using a FILE_MEDIUM
+
-- Retrieve object using a FILE_MEDIUM
 
do
 
do
 
create store_handler
 
create store_handler
Line 146: Line 148:
 
end
 
end
 
end
 
end
 +
 
end
 
end
</code>
+
</e>
  
 
==INDEPENDENT_BINARY_FORMAT==
 
==INDEPENDENT_BINARY_FORMAT==
<code>
+
<e>
 
class
 
class
 
INDEPENDENT_BINARY_FORMAT
 
INDEPENDENT_BINARY_FORMAT
Line 164: Line 167:
  
 
set_optimized_for_retrieval (is_optimized_for_retrieval:BOOLEAN)
 
set_optimized_for_retrieval (is_optimized_for_retrieval:BOOLEAN)
do
+
do
optimized_for_retrieval:= is_optimized_for_retrieval
+
optimized_for_retrieval:= is_optimized_for_retrieval
ensure
+
ensure
optimized_for_retrieval_set:optimized_for_retrieval = is_optimized_for_retrieval
+
optimized_for_retrieval_effect: optimized_for_retrieval = is_optimized_for_retrieval
end
+
end
  
 
feature -- Basic operations
 
feature -- Basic operations
  
 
store_now (object_to_store:ANY)
 
store_now (object_to_store:ANY)
do
+
do
store_handler.independent_store(object_to_store,serializer,optimized_for_retrieval)
+
store_handler.independent_store(object_to_store,serializer,optimized_for_retrieval)
end
+
end
  
 
end
 
end
</code>
+
</e>
  
 
==FILE_MEDIUM==
 
==FILE_MEDIUM==
<code>
+
<e>
 
class
 
class
 
FILE_MEDIUM
 
FILE_MEDIUM
Line 191: Line 194:
 
make
 
make
  
feature --Creation
+
feature -- Initialization
  
 
make (a_file_name:STRING)
 
make (a_file_name:STRING)
require
+
do
a_file_name_exists:a_file_name /= Void
+
create raw_file.make (a_file_name)
do
+
file_name:=a_file_name
create raw_file.make (a_file_name)
+
ensure
file_name:=a_file_name
+
file_name_effect: file_name = a_file_name
ensure
+
end
raw_file_exists: raw_file /=Void
+
file_name_set: file_name = a_file_name
+
end
+
  
 
feature -- Access
 
feature -- Access
  
raw_file: RAW_FILE  
+
raw_file: RAW_FILE
 +
 
file_name: STRING
 
file_name: STRING
  
Line 212: Line 213:
  
 
exists: BOOLEAN
 
exists: BOOLEAN
do
+
do
Result:= raw_file.exists
+
Result := raw_file.exists
end
+
end
  
 
feature -- Basic operations
 
feature -- Basic operations
  
reopen_write (a_file_name:STRING)
+
reopen_write (a_file_name: STRING)
-- reopens the file named 'a_file_name' for writing
+
-- Reopens the file named 'a_file_name' for writing.
require
+
do
a_file_name_exists:a_file_name /= Void
+
raw_file.reopen_write (a_file_name)
do
+
end
raw_file.reopen_write (a_file_name)
+
end
+
  
 
open_write
 
open_write
-- opens the current file for writing
+
-- Open the current file for writing.
require
+
do
a_file_to_open_exists:raw_file /= Void
+
raw_file.open_write
do
+
end
raw_file.open_write
+
end
+
  
 
open_read
 
open_read
-- opens the current file for reading
+
-- Open the current file for reading.
do
+
do
raw_file.open_read
+
raw_file.open_read
end
+
end
  
 
end
 
end
</code>
+
</e>

Latest revision as of 07:11, 20 May 2013

Below are sketches of essential classes of the PERSIST framework.

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

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

An example of an actually implemented library built according to the PERSIST framework is dadle.

You can also go back to the persistence unified page.

Simplified BON diagram

Abel class diagram.png

PERSISTENCE_MANAGER

class
	PERSISTENCE_MANAGER
 
feature -- Access
 
	medium: PERSISTENCE_MEDIUM
			-- Chosen medium
 
	format: PERSISTENCE_FORMAT
			-- Chosen format
 
feature -- Basic operations
 
	store (an_object: ANY)
			-- Persists an_object using the format and medium stored by current object.
		do
			format.store (an_object, medium)
		end
 
	retrieve: ANY
			-- Retrieves an_object using the medium and format stored by current object.
		do
			Result := format.retrieve (medium)
		end
 
end

BINARY_PERSISTENCE_MANAGER

class
	BINARY_PERSISTENCE_MANAGER
 
inherit
	PERSISTENCE_MANAGER
 
create
	make
 
feature -- Initialization
 
	make (file_name: STRING)
			-- Initialization procedure
		do
			create {FILE_MEDIUM} medium.make (file_name)
			create {INDEPENDENT_BINARY_FORMAT} format
		end
 
end

PERSISTENCE_FORMAT

deferred class
	PERSISTENCE_FORMAT
 
feature -- Basic operations
 
       serialized_form: SERIALIZED_FORM
			-- Desired serialized form. If exists, this is what will be serialized.
 
       set_serialized_form (custom_serialized: SERIALIZED_FORM)
			-- Set desired custom serialized form. This is what will be serialized.
		do
			 serialized_form := custom_serialized
		ensure
			object_effect: custom_ser = serialized_form
		end
 
	store (object: ANY; medium: PERSISTENCE_MEDIUM)
			-- Store 'object' using medium
		deferred
		end
 
	retrieve (medium: PERSISTENCE_MEDIUM):ANY
		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.
		deferred
		end
 
	retrieve (data_file: FILE_MEDIUM): ANY
			-- Retrieve 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_effect: 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 -- Initialization
 
	make (a_file_name:STRING)
		do
			create raw_file.make (a_file_name)
			file_name:=a_file_name
		ensure
			file_name_effect: 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.
		do
			raw_file.reopen_write (a_file_name)
		end
 
	open_write
			-- Open the current file for writing.
		do
			raw_file.open_write
		end
 
	open_read
			-- Open the current file for reading.
		do
			raw_file.open_read
		end
 
end