LiteDB source analytic series (2) Detailed database pages

 Technical works 1.LiteDB page

  LiteDB Although it is a single file type of database, but the database has a lot of information, such as indexes, collections, documents and so on. In order to manage this information, LiteDB implements the concept of a database page. P is a 4096-byte block of the same address information is stored. Page is the smallest unit disk file operation (read and write) of. 6 LiteDB page type, class diagram as follows:

1.1 BasePage

  BasePage database page type parent using a constant PAGE_SIZE field defines the size of the page is 4096 bytes.

  The main properties as follows:

  PageID: a uint type of ID, in LiteDB database, no matter what type of page, this PageID are not the same.

  PrevPageID: pointing to the previous page ID. If it points to a maximum uint (4294967295) means that no previous.

  NextPageID: points to the next page ID. If it points to a maximum uint (4294967295) means that no page one. From here page structure a bit like a doubly linked list, but in the actual store, seemingly from page to page is not stored in a linked list (Maybe I did here too thoroughly understand the specific role of these two ID's).

  PageType: a page type enumeration The enumeration may be cast to BasePage corresponding to the type of page.

  ItemCount: ItemCount calculation sheet for a specific data size, can be seen in its role in DataPage and IndexPage.

  IsDirty: We can probably guess from the name of its role in the LiteDB designed to mark whether the data on the page to complete Commit operation.

  FreeBytes: This attribute of each subclass overrides the need for calculating how many bytes available page.

1.2 CollectionPage

  A CollectionPage on behalf of a table, such as Customer and Order in the database has two tables, then there is information about two CollectionPage two tables are stored in the database. With Prev / Next connection CollectionPage.

  CollectionPage has a CollectionName represents the name of the table, such as Customer and Order two tables there are two names "Customer" and "Order". DocumentCount property marking the table how many records, such as inserting data into the Customer table 100, then DocumentCount to 100.

  CollectionPage inside a main attribute Indexes, this is a definition of the structure CollectionIndex array of self, it represents all the names of the index table. For example, the Customer table has three fields, respectively, "ID", "Name", "Age", ( with special emphasis on these three fields to be used as an index ) so Indexes array will contain each of these three fields. At the same time you can see this page in PK properties of a man named according to the name we know it almost certainly is the primary key, such as the above Customer table "ID".

1.3 HeaderPage

  HeaderPage store some information LiteDB database currently in use, including header files, database version, available spare page ID, user version. Which has a property named ChangeID, this is the time for processing transactions, verify that the client's ChangeID consistent.

1.4 DataPage

  DataPage is the data page, it's the simplest structure, in addition to the parent class only contains a dictionary of named DataBlocks, this dictionary is a ushort Key figures, Value is a DataBlock class. We analyze DataBlock behind this structure will generally be able to know the role of DataPage. While data page can also be seen that it is the override attribute bytes available FreeBytes subtracting the length of the dictionary, ItemCount equal to the length of the dictionary.

1.5 ExtendPage

  ExtendPage extended page data, if the data is too large insert. Just over Page ExtendPage data block into the Data, like it is the override attribute bytes available FreeBytes subtracting the length of the Data.

1.6 IndexPage

  IndexPage is the index page, its structure and DataPage Similarly, the dictionary contains only called Nodes of this dictionary is a key ushort digital, Value is a IndexNode class, use the index jump table form for storage.

2. Data Visualization - Page opened veil

  After reading the above description may be, you may still be confused on the data page, when I look at the source code as well. Later, after my efforts, came up with a way, is the real-time information page displayed, it is often said of data visualization . Later I will specifically describe how to display the information out of the data page, where we follow the first look down.

  First we create a database:

LiteEngine db = new LiteEngine(Path);

  Now I do with winfrom interface, Header following list to represent all HeaderPage information, other similar. Since ExtendPage function is relatively simple, so do not put the information displayed ExtendPage of. After executing this command, the interface you can see there are two pages are created:

      We can see HeaderPage and create a CollectionPage each, HeaderPage as to store information in the database, so a database is created and there is only one page, and then later add a new table, HeadPage only this page. Why is there a CollectionPage it, look at its table name, you know, master table, is not very familiar with? Yes, and system databases Sqlserver database of similar, but I do see this master table action LiteDB inside out.

  Then we add a named customer table fields are ID, Age, Name, Address. Namely the implementation of the following passage:

var col = db.GetCollection<Customer>("customer");
col.EnsureIndex ( " Age " ); // index field is determined Age
col.EnsureIndex ( " Name " ); // Name field index determined 

  After execution, we can see that there CollectionPage in a new table is created, its table named customer, this page has three table index, respectively, is the default ID primary key, then the Age and Name, note fields Address and no added.

  IndexPage increase of three, one for each page index, while there is only one index page node (IndexNode).

  DataPage data page is currently empty.

  We then insert three records, execute the following statement:

for (int i = 0; i < 3; i++)
{
    var customer = new Customer
    {
       Id = i,
       Name=i%2==1?"Jim1_"+i.ToString():"Jim2_"+i.ToString(),
       Age = i*10,
       Address = "Dump"
    }
     col.Insert(customer);
} 

  Then we will be able to see each index page index more than three nodes, but also created a data page. Finally, we add two tables, a table field to Order ID, Ordersum. Product another table Id, ProductName and ProductPrice. Two fields are all set to the index table, a data page structure as follows:

  

 

   Can be clearly seen from the above, each adding a table, it will create a CollectionPage page, add data to the table at the same time, if there is an index to add, then IndexPage page in the appropriate content will also be added. The above data in FIG out re-rendering detail is the following structure:

  From this figure, it should be very easy to understand the role of these types of page types. Note that the current version 0.0.8 of ID to be designated as BsonID, inside will be changed to "_id", which is also this version in some parts of the bug causes. While the data is not stored inside DataPage FIG json format but above Byte array.

3. behind the words

  This chapter blog finished, I just want to know one thing to say it was not easy, moreover is complicated source. I believe I will stick to update, but also hope to conduct more exchanges and see my blog you big God.

Guess you like

Origin www.cnblogs.com/xiaozhangStudent/p/11016139.html