WO2020233367A1 - Blockchain data storage and query method, apparatus and device, and storage medium - Google Patents

Blockchain data storage and query method, apparatus and device, and storage medium Download PDF

Info

Publication number
WO2020233367A1
WO2020233367A1 PCT/CN2020/087585 CN2020087585W WO2020233367A1 WO 2020233367 A1 WO2020233367 A1 WO 2020233367A1 CN 2020087585 W CN2020087585 W CN 2020087585W WO 2020233367 A1 WO2020233367 A1 WO 2020233367A1
Authority
WO
WIPO (PCT)
Prior art keywords
data
plan
query
blockchain
index
Prior art date
Application number
PCT/CN2020/087585
Other languages
French (fr)
Chinese (zh)
Inventor
鲁铁华
Original Assignee
深圳壹账通智能科技有限公司
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by 深圳壹账通智能科技有限公司 filed Critical 深圳壹账通智能科技有限公司
Publication of WO2020233367A1 publication Critical patent/WO2020233367A1/en

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/22Indexing; Data structures therefor; Storage structures
    • G06F16/2228Indexing structures
    • G06F16/2246Trees, e.g. B+trees
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/24Querying
    • G06F16/245Query processing
    • G06F16/2458Special types of queries, e.g. statistical queries, fuzzy queries or distributed queries
    • G06F16/2471Distributed queries

Definitions

  • This application relates to the field of block structure improvement, in particular to blockchain data storage and query methods, devices, equipment and storage media.
  • Blockchain is an intelligent peer-to-peer network that uses distributed databases to identify, disseminate and record information. It is also called the Internet of Value.
  • Blockchain is essentially a decentralized database.
  • Blockchain Contains a list called blocks, which has a continuously growing and neatly arranged record. Each block contains a timestamp and a link to the previous block. The block chain makes the data immutable. Once recorded, the data in a block will be irreversible.
  • Blockchain distributed storage data is of key-value type. Although the bottom layer of blockchain technology also provides interfaces for data operations, these interfaces are relatively simple.
  • the transaction information stored in the block is basically The existence of the string form requires the application to frequently parse the data information of the string. It only supports querying the data stored on the chain by key, and does not support complex queries, which is inconvenient to use.
  • the main purpose of this application is to solve the problem of insufficient support of the blockchain for complex queries and low retrieval efficiency. At the same time, it solves the need to rewrite the data query and storage logic code when the data storage is changed from a relational database to a blockchain distributed database. , Leading to the problems of large development workload, long time-consuming and low development efficiency.
  • the first aspect of this application provides a blockchain data storage and query method, including: generating an index database; obtaining a target structured query language SQL statement, the target SQL statement being legal in compliance with SQL syntax rules Statement; parse the target SQL statement according to a preset algorithm to obtain an abstract syntax tree; generate a logical execution plan according to the abstract syntax tree, and the logical execution plan includes inserting a data plan, modifying a data plan, and querying a data plan; according to The logic generation plan and the index database perform read and write operations on the blockchain distributed database to obtain read and write results.
  • the blockchain distributed database is used to store and query data.
  • the second aspect of the present application provides a block chain data storage and query device, including: a first generating unit for generating an index database; an obtaining unit for obtaining a target structured query language SQL statement, the target SQL statement It is a legal statement that conforms to the SQL grammar rules; the parsing unit is used to parse the target SQL statement according to a preset algorithm to obtain an abstract syntax tree; the second generation unit is used to generate a logical execution plan according to the abstract syntax tree, The logic execution plan includes inserting data plans, modifying data plans, and querying data plans; a reading and writing unit is used to perform read and write operations on the blockchain distributed database according to the logic generation plan and the index database to obtain read and write operations As a result, the blockchain distributed database is used to store and query data.
  • a third aspect of the present application provides a blockchain data storage and query method and equipment, including: a memory and at least one processor, the memory stores instructions, and the memory and the at least one processor are interconnected by wires; The at least one processor invokes the instructions in the memory, so that the blockchain data storage and query device executes the methods described in the foregoing aspects.
  • the fourth aspect of the present application provides a computer-readable storage medium that stores instructions in the computer-readable storage medium, which when run on a computer, causes the computer to execute the methods described in the above aspects.
  • the fifth aspect of the present application provides a computer program product containing instructions, which when run on a computer, causes the computer to execute the methods described in the foregoing aspects.
  • the use of SQL statements is compatible with query and storage in the blockchain distributed database and the relational database, and the corresponding relationship between the index database and the blockchain distributed database is established to improve the area.
  • Retrieval efficiency of blockchain business information encapsulate the underlying interface of blockchain, use SQL statements and index databases to achieve data storage and query on the chain.
  • Figure 1 is a schematic diagram of an embodiment of a blockchain data storage and query method in an embodiment of the application
  • FIG. 2 is a schematic diagram of another embodiment of a blockchain data storage and query method in an embodiment of the application
  • Fig. 3 is a schematic diagram of an embodiment of a block chain data storage and query device in an embodiment of the application
  • Figure 4 is a schematic diagram of another embodiment of a blockchain data storage and query device in an embodiment of the present invention.
  • Fig. 5 is a schematic diagram of another embodiment of a blockchain data storage and query device in an embodiment of the application.
  • the embodiment of the application provides a blockchain data storage and query method, device, device, and storage medium.
  • the use of SQL statements is compatible with blockchain distributed databases and relationships.
  • Query and storage of type database improve the retrieval efficiency of blockchain business information by establishing the corresponding relationship between the index database and the blockchain distributed database, encapsulate the underlying interface of the blockchain, and use SQL statements and index databases to achieve data on the chain Storage and query, lower development threshold and improve development efficiency.
  • An embodiment of the blockchain data storage and query method in the embodiment of the present application includes:
  • the server generates an index database according to business requirements.
  • the index database is located in the data middleware of the blockchain.
  • the data middleware also provides a unified interface for accessing the underlying blockchain of the blockchain.
  • the unified interface for accessing the underlying blockchain of the blockchain includes the creation of data , Query, modify and delete (create read update delete, CURD).
  • Index data is stored in a relational database MySQL, corresponding to the data type on a blockchain distributed database, and a one-to-one index data table is created in the index database.
  • the structure of the index data table contains all the information of the data structure in the blockchain distributed database. For example: Store user points data on the blockchain. The user points data includes user ID, user name, and user points. Then create Table 1 user points user index table in the index database.
  • Table 1 User score user index table
  • Field Name Field Type Remarks user_id bigint User ID, primary key user_name varchar username point int account credits data_id varchar Index identification, corresponding to the key of the blockchain data
  • Table 1 includes an index identification field data_id, which corresponds to the key key for storing data on the blockchain distributed database.
  • the server must query or store data on the blockchain distributed database according to the index identification.
  • Each index table in the index database contains a data_id field.
  • the data_id is unique in the index database and corresponds to the key in the blockchain distributed database one by one to establish the corresponding relationship between the index data and the blockchain data .
  • the server obtains the target structured query language SQL statement of the business request, and the target SQL statement is a legal statement that conforms to the SQL syntax rules.
  • the SQL statement is consistent with the SQL statement used to access the relational database, including four SQL statements inserting insert, modifying update, querying select, and deleting delete, corresponding to different interfaces.
  • the server receives the initial SQL statement and parameters, encapsulates the initial SQL statement and parameters, and the server calls the relevant interface to obtain the encapsulated initial SQL statement and parameters to obtain the target SQL statement.
  • the target SQL statement is a legal statement. The legality of the SQL has been judged before the business request, and the statement that does not conform to the SQL grammar rules is eliminated.
  • SQL structured query language
  • SQL is a database query and programming language, including SQL syntax rules, used to access data and manage relational database systems, SQL statements can be used to execute various Various operations, such as inserting data into the database, modifying data in the database, and extracting data from the database.
  • the server processes the target SQL statement according to the preset algorithm to obtain the abstract syntax tree. Specifically, the server parses the target SQL statement according to the SQL parser, and the parsing includes lexical analysis and syntax analysis. Among them, lexical analysis refers to splitting the target SQL statement into lexical units that cannot be subdivided. In SQL grammar, lexical units include keywords, identifiers, literals, operators, and delimiters. For example, when the server reads the UPDATE of the target SQL statement, the server determines that the first character "U" meets the rules of keywords and identifiers, and the second character "P" also meets the rules of keywords and identifiers.
  • the server judges that the rule is not met, and the server has completed the recognition of a lexical unit.
  • UPDATE is not only a keyword defined by the SQL specification, but also satisfies the rules of identifiers. Therefore, when a lexical unit is an identifier, the SQL parser judges the priority to determine whether a lexical unit is a keyword, and the priority of the keyword is high In the identifier rules.
  • Other rules include that characters starting with a number are read according to the literal value of the numerical rules; those starting with double quotation marks or single quotation marks are read according to the literal value of the string rules; operators or delimiters are read according to the symbol characteristics Recognition.
  • the grammatical analysis obtains a lexical unit from the lexical analysis each time. If the rules are met, the extraction and matching of the next lexical unit will continue until the end of the string; if the rules are not met, an error will be prompted and the analysis will end.
  • the grammar analysis finally converts the target SQL statement into an abstract syntax tree.
  • the server uses the SQL Parser module of the database connection pool druid to parse the target SQL statement, and parse the natural semantic SQL statement into a more abstract data structure, that is, an abstract syntax tree (AST).
  • AST is a tree representation of the abstract syntax structure of the source code. Each node on the tree represents a structure in the source code. The AST does not represent every detail of the actual syntax. For example, Nested parentheses are implicit in the structure of the tree and are not presented as nodes.
  • the logical execution plan includes inserting the data plan, modifying the data plan, and querying the data plan;
  • the server logically executes the plan according to the abstract syntax tree.
  • the logical execution plan includes inserting the data plan, modifying the data plan, and querying the data plan.
  • the server obtains SQL elements and data according to the abstract syntax tree, and generates a logical execution plan.
  • the logical execution plan describes what data needs to be queried or stored, and the logical execution plan includes insert data plan, modify data plan, and query data plan.
  • a query request for user points the server parses the received SQL query select statement, decomposes the original SQL string into a logical execution plan, that is, reads the value of the point field from the user index table, and the filter condition is the user name user_name The value is Tom's record.
  • the blockchain distributed database is used to store and query data
  • the server reads and writes the blockchain distributed database according to the logic generation plan and the index database to obtain the read and write results.
  • the blockchain distributed database is used to store and query data.
  • the read and write operations are inserted according to SQL commands into insert, modified update and query select.
  • the server parses the SQL statement to generate a logical execution plan, it operates the blockchain distributed database in conjunction with the index database to complete data query and storage operations.
  • the server When detecting that the logical execution plan is an insert data plan, the server performs an insert operation on the index database and the blockchain distributed database to obtain the read and write results. Specifically, the server generates the first index identifier; the server performs an insert operation on the index database according to the inserted data plan and the first index identifier to obtain the primary key value returned by the index database; the server queries the index database according to the primary key value, and obtains the returned value from the index database Insert data; the server performs an insert operation on the blockchain distributed database according to the first index identifier and the inserted data.
  • the server For example, for a request to insert user points data, the server first sets the first index identifier data_id of the user points index table to the character string USER_1, and then the server writes the data to the user of the index database according to the user points insertion data plan and the first index identifier In the score index table, get the primary key value 1 of the primary key user_id, query the user score index table according to the primary key value 1, and get the query data.
  • JSON JavaScript Object Notation
  • the server When detecting that the logical execution plan is a modified data plan, the server modifies the index database and the blockchain distributed database to obtain the read and write results. According to the second index identification, the data is queried from the blockchain distributed database to obtain the initial data, and the initial data is updated and then written into the blockchain distributed database to ensure the data integrity and consistency of the blockchain distributed database.
  • the server obtains the corresponding logical execution plan according to the request, and obtains the update element, then the server first updates the index database and obtains the second index identification data_id of the updated data Value, use the second index identifier as the key key, query the blockchain distributed database to obtain the initial value, which is JSON format data, modify the user’s point value for the initial value to obtain the target value, which is also JSON format data, and finally the server calls the underlying interface of the blockchain to write the key and target value into the blockchain distributed database.
  • the server When it is detected that the logical execution plan is a query data plan, the server performs a query operation on the index database and the blockchain distributed database to obtain the read and write results.
  • the server obtains the query results from the blockchain distributed database to ensure the accuracy of the data. For example, for a request to query user points, the server queries the user points data index table according to the query statement, and obtains the value USER_1 of the third index identifier data_id field.
  • the server uses USER_1 as the key to query the blockchain distributed database to obtain the query result.
  • the use of SQL statements is compatible with the query and storage of the blockchain distributed database and the relational database, and the corresponding relationship between the index database and the blockchain distributed database is established to improve the area.
  • Retrieval efficiency of blockchain business information encapsulate the underlying interface of the blockchain, use SQL statements and index databases to achieve data storage and query on the chain, reduce development thresholds, and improve development efficiency.
  • FIG. 2 another embodiment of the blockchain data storage and query method in the embodiment of the present application includes:
  • the server generates an index database according to business requirements.
  • the index database is located in the data middleware of the blockchain.
  • the data middleware also provides a unified interface for accessing the underlying blockchain of the blockchain.
  • the unified interface for accessing the underlying blockchain of the blockchain includes the creation of data , Query, modify and delete (create read update delete CURD).
  • Index data is stored in a relational database MySQL, corresponding to the data type on a blockchain distributed database, and a one-to-one index data table is created in the index database.
  • the structure of the index data table contains all the information of the data structure in the blockchain distributed database. For example: Store user points data on the blockchain. The user points data includes user ID, user name, and user points. Then create Table 1 user points user index table in the index database.
  • Table 1 includes an index identification field data_id, which corresponds to the key key for storing data on the blockchain distributed database.
  • the server must query or store data on the blockchain distributed database according to the index identification.
  • Each index table in the index database contains an index identification field.
  • the index identification is unique in the index database and corresponds to the key key in the blockchain distributed database one by one.
  • the index data and the blockchain data are correspondingly established. relationship. For example, if the index identifier data_id stored in the user score index table is USER_1, then a piece of user score data with a key of USER_1 is also stored in the blockchain distributed database.
  • the index identifier is named data_id or index_id, which does not conflict with other fields of the index table in the index database, and the specifics are not limited here.
  • target SQL statement is a legal statement that complies with SQL grammar rules
  • the server obtains the target structured query language SQL statement requested by the database, and the target SQL statement is a legal statement that conforms to the SQL syntax rules.
  • the SQL statement is consistent with the SQL statement used to access the relational database, including four SQL statements inserting insert, modifying update, querying select, and deleting delete, corresponding to different interfaces.
  • the target SQL statement is a legal statement. The legality of the SQL has been judged before the business request, and the statement that does not conform to the SQL grammar rules is eliminated.
  • the server receives a request including a target structured query language SQL statement and parameters.
  • the request is a data format that encapsulates the target SQL statement and parameters according to preset rules, so that the server can obtain the target SQL statement and parameters in a uniform manner. For example, after encapsulating the select statement and parameter param of the database SQL query of user points, as shown in Table 2 User Points Query Request Table.
  • the parameter sql is a structured query language SQL statement
  • the parameter param is a collection of other parameters of the JSON data structure.
  • the server calls the relevant interface to parse the request including the target SQL statement and parameters, obtains the parameter sql, and obtains the target SQL statement.
  • SQL structured query language
  • SQL is a database query and programming language, including SQL syntax rules, used to access data and manage relational database systems, SQL statements can be used to execute various Various operations, such as inserting data into the database, modifying data in the database, and extracting data from the database.
  • the server sets a configuration file for connecting to the database.
  • the configuration file includes two configuration files, one is the configuration file for connecting to the relational database, and the other is the configuration file for connecting to the blockchain.
  • the parameter names of the two configuration files are the same. Both include the URL of the accessed connection address and the database driver, but the parameter values are different.
  • the server processes the target SQL statement according to the preset algorithm to obtain the abstract syntax tree. Specifically, the server parses the target SQL statement according to the SQL parser, and the parsing includes lexical analysis and syntax analysis. Among them, lexical analysis refers to splitting the target SQL statement into lexical units that cannot be subdivided. In SQL grammar, lexical units include keywords, identifiers, literals, operators, and delimiters. For example, when the server reads the UPDATE of the target SQL statement, the server determines that the first character "U" meets the rules of keywords and identifiers, and the second character "P" also meets the rules of keywords and identifiers.
  • the server determines that the rule is not met, and then determines the recognition of a lexical unit.
  • UPDATE is not only a keyword defined by the SQL specification, but also satisfies the identifier rules. Therefore, when a lexical unit is an identifier, the SQL parser judges the priority to determine whether the lexical unit is a keyword.
  • Other rules include that characters starting with a number are read according to the literal value of the numerical rules; those starting with double quotation marks or single quotation marks are read according to the literal value of the string rules; operators or delimiters are read according to the symbol characteristics Recognition.
  • the grammatical analysis obtains a lexical unit from the lexical analysis each time. If the rules are met, the extraction and matching of the next lexical unit will continue until the end of the string; if the rules are not met, an error will be prompted and the analysis will end.
  • the grammar analysis finally converts the target SQL statement into an abstract syntax tree. For example, for the target SQL statement:
  • the lexical unit obtained includes the keywords SELECT, FROM and WHERE; the identifiers are user_id, user_name, point and user; the literal is 10; the operator is >; the delimiter is, and; .
  • Grammatical analysis is based on the lexical unit for regular loop processing and branch selection, and finally parsed into an abstract syntax tree including the root node.
  • the server uses the SQL Parser module of the database connection pool druid to parse the target SQL statement, and parse the natural semantic SQL statement into a more abstract data structure, that is, an abstract syntax tree (AST).
  • AST is a tree representation of the abstract syntax structure of the source code. Each node on the tree represents a structure in the source code. The AST does not represent every detail of the actual syntax. For example, Nested parentheses are implicit in the structure of the tree and are not presented as nodes.
  • the server generates a logical execution plan according to the abstract syntax tree, and the logical execution plan includes inserting the data plan, modifying the data plan, and querying the data plan.
  • the server obtains SQL elements and data according to the abstract syntax tree, and generates a logical execution plan.
  • the logical execution plan includes insert data plan, modify data plan, and query data plan, that is, it describes what data needs to be queried or what data is stored.
  • inserting data plan includes inserting insert element
  • modifying data plan includes modifying update element and query
  • the data plan includes the query select element.
  • the server uses the visitor mode to traverse the AST syntax tree to obtain each element in the target SQL statement, which includes basic SQL elements and related data.
  • the visitor pattern also known as the visitor pattern, separates structure and data, writes a visitor to visit the elements in the data structure, and then delegates all the processing of each element to the visitor class.
  • the server generates a logical execution plan through the elements in the SQL statement.
  • the logical plan describes which data needs to be queried or stored.
  • the data middleware gives the physical realization of the logical execution plan, calls the underlying block link port, and completes the actual execution.
  • Table 2 user points query request form the server parses the received SQL query select statement, and decomposes the original SQL string into a logical execution plan, that is, reads the point field value from the user points user index table, the filter condition is user
  • the name user_name is a record with Tom.
  • Logical execution plans include inserting data plans, modifying data plans, and querying data plans.
  • the server determines whether the logical execution plan is an insert data plan, a modified data plan, or a query data plan. Specifically, the server determines whether the logical execution plan includes the insert element of SQL. If the logical execution plan includes the insert element of SQL, it determines that the logical execution plan is an insert data plan; the server determines whether the logical execution plan includes the update element of SQL. If the plan includes the update element of SQL, the logical execution plan is determined to be a modified data plan; the server determines whether the logical execution plan includes the select element of SQL, and if the logical execution plan includes the select element of SQL, the logical execution plan is determined to be an insert data plan.
  • the server operates the blockchain distributed database according to the logical generation plan and the index database, and the blockchain distributed database is used to store and query data. The operation is divided into insert according to the SQL command, update and query select. After the server parses the SQL statement to generate a logical execution plan, it operates the blockchain distributed database in conjunction with the index database to complete data query and storage operations.
  • logical execution plan is an insert data plan, insert the index database and the blockchain distributed database to obtain the read and write results;
  • the server performs an insert operation on the index database and the blockchain distributed database to obtain the read and write results. Specifically, the server generates a first index identifier, where the first index identifier is dynamically generated by the server according to preset rules and is unique; the server generates a SQL insert statement according to the insert data plan and the first index identifier, and further, the server obtains the insert The insert element, index table name, multiple field names to be inserted into the data and their corresponding field values in the data plan; the server obtains the index field name corresponding to the first index identifier; the server converts the field names and first index identifiers of multiple data to be inserted The corresponding index field name is set to multiple insertion index data table fields, and the field values and first index identifiers corresponding to the multiple data to be inserted are set to multiple insertion index data in the order corresponding to the multiple insertion index data table fields.
  • the server constructs the SQL insert statement into the index database according to the SQL syntax rules for the insert element, the index table name, the data values corresponding to the insert element, the index table name, and the multiple insert index data table fields;
  • the server inserts the index database according to the SQL insert statement, and obtains the primary key value returned by the index database;
  • the server queries the index database according to the primary key value to obtain the inserted data returned by the index database;
  • the server compares the block according to the first index identifier and the inserted data
  • the chain distributed database performs an insert operation to obtain an insert result, which includes successful data insertion and data insertion failure, and the insert result is set as the read-write result. For example, an application requests to write user points.
  • the server generates the first index identifier USER_1 according to the preset rules, and its SQL insert statement is as follows:
  • the server then writes data into the user score user index table in the index database according to the SQL insert statement, and obtains the insertion result, as shown in Table 3, insert user score index table.
  • the server queries the user table in the index database to obtain the query data, uses USER_1 in the query data as the key key of the blockchain distributed database, and encapsulates other fields and data into JavaScript Object Notation (JSON) format , And set the JSON data ⁇ 'userId':1,'userName':'Tom',point:0 ⁇ as the value of the blockchain distributed database, and then call the underlying interface of the blockchain to write the key and value
  • JSON JavaScript Object Notation
  • Table 4 Blockchain distributed data table
  • logical execution plan is to modify the data plan, modify the index database and the blockchain distributed database to obtain the read and write results;
  • the server modifies the index database and the blockchain distributed database to obtain the read and write results.
  • the SQL modification statement is generated according to the modified data plan.
  • the server obtains the update element, index table name, modification condition, field names of multiple data to be modified and respective corresponding field values in the modified data plan; the server according to the SQL syntax rules Rebuild the update element, index table name, modification condition, field names of multiple data to be modified, and respective corresponding field values to obtain the SQL modification statement for modifying the index database; update the index database according to the SQL modification statement to obtain the second index identification; Query the blockchain distributed database according to the second index identifier to obtain the initial data of the blockchain; modify the initial data of the blockchain to obtain the target data of the blockchain; write the target data of the blockchain into the area according to the second index identifier
  • the block chain distributed database obtains the modification result, the modification result includes the successful modification of the data and the modification failure, and the modification result is set as the read-write result.
  • the server executes the SQL modification statement to update the user table in the index database, and the updated user table data is shown in Table 5 to update the user score index table.
  • the updated blockchain data is shown in Table 6 to update the blockchain distributed database, and Return the modified result to the terminal.
  • the data is queried from the blockchain distributed database according to the second index identification to obtain the initial data, and the initial data is updated and then written to the blockchain distributed database to ensure that the data in the blockchain distributed database is complete Sex and consistency.
  • the logical execution plan is a query data plan, perform a query operation on the index database and the blockchain distributed database to obtain read and write results.
  • the logical execution plan is a query data plan
  • the SQL query statement is generated according to the query data plan.
  • the server obtains the select element, index table name, query condition, and multiple field names to be queried in the query data plan; the server performs the query on the select element and index according to the SQL syntax rules
  • the table name, query conditions and multiple field names to be queried are reconstructed to obtain the SQL query statement for querying the index database; query the index database according to the SQL query statement to obtain the third index identification; read the blockchain according to the third index identification Distributed database, get the query result, and set the query result as the read-write result.
  • the basic idea of processing is to first parse the target SQL statement, analyze the logical execution plan, and then rewrite the SQL statement on the logical execution plan to obtain the third
  • the index identifies data_id, and reads the basic data from the blockchain distributed database according to the data_id, and finally aggregates and calculates the data to obtain the final query result.
  • the server sends the first message to the message queue to obtain the transaction identifier; the server writes the index database according to the transaction identifier to obtain the write result; the server determines whether the write result is successful; if the write result is If the write is successful, the server writes the blockchain distributed database through the message queue; if the write result is a write failure, the server deletes the index database through the message queue.
  • the server first writes the data into the index database, and then writes the data into the blockchain distributed database according to the index identification.
  • the server uses the transaction message function provided by the distributed message middleware RocketMQ to write index data and send distribution
  • the message is sent to RocketMQ in one transaction, ensuring that the writing of data and sending of the message succeed or fail at the same time.
  • the server writing to the blockchain distributed database is completed by detecting RocketMQ messages, adding the write log table to ensure idempotence, and achieving the ultimate consistency of the index database and the blockchain distributed database.
  • the use of SQL statements is compatible with the query and storage of the blockchain distributed database and the relational database, and the corresponding relationship between the index database and the blockchain distributed database is established to improve the area.
  • Retrieval efficiency of blockchain business information encapsulate the underlying interface of the blockchain, use SQL statements and index databases to achieve data storage and query on the chain, reduce development thresholds, and improve development efficiency.
  • An embodiment of the query device includes: a first generating unit 301, used to generate an index database; an obtaining unit 302, used to obtain a target structured query language SQL statement, the target SQL statement is a legal statement that conforms to SQL grammar rules; a parsing unit 303 , Used to parse the target SQL statement according to a preset algorithm to obtain an abstract syntax tree; the second generating unit 304 is used to generate a logical execution plan according to the abstract syntax tree, the logical execution plan includes inserting a data plan, modifying a data plan, and querying data Plan; The read-write unit 305 is used to perform read and write operations on the blockchain distributed database according to the logic generation plan and index database to obtain the read and write results.
  • the blockchain distributed database is used to store and query data.
  • the use of SQL statements is compatible with the query and storage of the blockchain distributed database and the relational database, and the corresponding relationship between the index database and the blockchain distributed database is established to improve the area.
  • Retrieval efficiency of blockchain business information encapsulate the underlying interface of the blockchain, use SQL statements and index databases to achieve data storage and query on the chain, reduce development thresholds, and improve development efficiency.
  • another embodiment of the device for storing and querying blockchain data in the embodiment of the present invention includes: a first generating unit 301, configured to generate an index database; and an acquiring unit 302, configured to acquire a target structured query language SQL Statement, the target SQL statement is a legal statement that conforms to the SQL grammar rules; the parsing unit 303 is used to parse the target SQL statement according to a preset algorithm to obtain an abstract syntax tree; the second generating unit 304 is used to generate logic according to the abstract syntax tree Execution plan, the logical execution plan includes inserting data plan, modifying data plan and querying data plan; the reading and writing unit 305 is used to read and write the blockchain distributed database according to the logic generation plan and index database to obtain the read and write results, The blockchain distributed database is used to store and query data.
  • the second generating unit 304 may also be specifically used to: traverse the abstract syntax tree through the visitor mode to obtain the elements of the target SQL statement; generate a logical execution plan according to the elements of the target SQL statement, and the logical execution plan includes an insert data plan , Modify the data plan and query the data plan.
  • the reading and writing unit 305 may further include: a judging subunit 3051, for judging whether the logical execution plan is an insert data plan, a modified data plan, or a query data plan; an insert subunit 3052, if the logical execution plan is an insert data plan , It is used to insert operations on the index database and the blockchain distributed database to obtain the read and write results; the modification subunit 3053, if the logical execution plan is to modify the data plan, it is used for the index database and the blockchain distributed database Perform modification operations to obtain read and write results; the query subunit 3054, if the logical execution plan is a query data plan, is used for query operations on the index database and blockchain distributed database to obtain the read and write results.
  • a judging subunit 3051 for judging whether the logical execution plan is an insert data plan, a modified data plan, or a query data plan
  • an insert subunit 3052 if the logical execution plan is an insert data plan , It is used to insert operations on the index database and the blockchain distributed database to obtain the
  • the insert subunit 3052 can also be specifically used to: generate a first index identifier; generate a SQL insert statement according to the insert data plan and the first index identifier; perform an insert operation on the index database according to the SQL insert statement, and obtain the return value of the index database Primary key value; query the index database according to the primary key value to get the inserted data returned by the index database; insert the blockchain distributed database according to the first index identifier and the inserted data to obtain the insert result, and set the insert result to read and write result.
  • the modification subunit 3053 can also be specifically used to: generate SQL modification statements according to the modified data plan; update the index database according to the SQL modification statements to obtain the second index identification; query the blockchain distributed database according to the second index identification, Obtain the initial data of the blockchain; modify the initial data of the blockchain to obtain the target data of the blockchain; write the target data of the blockchain into the distributed database of the blockchain according to the second index identification, obtain the modified result, and then modify the result Set to read and write results.
  • the query subunit 3054 can also be specifically used to: generate SQL query statements according to the query data plan; query the index database according to the SQL query statements to obtain the third index identifier; read the blockchain distributed database according to the third index identifier To get the query result, and set the query result as the read-write result.
  • the blockchain data storage and query device further includes: a sending unit 306, configured to send a first message to the message queue to obtain a transaction identifier; and a first writing unit 307, configured to write to the index database according to the transaction identifier
  • the writing result is obtained;
  • the judgment unit 308 is used to judge whether the writing result is successful;
  • the second writing unit 309 if the writing result is successful, is used to check the blockchain through the message queue
  • the distributed database performs a write operation;
  • the deleting unit 310 if the write result is a write failure, is used to delete the index database through the message queue.
  • the use of SQL statements is compatible with query and storage in the blockchain distributed database and the relational database, and the corresponding relationship between the index database and the blockchain distributed database is established to improve the area.
  • Retrieval efficiency of blockchain business information encapsulate the underlying interface of the blockchain, use SQL statements and index databases to achieve data storage and query on the chain, reduce development thresholds, and improve development efficiency.
  • the above figures 3 and 4 describe in detail the block chain data storage and query device in the embodiment of the present invention from the perspective of modular functional entities. The following describes the block chain data storage and query device in the embodiment of the present invention from the perspective of hardware processing Give a detailed description.
  • FIG. 4 is a schematic structural diagram of a blockchain data storage and query device provided by an embodiment of the present application.
  • the blockchain data storage and query device 500 may have relatively large differences due to different configurations or performance, and may include one or One or more processors (central processing units, CPU) 501 (for example, one or more processors) and memory 509, one or more storage media 508 for storing application programs 509 or data 509 (for example, one or one storage device with a large amount of ).
  • the memory 509 and the storage medium 508 may be short-term storage or persistent storage.
  • the program stored in the storage medium 508 may include one or more modules (not shown in the figure), and each module may include a series of instruction operations for storing and querying blockchain data.
  • the processor 501 may be configured to communicate with the storage medium 508, and execute a series of instruction operations in the storage medium 508 on the blockchain data storage and query device 500.
  • the instructions include: 101. Generate an index database; 102. Obtain a target structured query language SQL statement, where the target SQL statement is a legal statement that complies with the SQL grammar rules; 103. parse the target SQL statement according to a preset algorithm , Obtain an abstract syntax tree; 104, generate a logical execution plan according to the abstract syntax tree, the logical execution plan includes inserting a data plan, modifying a data plan, and querying a data plan; 105, generating a plan and the index database according to the logic Perform read and write operations on the blockchain distributed database to obtain read and write results.
  • the blockchain distributed database is used to store and query data.
  • the blockchain data storage and query device 500 may also include one or more power supplies 502, one or more wired or wireless network interfaces 503, one or more input and output interfaces 504, and/or, one or more operating systems 505 , Such as Windows Serve, Mac OS X, Unix, Linux, FreeBSD and so on.
  • operating systems 505 Such as Windows Serve, Mac OS X, Unix, Linux, FreeBSD and so on.
  • FIG. 4 does not constitute a limitation on the blockchain data storage and query device, and may include more or fewer components than shown in the figure, or Combining certain components, or different component arrangements.
  • the functional units in the various embodiments of the present application may be integrated into one processing unit, or each unit may exist alone physically, or two or more units may be integrated into one unit.
  • the above-mentioned integrated unit can be implemented in the form of hardware or software functional unit. If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it may be stored in a computer-readable storage medium, which may be a non-volatile readable storage The medium may also be a volatile readable storage medium.
  • the technical solution of this application essentially or the part that contributes to the existing technology or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium , Including several instructions to make a computer device (which can be a personal computer, a server, or a network device, etc.) execute all or part of the steps of the method described in each embodiment of the present application.
  • the steps include: 101. Generating an index database; 102. Obtaining a target structured query language SQL statement, where the target SQL statement is a legal statement complying with SQL grammar rules; 103.
  • Analyzing the target SQL statement according to a preset algorithm Obtain an abstract syntax tree; 104, generate a logical execution plan according to the abstract syntax tree, the logical execution plan includes inserting a data plan, modifying a data plan, and querying a data plan; 105, generating a plan and the index database according to the logic Perform read and write operations on the blockchain distributed database to obtain read and write results.
  • the blockchain distributed database is used to store and query data.
  • the aforementioned storage media include: U disk, mobile hard disk, read-only memory (read-only memory, ROM), random access memory (random access memory, RAM), magnetic disk or optical disk and other media that can store program code .

