We have introduced the parsing of SQL statements. Before we begin to introduce the implementation of query actions, we need to take a look at simpledb's management of metadata (metadata.
Integrity considerations correspondCodeHere we will introduce record and metadata together.
The class diagrams of metadata and record are as follows:
Figure 1 metadata and record class diagram
The class diagram shows that simpledb maintains four types of metadata: Table information (including field information), view information, index information, and statistical information. Some of these four types of data are stored on disks, such as table information, field information, view information, and index information. Others are maintained in the runtime memory, such as statistical information.
Figure 2 metadata composition diagram
The following describes how to manage these four types of information one by one.
1. (tablemgr) TABLE Information Management
I plan to introduce it in the top-down order, for example:
Figure 2 Table Management
Table Information Manager maintains two tableinfo objects: tcatinfo and fcatinfo. The table information is persisted to tcatinfo. TBL and fcatinfo. TBL.
Table 1 tcatinfo Schema
Table 2 schema of fcatinfo
Tablename |
Fldname |
Type |
Length |
Offset |
For example: Create Table students (SID varchar (5), sname varchar (20 ));
Tcatinfo adds the following records
Fcatinfo adds the following records
Students |
Sid |
4 |
14 |
0 |
Students |
Sname |
12 |
44 |
14 |
Note:
The A. Type field is marked with the const int type value:
Public Const IntInteger = 4, varchar = 12;
B. as mentioned in the previous storage, simpledb stores strings in the form of "Length + content", and each Char has two locations in the byte array, therefore, the length of varchar (5) is 2*5 + 4 = 44.
When creating a new table:
A. Write basic table information to tcatinfo. TBL;
B. Write the field information of the table to fcatinfo. TBL.
When querying:
A. Open the tcatinfo. TBL file,SequenceScan files to obtain basic information about the target table. The key is recordlength;
B. Open the fcatinfo. TBL file,SequenceScan the file to obtain the field information of the target table. The key is the table schema and the offsets of each field offset;
C. Return a tableinfo object that contains all the obtained table information.
Table information, including four elements: tablename, table schema, offsets of each field offset, and recordlength
Offsets is a <fieldname, Offset> dictionary. You can use offsets [fieldname] to obtain the offset of a specified field in a single-pick record.
Here, recordlength refers to the byte length, taking into account the special storage format of strings in simpledb mentioned above.
Maintains a <fieldname, fieldinfo> dictionary. fieldinfo contains the type and length of the corresponding field.
Note that only integer and varchar types are supported, and the field length is different as follows:
Public VoidAddintfield (StringFldname)
{
Addfiled (fldname, integer, 0 );
}
Public VoidAddstringfield (StringFldname,IntLength)
{
Addfiled (fldname, varchar, length );
}
The length of integer is set to 0.
Contains two key fields: type and length.
The above is the management of table information in metadata management, and the management of the other three types of information will be resumed later.