EOS database and persistent api--architecture

Source: Internet
Author: User

An explanation of the EOS database structure

In EOS, when the smart contract finishes executing, the memory consumed is freed. All variables in the program will be lost. If you want to keep track of information in a smart contract, such as a game intelligence contract that records each user's game, the data cannot be lost after the contract is executed, and the data needs to be stored in the EOS database. APIs that interact with the database are officially persistence APIs, and Chinese can be called persistent APIs. Describes how the EOS smart contract interacts with the database as it executes the Action.

To facilitate the interaction of the smart contract with the EOS database, Eos mimics theMulti-Index Containers, a C + + class has been developed:eosio::multi_index(hereinafter referred to asmulti_index), Chinese can be called multi-indexed list classes.

multi_index   Header file address:  
Https://github.com/EOSIO/eos/blob/master/contracts/eosiolib/multi_index.hpp

multi_index as an intermediate tool (or container), each multi_index instance interacts with a specific data table for a particular account (depending on the parameters at the time of instantiation). The EOS smart contract interacts with the data in the EOS database as shown.

Data Sheet

multi_indexis a very convenient database interaction container that can store any C + + data type. Each onemulti_indexis equivalent to a data table (table) of a traditional database, but changes the row and column form of a traditional database to a simple column. Other wordsmulti_indexis a linearly arranged table with only one column, and each row stores only one object. But generallymulti_indexStored objects are structs or classes that contain multiple member variables, somulti_indexThe flexibility to store data is no less than a traditional database.

We use the official "car repair shop" example, we create a data sheet that stores the account name, maintenance time, and mileage of each car repair shop customer. So multi_index data tables are stored in a project, each of which is the following structure:

struct Service_rec {uint64_t pkey;       Primary key account_name customer;   Owner's user name uint32_t service_date;       Maintenance time uint32_t odometer; Vehicle mileage};

In a traditional database, a 4-column data table is needed to store this 4 data per user, andmulti_indexOnly one column per data table, storing only the user'sservice_recThe entire structure can be. Formulti_indexData.

Multi-Index

First, each data table has a set of primary keys, which must be unsigned 64-bit integer type (64-bit integer), which is theservice_recThe first variable in a struct isuint64_tThe reason for the type. In the data table, all objects are arranged in ascending order of the primary key, small in front, and large in the rear. The primary key can be meaningful or meaningless, allowing the system to produce a primary key that is not used in the data table. In order to set the primary key, we need toservice_recStructure to add a callprimary_key()The return value of the member function function is the primary key.

Auto Primary_key () const {return pkey;}

This sets the pkey variable to the primary key.

multi_indexLiterally, a data table that can use multiple indexes. In EOS, eachmulti_indexOr, you can set up up to 16 indexes per table of data. Indexes are equivalent to reordering objects in a data table in a specific way. For example, in the Windows File Manager that we often use, you can sort by file name, sort by the time the files are modified, sort by file size, and there are 3 indexes. The EOS database index is more flexible, can be indexed individually by a variable in the struct, or the result of the operation between variables (function output). If we want to use车主用户名Index, you need to add aget_customer()member function, the return value of the function is an indexed variable.

Account_name Get_customer () const {return customer;}

This sets the customer variable to an index on the data table and the index to the customer right.

Iterators

multi_indexHow do you manipulate each object in a data table? The answer is iterators (iterator). You can search for "C + + iterators" or "iterator patterns" in design patterns to understand the design ideas of iterators. In the EOS database, I prefer to liken an iterator to an "elevator" that travels up and down the entire data table. All operations on the data must be done through an iterator. The typical data modification process is this: first using the iterator'sfind()method to find the required data in a particular index, such as looking for a user in the owner's user name index. The iterator is moved to the desired data object. You can then use the iterator'smodify()Method modifies the data corresponding to the current iterator. The case where the iterator points to the user Sue.

This article introduces the basic structure of the EOS database, and future articles will detail the EOS database using the actual combat, please look forward to.


EOS database and persistent api--architecture

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.