-
Persistent ``things'' exist over a period of time
independent of many operations
-
Transient ``things'' exist only for a short period
of time
Examples
-
A file on a CDROM is very persistent
-
A file on disk is fairly persistent. Its contents
may change
-
A server process is fairly persistent
-
An ordinary application is transient
-
Data in an application is transient.
Local variables may be very transient
-
A dialog is very transient
Application data persistence
-
If an application requires data to be in existence
before it starts, and leaves the data in existence
after it finishes, the data must be persistent
-
The data will need to be stored in file
-
Changes:
- The application may treat the data are read-only
e.g. CONFIG.SYS
- The application may make small changes
e.g. bookmarks in a Web browser
-
It may make major changes e.g. an editor
Complexity
-
The data may be simple in structure e.g. server log
-
An SQL database consists of many tables, with
relationships between them
Storing and retrieving simple data
-
Simple data can be stored in many ways
- fixed sixe record
- variable size record with delimiter
such as '\n'
- readable format as strings
- binary format
-
Data format can be
- assumed by all applications using it
- self-describing format such as ASN.1
Serialisation of data
-
Persistence is achieved by two techniqes
- serialisation means a piece of data can be
written as a sequence of bytes
- serialised data can be written/read to/from
disk or other media
-
Serialisation is also needed to communicate data
between applications
Example 1: structure
struct Customer {
char last[20];
char first[20;
int id;
}
Example 2: linked list
Linked list:
This can be represented by
Example 3: string
String: "hello"
Example 4: array
Can be represented by
Example 5: circular list
Can be represented by
Example 6: binary tree
Prefix order is: node left right
Trees can be stored and restored by writing them in prefix
order, with a special flag for empty tree.
Example 7: graph
These need to be stored in a table
Identity problem
- Consider the graph
with table
This could also be the graph
-
Content != identity
-
Need to introduce unique labels
label | contents
|
1 | a
|
2 | b
|
3 | c
|
4 | d
|
with links
Objects
- Objects in Java contain primitive data (int, char, etc)
and references to other objects
- An array is an object, so is a String
- Serialising an object means
- serialising each primitive attribute
- inventing a label for each
object reference
- serialising the label
- storing the table of cross-references between
labels and objects
Serializable interface
Serializing an object (graph) to file
Restoring an object (graph) from file
Versioning
- What happens if you save an object, change the class definition
and then try to restore it?
- Java calculates a
long serialVersionUID
for each class
definition
- If you change the class, this value changes
- If you save with one version, and restore with another, the restore
will normally fail
- To make it succeed
- This will only work if you have not removed attributes
- If you have added attributes, then you may need to fix up
their values
Jan Newmarch (http://pandonia.canberra.edu.au)
jan@ise.canberra.edu.au
Last modified: Mon Aug 28 13:30:18 EST 2000
Copyright ©Jan Newmarch