A method and device for querying data in a blockchain

By setting index relationships in the blockchain and using hash algorithm to determine the candidate primary key set, the problem of low efficiency in blockchain data query is solved, and more efficient data query is achieved.

CN114281825BActive Publication Date: 2025-06-27WEBANK (CHINA)
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202111611469.5
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-12-27
Publication Date
2025-06-27
Estimated Expiration
2041-12-27

AI Technical Summary

Technical Problem

In the existing technology, blockchain data query efficiency is low and cannot meet the financial industry's demand for real-time and efficientness.

Method used

By setting the index relationship of data records in the blockchain, using a preset hash algorithm to obtain the index value set, and determining the candidate primary key set based on the index value set and constraint relationship, thereby narrowing the query scope and improving the efficiency of data query.

Benefits of technology

It significantly improves the efficiency of data query in blockchain, and can obtain data records that meet the query conditions more quickly, meeting the financial industry's demand for efficient query.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114281825B_ABST
    Figure CN114281825B_ABST
Patent Text Reader

Abstract

The present invention discloses a method and apparatus for querying data in a blockchain. The method is as follows: according to query conditions, an index value set is obtained according to a preset hash algorithm; the query conditions include attribute information and constraint relationships between the attribute information; index relationships are set for data records in the blockchain, and the index relationships include the corresponding relationships between the attribute information in the data records and the index values, and the corresponding relationships between the primary keys in the data records and the index values; the first index value is obtained by the first attribute information according to the preset hash algorithm, so that a candidate primary key set can be determined according to the index value set and the constraint relationships, thereby narrowing the query range. Further, according to the query conditions and the candidate primary key set, query data is obtained, improving the query efficiency of data in the blockchain. The above method can be applied to financial technology (Fintech).
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of blockchain in the field of financial technology (Fintech), and particularly to a method and device for querying data of a blockchain. Background Art

[0002] With the development of computer technology, more and more technologies are applied in the financial field, and traditional finance is gradually transforming into financial technology (Fintech). However, due to the security and real-time requirements of the financial industry, higher requirements are also put forward for technologies. Currently, due to the immutability of the blockchain, transactions are often carried out through the blockchain in the field of financial technology.

[0003] The blockchain generally stores data in the form of key-value pairs. Currently, querying data in the blockchain can only be performed item by item based on the primary key, resulting in low efficiency of data query in the blockchain. This is an urgent problem to be solved. Summary of the Invention

[0004] The present invention provides a method and device for querying data of a blockchain, which solves the problem of low efficiency of data query in the existing blockchain technology.

[0005] In a first aspect, the present invention provides a method for querying data of a blockchain, including:

[0006] According to the query condition, obtain a set of index values according to a preset hash algorithm; the query condition includes attribute information and the constraint relationship between the attribute information, and the set of index values includes index values; an index relationship is set for the data records in the blockchain, and the index relationship includes the corresponding relationship between the attribute information in the data records and the index values; wherein, the first index value is obtained by the first attribute information according to the preset hash algorithm, the first index value is any index value in the index relationship, and the first attribute information is any attribute information corresponding to the first index value;

[0007] According to the set of index values and the constraint relationship, determine a set of candidate primary keys; the index relationship further includes the corresponding relationship between the primary keys in the data records and the index values, wherein the first primary key in the set of candidate primary keys is any primary key corresponding to the first index value;

[0008] According to the query condition and the set of candidate primary keys, obtain the query data.

[0009] In the above manner, since the index relationship of data records is set in the blockchain, an index value set can be obtained. According to the index value set and the constraint relationship, a candidate primary key set corresponding to the query condition is first determined, thereby narrowing the query range. Further, according to the query condition and the candidate primary key set, query data can be obtained, improving the query efficiency of data in the blockchain.

[0010] Optionally, determining the query data according to the query condition and the candidate primary key set includes:

[0011] For a second primary key, where the second primary key is any primary key in the candidate primary key set, according to the second primary key and the associated information corresponding to the second primary key based on the query condition, the associated hash value of the second primary key is determined according to the preset hash algorithm; the second primary key is the primary key corresponding to the second index value in the index value set;

[0012] If the associated hash value is located in the associated hash array of the second index value, it is determined that the second primary key meets the query condition; the hash values in the associated hash array are obtained according to the primary key corresponding to the second index value and the associated information of this primary key;

[0013] The data records corresponding to all the primary keys that meet the query condition are used as the query data.

[0014] In the above method, the associated hash value of the second primary key can be determined according to the preset hash algorithm. Since the hash values in the associated hash array are obtained according to the primary key corresponding to the second index value and the associated information of this primary key, then in the case where the associated hash value is located in the associated hash array of the second index value, it can be determined that the second primary key is indeed due to the relationship between the associated information corresponding to the query condition and the second index value, so that the data records that meet the query condition can be confirmed.

[0015] Optionally, the attribute information is divided into tag - type attribute information and non - tag - type attribute information, and the collision probability of obtaining an index value according to the preset hash algorithm for the tag - type attribute information is less than the collision probability of obtaining an index value according to the preset hash algorithm for the non - tag - type attribute information;

[0016] For the second attribute information in the query condition, if the second attribute information is tag - type attribute information, the associated information corresponding to the second primary key based on the query condition is specifically the second attribute information corresponding to the second index value in the query condition; or,

[0017] If the second attribute information is non-label attribute information, the index value set further includes a secondary index value of the non-label attribute information, and the second primary key is specifically the secondary index value corresponding to the second index value in the index value set based on the association information corresponding to the query condition.

[0018] In the above method, since the collision probability of the index value obtained by the label attribute information according to the preset hash algorithm is smaller than that of the non-label attribute information, the collision probability of directly setting the attribute information as the association information for the label attribute information is also smaller. For the non-label attribute information, setting the secondary index value as the association information can reduce its collision probability, thereby reducing the overall collision probability of the index value.

[0019] Optionally, before obtaining the index value set according to the query condition and according to the preset hash algorithm, it further includes:

