Use JAVA serialization to create your own "object database"
As we all know, Java has a powerful function-serialization, any class as long as the implementation of the Serializable interface (this interface is only an empty interface), then the class instance object can be saved and can also be exported to the disk of the previous file, You can also revert from the stream to the state of the object before it was saved. The most important point about serialization is that when an object is serialized, all other objects that are referenced are also serialized together, so the serialization operation gets the complete object connection graph. Of course, the prerequisite is that all objects must implement the Serializable interface.
With this feature, we can implement our own simple "object database", or rather "Object file". Based on this idea, I have achieved the most basic operations, such as the establishment of the database, the establishment of data tables, table queries, updates, inserts, deletes and simple transaction processing. Among them, transaction processing uses the caching table technology. A simple File organization chart is presented as follows:
Note: The main system table is primarily used to store all user-created database information, such as all current database names. The main system table consists of a single file (Systemtable.ser), and a Systemtable object is saved in the file, which is composed of a TreeMap object, and also provides some common methods for collection operations. The key in the TreeMap object is the name of the database, value is a metadata object, and the metadata object is used to hold some meta data information. such as table name, keyword, type of object stored.
Description: User library is a database created by the user, maintained by a TreeMap object, TreeMap key is the name of the datasheet, value is the Systemtable object and ArrayList object, The Systemtable object holds metadata information about all the data tables in the current user library, such as table names and the types of elements in each ArrayList object. The ArrayList object stores each individual object (the type of each element in the ArrayList must be consistent), equivalent to a table in the relational database, and each element in the ArrayList object corresponds to each record in the relational database.
The following are interface files: Databaseoperationimp.java
Public interface Databaseoperationimp {
/**
* Create database files, need to use synchronous processing
* @param databaseName The name of the database you want to create
* @throws readdbexception
* @throws systemtableexception
*/
public void CreateDatabase (String databaseName) throws iodbexception,systemtableexception;
/**
* Set up data table, need to use synchronous processing
* @param tablename The name of the data table you want to create
* @param className The types of objects stored in the table, such as Person.class, etc.
* @throws readdbexception
* @throws systemtableexception
*/
public void CreateTable (String tablename,class className) throws iodbexception,systemtableexception;
/**
* Inserts an object into the specified table, using synchronous processing
* @param Item object to store
* @throws iodbexception
*/