Abstract

The present application relates to the field of blockchains, and in particular to the field of block structure improvement. Disclosed are a blockchain data storage and query method, apparatus and device, and a storage medium. The blockchain data storage and query method comprises: generating an index database; acquiring a target structured query language (SQL) statement, with the target SQL statement being a legitimate statement conforming to an SQL syntax rule; parsing the target SQL statement according to a preset algorithm to obtain an abstract syntax tree; generating a logical execution plan according to the abstract syntax tree, with the logical execution plan comprising a data insertion plan, a data modification plan and a data query plan; and performing a read-write operation on a blockchain distributed database according to a logical generation plan and the index database to obtain a read-write result, with the blockchain distributed database being used for storing and querying data. In the present application, by encapsulating a blockchain underlying interface and using an SQL statement and an index database to realize data chaining storage and query, the development threshold is lowered, and the retrieval and development efficiency is improved.

Description

区块链数据存储和查询方法、装置、设备及存储介质Block chain data storage and query method, device, equipment and storage medium
本申请要求于2019年5月22日提交中国专利局、申请号为201910427137.8,发明名称为“区块链数据存储和查询方法、装置、设备及存储介质”的中国专利申请的优先权,其全部内容通过引用结合在本申请中。This application claims the priority of the Chinese patent application filed with the Chinese Patent Office on May 22, 2019, the application number is 201910427137.8, and the invention title is "Blockchain data storage and query methods, devices, equipment and storage media". The content is incorporated in this application by reference.
技术领域Technical field
本申请涉及区块结构改进领域,尤其涉及区块链数据存储和查询方法、装置、设备及存储介质。This application relates to the field of block structure improvement, in particular to blockchain data storage and query methods, devices, equipment and storage media.
背景技术Background technique
区块链(block chain,BC)是用分布式数据库识别、传播和记载信息的智能化对等网络,也称为价值互联网,区块链在本质上是一个去中心化的数据库,区块链包含一张被称为区块的列表,有着持续增长并且排列整齐的记录。每个区块都包含一个时间戳和一个与前一区块的链接,区块链使得数据不可篡改,一旦记录下来,在一个区块中的数据将不可逆。Blockchain (BC) is an intelligent peer-to-peer network that uses distributed databases to identify, disseminate and record information. It is also called the Internet of Value. Blockchain is essentially a decentralized database. Blockchain Contains a list called blocks, which has a continuously growing and neatly arranged record. Each block contains a timestamp and a link to the previous block. The block chain makes the data immutable. Once recorded, the data in a block will be irreversible.
区块链分布式存储数据是键值(key-value)型的,虽然区块链技术的底层也提供了进行数据操作的接口,但这些接口都比较简单,存储在块内的交易信息基本以字符串的形式存在,需要应用程序频繁解析字符串的数据信息。仅支持按照key查询链上存储的数据,不支持复杂查询,使用起来不方便。Blockchain distributed storage data is of key-value type. Although the bottom layer of blockchain technology also provides interfaces for data operations, these interfaces are relatively simple. The transaction information stored in the block is basically The existence of the string form requires the application to frequently parse the data information of the string. It only supports querying the data stored on the chain by key, and does not support complex queries, which is inconvenient to use.
在实际项目开发过程中,发明人意识到,为了将原来存储在关系型数据中的数据,修改为区块链存储,需要重新编写数据存储和查询部分的逻辑代码,同时还需要熟悉区块链数据操作接口,开发工作量大,耗费时间长,开发效率低。In the actual project development process, the inventor realized that in order to modify the data originally stored in the relational data into the blockchain storage, it is necessary to rewrite the logic code of the data storage and query part, and also need to be familiar with the blockchain Data operation interface requires a large amount of development work, time-consuming, and low development efficiency.
发明内容Summary of the invention
本申请的主要目的在于解决区块链对复杂查询支持不足,检索效率低的问题,同时解决了将数据存储由关系型数据库修改为区块链分布式数据库,需要重写数据查询和存储逻辑代码,导致开发工作量大、耗费时间长和开发效率低的问题。The main purpose of this application is to solve the problem of insufficient support of the blockchain for complex queries and low retrieval efficiency. At the same time, it solves the need to rewrite the data query and storage logic code when the data storage is changed from a relational database to a blockchain distributed database. , Leading to the problems of large development workload, long time-consuming and low development efficiency.
为实现上述目的,本申请第一方面提供了一种区块链数据存储和查询方法,包括:生成索引数据库;获取目标结构化查询语言SQL语句,所述目标SQL语句为符合SQL语法规则的合法语句;根据预置算法对所述目标SQL语句进行解析,得到抽象语法树;根据所述抽象语法树生成逻辑执行计划,所述逻辑执行计划包括插入数据计划、修改数据计划和查询数据计划;根据所述逻辑生成计划和所述索引数据库对区块链分布式数据库进行读写操作,得到读写结果,所述区块链分布式数据库用于存储和查询数据。In order to achieve the above purpose, the first aspect of this application provides a blockchain data storage and query method, including: generating an index database; obtaining a target structured query language SQL statement, the target SQL statement being legal in compliance with SQL syntax rules Statement; parse the target SQL statement according to a preset algorithm to obtain an abstract syntax tree; generate a logical execution plan according to the abstract syntax tree, and the logical execution plan includes inserting a data plan, modifying a data plan, and querying a data plan; according to The logic generation plan and the index database perform read and write operations on the blockchain distributed database to obtain read and write results. The blockchain distributed database is used to store and query data.
本申请第二方面提供了一种区块链数据存储和查询装置,包括:第一生成单元,用于生成索引数据库;获取单元,用于获取目标结构化查询语言SQL 语句,所述目标SQL语句为符合SQL语法规则的合法语句;解析单元,用于根据预置算法对所述目标SQL语句进行解析,得到抽象语法树;第二生成单元,用于根据所述抽象语法树生成逻辑执行计划,所述逻辑执行计划包括插入数据计划、修改数据计划和查询数据计划;读写单元,用于根据所述逻辑生成计划和所述索引数据库对区块链分布式数据库进行读写操作,得到读写结果,所述区块链分布式数据库用于存储和查询数据。The second aspect of the present application provides a block chain data storage and query device, including: a first generating unit for generating an index database; an obtaining unit for obtaining a target structured query language SQL statement, the target SQL statement It is a legal statement that conforms to the SQL grammar rules; the parsing unit is used to parse the target SQL statement according to a preset algorithm to obtain an abstract syntax tree; the second generation unit is used to generate a logical execution plan according to the abstract syntax tree, The logic execution plan includes inserting data plans, modifying data plans, and querying data plans; a reading and writing unit is used to perform read and write operations on the blockchain distributed database according to the logic generation plan and the index database to obtain read and write operations As a result, the blockchain distributed database is used to store and query data.
本申请第三方面提供了一种区块链数据存储和查询方法设备,包括:存储器和至少一个处理器,所述存储器中存储有指令,所述存储器和所述至少一个处理器通过线路互联;所述至少一个处理器调用所述存储器中的所述指令,以使得所述区块链数据存储和查询设备执行上述各方面所述的方法。A third aspect of the present application provides a blockchain data storage and query method and equipment, including: a memory and at least one processor, the memory stores instructions, and the memory and the at least one processor are interconnected by wires; The at least one processor invokes the instructions in the memory, so that the blockchain data storage and query device executes the methods described in the foregoing aspects.
本申请的第四方面提供了一种计算机可读存储介质,所述计算机可读存储介质中存储有指令,当其在计算机上运行时,使得计算机执行上述各方面所述的方法。The fourth aspect of the present application provides a computer-readable storage medium that stores instructions in the computer-readable storage medium, which when run on a computer, causes the computer to execute the methods described in the above aspects.
本申请的第五方面提供了一种包含指令的计算机程序产品,当其在计算机上运行时,使得计算机执行上述各方面所述的方法。The fifth aspect of the present application provides a computer program product containing instructions, which when run on a computer, causes the computer to execute the methods described in the foregoing aspects.
本发明实施例中,通过对SQL语句的处理,使用SQL语句兼容在区块链分布式数据库和关系型数据库的查询和存储,通过建立索引数据库和区块链分布式数据库的对应关系,提高区块链业务信息的检索效率,对区块链底层接口封装,使用SQL语句和索引数据库实现数据上链存储和查询。In the embodiment of the present invention, through the processing of SQL statements, the use of SQL statements is compatible with query and storage in the blockchain distributed database and the relational database, and the corresponding relationship between the index database and the blockchain distributed database is established to improve the area. Retrieval efficiency of blockchain business information, encapsulate the underlying interface of blockchain, use SQL statements and index databases to achieve data storage and query on the chain.
附图说明Description of the drawings
图1为本申请实施例中区块链数据存储和查询方法的一个实施例示意图;Figure 1 is a schematic diagram of an embodiment of a blockchain data storage and query method in an embodiment of the application;
图2为本申请实施例中区块链数据存储和查询方法的另一个实施例示意图;FIG. 2 is a schematic diagram of another embodiment of a blockchain data storage and query method in an embodiment of the application;
图3为本申请实施例中区块链数据存储和查询装置的一个实施例示意图;Fig. 3 is a schematic diagram of an embodiment of a block chain data storage and query device in an embodiment of the application;
图4为本发明实施例中区块链数据存储和查询装置的另一个实施例示意图;Figure 4 is a schematic diagram of another embodiment of a blockchain data storage and query device in an embodiment of the present invention;
图5为本申请实施例中区块链数据存储和查询设备的另一个实施例示意图。Fig. 5 is a schematic diagram of another embodiment of a blockchain data storage and query device in an embodiment of the application.
具体实施方式Detailed ways
本申请实施例提供了一种区块链数据存储和查询方法、装置、设备及存储介质,本申请实施例中,通过对SQL语句的处理,使用SQL语句兼容在区块链分布式数据库和关系型数据库的查询和存储,通过建立索引数据库和区块链分布式数据库的对应关系,提高区块链业务信息的检索效率,对区块链底层接口封装,使用SQL语句和索引数据库实现数据上链存储和查询,降低开发门槛,提高开发效率。The embodiment of the application provides a blockchain data storage and query method, device, device, and storage medium. In the embodiment of the application, through the processing of SQL statements, the use of SQL statements is compatible with blockchain distributed databases and relationships. Query and storage of type database, improve the retrieval efficiency of blockchain business information by establishing the corresponding relationship between the index database and the blockchain distributed database, encapsulate the underlying interface of the blockchain, and use SQL statements and index databases to achieve data on the chain Storage and query, lower development threshold and improve development efficiency.
为便于理解,下面对本申请实施例的具体流程进行描述,请参阅图1,本申请实施例中区块链数据存储和查询方法的一个实施例包括:For ease of understanding, the following describes the specific process of the embodiment of the present application. Please refer to Figure 1. An embodiment of the blockchain data storage and query method in the embodiment of the present application includes:
101、生成索引数据库;101. Generate an index database;
服务器根据业务需求,生成索引数据库。该索引数据库是位于区块链的数据中间件中,该数据中间件还提供了区块链底层区块链访问的统一接口,该区块链底层区块链访问的统一接口包括对数据的创建,查询,修改和删除(create read update delete,CURD)。索引数据使用关系型数据库MySQL存储,对应一个区块链分布式数据库上的数据类型,在索引数据库中创建一对一的索引数据表。索引数据表的结构包含了区块链分布式数据库中数据结构的全部信息。例如:在区块链上存储用户积分数据,该用户积分数据包括用户ID、用户姓名和用户积分,那么在索引库中创建表1用户积分user索引表。The server generates an index database according to business requirements. The index database is located in the data middleware of the blockchain. The data middleware also provides a unified interface for accessing the underlying blockchain of the blockchain. The unified interface for accessing the underlying blockchain of the blockchain includes the creation of data , Query, modify and delete (create read update delete, CURD). Index data is stored in a relational database MySQL, corresponding to the data type on a blockchain distributed database, and a one-to-one index data table is created in the index database. The structure of the index data table contains all the information of the data structure in the blockchain distributed database. For example: Store user points data on the blockchain. The user points data includes user ID, user name, and user points. Then create Table 1 user points user index table in the index database.
表1:用户积分user索引表Table 1: User score user index table
字段名称Field Name 字段类型Field Type 备注Remarks
user_iduser_id bigintbigint 用户ID,主键User ID, primary key
user_nameuser_name varcharvarchar 用户姓名username
pointpoint intint 用户积分account credits
data_iddata_id varcharvarchar 索引标识,对应区块链数据的keyIndex identification, corresponding to the key of the blockchain data
表1中包括一个索引标识字段data_id,对应于区块链分布式数据库上存储数据的键key,服务器必须根据索引标识才能查询或者存储区块链分布式数据库上的数据。索引数据库中的每张索引表都包含了data_id字段,该data_id在索引数据库中是唯一的,与区块链分布式数据库中的键key一一相对应,建立索引数据和区块链数据对应关系。Table 1 includes an index identification field data_id, which corresponds to the key key for storing data on the blockchain distributed database. The server must query or store data on the blockchain distributed database according to the index identification. Each index table in the index database contains a data_id field. The data_id is unique in the index database and corresponds to the key in the blockchain distributed database one by one to establish the corresponding relationship between the index data and the blockchain data .
102、获取目标结构化查询语言SQL语句,目标SQL语句为符合SQL语法规则的合法语句;102. Obtain a target structured query language SQL statement, where the target SQL statement is a legal statement that conforms to the SQL syntax rules;
服务器获取业务请求的目标结构化查询语言SQL语句,目标SQL语句为符合SQL语法规则的合法语句。该SQL语句与访问关系型数据库的SQL语句一致,包括插入insert、修改update、查询select和删除delete四种SQL语句,对应于不同的接口。具体的,服务器接收初始SQL语句和参数,封装初始SQL语句和参数,服务器调用相关接口获取已封装的初始SQL语句和参数,得到目标SQL语句。该目标SQL语句为合法语句,业务请求之前已经对SQL的合法性进行了判断,对于不符合SQL语法规则的语句进行剔除。The server obtains the target structured query language SQL statement of the business request, and the target SQL statement is a legal statement that conforms to the SQL syntax rules. The SQL statement is consistent with the SQL statement used to access the relational database, including four SQL statements inserting insert, modifying update, querying select, and deleting delete, corresponding to different interfaces. Specifically, the server receives the initial SQL statement and parameters, encapsulates the initial SQL statement and parameters, and the server calls the relevant interface to obtain the encapsulated initial SQL statement and parameters to obtain the target SQL statement. The target SQL statement is a legal statement. The legality of the SQL has been judged before the business request, and the statement that does not conform to the SQL grammar rules is eliminated.
需要说明的是,结构化查询语言(structured query language,SQL)是一种数据库查询和程序设计语言,包括SQL语法规则,用于存取数据和管理关系数据库系统,SQL语句可以用来执行各种各样的操作,例如,将数据插入数据库中,修改数据库中的数据和从数据库中提取数据等。It should be noted that structured query language (SQL) is a database query and programming language, including SQL syntax rules, used to access data and manage relational database systems, SQL statements can be used to execute various Various operations, such as inserting data into the database, modifying data in the database, and extracting data from the database.
103、根据预置算法对目标SQL语句进行解析,得到抽象语法树;103. Analyze the target SQL statement according to the preset algorithm to obtain an abstract syntax tree;
服务器根据预置算法对目标SQL语句进行处理,得到抽象语法树。具体的,服务器根据SQL解析器对目标SQL语句进行解析,该解析包括词法解析和语法解析。其中,词法解析是指将目标SQL语句拆分为一个个不可再分的词法单元。在SQL语法中,词法单元包括关键字、标识符、字面量、运算符 和分界符。例如,服务器读取目标SQL语句的UPDATE时,服务器确定第一个字符“U”满足关键字和标示符的规则,确定第二个字符“P”也同样满足关键字和标示符的规则,以此类推,直到第7个字符是空格时,服务器判断不满足该规则,那么服务器就完成了一个词法单元的识别。UPDATE既是SQL规范定义的关键字,又同时满足标识符规则,因此当一个词法单元是标识符时,SQL解析器进行优先级的判断,确定一个词法单元是否为关键字,关键字的优先级高于标识符规则。其他的规则包括以数字开头的字符则根据数值规则的字面量读取字符;以双引号或单引号开头的则根据字符串规则的字面量读取字符;运算符或分界符则根据符号特征进行识别。The server processes the target SQL statement according to the preset algorithm to obtain the abstract syntax tree. Specifically, the server parses the target SQL statement according to the SQL parser, and the parsing includes lexical analysis and syntax analysis. Among them, lexical analysis refers to splitting the target SQL statement into lexical units that cannot be subdivided. In SQL grammar, lexical units include keywords, identifiers, literals, operators, and delimiters. For example, when the server reads the UPDATE of the target SQL statement, the server determines that the first character "U" meets the rules of keywords and identifiers, and the second character "P" also meets the rules of keywords and identifiers. By analogy, until the seventh character is a space, the server judges that the rule is not met, and the server has completed the recognition of a lexical unit. UPDATE is not only a keyword defined by the SQL specification, but also satisfies the rules of identifiers. Therefore, when a lexical unit is an identifier, the SQL parser judges the priority to determine whether a lexical unit is a keyword, and the priority of the keyword is high In the identifier rules. Other rules include that characters starting with a number are read according to the literal value of the numerical rules; those starting with double quotation marks or single quotation marks are read according to the literal value of the string rules; operators or delimiters are read according to the symbol characteristics Recognition.
语法解析每次从词法解析中获取一个词法单元。如果满足规则,则继续下一个词法单元的提取和匹配,直至字符串结束;若不满足规则,便提示错误并结束本次解析。语法解析最终将目标SQL语句转换为抽象语法树。The grammatical analysis obtains a lexical unit from the lexical analysis each time. If the rules are met, the extraction and matching of the next lexical unit will continue until the end of the string; if the rules are not met, an error will be prompted and the analysis will end. The grammar analysis finally converts the target SQL statement into an abstract syntax tree.
可选的,服务器使用数据库连接池druid的SQL Parser模块解析目标SQL语句,将自然语义的SQL语句解析成较为抽象地数据结构,即抽象语法树(abstract syntax tree,AST)。需要说明的是,AST是源代码的抽象语法结构的树状表示,树上的每个节点都表示源代码中的一种结构,AST并不表示出真实语法出现的每一个细节,比如说,嵌套括号被隐含在树的结构中,并没有以节点的形式呈现。Optionally, the server uses the SQL Parser module of the database connection pool druid to parse the target SQL statement, and parse the natural semantic SQL statement into a more abstract data structure, that is, an abstract syntax tree (AST). It should be noted that the AST is a tree representation of the abstract syntax structure of the source code. Each node on the tree represents a structure in the source code. The AST does not represent every detail of the actual syntax. For example, Nested parentheses are implicit in the structure of the tree and are not presented as nodes.
104、根据抽象语法树逻辑执行计划,逻辑执行计划包括插入数据计划、修改数据计划和查询数据计划;104. According to the abstract syntax tree logical execution plan, the logical execution plan includes inserting the data plan, modifying the data plan, and querying the data plan;
服务器根据抽象语法树逻辑执行计划,逻辑执行计划包括插入数据计划、修改数据计划和查询数据计划。具体的,服务器根据抽象语法树获取SQL元素以及数据,生成逻辑执行计划。该逻辑执行计划描述了需要查询哪些数据,或者存储哪些数据,该逻辑执行计划包括插入数据计划、修改数据计划和查询数据计划。例如:用户积分的查询请求,服务器解析接收的SQL查询select语句,将原始SQL字符串分解为逻辑执行计划,即从用户积分user索引表中读取积分point字段的值,过滤条件是用户名称user_name值为Tom的记录。The server logically executes the plan according to the abstract syntax tree. The logical execution plan includes inserting the data plan, modifying the data plan, and querying the data plan. Specifically, the server obtains SQL elements and data according to the abstract syntax tree, and generates a logical execution plan. The logical execution plan describes what data needs to be queried or stored, and the logical execution plan includes insert data plan, modify data plan, and query data plan. For example: a query request for user points, the server parses the received SQL query select statement, decomposes the original SQL string into a logical execution plan, that is, reads the value of the point field from the user index table, and the filter condition is the user name user_name The value is Tom's record.
105、根据逻辑生成计划和索引数据库对区块链分布式数据库进行读写操作,得到读写结果,区块链分布式数据库用于存储和查询数据;105. Perform read and write operations on the blockchain distributed database according to the logical generation plan and the index database to obtain the read and write results. The blockchain distributed database is used to store and query data;
服务器根据逻辑生成计划和索引数据库对区块链分布式数据库进行读写操作,得到读写结果,该区块链分布式数据库用于存储和查询数据。将该读写操作根据SQL命令插入insert,修改update和查询select进行划分,服务器在将SQL语句解析生成逻辑执行计划后,结合索引数据库操作区块链分布式数据库,完成数据的查询和存储操作。The server reads and writes the blockchain distributed database according to the logic generation plan and the index database to obtain the read and write results. The blockchain distributed database is used to store and query data. The read and write operations are inserted according to SQL commands into insert, modified update and query select. After the server parses the SQL statement to generate a logical execution plan, it operates the blockchain distributed database in conjunction with the index database to complete data query and storage operations.
当检测到逻辑执行计划为插入数据计划时,服务器对索引数据库和区块链分布式数据库进行插入操作,得到读写结果。具体的,服务器生成第一索引标识;服务器根据插入数据计划和第一索引标识对索引数据库进行插入操作,得到索引数据库返回的主键值;服务器根据主键值查询索引数据库,得到索引数据库返回的插入数据;服务器根据第一索引标识和插入数据对区块 链分布式数据库进行插入操作。例如,对于插入用户积分数据请求,服务器首先设置用户积分索引表的第一索引标识data_id为字符串USER_1,然后服务器根据用户积分的插入数据计划和第一索引标识将数据写入到索引数据库的用户积分索引表中,得到主键user_id的主键值1,根据主键值1查询用户积分索引表,得到查询数据,将查询数据中的USER_1作为区块链分布式数据库的键key,将其他字段和数据封装为JS对象简谱(JavaScript Object Notation,JSON)格式,并将该JSON数据设置为区块链分布式数据库的值value,再调用区块链底层接口,将key和value写入区块链分布式数据库中。When detecting that the logical execution plan is an insert data plan, the server performs an insert operation on the index database and the blockchain distributed database to obtain the read and write results. Specifically, the server generates the first index identifier; the server performs an insert operation on the index database according to the inserted data plan and the first index identifier to obtain the primary key value returned by the index database; the server queries the index database according to the primary key value, and obtains the returned value from the index database Insert data; the server performs an insert operation on the blockchain distributed database according to the first index identifier and the inserted data. For example, for a request to insert user points data, the server first sets the first index identifier data_id of the user points index table to the character string USER_1, and then the server writes the data to the user of the index database according to the user points insertion data plan and the first index identifier In the score index table, get the primary key value 1 of the primary key user_id, query the user score index table according to the primary key value 1, and get the query data. Use USER_1 in the query data as the key key of the blockchain distributed database, and combine other fields with The data is encapsulated in JavaScript Object Notation (JSON) format, and the JSON data is set as the value of the blockchain distributed database, and then the underlying interface of the blockchain is called to write the key and value to the blockchain distribution Style database.
当检测到逻辑执行计划为修改数据计划时,服务器对索引数据库和区块链分布式数据库进行修改操作,得到读写结果。根据第二索引标识从区块链分布式数据库中查询数据,得到初始数据,并对初始数据更新再写入区块链分布式数据库,确保区块链分布式数据库的数据完整性和一致性。例如,对于修改用户积分索引表user的用户积分point字段的请求,服务器根据该请求获取对应的逻辑执行计划,得到update元素,则服务器首先更新索引数据库,并获取更新数据的第二索引标识data_id的值,将该第二索引标识作为键key,查询区块链分布式数据库,得到初始value,该初始value为JSON格式数据,对该初始value修改用户积分point的值,得到目标value,目标value也是JSON格式数据,最后服务器调用区块链底层接口将key和目标value写入区块链分布式数据库中。When detecting that the logical execution plan is a modified data plan, the server modifies the index database and the blockchain distributed database to obtain the read and write results. According to the second index identification, the data is queried from the blockchain distributed database to obtain the initial data, and the initial data is updated and then written into the blockchain distributed database to ensure the data integrity and consistency of the blockchain distributed database. For example, for a request to modify the user score point field of the user score index table user, the server obtains the corresponding logical execution plan according to the request, and obtains the update element, then the server first updates the index database and obtains the second index identification data_id of the updated data Value, use the second index identifier as the key key, query the blockchain distributed database to obtain the initial value, which is JSON format data, modify the user’s point value for the initial value to obtain the target value, which is also JSON format data, and finally the server calls the underlying interface of the blockchain to write the key and target value into the blockchain distributed database.
当检测到逻辑执行计划为查询数据计划时,服务器对索引数据库和区块链分布式数据库进行查询操作,得到读写结果。服务器从区块链分布式数据库中得到查询结果,确保数据的准确性。例如,对于查询用户积分的请求,服务器根据查询语句查询用户积分数据索引表,得到第三索引标识data_id字段的值USER_1,服务器将USER_1作为key查询区块链分布式数据库,得到查询结果。When it is detected that the logical execution plan is a query data plan, the server performs a query operation on the index database and the blockchain distributed database to obtain the read and write results. The server obtains the query results from the blockchain distributed database to ensure the accuracy of the data. For example, for a request to query user points, the server queries the user points data index table according to the query statement, and obtains the value USER_1 of the third index identifier data_id field. The server uses USER_1 as the key to query the blockchain distributed database to obtain the query result.
本申请实施例中,通过对SQL语句的处理,使用SQL语句兼容在区块链分布式数据库和关系型数据库的查询和存储,通过建立索引数据库和区块链分布式数据库的对应关系,提高区块链业务信息的检索效率,对区块链底层接口封装,使用SQL语句和索引数据库实现数据上链存储和查询,降低开发门槛,提高开发效率。In the embodiment of the application, through the processing of SQL statements, the use of SQL statements is compatible with the query and storage of the blockchain distributed database and the relational database, and the corresponding relationship between the index database and the blockchain distributed database is established to improve the area. Retrieval efficiency of blockchain business information, encapsulate the underlying interface of the blockchain, use SQL statements and index databases to achieve data storage and query on the chain, reduce development thresholds, and improve development efficiency.
请参阅图2,本申请实施例中区块链数据存储和查询方法的另一个实施例包括:Referring to Fig. 2, another embodiment of the blockchain data storage and query method in the embodiment of the present application includes:
201、生成索引数据库;201. Generate an index database;
服务器根据业务需求,生成索引数据库。该索引数据库是位于区块链的数据中间件中,该数据中间件还提供了区块链底层区块链访问的统一接口,该区块链底层区块链访问的统一接口包括对数据的创建,查询,修改和删除(create read update delete CURD)。索引数据使用关系型数据库MySQL存储,对应一个区块链分布式数据库上的数据类型,在索引数据库中创建一对一的索引数据表。索引数据表的结构包含了区块链分布式数据库中数据结构 的全部信息。例如:在区块链上存储用户积分数据,该用户积分数据包括用户ID、用户姓名和用户积分,那么在索引库中创建表1用户积分user索引表。The server generates an index database according to business requirements. The index database is located in the data middleware of the blockchain. The data middleware also provides a unified interface for accessing the underlying blockchain of the blockchain. The unified interface for accessing the underlying blockchain of the blockchain includes the creation of data , Query, modify and delete (create read update delete CURD). Index data is stored in a relational database MySQL, corresponding to the data type on a blockchain distributed database, and a one-to-one index data table is created in the index database. The structure of the index data table contains all the information of the data structure in the blockchain distributed database. For example: Store user points data on the blockchain. The user points data includes user ID, user name, and user points. Then create Table 1 user points user index table in the index database.
表1中包括一个索引标识字段data_id,对应于区块链分布式数据库上存储数据的键key,服务器必须根据索引标识才能查询或者存储区块链分布式数据库上的数据。索引数据库中的每张索引表都包含了索引标识字段,索引标识在索引数据库中是唯一的,与区块链分布式数据库中的键key一一相对应,建立索引数据和区块链数据对应关系。例如,用户积分索引表中存储的索引标识data_id为USER_1,那么区块链分布式数据库中也存储了一条键key为USER_1的用户积分数据。需要说明的是,索引标识命名为data_id,也可以为index_id,与索引数据库中的索引表的其他字段不冲突即可,具体此处不做限定。Table 1 includes an index identification field data_id, which corresponds to the key key for storing data on the blockchain distributed database. The server must query or store data on the blockchain distributed database according to the index identification. Each index table in the index database contains an index identification field. The index identification is unique in the index database and corresponds to the key key in the blockchain distributed database one by one. The index data and the blockchain data are correspondingly established. relationship. For example, if the index identifier data_id stored in the user score index table is USER_1, then a piece of user score data with a key of USER_1 is also stored in the blockchain distributed database. It should be noted that the index identifier is named data_id or index_id, which does not conflict with other fields of the index table in the index database, and the specifics are not limited here.
202、获取目标结构化查询语言SQL语句,目标SQL语句为符合SQL语法规则的合法语句;202. Obtain a target structured query language SQL statement, where the target SQL statement is a legal statement that complies with SQL grammar rules;
服务器获取数据库请求的目标结构化查询语言SQL语句,目标SQL语句为符合SQL语法规则的合法语句。该SQL语句与访问关系型数据库的SQL语句一致,包括插入insert、修改update、查询select和删除delete四种SQL语句,对应于不同的接口。该目标SQL语句为合法语句,业务请求之前已经对SQL的合法性进行了判断,对于不符合SQL语法规则的语句进行剔除。具体的,服务器接收包括目标结构化查询语言SQL语句和参数的请求,该请求为根据预设规则对目标SQL语句和参数进行封装的数据格式,以便于服务器使用统一方式获取目标SQL语句和参数。例如,将用户积分的数据库SQL查询select语句和参数param封装后如表2用户积分查询请求表所示。The server obtains the target structured query language SQL statement requested by the database, and the target SQL statement is a legal statement that conforms to the SQL syntax rules. The SQL statement is consistent with the SQL statement used to access the relational database, including four SQL statements inserting insert, modifying update, querying select, and deleting delete, corresponding to different interfaces. The target SQL statement is a legal statement. The legality of the SQL has been judged before the business request, and the statement that does not conform to the SQL grammar rules is eliminated. Specifically, the server receives a request including a target structured query language SQL statement and parameters. The request is a data format that encapsulates the target SQL statement and parameters according to preset rules, so that the server can obtain the target SQL statement and parameters in a uniform manner. For example, after encapsulating the select statement and parameter param of the database SQL query of user points, as shown in Table 2 User Points Query Request Table.
表2:用户积分查询请求表Table 2: User points query request form
参数名称parameter name 参数类型Parameter Type value
sqlsql 字符串String select point from user where user_name=?select point from user where user_name=?
paramparam 字符串String {0:{sqlType:12,value:’Tom’}}{0:{sqlType:12,value:’Tom’}}
参数sql为结构化查询语言SQL语句,参数param是JSON数据结构的其他参数集合。服务器调用相关接口解析包括目标SQL语句和参数的请求,获取参数sql,得到目标SQL语句。The parameter sql is a structured query language SQL statement, and the parameter param is a collection of other parameters of the JSON data structure. The server calls the relevant interface to parse the request including the target SQL statement and parameters, obtains the parameter sql, and obtains the target SQL statement.
需要说明的是,结构化查询语言(structured query language,SQL)是一种数据库查询和程序设计语言,包括SQL语法规则,用于存取数据和管理关系数据库系统,SQL语句可以用来执行各种各样的操作,例如,将数据插入数据库中,修改数据库中的数据和从数据库中提取数据等。It should be noted that structured query language (SQL) is a database query and programming language, including SQL syntax rules, used to access data and manage relational database systems, SQL statements can be used to execute various Various operations, such as inserting data into the database, modifying data in the database, and extracting data from the database.
可选的,服务器设置连接数据库的配置文件,该配置文件包括两个,一个为连接关系型数据库的配置文件,另一个为连接区块链的配置文件,两个配置文件的参数名称保持一致,都包括访问的连接地址url,数据库驱动driver,只是参数值不同。服务器通过设置不同的配置文件,则在不改变现有业务逻辑代码的情况下,实现在关系型数据库和区块链分布式数据库中的快速切换和使用相同的SQL语句进行存储和查询等操作。例如,通过设置不同的配置 文件,用户积分服务就可以在MySQL数据库中作集中式存储和查询,也可以在区块链分布式数据库中作分布式存储和查询。Optionally, the server sets a configuration file for connecting to the database. The configuration file includes two configuration files, one is the configuration file for connecting to the relational database, and the other is the configuration file for connecting to the blockchain. The parameter names of the two configuration files are the same. Both include the URL of the accessed connection address and the database driver, but the parameter values are different. By setting different configuration files, the server realizes fast switching between relational databases and blockchain distributed databases, and uses the same SQL statement for storage and query operations without changing the existing business logic code. For example, by setting different configuration files, the user points service can be stored and queried centrally in the MySQL database, or distributed storage and query in the distributed blockchain database.
203、根据预置算法对目标SQL语句进行解析,得到抽象语法树;203. parse the target SQL statement according to the preset algorithm to obtain an abstract syntax tree;
服务器根据预置算法对目标SQL语句进行处理,得到抽象语法树。具体的,服务器根据SQL解析器对目标SQL语句进行解析,该解析包括词法解析和语法解析。其中,词法解析是指将目标SQL语句拆分为一个个不可再分的词法单元。在SQL语法中,词法单元包括关键字、标识符、字面量、运算符和分界符。例如,服务器读取目标SQL语句的UPDATE时,服务器确定第一个字符“U”满足关键字和标示符的规则,确定第二个字符“P”也同样满足关键字和标示符的规则,以此类推,直到第7个字符是空格时,服务器判断不满足该规则,则确定一个词法单元的识别。需要说明的是,UPDATE既是SQL规范定义的关键字,又同时满足标识符规则,因此当一个词法单元是标识符时,SQL解析器进行优先级的判断,确定词法单元是否为关键字。其他的规则包括以数字开头的字符则根据数值规则的字面量读取字符;以双引号或单引号开头的则根据字符串规则的字面量读取字符;运算符或分界符则根据符号特征进行识别。The server processes the target SQL statement according to the preset algorithm to obtain the abstract syntax tree. Specifically, the server parses the target SQL statement according to the SQL parser, and the parsing includes lexical analysis and syntax analysis. Among them, lexical analysis refers to splitting the target SQL statement into lexical units that cannot be subdivided. In SQL grammar, lexical units include keywords, identifiers, literals, operators, and delimiters. For example, when the server reads the UPDATE of the target SQL statement, the server determines that the first character "U" meets the rules of keywords and identifiers, and the second character "P" also meets the rules of keywords and identifiers. By analogy, until the seventh character is a space, the server determines that the rule is not met, and then determines the recognition of a lexical unit. It should be noted that UPDATE is not only a keyword defined by the SQL specification, but also satisfies the identifier rules. Therefore, when a lexical unit is an identifier, the SQL parser judges the priority to determine whether the lexical unit is a keyword. Other rules include that characters starting with a number are read according to the literal value of the numerical rules; those starting with double quotation marks or single quotation marks are read according to the literal value of the string rules; operators or delimiters are read according to the symbol characteristics Recognition.
语法解析每次从词法解析中获取一个词法单元。如果满足规则,则继续下一个词法单元的提取和匹配,直至字符串结束;若不满足规则,便提示错误并结束本次解析。语法解析最终将目标SQL语句转换为抽象语法树。举例说明,对于目标SQL语句:The grammatical analysis obtains a lexical unit from the lexical analysis each time. If the rules are met, the extraction and matching of the next lexical unit will continue until the end of the string; if the rules are not met, an error will be prompted and the analysis will end. The grammar analysis finally converts the target SQL statement into an abstract syntax tree. For example, for the target SQL statement:
SELECT user_id,user_name,point FROM user WHERE user_id>10;SELECT user_id,user_name,point FROM user WHERE user_id>10;
服务器使用SQL解析器进行词法分析之后,得到词法单元包括关键字为SELECT、FROM和WHERE;标识符为user_id、user_name、point和user;字面量为10;运算符为>;分界符为,和;。语法分析则是在词法单元基础上进行规则的循环处理和分支选择,最后解析为一个包括根节点的抽象语法树。After the server uses the SQL parser for lexical analysis, the lexical unit obtained includes the keywords SELECT, FROM and WHERE; the identifiers are user_id, user_name, point and user; the literal is 10; the operator is >; the delimiter is, and; . Grammatical analysis is based on the lexical unit for regular loop processing and branch selection, and finally parsed into an abstract syntax tree including the root node.
可选的,服务器使用数据库连接池druid的SQL Parser模块解析目标SQL语句,将自然语义的SQL语句解析成较为抽象地数据结构,即抽象语法树(abstract syntax tree,AST)。需要说明的是,AST是源代码的抽象语法结构的树状表示,树上的每个节点都表示源代码中的一种结构,AST并不表示出真实语法出现的每一个细节,比如说,嵌套括号被隐含在树的结构中,并没有以节点的形式呈现。Optionally, the server uses the SQL Parser module of the database connection pool druid to parse the target SQL statement, and parse the natural semantic SQL statement into a more abstract data structure, that is, an abstract syntax tree (AST). It should be noted that the AST is a tree representation of the abstract syntax structure of the source code. Each node on the tree represents a structure in the source code. The AST does not represent every detail of the actual syntax. For example, Nested parentheses are implicit in the structure of the tree and are not presented as nodes.
204、根据抽象语法树生成逻辑执行计划,该逻辑执行计划包括插入数据计划、修改数据计划和查询数据计划;204. Generate a logical execution plan according to the abstract syntax tree, where the logical execution plan includes inserting a data plan, modifying a data plan, and querying a data plan;
服务器根据抽象语法树生成逻辑执行计划,该逻辑执行计划包括插入数据计划、修改数据计划和查询数据计划。具体的,服务器根据抽象语法树获取SQL元素以及数据,生成逻辑执行计划。该逻辑执行计划包括插入数据计划、修改数据计划和查询数据计划,也就是描述了需要查询哪些数据,或者存储哪些数据,其中,插入数据计划包括插入insert元素,修改数据计划包括修改update元素和查询数据计划包括查询select元素。The server generates a logical execution plan according to the abstract syntax tree, and the logical execution plan includes inserting the data plan, modifying the data plan, and querying the data plan. Specifically, the server obtains SQL elements and data according to the abstract syntax tree, and generates a logical execution plan. The logical execution plan includes insert data plan, modify data plan, and query data plan, that is, it describes what data needs to be queried or what data is stored. Among them, inserting data plan includes inserting insert element, and modifying data plan includes modifying update element and query The data plan includes the query select element.
首先,服务器使用访问者模式,遍历AST语法树,获取目标SQL语句中的每个元素,该元素包括SQL基本元素和相关数据。访问者模式,又称visitor模式,把结构和数据分开,编写一个访问者,去访问数据结构中的元素,然后把对各元素的处理全部交给访问者类。First, the server uses the visitor mode to traverse the AST syntax tree to obtain each element in the target SQL statement, which includes basic SQL elements and related data. The visitor pattern, also known as the visitor pattern, separates structure and data, writes a visitor to visit the elements in the data structure, and then delegates all the processing of each element to the visitor class.
然后,服务器通过SQL语句中的元素生成逻辑执行计划。该逻辑计划描述了需要查询哪些数据,或者存储哪些数据。数据中间件给出逻辑执行计划的物理实现,调用底层区块链接口,完成实际的执行。例如:表2用户积分查询请求表,服务器解析接收的SQL查询select语句,将原始SQL字符串分解为逻辑执行计划,即从用户积分user索引表中读取积分point字段的值,过滤条件是用户名称user_name值为Tom的记录。逻辑执行计划包括插入数据计划、修改数据计划和查询数据计划。Then, the server generates a logical execution plan through the elements in the SQL statement. The logical plan describes which data needs to be queried or stored. The data middleware gives the physical realization of the logical execution plan, calls the underlying block link port, and completes the actual execution. For example: Table 2 user points query request form, the server parses the received SQL query select statement, and decomposes the original SQL string into a logical execution plan, that is, reads the point field value from the user points user index table, the filter condition is user The name user_name is a record with Tom. Logical execution plans include inserting data plans, modifying data plans, and querying data plans.
205、判断逻辑执行计划是否为插入数据计划、修改数据计划或者查询数据计划;205. Determine whether the logical execution plan is to insert a data plan, modify a data plan, or query a data plan;
服务器判断逻辑执行计划是否为插入数据计划、修改数据计划或者查询数据计划。具体的,服务器判断逻辑执行计划是否包括SQL的insert元素,若逻辑执行计划包括SQL的insert元素,则确定逻辑执行计划为插入数据计划;服务器判断逻辑执行计划是否包括SQL的update元素,若逻辑执行计划包括SQL的update元素,则确定逻辑执行计划为修改数据计划;服务器判断逻辑执行计划是否包括SQL的select元素,若逻辑执行计划包括SQL的select元素,则确定逻辑执行计划为插入数据计划。The server determines whether the logical execution plan is an insert data plan, a modified data plan, or a query data plan. Specifically, the server determines whether the logical execution plan includes the insert element of SQL. If the logical execution plan includes the insert element of SQL, it determines that the logical execution plan is an insert data plan; the server determines whether the logical execution plan includes the update element of SQL. If the plan includes the update element of SQL, the logical execution plan is determined to be a modified data plan; the server determines whether the logical execution plan includes the select element of SQL, and if the logical execution plan includes the select element of SQL, the logical execution plan is determined to be an insert data plan.
需要说明的是,服务器根据逻辑生成计划和索引数据库对区块链分布式数据库进行操作,该区块链分布式数据库用于存储和查询数据。将该操作根据SQL命令插入insert,修改update和查询select进行划分,服务器在将SQL语句解析生成逻辑执行计划后,结合索引数据库操作区块链分布式数据库,完成数据的查询和存储操作。It should be noted that the server operates the blockchain distributed database according to the logical generation plan and the index database, and the blockchain distributed database is used to store and query data. The operation is divided into insert according to the SQL command, update and query select. After the server parses the SQL statement to generate a logical execution plan, it operates the blockchain distributed database in conjunction with the index database to complete data query and storage operations.
206、若逻辑执行计划为插入数据计划,则对索引数据库和区块链分布式数据库进行插入操作,得到读写结果;206. If the logical execution plan is an insert data plan, insert the index database and the blockchain distributed database to obtain the read and write results;
若逻辑执行计划为插入数据计划,则服务器对索引数据库和区块链分布式数据库进行插入操作,得到读写结果。具体的,服务器生成第一索引标识,其中,第一索引标识为服务器根据预置规则动态生成,具有唯一性;服务器根据插入数据计划和第一索引标识生成SQL插入语句,进一步地,服务器获取插入数据计划中的insert元素、索引表名、多个待插入数据的字段名和各自对应字段值;服务器获取第一索引标识对应的索引字段名;服务器将多个待插入数据的字段名和第一索引标识对应的索引字段名设置为多个插入索引数据表字段,将多个待插入数据各自对应的字段值和第一索引标识按照与多个插入索引数据表字段对应顺序依次设置为多个插入索引数据表字段各自对应的数据值;服务器根据SQL语法规则对insert元素、索引表名、多个插入索引数据表字段和多个插入索引数据表字段各自对应的数据值构建插入索引数据库的SQL插入语句;服务器根据SQL插入语句对索引数据库进行插入操作, 得到索引数据库返回的主键值;服务器根据主键值查询索引数据库,得到索引数据库返回的插入数据;服务器根据第一索引标识和插入数据对区块链分布式数据库进行插入操作得到插入结果,该插入结果包括插入数据成功和插入数据失败,将插入结果设置为读写结果。例如,应用程序请求写入用户积分,首先,服务器根据预置规则生成第一索引标识为USER_1,其SQL插入语句如下:If the logical execution plan is an insert data plan, the server performs an insert operation on the index database and the blockchain distributed database to obtain the read and write results. Specifically, the server generates a first index identifier, where the first index identifier is dynamically generated by the server according to preset rules and is unique; the server generates a SQL insert statement according to the insert data plan and the first index identifier, and further, the server obtains the insert The insert element, index table name, multiple field names to be inserted into the data and their corresponding field values in the data plan; the server obtains the index field name corresponding to the first index identifier; the server converts the field names and first index identifiers of multiple data to be inserted The corresponding index field name is set to multiple insertion index data table fields, and the field values and first index identifiers corresponding to the multiple data to be inserted are set to multiple insertion index data in the order corresponding to the multiple insertion index data table fields. The corresponding data value of the table field; the server constructs the SQL insert statement into the index database according to the SQL syntax rules for the insert element, the index table name, the data values corresponding to the insert element, the index table name, and the multiple insert index data table fields; The server inserts the index database according to the SQL insert statement, and obtains the primary key value returned by the index database; the server queries the index database according to the primary key value to obtain the inserted data returned by the index database; the server compares the block according to the first index identifier and the inserted data The chain distributed database performs an insert operation to obtain an insert result, which includes successful data insertion and data insertion failure, and the insert result is set as the read-write result. For example, an application requests to write user points. First, the server generates the first index identifier USER_1 according to the preset rules, and its SQL insert statement is as follows:
insert into`user`(user_id,user_name,point,data_id)values(1,‘Tom’,0,‘USER_1’);insert into`user`(user_id,user_name,point,data_id)values(1,‘Tom’,0,‘USER_1’);
服务器再根据SQL插入语句将数据写入索引数据库中的用户积分user索引表,得到插入结果,如表3插入用户积分索引表所示。The server then writes data into the user score user index table in the index database according to the SQL insert statement, and obtains the insertion result, as shown in Table 3, insert user score index table.
表3:插入用户积分索引表Table 3: Insert user points index table
user_iduser_id user_nameuser_name pointpoint data_iddata_id
11 TomTom 00 USER_1USER_1
最后,服务器查询索引数据库中的user表,得到查询数据,将查询数据中的USER_1作为区块链分布式数据库的键key,将其他字段和数据封装为JS对象简谱(JavaScript Object Notation,JSON)格式,并将该JSON数据{‘userId’:1,‘userName’:’Tom’,point:0}设置为区块链分布式数据库的值value,再调用区块链底层接口将key和value写入区块链分布式数据库中,写入后区块链上存储的数据如下表4区块链分布式数据表,并将得到的插入结果返回给终端。Finally, the server queries the user table in the index database to obtain the query data, uses USER_1 in the query data as the key key of the blockchain distributed database, and encapsulates other fields and data into JavaScript Object Notation (JSON) format , And set the JSON data {'userId':1,'userName':'Tom',point:0} as the value of the blockchain distributed database, and then call the underlying interface of the blockchain to write the key and value In the blockchain distributed database, the data stored on the blockchain after writing is shown in Table 4 Blockchain distributed data table, and the inserted result is returned to the terminal.
表4:区块链分布式数据表Table 4: Blockchain distributed data table
keykey valuevalue
USER_1USER_1 {‘userId’:1,‘userName’:’Tom’,point:0}{‘UserId’:1,‘userName’:’Tom’,point:0}
207、若逻辑执行计划为修改数据计划,则对索引数据库和区块链分布式数据库进行修改操作,得到读写结果;207. If the logical execution plan is to modify the data plan, modify the index database and the blockchain distributed database to obtain the read and write results;
若逻辑执行计划为修改数据计划,则服务器对索引数据库和区块链分布式数据库进行修改操作,得到读写结果。具体的,根据修改数据计划生成SQL修改语句,进一步地,服务器获取修改数据计划中的update元素、索引表名、修改条件、多个待修改数据的字段名和各自对应字段值;服务器根据SQL语法规则对update元素、索引表名、修改条件、多个待修改数据的字段名和各自对应字段值进行重新构建,得到修改索引数据库的SQL修改语句;根据SQL修改语句更新索引数据库,得到第二索引标识;根据第二索引标识查询区块链分布式数据库,得到区块链初始数据;对区块链初始数据进行修改,得到区块链目标数据;根据第二索引标识将区块链目标数据写入区块链分布式数据库,得到修改结果,该修改结果包括修改数据成功和修改数据失败,将修改结果设置为读写结果。例如,应用程序请求给用户标识1增加100积分,首先服务器生成SQL修改语句如下:If the logical execution plan is to modify the data plan, the server modifies the index database and the blockchain distributed database to obtain the read and write results. Specifically, the SQL modification statement is generated according to the modified data plan. Further, the server obtains the update element, index table name, modification condition, field names of multiple data to be modified and respective corresponding field values in the modified data plan; the server according to the SQL syntax rules Rebuild the update element, index table name, modification condition, field names of multiple data to be modified, and respective corresponding field values to obtain the SQL modification statement for modifying the index database; update the index database according to the SQL modification statement to obtain the second index identification; Query the blockchain distributed database according to the second index identifier to obtain the initial data of the blockchain; modify the initial data of the blockchain to obtain the target data of the blockchain; write the target data of the blockchain into the area according to the second index identifier The block chain distributed database obtains the modification result, the modification result includes the successful modification of the data and the modification failure, and the modification result is set as the read-write result. For example, the application requests to add 100 points to the user ID 1. First, the server generates the SQL modification statement as follows:
update`user`set point=point+100 where user_id=1;update`user`set point=point+100 where user_id=1;
然后,服务器执行该SQL修改语句更新索引数据库中的user表,更新后的user表数据如表5更新用户积分索引表。Then, the server executes the SQL modification statement to update the user table in the index database, and the updated user table data is shown in Table 5 to update the user score index table.
表5:更新用户积分索引表Table 5: Update user points index table
user_iduser_id user_nameuser_name pointpoint data_iddata_id
11 TomTom 100100 USER_1USER_1
最后,服务器更新区块链上的数据,具体的,服务器执行select data_id from`user`where user_id=1,得到第二索引标识data_id字段的值USERT_1;服务器将USERT_1作为key查询区块链数据库,返回JSON数据{‘userId’:1,‘userName’:’Tom’,point:0};服务器将该JSON数据转换积分更新操作,更新后的积分数据为{‘userId’:1,‘userName’:‘Tom’,point:100},最后,将更新后的积分数据调用区块链底层接口写入区块链分布式数据库上,更新后区块链数据如表6更新区块链分布式数据库,并将得到的修改结果返回给终端。Finally, the server updates the data on the blockchain. Specifically, the server executes select data_id from`user`where user_id=1 to obtain the value of the second index identifier data_id field USERT_1; the server uses USERT_1 as the key to query the blockchain database and returns JSON data {'userId':1,'userName':'Tom',point:0}; the server converts the JSON data into the point update operation, and the updated point data is {'userId':1,'userName':' Tom',point:100}, finally, call the bottom-level interface of the blockchain to write the updated points data to the blockchain distributed database. The updated blockchain data is shown in Table 6 to update the blockchain distributed database, and Return the modified result to the terminal.
表6:更新区块链分布式数据库Table 6: Update the blockchain distributed database
KeyKey ValueValue
USER_1USER_1 {‘userId’:1,‘userName’:‘Tom’,point:100}{‘UserId’:1,‘userName’:‘Tom’,point:100}
需要说明的是,根据第二索引标识从区块链分布式数据库中查询数据,得到初始数据,并对初始数据更新再写入区块链分布式数据库,确保区块链分布式数据库的数据完整性和一致性。It should be noted that the data is queried from the blockchain distributed database according to the second index identification to obtain the initial data, and the initial data is updated and then written to the blockchain distributed database to ensure that the data in the blockchain distributed database is complete Sex and consistency.
208、若逻辑执行计划为查询数据计划,则对索引数据库和区块链分布式数据库进行查询操作,得到读写结果。208. If the logical execution plan is a query data plan, perform a query operation on the index database and the blockchain distributed database to obtain read and write results.
若逻辑执行计划为查询数据计划,则对索引数据库和区块链分布式数据库进行查询操作,得到读写结果。具体的,根据查询数据计划生成SQL查询语句,进一步地,服务器获取查询数据计划中的select元素、索引表名、查询条件和多个待查询的字段名;服务器根据SQL语法规则对select元素、索引表名、查询条件和多个待查询的字段名进行重新构建,得到查询索引数据库的SQL查询语句;根据SQL查询语句查询索引数据库,得到第三索引标识;根据第三索引标识读取区块链分布式数据库,得到查询结果,将查询结果设置为读写结果。例如,应用程序请求查询用户标识1的积分,目标SQL语句为select point from`user`where user_id=1,服务器生成的SQL查询语句为select data_id from`user`where user_id=1;服务器执行SQL查询语句读取索引数据库中的user表,返回结果为USER_1;服务器使用key值USER_1读取区块链分布式数据库,得到查询数据为{‘userId’:1,‘userName’:’Tom’,point:100},服务器解析查询数据,得到point=100并返回给应用程序。需要说明的是,其中,当查询结果的数据量是0时,表示查询结果为空。服务器从区块链分布式数据库中得到查询结果,确保数据的准确性。If the logical execution plan is a query data plan, then perform a query operation on the index database and the blockchain distributed database to obtain the read and write results. Specifically, the SQL query statement is generated according to the query data plan. Further, the server obtains the select element, index table name, query condition, and multiple field names to be queried in the query data plan; the server performs the query on the select element and index according to the SQL syntax rules The table name, query conditions and multiple field names to be queried are reconstructed to obtain the SQL query statement for querying the index database; query the index database according to the SQL query statement to obtain the third index identification; read the blockchain according to the third index identification Distributed database, get the query result, and set the query result as the read-write result. For example, the application requests to query the points of user ID 1, the target SQL statement is select point from`user`where user_id=1, the SQL query statement generated by the server is select data_id from`user`where user_id=1; the server executes the SQL query statement Read the user table in the index database, and the return result is USER_1; the server uses the key value USER_1 to read the blockchain distributed database, and the query data is {'userId':1,'userName':'Tom',point:100 }, the server parses the query data, obtains point=100 and returns it to the application program. It should be noted that, when the data amount of the query result is 0, it means that the query result is empty. The server obtains the query results from the blockchain distributed database to ensure the accuracy of the data.
进一步地,对于多表联合查询、分组和排序等复杂的SQL语句请求,处理的基本思路都是先解析目标SQL语句,分析出逻辑执行计划,然后在逻辑执行计划上改写SQL语句,获取第三索引标识data_id,根据data_id从区块链分布式数据库中读取基础数据,最后再对数据进行聚合和计算,得到最终的查询结果。Further, for complex SQL statement requests such as multi-table joint query, grouping and sorting, the basic idea of processing is to first parse the target SQL statement, analyze the logical execution plan, and then rewrite the SQL statement on the logical execution plan to obtain the third The index identifies data_id, and reads the basic data from the blockchain distributed database according to the data_id, and finally aggregates and calculates the data to obtain the final query result.
可选的,服务器发送第一消息给消息队列,得到事务标识;服务器根据 事务标识对索引数据库进行写入操作,得到写入结果;服务器判断写入结果是否为写入成功;若写入结果为写入成功,则服务器通过消息队列对区块链分布式数据库进行写入操作;若写入结果为写入失败,则服务器通过消息队列对索引数据库进行删除操作。具体的,服务器都是先将数据写入索引数据库,根据索引标识再将数据写入区块链分布式数据库,服务器使用分布式消息中间件RocketMQ提供的事物消息功能,让写索引数据和发送分布式消息给RocketMQ在一个事物里面,保证写入数据和发送消息同时成功或者同时失败。服务器写入区块链分布式数据库通过检测RocketMQ消息完成,增加写入日志表保证幂等性,实现索引数据库和区块链分布式数据库的最终一致性。Optionally, the server sends the first message to the message queue to obtain the transaction identifier; the server writes the index database according to the transaction identifier to obtain the write result; the server determines whether the write result is successful; if the write result is If the write is successful, the server writes the blockchain distributed database through the message queue; if the write result is a write failure, the server deletes the index database through the message queue. Specifically, the server first writes the data into the index database, and then writes the data into the blockchain distributed database according to the index identification. The server uses the transaction message function provided by the distributed message middleware RocketMQ to write index data and send distribution The message is sent to RocketMQ in one transaction, ensuring that the writing of data and sending of the message succeed or fail at the same time. The server writing to the blockchain distributed database is completed by detecting RocketMQ messages, adding the write log table to ensure idempotence, and achieving the ultimate consistency of the index database and the blockchain distributed database.
本申请实施例中,通过对SQL语句的处理,使用SQL语句兼容在区块链分布式数据库和关系型数据库的查询和存储,通过建立索引数据库和区块链分布式数据库的对应关系,提高区块链业务信息的检索效率,对区块链底层接口封装,使用SQL语句和索引数据库实现数据上链存储和查询,降低开发门槛,提高开发效率。In the embodiment of the application, through the processing of SQL statements, the use of SQL statements is compatible with the query and storage of the blockchain distributed database and the relational database, and the corresponding relationship between the index database and the blockchain distributed database is established to improve the area. Retrieval efficiency of blockchain business information, encapsulate the underlying interface of the blockchain, use SQL statements and index databases to achieve data storage and query on the chain, reduce development thresholds, and improve development efficiency.
上面对本申请实施例中区块链数据存储和查询方法进行了描述,下面对本申请实施例中区块链数据存储和查询装置进行描述,请参阅图3,本申请实施例中区块链数据存储和查询装置一个实施例包括:第一生成单元301,用于生成索引数据库;获取单元302,用于获取目标结构化查询语言SQL语句,目标SQL语句为符合SQL语法规则的合法语句;解析单元303,用于根据预置算法对目标SQL语句进行解析,得到抽象语法树;第二生成单元304,用于根据抽象语法树生成逻辑执行计划,逻辑执行计划包括插入数据计划、修改数据计划和查询数据计划;读写单元305,用于根据逻辑生成计划和索引数据库对区块链分布式数据库进行读写操作,得到读写结果,该区块链分布式数据库用于存储和查询数据。The block chain data storage and query method in the embodiment of the application is described above, and the block chain data storage and query device in the embodiment of the application is described below. Please refer to FIG. 3, the block chain data storage in the embodiment of the application An embodiment of the query device includes: a first generating unit 301, used to generate an index database; an obtaining unit 302, used to obtain a target structured query language SQL statement, the target SQL statement is a legal statement that conforms to SQL grammar rules; a parsing unit 303 , Used to parse the target SQL statement according to a preset algorithm to obtain an abstract syntax tree; the second generating unit 304 is used to generate a logical execution plan according to the abstract syntax tree, the logical execution plan includes inserting a data plan, modifying a data plan, and querying data Plan; The read-write unit 305 is used to perform read and write operations on the blockchain distributed database according to the logic generation plan and index database to obtain the read and write results. The blockchain distributed database is used to store and query data.
本申请实施例中,通过对SQL语句的处理,使用SQL语句兼容在区块链分布式数据库和关系型数据库的查询和存储,通过建立索引数据库和区块链分布式数据库的对应关系,提高区块链业务信息的检索效率,对区块链底层接口封装,使用SQL语句和索引数据库实现数据上链存储和查询,降低开发门槛,提高开发效率。In the embodiment of the application, through the processing of SQL statements, the use of SQL statements is compatible with the query and storage of the blockchain distributed database and the relational database, and the corresponding relationship between the index database and the blockchain distributed database is established to improve the area. Retrieval efficiency of blockchain business information, encapsulate the underlying interface of the blockchain, use SQL statements and index databases to achieve data storage and query on the chain, reduce development thresholds, and improve development efficiency.
请参阅图4,本发明实施例中区块链数据存储和查询装置的另一个实施例包括:第一生成单元301,用于生成索引数据库;获取单元302,用于获取目标结构化查询语言SQL语句,目标SQL语句为符合SQL语法规则的合法语句;解析单元303,用于根据预置算法对目标SQL语句进行解析,得到抽象语法树;第二生成单元304,用于根据抽象语法树生成逻辑执行计划,逻辑执行计划包括插入数据计划、修改数据计划和查询数据计划;读写单元305,用于根据逻辑生成计划和索引数据库对区块链分布式数据库进行读写操作,得到读写结果,该区块链分布式数据库用于存储和查询数据。4, another embodiment of the device for storing and querying blockchain data in the embodiment of the present invention includes: a first generating unit 301, configured to generate an index database; and an acquiring unit 302, configured to acquire a target structured query language SQL Statement, the target SQL statement is a legal statement that conforms to the SQL grammar rules; the parsing unit 303 is used to parse the target SQL statement according to a preset algorithm to obtain an abstract syntax tree; the second generating unit 304 is used to generate logic according to the abstract syntax tree Execution plan, the logical execution plan includes inserting data plan, modifying data plan and querying data plan; the reading and writing unit 305 is used to read and write the blockchain distributed database according to the logic generation plan and index database to obtain the read and write results, The blockchain distributed database is used to store and query data.
可选的,第二生成单元304还可以具体用于:通过访问者模式遍历抽象 语法树,得到目标SQL语句的元素;根据目标SQL语句的元素生成逻辑执行计划,该逻辑执行计划包括插入数据计划、修改数据计划和查询数据计划。Optionally, the second generating unit 304 may also be specifically used to: traverse the abstract syntax tree through the visitor mode to obtain the elements of the target SQL statement; generate a logical execution plan according to the elements of the target SQL statement, and the logical execution plan includes an insert data plan , Modify the data plan and query the data plan.
可选的,读写单元305可进一步包括:判断子单元3051,用于判断逻辑执行计划是否为插入数据计划、修改数据计划或者查询数据计划;插入子单元3052,若逻辑执行计划为插入数据计划,则用于对索引数据库和区块链分布式数据库进行插入操作,得到读写结果;修改子单元3053,若逻辑执行计划为修改数据计划,则用于对索引数据库和区块链分布式数据库进行修改操作,得到读写结果;查询子单元3054,若逻辑执行计划为查询数据计划,则用于索引数据库和区块链分布式数据库进行查询操作,得到读写结果。Optionally, the reading and writing unit 305 may further include: a judging subunit 3051, for judging whether the logical execution plan is an insert data plan, a modified data plan, or a query data plan; an insert subunit 3052, if the logical execution plan is an insert data plan , It is used to insert operations on the index database and the blockchain distributed database to obtain the read and write results; the modification subunit 3053, if the logical execution plan is to modify the data plan, it is used for the index database and the blockchain distributed database Perform modification operations to obtain read and write results; the query subunit 3054, if the logical execution plan is a query data plan, is used for query operations on the index database and blockchain distributed database to obtain the read and write results.
可选的,插入子单元3052还可以具体用于:生成第一索引标识;根据插入数据计划和第一索引标识生成SQL插入语句;根据SQL插入语句对索引数据库进行插入操作,得到索引数据库返回的主键值;根据主键值查询索引数据库,得到索引数据库返回的插入数据;根据第一索引标识和插入数据对区块链分布式数据库进行插入操作,得到插入结果,将插入结果设置为读写结果。Optionally, the insert subunit 3052 can also be specifically used to: generate a first index identifier; generate a SQL insert statement according to the insert data plan and the first index identifier; perform an insert operation on the index database according to the SQL insert statement, and obtain the return value of the index database Primary key value; query the index database according to the primary key value to get the inserted data returned by the index database; insert the blockchain distributed database according to the first index identifier and the inserted data to obtain the insert result, and set the insert result to read and write result.
可选的,修改子单元3053还可以具体用于:根据修改数据计划生成SQL修改语句;根据SQL修改语句更新索引数据库,得到第二索引标识;根据第二索引标识查询区块链分布式数据库,得到区块链初始数据;对区块链初始数据进行修改,得到区块链目标数据;根据第二索引标识将区块链目标数据写入区块链分布式数据库,得到修改结果,将修改结果设置为读写结果。Optionally, the modification subunit 3053 can also be specifically used to: generate SQL modification statements according to the modified data plan; update the index database according to the SQL modification statements to obtain the second index identification; query the blockchain distributed database according to the second index identification, Obtain the initial data of the blockchain; modify the initial data of the blockchain to obtain the target data of the blockchain; write the target data of the blockchain into the distributed database of the blockchain according to the second index identification, obtain the modified result, and then modify the result Set to read and write results.
可选的,查询子单元3054还可以具体用于:根据查询数据计划生成SQL查询语句;根据SQL查询语句查询索引数据库,得到第三索引标识;根据第三索引标识读取区块链分布式数据库,得到查询结果,将查询结果设置为读写结果。Optionally, the query subunit 3054 can also be specifically used to: generate SQL query statements according to the query data plan; query the index database according to the SQL query statements to obtain the third index identifier; read the blockchain distributed database according to the third index identifier To get the query result, and set the query result as the read-write result.
可选的,区块链数据存储和查询装置还包括:发送单元306,用于发送第一消息给消息队列,得到事务标识;第一写入单元307,用于根据事务标识对索引数据库进行写入操作,得到写入结果;判断单元308,用于判断写入结果是否为写入成功;第二写入单元309,若写入结果为写入成功,则用于通过消息队列对区块链分布式数据库进行写入操作;删除单元310,若写入结果为写入失败,则用于通过消息队列对索引数据库进行删除操作。Optionally, the blockchain data storage and query device further includes: a sending unit 306, configured to send a first message to the message queue to obtain a transaction identifier; and a first writing unit 307, configured to write to the index database according to the transaction identifier In operation, the writing result is obtained; the judgment unit 308 is used to judge whether the writing result is successful; the second writing unit 309, if the writing result is successful, is used to check the blockchain through the message queue The distributed database performs a write operation; the deleting unit 310, if the write result is a write failure, is used to delete the index database through the message queue.
本发明实施例中,通过对SQL语句的处理,使用SQL语句兼容在区块链分布式数据库和关系型数据库的查询和存储,通过建立索引数据库和区块链分布式数据库的对应关系,提高区块链业务信息的检索效率,对区块链底层接口封装,使用SQL语句和索引数据库实现数据上链存储和查询,降低开发门槛,提高开发效率。上面图3和图4从模块化功能实体的角度对本发明实施例中的区块链数据存储和查询装置进行详细描述,下面从硬件处理的角度对本发明实施例中区块链数据存储和查询设备进行详细描述。In the embodiment of the present invention, through the processing of SQL statements, the use of SQL statements is compatible with query and storage in the blockchain distributed database and the relational database, and the corresponding relationship between the index database and the blockchain distributed database is established to improve the area. Retrieval efficiency of blockchain business information, encapsulate the underlying interface of the blockchain, use SQL statements and index databases to achieve data storage and query on the chain, reduce development thresholds, and improve development efficiency. The above figures 3 and 4 describe in detail the block chain data storage and query device in the embodiment of the present invention from the perspective of modular functional entities. The following describes the block chain data storage and query device in the embodiment of the present invention from the perspective of hardware processing Give a detailed description.
图4是本申请实施例提供的一种区块链数据存储和查询设备的结构示意图,该区块链数据存储和查询设备500可因配置或性能不同而产生比较大的 差异,可以包括一个或一个以上处理器(central processing units,CPU)501(例如,一个或一个以上处理器)和存储器509,一个或一个以上存储应用程序509或数据509的存储介质508(例如一个或一个以上海量存储设备)。其中,存储器509和存储介质508可以是短暂存储或持久存储。存储在存储介质508的程序可以包括一个或一个以上模块(图示没标出),每个模块可以包括对区块链数据存储和查询中的一系列指令操作。更进一步地,处理器501可以设置为与存储介质508通信,在区块链数据存储和查询设备500上执行存储介质508中的一系列指令操作。所述指令包括:101、生成索引数据库;102、获取目标结构化查询语言SQL语句,所述目标SQL语句为符合SQL语法规则的合法语句;103、根据预置算法对所述目标SQL语句进行解析,得到抽象语法树;104、根据所述抽象语法树生成逻辑执行计划,所述逻辑执行计划包括插入数据计划、修改数据计划和查询数据计划;105、根据所述逻辑生成计划和所述索引数据库对区块链分布式数据库进行读写操作,得到读写结果,所述区块链分布式数据库用于存储和查询数据。FIG. 4 is a schematic structural diagram of a blockchain data storage and query device provided by an embodiment of the present application. The blockchain data storage and query device 500 may have relatively large differences due to different configurations or performance, and may include one or One or more processors (central processing units, CPU) 501 (for example, one or more processors) and memory 509, one or more storage media 508 for storing application programs 509 or data 509 (for example, one or one storage device with a large amount of ). Among them, the memory 509 and the storage medium 508 may be short-term storage or persistent storage. The program stored in the storage medium 508 may include one or more modules (not shown in the figure), and each module may include a series of instruction operations for storing and querying blockchain data. Furthermore, the processor 501 may be configured to communicate with the storage medium 508, and execute a series of instruction operations in the storage medium 508 on the blockchain data storage and query device 500. The instructions include: 101. Generate an index database; 102. Obtain a target structured query language SQL statement, where the target SQL statement is a legal statement that complies with the SQL grammar rules; 103. parse the target SQL statement according to a preset algorithm , Obtain an abstract syntax tree; 104, generate a logical execution plan according to the abstract syntax tree, the logical execution plan includes inserting a data plan, modifying a data plan, and querying a data plan; 105, generating a plan and the index database according to the logic Perform read and write operations on the blockchain distributed database to obtain read and write results. The blockchain distributed database is used to store and query data.
区块链数据存储和查询设备500还可以包括一个或一个以上电源502,一个或一个以上有线或无线网络接口503,一个或一个以上输入输出接口504,和/或,一个或一个以上操作系统505,例如Windows Serve,Mac OS X,Unix,Linux,FreeBSD等等。本领域技术人员可以理解,图4示出的区块链数据存储和查询设备结构并不构成对区块链数据存储和查询设备的限定,可以包括比图示更多或更少的部件,或者组合某些部件,或者不同的部件布置。The blockchain data storage and query device 500 may also include one or more power supplies 502, one or more wired or wireless network interfaces 503, one or more input and output interfaces 504, and/or, one or more operating systems 505 , Such as Windows Serve, Mac OS X, Unix, Linux, FreeBSD and so on. Those skilled in the art can understand that the structure of the blockchain data storage and query device shown in FIG. 4 does not constitute a limitation on the blockchain data storage and query device, and may include more or fewer components than shown in the figure, or Combining certain components, or different component arrangements.
在本申请各个实施例中的各功能单元可以集成在一个处理单元中,也可以是各个单元单独物理存在,也可以两个或两个以上单元集成在一个单元中。上述集成的单元既可以采用硬件的形式实现,也可以采用软件功能单元的形式实现。所述集成的单元如果以软件功能单元的形式实现并作为独立的产品销售或使用时,可以存储在一个计算机可读取存储介质中,所述计算机可读存储介质可以是非易失性可读存储介质,也可以是易失性可读存储介质。基于这样的理解,本申请的技术方案本质上或者说对现有技术做出贡献的部分或者该技术方案的全部或部分可以以软件产品的形式体现出来,该计算机软件产品存储在一个存储介质中,包括若干指令用以使得一台计算机设备(可以是个人计算机,服务器,或者网络设备等)执行本申请各个实施例所述方法的全部或部分步骤。所述步骤包括:101、生成索引数据库;102、获取目标结构化查询语言SQL语句,所述目标SQL语句为符合SQL语法规则的合法语句;103、根据预置算法对所述目标SQL语句进行解析,得到抽象语法树;104、根据所述抽象语法树生成逻辑执行计划,所述逻辑执行计划包括插入数据计划、修改数据计划和查询数据计划;105、根据所述逻辑生成计划和所述索引数据库对区块链分布式数据库进行读写操作,得到读写结果,所述区块链分布式数据库用于存储和查询数据。而前述的存储介质包括:U盘、移动硬盘、只读存储器(read-only memory,ROM)、随机存取存储器(random access memory,RAM)、磁碟或者光盘等各种可以存储程序代码的介质。The functional units in the various embodiments of the present application may be integrated into one processing unit, or each unit may exist alone physically, or two or more units may be integrated into one unit. The above-mentioned integrated unit can be implemented in the form of hardware or software functional unit. If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it may be stored in a computer-readable storage medium, which may be a non-volatile readable storage The medium may also be a volatile readable storage medium. Based on this understanding, the technical solution of this application essentially or the part that contributes to the existing technology or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium , Including several instructions to make a computer device (which can be a personal computer, a server, or a network device, etc.) execute all or part of the steps of the method described in each embodiment of the present application. The steps include: 101. Generating an index database; 102. Obtaining a target structured query language SQL statement, where the target SQL statement is a legal statement complying with SQL grammar rules; 103. Analyzing the target SQL statement according to a preset algorithm , Obtain an abstract syntax tree; 104, generate a logical execution plan according to the abstract syntax tree, the logical execution plan includes inserting a data plan, modifying a data plan, and querying a data plan; 105, generating a plan and the index database according to the logic Perform read and write operations on the blockchain distributed database to obtain read and write results. The blockchain distributed database is used to store and query data. The aforementioned storage media include: U disk, mobile hard disk, read-only memory (read-only memory, ROM), random access memory (random access memory, RAM), magnetic disk or optical disk and other media that can store program code .