[0020] For the third attribute information in any data record in the blockchain, calculate the third attribute information according to the preset hash algorithm to obtain the third index value corresponding to the third attribute information; if the third index value does not exist in the index relationship, add the third index value to the index relationship;

[0021] According to the third primary key in the data record and the association information corresponding to the third attribute information, calculate according to the preset hash algorithm to obtain the association hash value of the third attribute information;

[0022] Store the association hash value of the third attribute information into the association hash array of the third index value in the index relationship; according to the third primary key, set the value corresponding to the third primary key in the primary key array of the third index value in the index relationship.

[0023] In the above manner, by adding the index value to the index relationship, adding the association hash value to the association hash array, and setting the value corresponding to the third primary key in the primary key array of the third index value in the index relationship, the corresponding relationship between the index value, the primary key, and the attribute value is indicated, providing a basis for query.

[0024] Optionally, if the third attribute information is label attribute information, the association information corresponding to the third attribute information is the third attribute information; or,

[0025] If the third attribute information is non-label attribute information, the method further includes:

[0026] Calculate the third attribute information according to the preset hash algorithm to obtain the secondary index value of the third index value; the association information corresponding to the third attribute information is the secondary index value.

[0027] Optionally, determining the candidate primary key set according to the index value set and the constraint relationship includes:

[0028] Determine the primary key array corresponding to each index value in the index value set, where the values of the elements in the primary key array are the first preset value or the second preset value. The first preset value indicates that there is a corresponding relationship between the primary key and the index value, and the second preset value indicates that there is no corresponding relationship between the primary key and the index value;

[0029] Perform operations according to the constraint relationship based on the values of the elements in the primary key array corresponding to each index value in the index value set to obtain a combined primary key array;

[0030] Determine the candidate primary key set according to the values of the elements in the combined primary key array.

[0031] In the above manner, by performing operations on the primary key array corresponding to the index value according to the constraint relationship, a combined primary key array can be obtained through simple operations, which is used to indicate the candidate primary key set, so that the candidate primary key set can be determined more efficiently.

[0032] Optionally, the blockchain provides a smart contract, and the method is executed by a node of the blockchain by invoking the smart contract.

[0033] In the above manner, by a node of the blockchain executing the above data query method by invoking the smart contract, reliability is provided for the data query method.

[0034] In a second aspect, the present invention provides a data query device for a blockchain, including:

[0035] A processing module, configured to obtain an index value set according to a query condition according to a preset hash algorithm; the query condition includes attribute information and a constraint relationship between attribute information, and the index value set includes index values; an index relationship is set for data records in the blockchain, and the index relationship includes a corresponding relationship between attribute information in the data records and index values; wherein, a first index value is obtained by the first attribute information according to the preset hash algorithm, the first index value is any index value in the index relationship, and the first attribute information is any attribute information corresponding to the first index value; and

[0036] configured to determine a candidate primary key set according to the index value set and the constraint relationship; the index relationship further includes a corresponding relationship between a primary key in the data record and an index value, wherein a first primary key in the candidate primary key set is any primary key corresponding to the first index value;

[0037] A query module, configured to obtain query data according to the query condition and the candidate primary key set.

[0038] Optionally, the query module is specifically configured to:

[0039] For the second primary key, where the second primary key is any primary key in the candidate primary key set, based on the second primary key and the association information corresponding to the second primary key based on the query condition, determine the associated hash value of the second primary key according to the preset hash algorithm; the second primary key is the primary key corresponding to the second index value in the index value set;

[0040] If the associated hash value is located in the associated hash array of the second index value, it is determined that the second primary key meets the query condition; the hash values in the associated hash array are obtained based on the primary key corresponding to the second index value and the association information of this primary key;

[0041] Use the data records corresponding to all the primary keys that meet the query condition as the query data.

[0042] Optionally, the attribute information is divided into tag - type attribute information and non - tag - type attribute information, and the collision probability of the index value obtained by the tag - type attribute information according to the preset hash algorithm is less than the collision probability of the index value obtained by the non - tag - type attribute information according to the preset hash algorithm;

[0043] For the second attribute information in the query condition, if the second attribute information is tag - type attribute information, the association information corresponding to the second primary key based on the query condition is specifically the second attribute information corresponding to the second index value in the query condition; or,

[0044] If the second attribute information is non - tag - type attribute information, the index value set further includes a secondary index value of this non - tag - type attribute information, and the association information corresponding to the second primary key based on the query condition is specifically the secondary index value corresponding to the second index value in the index value set.

[0045] Optionally, the processing module is further configured to:

[0046] For the third attribute information in any data record in the blockchain, perform an operation on the third attribute information according to the preset hash algorithm to obtain a third index value corresponding to the third attribute information; if the third index value does not exist in the index relationship, add the third index value to the index relationship;

[0047] According to the third primary key in this data record and the association information corresponding to the third attribute information, perform an operation according to the preset hash algorithm to obtain the associated hash value of the third attribute information;

[0048] Store the associated hash value of the third attribute information into the associated hash array of the third index value in the index relationship; according to the third primary key, set the value corresponding to the third primary key in the primary key array of the third index value in the index relationship.

[0049] Optionally, if the third attribute information is tag - type attribute information, the associated information corresponding to the third attribute information is the third attribute information; or,

[0050] If the third attribute information is non - tag - type attribute information, the processing module is further configured to:

[0051] Perform an operation on the third attribute information according to the preset hash algorithm to obtain a secondary index value of the third index value; the associated information corresponding to the third attribute information is the secondary index value.

[0052] Optionally, the query module is specifically configured to:

[0053] Determine the primary key array corresponding to each index value in the index value set, where the value of an element in the primary key array is a first preset value or a second preset value, the first preset value indicates that there is a corresponding relationship between the primary key and the index value, and the second preset value indicates that there is no corresponding relationship between the primary key and the index value;

[0054] Perform an operation according to the constraint relationship based on the values of the elements in the primary key array corresponding to each index value in the index value set to obtain a combined primary key array.

[0055] Optionally, the blockchain provides a smart contract, the device is a node of the blockchain, and the device executes the data query method of the blockchain by invoking the smart contract.

