Use Java serialization to build your own "object database"

Source: Internet
Author: User
Tags serialization

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:

Master System tables

Systemtable objects

TreeMap objects

Metadata objects

Database name


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.

User Library

TreeMap objects

Name of the system table

ArrayList objects

Systemtable objects

Data table name

Object objects

TreeMap objects

Metadata objects

Database name


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

*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.