Claims (20)

  1. 一种区块链数据存储和查询方法,其中,包括:A blockchain data storage and query method, which includes:
    生成索引数据库;Generate index database;
    获取目标结构化查询语言SQL语句,所述目标SQL语句为符合SQL语法规则的合法语句;Acquiring a target structured query language SQL statement, where the target SQL statement is a legal statement that complies with SQL grammar rules;
    根据预置算法对所述目标SQL语句进行解析,得到抽象语法树;Parse the target SQL statement according to a preset algorithm to obtain an abstract syntax tree;
    根据所述抽象语法树生成逻辑执行计划,所述逻辑执行计划包括插入数据计划、修改数据计划和查询数据计划;Generating a logical execution plan according to the abstract syntax tree, the logical execution plan including inserting a data plan, modifying a data plan, and querying a data plan;
    根据所述逻辑生成计划和所述索引数据库对区块链分布式数据库进行读写操作,得到读写结果,所述区块链分布式数据库用于存储和查询数据。Perform read and write operations on the blockchain distributed database according to the logic generation plan and the index database to obtain read and write results. The blockchain distributed database is used to store and query data.
  2. 根据权利要求1所述的区块链数据存储和查询方法,其中,所述根据所述抽象语法树生成逻辑执行计划,所述逻辑执行计划包括插入数据计划、修改数据计划和查询数据计划包括:The blockchain data storage and query method according to claim 1, wherein said generating a logical execution plan according to said abstract syntax tree, said logical execution plan including inserting data plan, modifying data plan and querying data plan comprises:
    通过访问者模式遍历所述抽象语法树,得到所述目标SQL语句的元素;Traverse the abstract syntax tree through the visitor mode to obtain the elements of the target SQL statement;
    根据所述目标SQL语句的元素生成逻辑执行计划,所述逻辑执行计划包括插入数据计划、修改数据计划和查询数据计划。A logical execution plan is generated according to the elements of the target SQL statement, and the logical execution plan includes an insert data plan, a modified data plan, and a query data plan.
  3. 根据权利要求2所述的区块链数据存储和查询方法,其中,所述根据所述逻辑生成计划和所述索引数据库对区块链分布式数据库进行读写操作,得到读写结果,所述区块链分布式数据库用于存储和查询数据包括:The blockchain data storage and query method according to claim 2, wherein the read and write operations on the blockchain distributed database are performed according to the logic generation plan and the index database to obtain a read and write result, the Blockchain distributed database is used to store and query data including:
    判断所述逻辑执行计划是否为所述插入数据计划、所述修改数据计划或者所述查询数据计划;Judging whether the logical execution plan is the inserted data plan, the modified data plan, or the query data plan;
    若所述逻辑执行计划为所述插入数据计划,则对所述索引数据库和所述区块链分布式数据库进行插入操作,得到所述读写结果;If the logical execution plan is the insert data plan, perform an insert operation on the index database and the blockchain distributed database to obtain the read and write result;
    若所述逻辑执行计划为所述修改数据计划,则对所述索引数据库和所述区块链分布式数据库进行修改操作,得到所述读写结果;If the logical execution plan is the modified data plan, modify the index database and the blockchain distributed database to obtain the read and write results;
    若所述逻辑执行计划为所述查询数据计划,则对所述索引数据库和所述区块链分布式数据库进行查询操作,得到所述读写结果。If the logical execution plan is the query data plan, a query operation is performed on the index database and the blockchain distributed database to obtain the read and write results.
  4. 根据权利要求3所述的区块链数据存储和查询方法,其中,所述若所述逻辑执行计划为所述插入数据计划,则对所述索引数据库和所述区块链分布式数据库进行插入操作,得到所述读写结果包括:The blockchain data storage and query method according to claim 3, wherein if the logical execution plan is the inserted data plan, insert the index database and the blockchain distributed database The operation to obtain the read and write result includes:
    生成第一索引标识;Generate the first index identifier;
    根据所述插入数据计划和所述第一索引标识生成SQL插入语句;Generating an SQL insert statement according to the insert data plan and the first index identifier;
    根据所述SQL插入语句对所述索引数据库进行插入操作,得到所述索引数据库返回的主键值;Performing an insert operation on the index database according to the SQL insert statement to obtain the primary key value returned by the index database;
    根据所述主键值查询所述索引数据库,得到所述索引数据库返回的插入数据;Query the index database according to the primary key value to obtain the inserted data returned by the index database;
    根据所述第一索引标识和所述插入数据对区块链分布式数据库进行插入操作,得到插入结果,将所述插入结果设置为所述读写结果。Perform an insert operation on the blockchain distributed database according to the first index identifier and the insert data to obtain an insert result, and set the insert result as the read and write result.
  5. 根据权利要求3所述的区块链数据存储和查询方法,其中,所述若所述逻辑执行计划为所述修改数据计划,则对所述索引数据库和所述区块链分布式数据库进行修改操作,得到所述读写结果包括:The blockchain data storage and query method according to claim 3, wherein if the logical execution plan is the modified data plan, then the index database and the blockchain distributed database are modified The operation to obtain the read and write result includes:
    根据所述修改数据计划生成SQL修改语句;Generate SQL modification statements according to the modification data plan;
    根据所述SQL修改语句更新所述索引数据库,得到第二索引标识;Update the index database according to the SQL modification statement to obtain a second index identifier;
    根据所述第二索引标识查询所述区块链分布式数据库,得到区块链初始数据;Query the blockchain distributed database according to the second index identifier to obtain initial blockchain data;
    对所述区块链初始数据进行修改,得到区块链目标数据;Modify the initial data of the blockchain to obtain the target data of the blockchain;
    根据所述第二索引标识将所述区块链目标数据写入所述区块链分布式数据库,得到修改结果,将所述修改结果设置为所述读写结果。Write the block chain target data to the block chain distributed database according to the second index identifier, obtain a modification result, and set the modification result as the read and write result.
  6. 根据权利要求3所述的区块链数据存储和查询方法,其中,所述若所述逻辑执行计划为所述查询数据计划,则对所述索引数据库和所述区块链分布式数据库进行查询操作,得到所述操作结果包括:The blockchain data storage and query method according to claim 3, wherein, if the logical execution plan is the query data plan, query the index database and the blockchain distributed database The operation to obtain the operation result includes:
    根据所述查询数据计划生成SQL查询语句;Generating SQL query statements according to the query data plan;
    根据所述SQL查询语句查询所述索引数据库,得到第三索引标识;Query the index database according to the SQL query statement to obtain a third index identifier;
    根据所述第三索引标识读取所述区块链分布式数据库,得到查询结果,将所述查询结果设置为所述读写结果。Read the blockchain distributed database according to the third index identifier to obtain a query result, and set the query result as the read-write result.
  7. 根据权利要求1至6任一项所述的区块链数据存储和查询方法,其中,所述方法还包括:The blockchain data storage and query method according to any one of claims 1 to 6, wherein the method further comprises:
    发送第一消息给消息队列,得到事务标识;Send the first message to the message queue to obtain the transaction identifier;
    根据所述事务标识对所述索引数据库进行写入操作,得到写入结果;Perform a write operation on the index database according to the transaction identifier to obtain a write result;
    判断所述写入结果是否为写入成功;Judging whether the writing result is successful;
    若所述写入结果为写入成功,则通过所述消息队列对所述区块链分布式数据库进行写入操作;If the writing result is that the writing is successful, perform a writing operation on the blockchain distributed database through the message queue;
    若所述写入结果为写入失败,则通过所述消息队列对所述索引数据库进行删除操作。If the writing result is a writing failure, the index database is deleted through the message queue.
  8. 一种区块链数据存储和查询装置,其中,所述区块链数据存储和查询装置包括:A block chain data storage and query device, wherein the block chain data storage and query device includes:
    第一生成单元,用于生成索引数据库;The first generating unit is used to generate an index database;
    获取单元,用于获取目标结构化查询语言SQL语句,所述目标SQL语句为符合SQL语法规则的合法语句;The obtaining unit is used to obtain a target structured query language SQL statement, where the target SQL statement is a legal statement that complies with SQL grammar rules;
    解析单元,用于根据预置算法对所述目标SQL语句进行解析,得到抽象语法树;The parsing unit is used to parse the target SQL statement according to a preset algorithm to obtain an abstract syntax tree;
    第二生成单元,用于根据所述抽象语法树生成逻辑执行计划,所述逻辑执行计划包括插入数据计划、修改数据计划和查询数据计划;The second generating unit is configured to generate a logical execution plan according to the abstract syntax tree, and the logical execution plan includes inserting a data plan, modifying a data plan, and querying a data plan;
    读写单元,用于根据所述逻辑生成计划和所述索引数据库对区块链分布式数据库进行读写操作,得到读写结果,所述区块链分布式数据库用于存储和查询数据。The reading and writing unit is configured to perform read and write operations on the blockchain distributed database according to the logic generation plan and the index database to obtain read and write results. The blockchain distributed database is used to store and query data.
  9. 一种区块链数据存储和查询设备,其中,所述区块链数据存储和查询 设备包括:存储器和至少一个处理器,所述存储器中存储有指令,所述存储器和对数至少一个处理器通过线路互联;A blockchain data storage and query device, wherein the blockchain data storage and query device includes a memory and at least one processor, the memory stores instructions, the memory and the logarithmic at least one processor Interconnected by wires;
    所述至少一个处理器调用所述存储器中的所述指令,以使得所述区块链数据存储和查询设备执行如下步骤:The at least one processor invokes the instructions in the memory, so that the blockchain data storage and query device executes the following steps:
    生成索引数据库;Generate index database;
    获取目标结构化查询语言SQL语句,所述目标SQL语句为符合SQL语法规则的合法语句;Acquiring a target structured query language SQL statement, where the target SQL statement is a legal statement that complies with SQL grammar rules;
    根据预置算法对所述目标SQL语句进行解析,得到抽象语法树;Parse the target SQL statement according to a preset algorithm to obtain an abstract syntax tree;
    根据所述抽象语法树生成逻辑执行计划,所述逻辑执行计划包括插入数据计划、修改数据计划和查询数据计划;Generating a logical execution plan according to the abstract syntax tree, the logical execution plan including inserting a data plan, modifying a data plan, and querying a data plan;
    根据所述逻辑生成计划和所述索引数据库对区块链分布式数据库进行读写操作,得到读写结果,所述区块链分布式数据库用于存储和查询数据。Perform read and write operations on the blockchain distributed database according to the logic generation plan and the index database to obtain read and write results. The blockchain distributed database is used to store and query data.
  10. 根据权利要求9所述的区块链数据存储和查询设备,其中,所述根据所述抽象语法树生成逻辑执行计划,所述逻辑执行计划包括插入数据计划、修改数据计划和查询数据计划包括:The blockchain data storage and query device according to claim 9, wherein said generating a logical execution plan according to said abstract syntax tree, said logical execution plan including inserting data plan, modifying data plan and querying data plan comprises:
    通过访问者模式遍历所述抽象语法树,得到所述目标SQL语句的元素;Traverse the abstract syntax tree through the visitor mode to obtain the elements of the target SQL statement;
    根据所述目标SQL语句的元素生成逻辑执行计划,所述逻辑执行计划包括插入数据计划、修改数据计划和查询数据计划。A logical execution plan is generated according to the elements of the target SQL statement, and the logical execution plan includes an insert data plan, a modified data plan, and a query data plan.
  11. 根据权利要求10所述的区块链数据存储和查询设备,其中,所述根据所述逻辑生成计划和所述索引数据库对区块链分布式数据库进行读写操作,得到读写结果,所述区块链分布式数据库用于存储和查询数据包括:The blockchain data storage and query device according to claim 10, wherein the read and write operations on the blockchain distributed database are performed according to the logic generation plan and the index database to obtain a read and write result, the Blockchain distributed database is used to store and query data including:
    判断所述逻辑执行计划是否为所述插入数据计划、所述修改数据计划或者所述查询数据计划;Judging whether the logical execution plan is the inserted data plan, the modified data plan, or the query data plan;
    若所述逻辑执行计划为所述插入数据计划,则对所述索引数据库和所述区块链分布式数据库进行插入操作,得到所述读写结果;If the logical execution plan is the insert data plan, perform an insert operation on the index database and the blockchain distributed database to obtain the read and write result;
    若所述逻辑执行计划为所述修改数据计划,则对所述索引数据库和所述区块链分布式数据库进行修改操作,得到所述读写结果;If the logical execution plan is the modified data plan, modify the index database and the blockchain distributed database to obtain the read and write results;
    若所述逻辑执行计划为所述查询数据计划,则对所述索引数据库和所述区块链分布式数据库进行查询操作,得到所述读写结果。If the logical execution plan is the query data plan, a query operation is performed on the index database and the blockchain distributed database to obtain the read and write results.
  12. 根据权利要求11所述的区块链数据存储和查询设备,其中,所述若所述逻辑执行计划为所述插入数据计划,则对所述索引数据库和所述区块链分布式数据库进行插入操作,得到所述读写结果包括:The blockchain data storage and query device according to claim 11, wherein, if the logical execution plan is the inserted data plan, insert the index database and the blockchain distributed database The operation to obtain the read and write result includes:
    生成第一索引标识;Generate the first index identifier;
    根据所述插入数据计划和所述第一索引标识生成SQL插入语句;Generating an SQL insert statement according to the insert data plan and the first index identifier;
    根据所述SQL插入语句对所述索引数据库进行插入操作,得到所述索引数据库返回的主键值;Performing an insert operation on the index database according to the SQL insert statement to obtain the primary key value returned by the index database;
    根据所述主键值查询所述索引数据库,得到所述索引数据库返回的插入数据;Query the index database according to the primary key value to obtain the inserted data returned by the index database;
    根据所述第一索引标识和所述插入数据对区块链分布式数据库进行插入 操作,得到插入结果,将所述插入结果设置为所述读写结果。Perform an insert operation on the blockchain distributed database according to the first index identifier and the insert data to obtain an insert result, and set the insert result as the read and write result.
  13. 根据权利要求11所述的区块链数据存储和查询设备,其中,所述若所述逻辑执行计划为所述修改数据计划,则对所述索引数据库和所述区块链分布式数据库进行修改操作,得到所述读写结果包括:The blockchain data storage and query device according to claim 11, wherein if the logical execution plan is the modified data plan, the index database and the blockchain distributed database are modified The operation to obtain the read and write result includes:
    根据所述修改数据计划生成SQL修改语句;Generate SQL modification statements according to the modification data plan;
    根据所述SQL修改语句更新所述索引数据库,得到第二索引标识;Update the index database according to the SQL modification statement to obtain a second index identifier;
    根据所述第二索引标识查询所述区块链分布式数据库,得到区块链初始数据;Query the blockchain distributed database according to the second index identifier to obtain initial blockchain data;
    对所述区块链初始数据进行修改,得到区块链目标数据;Modify the initial data of the blockchain to obtain the target data of the blockchain;
    根据所述第二索引标识将所述区块链目标数据写入所述区块链分布式数据库,得到修改结果,将所述修改结果设置为所述读写结果。Write the block chain target data to the block chain distributed database according to the second index identifier, obtain a modification result, and set the modification result as the read and write result.
  14. 根据权利要求11所述的区块链数据存储和查询设备,其中,所述若所述逻辑执行计划为所述查询数据计划,则对所述索引数据库和所述区块链分布式数据库进行查询操作,得到所述操作结果包括:The blockchain data storage and query device according to claim 11, wherein, if the logical execution plan is the query data plan, query the index database and the blockchain distributed database The operation to obtain the operation result includes:
    根据所述查询数据计划生成SQL查询语句;Generating SQL query statements according to the query data plan;
    根据所述SQL查询语句查询所述索引数据库,得到第三索引标识;Query the index database according to the SQL query statement to obtain a third index identifier;
    根据所述第三索引标识读取所述区块链分布式数据库,得到查询结果,将所述查询结果设置为所述读写结果。Read the blockchain distributed database according to the third index identifier to obtain a query result, and set the query result as the read-write result.
  15. 根据权利要求9-14任一项所述的区块链数据存储和查询设备,其中,所述方法还包括:The blockchain data storage and query device according to any one of claims 9-14, wherein the method further comprises:
    发送第一消息给消息队列,得到事务标识;Send the first message to the message queue to obtain the transaction identifier;
    根据所述事务标识对所述索引数据库进行写入操作,得到写入结果;Perform a write operation on the index database according to the transaction identifier to obtain a write result;
    判断所述写入结果是否为写入成功;Judging whether the writing result is successful;
    若所述写入结果为写入成功,则通过所述消息队列对所述区块链分布式数据库进行写入操作;If the writing result is that the writing is successful, perform a writing operation on the blockchain distributed database through the message queue;
    若所述写入结果为写入失败,则通过所述消息队列对所述索引数据库进行删除操作。If the writing result is a writing failure, the index database is deleted through the message queue.
  16. 一种计算机可读存储介质,其上存储有计算机程序,其中:所述计算机程序被处理器执行时实现如下步骤:A computer-readable storage medium having a computer program stored thereon, wherein: the computer program is executed by a processor to implement the following steps:
    生成索引数据库;Generate index database;
    获取目标结构化查询语言SQL语句,所述目标SQL语句为符合SQL语法规则的合法语句;Acquiring a target structured query language SQL statement, where the target SQL statement is a legal statement that complies with SQL grammar rules;
    根据预置算法对所述目标SQL语句进行解析,得到抽象语法树;Parse the target SQL statement according to a preset algorithm to obtain an abstract syntax tree;
    根据所述抽象语法树生成逻辑执行计划,所述逻辑执行计划包括插入数据计划、修改数据计划和查询数据计划;Generating a logical execution plan according to the abstract syntax tree, the logical execution plan including inserting a data plan, modifying a data plan, and querying a data plan;
    根据所述逻辑生成计划和所述索引数据库对区块链分布式数据库进行读写操作,得到读写结果,所述区块链分布式数据库用于存储和查询数据。Perform read and write operations on the blockchain distributed database according to the logic generation plan and the index database to obtain read and write results. The blockchain distributed database is used to store and query data.
  17. 根据权利要求16所述的计算机可读存储介质,其中,所述根据所述抽象语法树生成逻辑执行计划,所述逻辑执行计划包括插入数据计划、修改 数据计划和查询数据计划包括:The computer-readable storage medium according to claim 16, wherein the generating a logical execution plan according to the abstract syntax tree, the logical execution plan including inserting a data plan, modifying a data plan, and querying a data plan comprises:
    通过访问者模式遍历所述抽象语法树,得到所述目标SQL语句的元素;Traverse the abstract syntax tree through the visitor mode to obtain the elements of the target SQL statement;
    根据所述目标SQL语句的元素生成逻辑执行计划,所述逻辑执行计划包括插入数据计划、修改数据计划和查询数据计划。A logical execution plan is generated according to the elements of the target SQL statement, and the logical execution plan includes an insert data plan, a modified data plan, and a query data plan.
  18. 根据权利要求17所述的计算机可读存储介质,其中,所述根据所述逻辑生成计划和所述索引数据库对区块链分布式数据库进行读写操作,得到读写结果,所述区块链分布式数据库用于存储和查询数据包括:The computer-readable storage medium according to claim 17, wherein the read and write operations are performed on the blockchain distributed database according to the logic generation plan and the index database to obtain a read and write result, and the blockchain The distributed database used to store and query data includes:
    判断所述逻辑执行计划是否为所述插入数据计划、所述修改数据计划或者所述查询数据计划;Judging whether the logical execution plan is the inserted data plan, the modified data plan, or the query data plan;
    若所述逻辑执行计划为所述插入数据计划,则对所述索引数据库和所述区块链分布式数据库进行插入操作,得到所述读写结果;If the logical execution plan is the insert data plan, perform an insert operation on the index database and the blockchain distributed database to obtain the read and write result;
    若所述逻辑执行计划为所述修改数据计划,则对所述索引数据库和所述区块链分布式数据库进行修改操作,得到所述读写结果;If the logical execution plan is the modified data plan, modify the index database and the blockchain distributed database to obtain the read and write results;
    若所述逻辑执行计划为所述查询数据计划,则对所述索引数据库和所述区块链分布式数据库进行查询操作,得到所述读写结果。If the logical execution plan is the query data plan, a query operation is performed on the index database and the blockchain distributed database to obtain the read and write results.
  19. 根据权利要求18所述的计算机可读存储介质,其中,所述若所述逻辑执行计划为所述插入数据计划,则对所述索引数据库和所述区块链分布式数据库进行插入操作,得到所述读写结果包括:The computer-readable storage medium according to claim 18, wherein, if the logical execution plan is the insert data plan, insert operations are performed on the index database and the blockchain distributed database to obtain The read and write results include:
    生成第一索引标识;Generate the first index identifier;
    根据所述插入数据计划和所述第一索引标识生成SQL插入语句;Generating an SQL insert statement according to the insert data plan and the first index identifier;
    根据所述SQL插入语句对所述索引数据库进行插入操作,得到所述索引数据库返回的主键值;Performing an insert operation on the index database according to the SQL insert statement to obtain the primary key value returned by the index database;
    根据所述主键值查询所述索引数据库,得到所述索引数据库返回的插入数据;Query the index database according to the primary key value to obtain the inserted data returned by the index database;
    根据所述第一索引标识和所述插入数据对区块链分布式数据库进行插入操作,得到插入结果,将所述插入结果设置为所述读写结果。Perform an insert operation on the blockchain distributed database according to the first index identifier and the insert data to obtain an insert result, and set the insert result as the read and write result.
  20. 根据权利要求18所述的计算机可读存储介质,其中,所述若所述逻辑执行计划为所述修改数据计划,则对所述索引数据库和所述区块链分布式数据库进行修改操作,得到所述读写结果包括:The computer-readable storage medium according to claim 18, wherein if the logical execution plan is the modified data plan, a modification operation is performed on the index database and the blockchain distributed database to obtain The read and write results include:
    根据所述修改数据计划生成SQL修改语句;Generate SQL modification statements according to the modification data plan;
    根据所述SQL修改语句更新所述索引数据库,得到第二索引标识;Update the index database according to the SQL modification statement to obtain a second index identifier;
    根据所述第二索引标识查询所述区块链分布式数据库,得到区块链初始数据;Query the blockchain distributed database according to the second index identifier to obtain initial blockchain data;
    对所述区块链初始数据进行修改,得到区块链目标数据;Modify the initial data of the blockchain to obtain the target data of the blockchain;
    根据所述第二索引标识将所述区块链目标数据写入所述区块链分布式数据库,得到修改结果,将所述修改结果设置为所述读写结果。Write the block chain target data to the block chain distributed database according to the second index identifier, obtain a modification result, and set the modification result as the read and write result.