[0056] For the beneficial effects of the second aspect and each optional device of the second aspect, reference can be made to the beneficial effects of the first aspect and each optional method of the first aspect, which will not be elaborated here.

[0057] In a third aspect, the present invention provides a computer device, including a program or instruction, which when executed, is used to execute the first aspect and each optional method of the first aspect.

[0058] In a fourth aspect, the present invention provides a storage medium, including a program or instruction, which when executed, is used to execute the first aspect and each optional method of the first aspect.

[0059] These aspects or other aspects of the present invention will be more clearly understood in the following description of the embodiments. Description of the Drawings

[0060] To more clearly illustrate the technical solutions in the embodiments of the present invention, the following briefly introduces the accompanying drawings required for the description of the embodiments. Obviously, the accompanying drawings in the following description are only some embodiments of the present invention. For those of ordinary skill in the art, without creative efforts, other accompanying drawings can be obtained based on these drawings.

[0061] Figure 1 A schematic diagram of an architecture available to a data query party of a blockchain provided by an embodiment of the present invention;

[0062] Figure 2 A schematic diagram of the structure of the smart contract layer in an architecture available to a data query party of a blockchain provided by an embodiment of the present invention;

[0063] Figure 3 A schematic flowchart corresponding to a data query method of a blockchain provided by an embodiment of the present invention;

[0064] Figure 4 A schematic diagram of generating an index column in a data query method of a blockchain provided by an embodiment of the present invention;

[0065] Figure 5 A schematic diagram of the index column and the primary key array of each index value in a data query method of a blockchain provided by an embodiment of the present invention;

[0066] Figure 6 A schematic illustration of the correspondence between the index value and the primary key array in a data query method of a blockchain provided by an embodiment of the present invention Figure 1 ;

[0067] Figure 7 A schematic illustration of the correspondence between the index value and the primary key array in a data query method of a blockchain provided by an embodiment of the present invention Figure 2 ;

[0068] Figure 8 A schematic storage diagram of the associated hash value of the tag class attribute information in a data query method of a blockchain provided by an embodiment of the present invention;

[0069] Figure 9 A schematic storage diagram of the associated hash value of the non - tag class attribute information in a data query method of a blockchain provided by an embodiment of the present invention;

[0070] Figure 10 A schematic storage diagram of the primary key array and the associated hash array of the index value in a data query method of a blockchain provided by an embodiment of the present invention;

[0071] Figure 11 A schematic illustration of the matching of the associated hash value of the index value in a data query method of a blockchain provided by an embodiment of the present inventionFigure 1 ;

[0072] Figure 12 Schematic diagram of matching the associated hash value of the index value in a data query method for a blockchain provided by an embodiment of the present invention Figure 2 ;

[0073] Figure 13 Schematic structural diagram of a data query device for a blockchain provided by an embodiment of the present invention. Detailed implementation manners

[0074] In order to make the objectives, technical solutions and advantages of the present invention clearer, the present invention will be further described in detail below with reference to the accompanying drawings. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all of the embodiments. All other embodiments obtained by those of ordinary skill in the art based on the embodiments of the present invention without creative efforts shall fall within the protection scope of the present invention.

[0075] During the operation of financial institutions (banking institutions, insurance institutions or securities institutions) in their business operations (such as loan business, deposit business, etc. of banks), due to the immutability of the blockchain, transactions are often carried out through the blockchain in the field of fintech. Currently, querying data in the blockchain can only be performed on a data-by-data basis based on the primary key, resulting in low data query efficiency in the blockchain. This situation does not meet the requirements of financial institutions such as banks and cannot ensure the efficient operation of various businesses of financial institutions.

[0076] For this reason, the present application provides a data query method for a blockchain. As Figure 1 shown, it is the architecture to which this method can be applied. This architecture includes a storage layer, a smart contract layer, and a business application.

[0077] Data is written into the blockchain or data is queried from the blockchain through the business application of the blockchain. Among them, the storage layer stores multiple data records, and any data record includes a primary key and data. An index relationship (which can also be called index data) is constructed through the non-primary key index module in the smart contract layer of the blockchain, and the index and the data record are written onto the blockchain. This index relationship is different from the primary key index in a relational database, so it can also be called a non-primary key index. Nodes on the blockchain can reach a consensus, enabling the local business applications of other blockchain nodes to query the data records of the blockchain through the index relationship.

[0078] Figure 1 The specific structure of the smart contract layer shown can be as Figure 2 shown.

[0079] As Figure 2As shown, the smart contract layer may include a business logic contract, a primary key generator contract, and a non-primary key index contract. The non-primary key index contract can implement the non-primary key index module.

[0080] Through the business logic contract, building an index and index query can be realized. Building an index means that users write data to the blockchain, establish a non-primary key index (index relationship) according to it, associate it with the primary key, and further synchronize the index relationship on the blockchain; Index query means obtaining a set of index values based on the hash algorithm, and then obtaining query data according to the set of index values through the blockchain storage query interface. Specifically, the specific functions of the smart contract are as follows:

[0081] The non-primary key index contract is used to build index relationships and query index relationships. The index relationship includes the correspondence between the attribute information in the data record and the index value, and the correspondence between the primary key in the data record and the index value. Among them, the attribute information can be divided into two types: labeled attribute information and non-labeled attribute information. For example, fields of bool (Boolean type) and short_int (integer type) can be used as labeled attribute information, and string (character type) can be used as non-labeled attribute information.

[0082] Based on the synchronous characteristic of the data generated by the smart contract on the blockchain, the primary key generator contract is used to generate the primary key, and a distributed sequential primary key generator can be realized based on the smart contract, without concurrency problems.

[0083] The business logic contract is a smart contract defined by users according to specific business, and can realize data writing and query on the blockchain.

[0084] As Figure 3 shown, a data query method for a blockchain provided by an embodiment of the present invention.

[0085] Step 301: Obtain a set of index values according to the query conditions and the preset hash algorithm.

