As is well known, Java has a powerful feature - serialization, any class as long as the Serializable interface (this interface is just an empty interface), then this type of instance object can be saved and can also be output to disk A file, but also recover from the stream to the status of the object before saving. About Serialization Most is that when the object is serialized, all other objects referenced will also be serialized together, so serialization can get a complete object connection graphic. Of course, the premise is that all objects must implement the serializable interface.
Using this feature, we can implement your own simple "object database", and more exactly "Object files". Based on this idea, I have achieved the most basic operations, such as establishing a database, establishing a data sheet, query, update, insertion, deletion, and simple transaction. Among them, transaction processing uses a cache table technology. A simple file organization structure map will now be displayed as follows:
Note: The main system table is mainly used to store database information created by all users, such as: currently all database names, etc. The main system table is composed of separate files (SystemTable.ser), and a systemTable object is saved in the file, which is composed of a TreeMap object, and there is also a commonly used collection operation method. The key in the TreeMap object is the name of the database, and Value is a Metadata object, and the Metadata object is used to save some metadata information. If the type of table name, keyword, storage object, etc.
Description: The user library is the database created by the user. It is maintained by a TreeMap object. The TreeMap's key is the name of the data sheet. Value is the systemTable object and the ArrayList object. The SystemTable object saves metadata information for all data tables in the current user library. For example, the table name and the type of elements in each ArrayList object. The ArrayList object stores the specific individual objects (the type of each element in ArrayList must be consistent), which is equivalent to a table in the relational database, and each element in the ArrayList object is equivalent to the relational database. Each record.
The following is an interface file: DatabaseOperationImp.java
Public interface databaseOperationImp {
/ **
* Establish a database file, you need to use synchronous processing
* @Param DatabaseName Database Name to create
* @Throws ReadDbexception
* @Throws SystemTableException
* /
Public void createDatabase (String DatabaseName) throws iodbexception, SystemTableException;
/ **
* Establish a data sheet, you need to use synchronous processing
* @Param TableName Data Table Name to Create
* @Param classname The type of object stored in the table, such as Person.class, etc.
* @Throws ReadDbexception
* @Throws SystemTableException
* /
Public void CreateTable (String Tablename, Class ClassName) Throws Iodbexception, SystemTableException;
/ **
* Insert an object in the specified table, you need to use synchronous processing
* @Param Item To store the object * @throws iodbexception
* /
Public void insert (Object item) throws iodbexception, systemTableException
/ **
* Read the first object in the specified table
* @Return Object
* @Throws Iodbexception
* /
Public Object Readfirst () THROWS IODBEXCEPTION;
/ **
* Read the last object in the specified table
* @Return Object
* @Throws Iodbexception
* /
Public Object Readlast () THROWS IODBEXCEPTION;
/ **
* Read all objects in the specified table
* @Return List
* @Throws Iodbexception
* /
Public List Readall () THROWS IODBEXCEPTION;
/ **
* Read all objects starting from Start in the specified table
* @Param Start start index
* @Return List
* @Throws Iodbexception
* @Throws IndexOutofboundSexception
* /
Public List Readpart (Int Start) Throws Iodbexception, IndexOfboundSexception
/ **
* Read all objects (overload method) from start to END from START in the specified table
* @Param Start start index
* @Param END end index
* @Return List
* @Throws Iodbexception
* @Throws IndexOutofboundSexception
* /
Public List Readpart (int Start, int end) throws iod/exception, indexoutofboundsexception
/ **
* Read records in the specified table according to query conditions
* @Param Condition Save the criterion object of the query condition
* @Return List
* @Throws Iodbexception
* @Throws SystemTableException
* /
Public List Read (criterion condition).
/ **
* Update records in the table, need to use synchronous processing
* @Param Condition Saves criterion objects for updates
* @Param FieldValue Save the FieldValueBean object to be updated
* @Return INT number of records
* @Throws Iodbexception
* /
Public int update (criterion condition, fieldvaluebean fieldvalue).
/ **
* Delete records in the table, you need to use synchronous processing
* @Param Condition Save Criterion objects for delete conditions
* @Return INT number of records
* @Throws Iodbexception
* /
Public Int Delete (criterion condition).
/ **
* Delete all records in the table (overload method), you need to use synchronous processing
* @Return INT Affected Document * @Throws Iodbexception
* /
Public int delete () throws iodbexception;
/ **
* Start a transaction processing
* /
Public void begintrans ();
/ **
* Submit a transaction
* /
Public void commit ();
/ **
* Roll back a transaction
* /
Public void rollback ();
}
In addition, it is important to note that when the object is read or saved, since the user needs to read or saved the object, it is necessary to provide the Class object of the object when the table is established. Then you can use the Java reflection mechanism to move the SETER and GETTER methods of the object, so you can set or read the attribute values in the JavaBean object.
Since I have limited capacity, this Dongdong is currently written very rough, and it takes a long time to improve and expand, Java socket interface and simple serialized object protocol, which makes us create their own "" Network Object Database.