PCT/CN2020/087585 2019-05-22 2020-04-28 Blockchain data storage and query method, apparatus and device, and storage medium WO2020233367A1 (en)

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN201910427137.8A CN110309196A (en) 2019-05-22 2019-05-22 Block chain data storage and query method, apparatus, equipment and storage medium
CN201910427137.8 2019-05-22

Publications (1)

Publication Number Publication Date
WO2020233367A1 true WO2020233367A1 (en) 2020-11-26

Family

ID=68074933

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2020/087585 WO2020233367A1 (en) 2019-05-22 2020-04-28 Blockchain data storage and query method, apparatus and device, and storage medium

Country Status (2)

Country Link
CN (1) CN110309196A (en)
WO (1) WO2020233367A1 (en)

Cited By (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN112527794A (en) * 2020-12-07 2021-03-19 广州海量数据库技术有限公司 Data processing method and system for realizing set data types in database
CN113312373A (en) * 2021-05-12 2021-08-27 上海哲锦信息科技有限公司 Method and equipment for analyzing data structured query statement
CN113688148A (en) * 2021-07-13 2021-11-23 交控科技股份有限公司 Urban rail data query method and device, electronic equipment and readable storage medium
CN114138899A (en) * 2021-11-22 2022-03-04 支付宝(杭州)信息技术有限公司 Block chain-based data stream transfer method and device
CN117055895A (en) * 2023-08-07 2023-11-14 北京东方金信科技股份有限公司 Method, system and readable medium for converting SQL sentence into HTTP interface

Families Citing this family (31)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN110309196A (en) * 2019-05-22 2019-10-08 深圳壹账通智能科技有限公司 Block chain data storage and query method, apparatus, equipment and storage medium
CN111046066B (en) * 2019-12-09 2023-05-12 上海达梦数据库有限公司 Remote database object optimization method, device, equipment and storage medium
CN111177182B (en) * 2019-12-19 2023-06-30 达闼机器人股份有限公司 Data query method and device, storage medium and electronic equipment
CN111309765B (en) * 2020-01-22 2023-07-04 北京飞漫软件技术有限公司 Method and device for directly inquiring structured data
CN111414438B (en) * 2020-03-13 2023-06-02 上海银赛计算机科技有限公司 Real-time rule matching method and system based on mysql
CN111400297B (en) * 2020-03-19 2023-11-03 上海德拓信息技术股份有限公司 Hadoop-based mass data quality verification method
CN111310137B (en) * 2020-03-23 2022-08-26 杭州溪塔科技有限公司 Block chain associated data evidence storing method and device and electronic equipment
CN111680067A (en) * 2020-05-28 2020-09-18 中国工商银行股份有限公司 Data processing method, device and system based on block chain
CN111475588B (en) * 2020-06-19 2020-12-08 阿里云计算有限公司 Data processing method and device
CN111767301B (en) * 2020-06-24 2022-07-15 江苏荣泽信息科技股份有限公司 Relational database solution method based on block chain
CN112052255B (en) * 2020-09-02 2022-05-03 福建天晴在线互动科技有限公司 SQL (structured query language) interpretation method and device for splitting multi-table slow query from top to bottom
CN112069201A (en) * 2020-09-04 2020-12-11 北京百度网讯科技有限公司 Target data acquisition method and device
CN113297306B (en) * 2020-09-30 2023-02-07 阿里云计算有限公司 Data processing method and device
CN112487027B (en) * 2020-12-02 2022-08-23 山东浪潮科学研究院有限公司 Efficient data query implementation method based on block chain electronic transaction
CN112506946B (en) * 2020-12-03 2024-04-05 平安科技(深圳)有限公司 Service data query method, device, equipment and storage medium
CN112579610A (en) * 2020-12-23 2021-03-30 安徽航天信息有限公司 Multi-data source structure analysis method, system, terminal device and storage medium
CN112667489B (en) * 2020-12-28 2022-09-16 杭州趣链科技有限公司 Method and device for measuring data query workload in block chain and terminal
WO2022140936A1 (en) * 2020-12-28 2022-07-07 杭州趣链科技有限公司 Metering method and device for data query workload in blockchain, and terminal
CN112711592B (en) * 2020-12-31 2022-06-28 中国科学院计算技术研究所数字经济产业研究院 Data indexing method based on block chain
CN112817990B (en) * 2021-01-28 2024-03-08 北京百度网讯科技有限公司 Data processing method, device, electronic equipment and readable storage medium
CN113204571B (en) * 2021-04-23 2022-08-30 新华三大数据技术有限公司 SQL execution method and device related to write-in operation and storage medium
CN113139019B (en) * 2021-06-18 2022-03-25 智己汽车科技有限公司 Method and device for recording mileage data of vehicle on block chain
CN113535777B (en) * 2021-06-24 2023-05-23 上海浦东发展银行股份有限公司 Database query method, device and system
CN113434503A (en) * 2021-06-25 2021-09-24 上海佩俪信息科技有限公司 Block chain browsing method and system
CN113626423B (en) * 2021-06-29 2024-01-30 欧电云信息科技(江苏)有限公司 Log management method, device and system of business database
CN113687827B (en) * 2021-08-24 2024-03-12 平安国际智慧城市科技股份有限公司 Data list generation method, device and equipment based on widget and storage medium
CN114238469B (en) * 2021-12-07 2022-07-12 杭州天均数聚科技有限公司 Data extraction interface opening method and device, computer equipment and storage medium
CN114880351B (en) * 2022-05-31 2024-02-06 中国电信股份有限公司 Recognition method and device of slow query statement, storage medium and electronic equipment
CN115421698A (en) * 2022-08-30 2022-12-02 敏于行(北京)科技有限公司 Data processing method and device based on declarative and distributed accounts book and electronic device
CN116089476B (en) * 2023-04-07 2023-07-04 北京宝兰德软件股份有限公司 Data query method and device and electronic equipment
CN116975126B (en) * 2023-09-25 2023-12-19 恒生电子股份有限公司 Data query method, device, computing equipment and computer readable storage medium

Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US6325780B1 (en) * 1999-09-13 2001-12-04 Advanced Cardiovascular Systems, Inc. Inflatable member formed of liquid crystal polymeric material blend
CN109241104A (en) * 2018-10-12 2019-01-18 北京聚云位智信息科技有限公司 The resolver and its implementation of AISQL in decision type distributed data base system
CN109492053A (en) * 2018-11-08 2019-03-19 北京百度网讯科技有限公司 Method and apparatus for accessing data
CN109559231A (en) * 2018-12-29 2019-04-02 华东师范大学 A kind of retrospect querying method towards block chain
CN110309196A (en) * 2019-05-22 2019-10-08 深圳壹账通智能科技有限公司 Block chain data storage and query method, apparatus, equipment and storage medium

Family Cites Families (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN105302842A (en) * 2014-07-31 2016-02-03 北大方正集团有限公司 Data processing method and device
CN105608086B (en) * 2014-11-17 2021-07-27 中兴通讯股份有限公司 Transaction processing method and device for distributed database system
US20170228371A1 (en) * 2016-02-05 2017-08-10 Manifold Technology, Inc. Blockchain-enhanced database
CN108563694B (en) * 2018-03-19 2021-04-13 广州视源电子科技股份有限公司 SQL execution method and device for logic deletion, computer equipment and storage medium
CN108647361B (en) * 2018-05-21 2021-12-03 中国工商银行股份有限公司 Data storage method, device and system based on block chain
CN109522008B (en) * 2018-11-06 2020-07-10 陕西医链区块链集团有限公司 Intelligent contract construction method for block chain
CN109741192A (en) * 2018-11-15 2019-05-10 深圳壹账通智能科技有限公司 Date storage method, device, equipment and readable storage medium storing program for executing based on block chain
CN109784918A (en) * 2018-12-15 2019-05-21 深圳壹账通智能科技有限公司 Information measure of supervision, device, equipment and storage medium based on block chain

Patent Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US6325780B1 (en) * 1999-09-13 2001-12-04 Advanced Cardiovascular Systems, Inc. Inflatable member formed of liquid crystal polymeric material blend
CN109241104A (en) * 2018-10-12 2019-01-18 北京聚云位智信息科技有限公司 The resolver and its implementation of AISQL in decision type distributed data base system
CN109492053A (en) * 2018-11-08 2019-03-19 北京百度网讯科技有限公司 Method and apparatus for accessing data
CN109559231A (en) * 2018-12-29 2019-04-02 华东师范大学 A kind of retrospect querying method towards block chain
CN110309196A (en) * 2019-05-22 2019-10-08 深圳壹账通智能科技有限公司 Block chain data storage and query method, apparatus, equipment and storage medium

Cited By (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN112527794A (en) * 2020-12-07 2021-03-19 广州海量数据库技术有限公司 Data processing method and system for realizing set data types in database
CN113312373A (en) * 2021-05-12 2021-08-27 上海哲锦信息科技有限公司 Method and equipment for analyzing data structured query statement
CN113688148A (en) * 2021-07-13 2021-11-23 交控科技股份有限公司 Urban rail data query method and device, electronic equipment and readable storage medium
CN114138899A (en) * 2021-11-22 2022-03-04 支付宝(杭州)信息技术有限公司 Block chain-based data stream transfer method and device
CN117055895A (en) * 2023-08-07 2023-11-14 北京东方金信科技股份有限公司 Method, system and readable medium for converting SQL sentence into HTTP interface
CN117055895B (en) * 2023-08-07 2024-03-29 北京东方金信科技股份有限公司 Method, system and readable medium for converting SQL sentence into HTTP interface

Also Published As

Publication number Publication date
CN110309196A (en) 2019-10-08

Similar Documents

Publication Publication Date Title
WO2020233367A1 (en) Blockchain data storage and query method, apparatus and device, and storage medium
US11681702B2 (en) Conversion of model views into relational models
US8065323B2 (en) Offline validation of data in a database system for foreign key constraints
US11475034B2 (en) Schemaless to relational representation conversion
US8782081B2 (en) Query template definition and transformation
US7822710B1 (en) System and method for data collection
CN103168300B (en) There is the incremental maintenance of the materialized view immediately of outer connection
WO2018177060A1 (en) Query optimization method and related device
CN109614413B (en) Memory flow type computing platform system
WO2021139426A1 (en) Method, device and apparatus for querying data in database, and storage medium
CN111177788A (en) Hive dynamic desensitization method and dynamic desensitization system
US20090144252A1 (en) System and method for translating and executing update requests
US20200265028A1 (en) Method and systems for mapping object oriented/functional languages to database languages
US20160034582A1 (en) Computing device and method for executing database operation command
US20210209098A1 (en) Converting database language statements between dialects
CN110990425B (en) JDBC driving method and device of hardware database
CN112235311B (en) OVSDB client code automatic generation method, system, device and medium
WO2019015364A1 (en) Method and device for executing structured query language (sql) instruction
CN113297250A (en) Method and system for multi-table association query of distributed database
CN107077496A (en) For indexing system, the method and apparatus that compiling is used with optimum indexing
WO2022057357A1 (en) Data query method and apparatus, and database system
CN113204571B (en) SQL execution method and device related to write-in operation and storage medium
CN105335482B (en) Towards the batch insertion method of magnanimity distributed data base
WO2016177027A1 (en) Batch data query method and device
KR20190005578A (en) Systemt and method of managing distributed database based on inmemory

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 20810779

Country of ref document: EP

Kind code of ref document: A1

NENP Non-entry into the national phase

Ref country code: DE

122 Ep: pct application non-entry in european phase

Ref document number: 20810779

Country of ref document: EP

Kind code of ref document: A1

32PN Ep: public notification in the ep bulletin as address of the adressee cannot be established

Free format text: NOTING OF LOSS OF RIGHTS PURSUANT TO RULE 112(1) EPC (EPO FORM 1205 DATED 18/03/2022)

122 Ep: pct application non-entry in european phase

Ref document number: 20810779

Country of ref document: EP

Kind code of ref document: A1