[0086] The query conditions include attribute information and the constraint relationship between attribute information. The set of index values includes index values; Index relationships are set for data records in the blockchain, and the index relationships include the correspondence between the attribute information in the data records and the index values; Among them, the first index value is obtained by the first attribute information according to the preset hash algorithm, the first index value is any index value in the index relationship, and the first attribute information is any attribute information corresponding to the first index value.

[0087] For example, any data record D, taking the ledger record as an example, can be expressed as follows:

[0088] D = {id, [w1v1, w2v2…wi v i};

[0089] Among them, id is the primary key in the data record, which can be the serial number of the ledger data. [w1v1, w2v2... w i v i is the ledger data, and a pair of w i v i is the attribute information. i is a positive integer, where w i is the attribute field, and v i is the attribute data. For example, the attribute information is w1v1.

[0090] from, to, amount, transaction_type, and subject are all attribute fields. The data structures of these attribute fields can be as shown in the following table:

[0091] Table 1 Data Structures of Attribute Fields

[0092]

[0093] Suppose user a deposits 100 in bank deposits, then transfers 10 to user b, user b transfers out 1 bank deposit, and a transfers out 10 bank deposits, generating four ledger data. Then the submitted [w1v1, w2v2... w i v i are as follows:

[0094] [w1v1, w2v2, w3v3, w4v4, w5v5] = [(from: ""), (to: "a"), (amount: 100), (transaction_type: 0), (subject: 1)];

[0095] [w1v1, w2v2, w3v3, w4v4, w5v5] = [(from: "a"), (to: "b"), (amount: 10), (transaction_type: 2), (subject: 2)];

[0096] [w1v1, w2v2, w3v3, w4v4, w5v5] = [(from: "b"), (to: ""), (amount: 1), (transaction_type: 1), (subject: 1)];

[0097] [w1v1, w2v2, w3v3, w4v4, w5v5] = [(from: "a"), (to: ""), (amount: 10), (transaction_type: 1), (subject: 2)];

[0098] After obtaining the ledger data, the primary key generator contract can be called to generate the primary key id of the ledger record. The generation sequence number of the ledger data can be used as the primary key id, and the complete data record D is generated in the following format:

[0099] D = {id, [w1v1, w2v2…w i v i};

[0100] Then the four complete data records are as follows:

[0101] D 转入 = {16, [(from:””), (to:”a”), (amount:100), (transaction_type:0), (subject:1)]};

[0102] D 转账 = {27, [(from:”a”), (to:”b”), (amount:10), (transaction_type:2), (subject:2)]};

[0103] D 转出1 = {38, [(from:”b”), (to:””), (amount:1), (transaction_type:1), (subject:1)]};

[0104] D 转出2 = {49, [(from:”a”), (to:””), (amount:10), (transaction_type:1), (subject:2)]}.

[0105] After the data record is obtained, the corresponding relationship between the attribute information and the index value in the index relationship can be constructed according to the attribute information. The corresponding relationship between the attribute information and the index value in the data record can be constructed in the following way:

[0106] For the attribute information in any data record in the blockchain, the attribute information is operated according to a preset hash algorithm to obtain the index value corresponding to the attribute information; if the index value does not exist in the index relationship, the index value is added to the index relationship; the preset hash algorithm can be the cyclic redundancy check (CRC) algorithm. Cyclic redundancy check is a hash function that generates a short fixed-length check code according to data such as network data packets or computer files. It can be further divided into CRC16 and CRC32; CRC16 is a hash function that generates a 16-bit hash value, and CRC32 is a hash function that generates a 32-bit hash value. The specific process can be as follows:

[0107] First, according to any data record D, extract each attribute information (including attribute field w and attribute data v). Taking the above four data records as an example, summarize each attribute data by attribute field:

[0108] from: a, b;

[0109] to: a, b;

[0110] amount: 1, 10, 100;

[0111] transaction_type: 0, 1, 2;

[0112] subject: 1, 2.

[0113] Corresponding index values can be added to the index relationship according to different situations of tag - type attribute information and sub - tag - type attribute information. Fields of bool (Boolean type) and short_int (integer type) can be classified as tag - type attribute information, and string (character type) as non - tag - type attribute information. Then transaction_type and subject are tag - type attribute information, while from, to, and amount are non - tag - type attribute information.

[0114] For the construction of index values for tag - type attribute information, the following calculation can be performed on the attribute field w and the attribute data v:

[0115] k i = CRC16(ASCII(w i , v i ))), where ASCII means taking the ASCII code;

[0116] Get a candidate index value k of 16 - bit size i , in this application, unless otherwise specified, index values are represented in hexadecimal.

[0117] Taking transaction_type and subject as examples:

[0118] k transaction_type=0 = C58D;

[0119] k transaction_type=1 = 054C;

[0120] k transaction_type=2 = 040C;

[0121] k subject=1 = 2460;

[0122] k subject=2 = 560A.

[0123] For the tag-type attribute information, k can be directly used i as the index value corresponding to the tag-type attribute information.

[0124] Then, the index column R used to store the index value in the index relationship can be queried. The binary search method can be adopted to query whether k exists in the index column R i . If it does not exist, it is added to the index column in order, and a primary key array c with a size of 2 16 bit is generated i , and all values are defaulted to 0; if it exists, there is no need to add it. When the subsequent added data volume exceeds the primary key array c i , dynamic expansion is performed again. The value of each bit in the primary key array c i corresponds to a primary key, and this value is used to indicate whether the primary key corresponds to k i .

[0125] For the construction of the index value of non-tag-type attribute information, the following calculation can be performed on the attribute field w and the attribute data v:

[0126] m i = CRC32(ASCII(w i , v i ))), where ASCII represents taking the ASCII code;

[0127] The 32-bit candidate index value m is obtained i :

[0128] m i = CRC32(ASCII(w i , v i ));

[0129] Taking from and to as examples:

[0130] m from=a = 9F6A581D;

[0131] m from=b = 066309A7;

[0132] m to=a = 19B5937C;

[0133] m to=b = C58DC0BF;

[0134] For the candidate index value m of non-tag-type attribute information i , the high 16-bit value k of m i can be taken iAs the index value, and the lower 16 bits as the secondary index value, which is used for further indexing after the collision of the higher 16-bit value. Specifically:

[0135] k i = m i % 2^16;

[0136] k from=a = 9F6A;

[0137] k from=b = 0663;

[0138] k to=a = 19B5;

[0139] k to=b = C58D.

[0140] Similar to the label class attribute information, the index column R used to store the index value in the index relationship can also be queried. The binary search method can be used to query whether k i exists in the index column R. If it does not exist, it is added to the index column in order, and a primary key array c 16 with a size of 2 i bits is generated, and all values are defaulted to 0; if it exists, there is no need to add it. When the subsequent added data volume exceeds the primary key array c i , dynamic expansion is performed again. Each bit value in the primary key array c i corresponds to a primary key, and this value is used to indicate whether this primary key corresponds to k i . Taking transaction_type and subject as examples, the corresponding relationship is as follows:

[0141] k transaction_type=0 = C58D matches id:

[16] ;

[0142] k transaction_type=1 = 054C matches id: [38, 49];

[0143] k transaction_type=2 = 040C matches id:

[27] ;

[0144] k subject=0 = 2460 matches id: [16, 38];

[0145] k subject=1 = 560A matches id: [27, 49].

[0146] Specifically, the generated index column can be as shown in Figure 4 and the schematic diagram of the index column and the primary key array of each index value can be as shown in Figure 5As shown, the array subscript storing the primary key correspondence element in each array is the same as the primary key value.

[0147] Obviously, for non-tag attribute information, the index value generated for the attribute information in the above process is obtained through the CRC hash algorithm to get a candidate index value with a total length of 32 bits. To optimize the storage consumption problem of the hash algorithm, the 32-bit length data is separated into a high 16-bit index value and a low 16-bit index value. The high 16-bit index value is stored in the index column R, and the corresponding low 16-bit index value is used as the secondary index value. In principle, through the repetitive collision of the high 16-bit index value of the 32-bit candidate index value, the occupied space of the index value is effectively saved, and the maximum occupied space of the high 16-bit index value is 2 16 bit.

[0148] Based on the constructed index column and the primary key array of each index value, the index value set can be obtained according to the query condition, specifically as shown in step 302. The query condition can include attribute information and constraint relationships. For example, the query condition can be w1v1 and w2v2. In this example, the constraint relationship is the logical operation and, and the logical operation and means that the attribute information participating in the and logical operation needs to be satisfied simultaneously. There can be various situations for the constraint relationship. For example, the logical operation or means that the attribute information participating in the or logical operation needs to satisfy one of them, and the not logical operation means that the attribute information participating in the not logical operation needs to be not satisfied; it should be noted that the constraint relationship can be either a single logical operation or composed of various logical operations.

[0149] Step 302: Determine the candidate primary key set according to the index value set and the constraint relationship.

[0150] The index relationship also includes the correspondence between the primary key in the data record and the index value. Among them, the first primary key in the candidate primary key set is any primary key corresponding to the first index value.

[0151] In a possible implementation, step 302 can be as follows:

[0152] Determine the primary key array corresponding to each index value in the index value set; perform operations according to the constraint relationship based on the values of the elements in the primary key array corresponding to each index value in the index value set to obtain a combined primary key array; determine the candidate primary key set according to the values of the elements in the combined primary key array.

[0153] The value of the element in the primary key array is the first preset value or the second preset value. The first preset value indicates that there is a correspondence between the primary key and the index value, and the second preset value indicates that there is no correspondence between the primary key and the index value.

[0154] For example, the query condition is transaction_type = 0 and subject = 1. Since the attribute information in the query condition is all tag-type attribute information, the index value set W = [054C, 2460] can be directly obtained through a preset hash algorithm. In this case, the correspondence between the index value and the primary key array can be as Figure 6 shown.

[0155] Here, it is considered that the data records only include 16, 27, 38, 49. 0 or 1 is used to represent whether there is a correspondence between the primary key and the index value. 1 means there is, and 0 means there is no. Then the primary key array of 054C is 1100, the primary key array of 2460 is 0101, and the combined primary key array is 1100 & 0101 = 0100. & represents the logical AND operation. So only the value corresponding to 27 is 1, and the candidate primary key set is ID {27}.

[0156] Correspondingly, if the query condition is transaction_type = 0 or subject = 1, the combined primary key array is 1100 | 0101 = 1101. | represents the logical OR operation, and the candidate primary key set is ID {16, 27, 49}.

[0157] It should be noted that when the attribute information in the query condition includes tag-type attribute information, after calculating the tag-type attribute information through a preset hash algorithm, the first 16 bits need to be taken as the index value of the tag-type attribute information.

[0158] For example, if the query condition is transaction_type = 0 and to = b, then

[0159] CRC16(ASCII(transaction_type = 0)) = C58D;

[0160] CRC32(ASCII(to = b)) = C58DC0BF. Take the first 16 bits (since it is hexadecimal, that is, take C58D).

[0161] Then the candidate primary key set is {C58D}. In this case, the correspondence between the index value and the primary key array can be as Figure 7 shown, and there are two primary key arrays corresponding to the index value, both of which are {1100}.

[0162] The combined primary key array is 1100 & 1100 = 1100, where & represents the logical AND operation. Then the candidate primary key set is IDs {16, 27}. Correspondingly, if the query condition is transaction_type = 0 or to = b, the combined primary key array is 1100 | 1100 = 1100, where | represents the logical OR operation. Then the candidate primary key set is IDs {16, 27}.

[0163] Step 303: Obtain query data according to the query condition and the candidate primary key set.

[0164] It should be noted that steps 301 to 303 can be executed by nodes of the blockchain by calling Figure 2 the smart contract shown.

[0165] In a possible implementation, step 303 can specifically be:[[]]

[0166] For the second primary key, according to the second primary key and the associated information corresponding to the second primary key based on the query condition, determine the associated hash value of the second primary key according to the preset hash algorithm; if the associated hash value is located in the associated hash array of the second index value, determine that the second primary key meets the query condition; use the data records corresponding to all the primary keys that meet the query condition as the query data.

[0167] Among them, the second primary key is any primary key in the candidate primary key set, and the second primary key is the primary key corresponding to the second index value in the index value set; the hash values in the associated hash array are obtained according to the primary key corresponding to the second index value and the associated information of this primary key.

[0168] The implementation of the above step 303 is based on the construction of the associated hash value in the index relationship. The specific process can be as follows:[[]]

[0169] For the third attribute information in any data record in the blockchain, according to the third primary key in this data record and the associated information corresponding to the third attribute information, perform an operation according to the preset hash algorithm to obtain the associated hash value of the third attribute information;

[0170] Store the associated hash value of the third attribute information into the associated hash array of the third index value corresponding to the third attribute information in the index relationship.

[0171] In a possible design, the attribute information is divided into tag - type attribute information and non - tag - type attribute information. The collision probability of the index value obtained from the tag - type attribute information according to the preset hash algorithm is less than the collision probability of the index value obtained from the non - tag - type attribute information according to the preset hash algorithm. If the third attribute information is tag - type attribute information, the associated information corresponding to the third attribute information is the third attribute information; or,

[0172] If the third attribute information is non - tag - type attribute information, the third attribute information is operated according to the preset hash algorithm to obtain a secondary index value of the third index value; the associated information corresponding to the third attribute information is the secondary index value.

[0173] For example, the specific construction process can be as follows:

[0174] For tag - type attribute information, the attribute data in the tag - type attribute information can be subjected to CRC16 calculation with the primary key id to obtain an associated hash value d i :

[0175] d i = CRC16(id, v i );

[0176] d transaction_type=0 (16,0)= 16F0 - decimal -> 5872;

[0177] d transaction_type=1 (38,1)= AD51 - decimal -> 44369;

[0178] d transaction_type=1 (49,1)= 1901 - decimal -> 6401;

[0179] d transaction_type=2 (27,2)= 5320 - decimal -> 21280;

[0180] d subject=0 (16,0)= 16F0 - decimal -> 5872;

[0181] d subject=0 (38,0)= 6D90 - decimal -> 28048;

[0182] d subject=1 (27,1)= 2FD2 - decimal -> 12242;

[0183] d subject=1 (49,1)= 1901 - decimal -> 6401.

[0184] The associated hash value of the label class attribute information can be stored in the associated hash array (lower 16-bit array), as shown in Figure 8 shown.

[0185] For non-label class attribute information, the secondary index value and the primary key id can be used to perform a CRC16 calculation to obtain the associated hash value d i :

[0186] d from=a (27)=22541;

[0187] d from=a (49)=34201;

[0188] d from=b (38)=2417;

[0189] d to=a (16)=37756;

[0190] d to=b (27)=49343.

[0191] The associated hash value of the non-label class attribute information can be stored in the associated hash array (lower 16-bit array), as shown in Figure 9 shown.

[0192] Then for each index value, there is a corresponding bitwise storage array, namely the primary key array (which can also be called the primary key matching array) and the associated hash array (lower 16-bit hash array), as shown in Figure 10 shown.

[0193] Then in the query process, a possible situation is that for the second attribute information in the query condition, if the second attribute information is label class attribute information, the associated information corresponding to the second primary key based on the query condition is specifically the second attribute information corresponding to the second index value in the query condition; or, if the second attribute information is non-label class attribute information, the index value set further includes the secondary index value of this non-label class attribute information, and the associated information corresponding to the second primary key based on the query condition is specifically the secondary index value corresponding to the second index value in the index value set.

[0194] For label class attribute information, taking the query condition transaction_type = 0 or subject = 1 as an example, the candidate primary key set is {16, 27, 49}, and it can be calculated that only the data with id = 27 does not meet the requirements:

[0195] d transaction_type=0 (27,0)=37537;

[0196] dsubject=1 (27, 1) = 21088。

[0197] It can be seen that the corresponding associated hash values of 27 are not in the hash array of the corresponding attribute information.

[0198] Therefore, id = 27 does not match the query condition. Thus, the set of primary key IDs that meet the query condition is [16, 49], and the query data returned is the data records corresponding to 16 and 49. Specifically, it can be as Figure 11 shown.

[0199] For non-tag attribute information, taking the query condition transaction_type = 0 and to = b as an example, the index value of the non-tag attribute information to = b is C58D, and the secondary index value is C0BF; the candidate primary key set is {16, 27}. It is calculated that no data meets:

[0200] d to=b (16) = 23810;

[0201] d to=b (27) = 49343;

[0202] d transaction_type=0 (27, 0) = 37537;

[0203] d transaction_type=0 (16, 0) = 5872.

[0204] Therefore, neither of the two pairs of combinations of 23810 and 5872, 49343 and 37537 exists in the associated hash array corresponding to C58D. Specifically, it can be as Figure 12 shown.

[0205] In the method provided by this application, multi-dimensional non-primary key query on the blockchain is realized based on the hash algorithm and basic logical operations. In terms of storage optimization, a 16-bit index column with hash repeatability is constructed to compress the original 32-bit hash index length and merge index data with the same part of the hash.

[0206] Due to bit-by-bit storage, when counting according to 2 billion data volumes, the maximum space occupied by a single index does not exceed 500M, (2000000000 / 8 / 1024 / 1024 * 2 + 2 16 / 8 (space occupied by the index column)), greatly reducing the storage. In terms of query efficiency, the problem of hash repeatability collision is solved by using the double matching of the id matching bit array and the low 16-bit hash bit array, and the time complexity of the matching is guaranteed to be O(1) ~ O(log2 n) through the binary method and logical operations.

[0207] In terms of the complexity of smart contracts, for each column corresponding to a sorted array c according to the index column R, there is no complex nesting in the data structure. Therefore, the consumption of smart contract calls will not affect the consumption of writing and querying. At the same time, based on the present invention, a lightweight blockchain index is realized, which is compatible with EVM and supports all blockchains. The index at the smart contract level reduces the coupling with blockchain data. In addition to meeting the query optimization requirements of the blockchain with a KV storage structure, it also meets the requirements on blockchains with a relational or other storage structure. And based on the consensus characteristics of the blockchain, in some cross-chain scenarios, such as (for example, one blockchain supports smart contracts and one blockchain does not support smart contracts but only serves as data storage), the present invention can still support multi-dimensional queries of heterogeneous blockchains.

[0208] As Figure 13 shown, a blockchain data query device provided by an embodiment of the present invention includes:

[0209] A processing module 1301, configured to obtain a set of index values according to a query condition and a preset hash algorithm; the query condition includes attribute information and the constraint relationship between attribute information, and the set of index values includes index values; an index relationship is set for data records in the blockchain, and the index relationship includes the corresponding relationship between the attribute information in the data record and the index value; wherein, the first index value is obtained by the first attribute information according to the preset hash algorithm, the first index value is any index value in the index relationship, and the first attribute information is any attribute information corresponding to the first index value; and

[0210] configured to determine a set of candidate primary keys according to the set of index values and the constraint relationship; the index relationship further includes the corresponding relationship between the primary key in the data record and the index value, wherein the first primary key in the set of candidate primary keys is any primary key corresponding to the first index value;

[0211] A query module 1302, configured to obtain query data according to the query condition and the set of candidate primary keys.

[0212] Optionally, the query module 1302 is specifically configured to:

[0213] For a second primary key, the second primary key is any primary key in the set of candidate primary keys, determine an associated hash value of the second primary key according to the second primary key and the associated information corresponding to the second primary key based on the query condition according to the preset hash algorithm; the second primary key is the primary key corresponding to the second index value in the set of index values;

[0214] If the associated hash value is located in the associated hash array of the second index value, it is determined that the second primary key meets the query condition; the hash values in the associated hash array are obtained according to the primary key corresponding to the second index value and the associated information of the primary key;

[0215] All the data records corresponding to the primary keys that meet the query condition are used as the query data.

[0216] Optionally, the attribute information is divided into tag-type attribute information and non-tag-type attribute information, and the collision probability of the index value obtained by the tag-type attribute information according to the preset hash algorithm is less than the collision probability of the index value obtained by the non-tag-type attribute information according to the preset hash algorithm;

[0217] For the second attribute information in the query condition, if the second attribute information is tag-type attribute information, the association information corresponding to the second primary key based on the query condition is specifically the second attribute information corresponding to the second index value in the query condition; or,

[0218] If the second attribute information is non-tag-type attribute information, the index value set further includes the secondary index value of the non-tag-type attribute information, and the association information corresponding to the second primary key based on the query condition is specifically the secondary index value corresponding to the second index value in the index value set.

[0219] Optionally, the processing module 1301 is further configured to:

[0220] For the third attribute information in any data record in the blockchain, the third attribute information is operated according to the preset hash algorithm to obtain the third index value corresponding to the third attribute information; if the third index value does not exist in the index relationship, the third index value is added to the index relationship;

[0221] According to the third primary key in the data record and the association information corresponding to the third attribute information, it is operated according to the preset hash algorithm to obtain the associated hash value of the third attribute information;

[0222] The associated hash value of the third attribute information is stored in the associated hash array of the third index value in the index relationship; according to the third primary key, the value corresponding to the third primary key in the primary key array of the third index value in the index relationship is set.

[0223] Optionally, if the third attribute information is tag-type attribute information, the association information corresponding to the third attribute information is the third attribute information; or,

[0224] If the third attribute information is non-tag-type attribute information, the processing module 1301 is further configured to:

[0225] Operate the third attribute information according to the preset hash algorithm to obtain a secondary index value of the third index value; the associated information corresponding to the third attribute information is the secondary index value.

[0226] Optionally, the query module 1302 is specifically configured to:

[0227] Determine a primary key array corresponding to each index value in the index value set, where the values of the elements in the primary key array are a first preset value or a second preset value. The first preset value indicates that there is a corresponding relationship between the primary key and the index value, and the second preset value indicates that there is no corresponding relationship between the primary key and the index value;

[0228] Perform an operation according to the constraint relationship based on the values of the elements in the primary key array corresponding to each index value in the index value set to obtain a combined primary key array.

[0229] Optionally, the blockchain provides a smart contract, the device is a node of the blockchain, and the device executes the data query method of the blockchain by invoking the smart contract.

[0230] Based on the same inventive concept, an embodiment of the present invention further provides a computer device, including a program or instruction, which when executed, executes the data query method of the blockchain provided by the embodiment of the present invention and any optional method.

[0231] Based on the same inventive concept, an embodiment of the present invention further provides a computer-readable storage medium, including a program or instruction, which when executed, executes the data query method of the blockchain provided by the embodiment of the present invention and any optional method.

[0232] Those skilled in the art should understand that the embodiments of the present invention can be provided as a method, or a computer program product. Therefore, the present invention can be in the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware aspects. Moreover, the present invention can be in the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

[0233] The present invention is described with reference to the flowcharts and / or block diagrams of methods, apparatuses (systems), and computer program products according to embodiments of the present invention. It should be understood that each flow and / or block in the flowchart and / or block diagram, and the combination of flows and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to the processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing devices to generate a machine, such that the instructions executed by the processor of the computer or other programmable data processing devices produce a means for implementing the functions specified in one process Figure 1 one process or multiple processes and / or blocks Figure 1 or multiple blocks.

[0234] These computer program instructions can also be stored in a computer-readable memory that can direct a computer or other programmable data processing device to work in a specific manner, such that the instructions stored in the computer-readable memory produce a manufactured article including an instruction means that implements the functions specified in one process Figure 1 one process or multiple processes and / or blocks Figure 1 or multiple blocks.

[0235] These computer program instructions can also be loaded onto a computer or other programmable data processing device, such that a series of operation steps are executed on the computer or other programmable device to generate a computer-implemented process, and thus the instructions executed on the computer or other programmable device provide steps for implementing the functions specified in one process Figure 1 one process or multiple processes and / or blocks Figure 1 or multiple blocks.

[0236] Although the preferred embodiments of the present invention have been described, those skilled in the art can make additional changes and modifications once they learn the basic creative concept. Therefore, the appended claims are intended to be construed to include the preferred embodiments and all changes and modifications that fall within the scope of the present invention.

[0237] Obviously, those skilled in the art can make various changes and variations to the present invention without departing from the spirit and scope of the present invention. Thus, if these modifications and variations of the present invention fall within the scope of the claims of the present invention and their equivalent technologies, the present invention is also intended to include these modifications and variations.

Claims

1. A method for querying data in a blockchain, characterized in that, Including: Obtain a set of index values according to the query conditions and a preset hash algorithm; The query conditions include attribute information and the constraint relationships between the attribute information. The set of index values includes index values. Index relationships are set for the data records in the blockchain, and the index relationships include the corresponding relationships between the attribute information in the data records and the index values. Among them, the first index value is obtained by the first attribute information according to the preset hash algorithm. The first index value is any index value in the index relationship, and the first attribute information is any attribute information corresponding to the first index value; Determine a set of candidate primary keys according to the set of index values and the constraint relationships. The index relationship also includes the corresponding relationship between the primary keys in the data records and the index values. Among them, the first primary key in the set of candidate primary keys is any primary key corresponding to the first index value; The second primary key is any primary key corresponding to the second index value in the set of index values in the set of candidate primary keys. If the second attribute information in the query conditions is label-type attribute information, then according to the second primary key, the second attribute information corresponding to the second index value in the query conditions, and according to the preset hash algorithm, determine the associated hash value of the second primary key. If the second attribute information is non-label-type attribute information and the set of index values also includes the secondary index value of the non-label-type attribute information, then according to the second primary key and the secondary index value corresponding to the second index value in the set of index values, determine the associated hash value of the second primary key; The collision probability of the index values obtained by the label-type attribute information according to the preset hash algorithm is less than the collision probability of the index values obtained by the non-label-type attribute information according to the preset hash algorithm; If the associated hash value is located in the associated hash array of the second index value, then determine that the second primary key meets the query conditions. The hash values in the associated hash array are obtained according to the primary key corresponding to the second index value and the associated information of the primary key; Use the data records corresponding to all the primary keys that meet the query conditions as the query data.

2. The method according to claim 1, characterized in that Before obtaining the set of index values according to the query conditions and the preset hash algorithm, it further includes: For the third attribute information in any data record in the blockchain, perform an operation on the third attribute information according to the preset hash algorithm to obtain the third index value corresponding to the third attribute information. If the third index value does not exist in the index relationship, then add the third index value to the index relationship; According to the third primary key in the data record and the associated information corresponding to the third attribute information, perform an operation according to the preset hash algorithm to obtain the associated hash value of the third attribute information; Store the associated hash value of the third attribute information into the associated hash array of the third index value in the index relationship. According to the third primary key, set the value corresponding to the third primary key in the primary key array of the third index value in the index relationship.

3. The method according to claim 2, wherein If the third attribute information is label - type attribute information, the associated information corresponding to the third attribute information is the third attribute information; Or, If the third attribute information is non - label - type attribute information, the method further includes: Performing an operation on the third attribute information according to the preset hash algorithm to obtain a secondary index value of the third index value; The associated information corresponding to the third attribute information is the secondary index value.

4. The method according to any one of claims 1 to 3, characterized in that The determining the candidate primary key set according to the index value set and the constraint relationship includes: Determining a primary key array corresponding to each index value in the index value set, where the values of the elements in the primary key array are a first preset value or a second preset value. The first preset value indicates that there is a corresponding relationship between the primary key and the index value, and the second preset value indicates that there is no corresponding relationship between the primary key and the index value; Performing an operation according to the constraint relationship based on the values of the elements in the primary key array corresponding to each index value in the index value set to obtain a combined primary key array; Determining the candidate primary key set according to the values of the elements in the combined primary key array.

5. The method according to any one of claims 1 to 3, characterized in that The blockchain provides a smart contract, and the method is executed by a node of the blockchain by invoking the smart contract.

6. A data query device for a blockchain, characterized in that Including: A processing module, configured to obtain an index value set according to a query condition according to a preset hash algorithm; The query condition includes attribute information and the constraint relationship between attribute information. The index value set includes index values. An index relationship is set for data records in the blockchain, and the index relationship includes the corresponding relationship between the attribute information in the data record and the index value. Among them, the first index value is obtained by the first attribute information according to the preset hash algorithm, the first index value is any index value in the index relationship, and the first attribute information is any attribute information corresponding to the first index value; and For determining a candidate primary key set according to the index value set and the constraint relationship; the index relationship further includes the corresponding relationship between the primary key in the data record and the index value. Among them, the first primary key in the candidate primary key set is any primary key corresponding to the first index value; The second primary key is any primary key corresponding to the second index value in the index value set in the candidate primary key set; A query module, configured to, if the second attribute information in the query condition is label - type attribute information, determine an associated hash value of the second primary key according to the second primary key, the second index value, and the second attribute information corresponding to the second index value in the query condition according to the preset hash algorithm; if the second attribute information is non - label - type attribute information and the index value set further includes a secondary index value of the non - label - type attribute information, determine an associated hash value of the second primary key according to the second primary key and the secondary index value corresponding to the second index value in the index value set; The collision probability of the index value obtained by the label - type attribute information according to the preset hash algorithm is less than the collision probability of the index value obtained by the non - label - type attribute information according to the preset hash algorithm; The query module is further configured to determine that the second primary key meets the query condition if the associated hash value is located in the associated hash array of the second index value; the hash values in the associated hash array are obtained according to the primary key corresponding to the second index value and the associated information of the primary key; All the data records corresponding to the primary keys that meet the query condition are used as query data.

7. A computer device, characterized in that, It includes a program or instruction, and when the program or instruction is executed by a processor, the method according to any one of claims 1 to 5 is executed.

8. A computer-readable storage medium, characterized in that, It includes a program or instruction, and when the program or instruction is executed by a processor, the method according to any one of claims 1 to 5 is executed.

Citation Information

Patent Citations

  • Method and device for querying data in non-relational database

    CN103514201A

  • Query method, device and apparatus in block chain type account book

    CN110716965A