Tree structure data storage method and device, electronic equipment and computer program product

By using a new tree structure data storage method in blockchain projects, using one file to store the data of all nodes and achieve efficient addressing, the problem of slow reading of tree structure data in the existing technology is solved, and the problem of slow reading of tree structure data in the existing technology is improved Data reading speed and storage efficiency.

CN119474097APending Publication Date: 2025-02-18HANGZHOU HIGH-TECH ZONE (BINJIANG) INSTITUTE OF BLOCKCHAIN & DATA SECURITY
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202411564332.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-11-04
Publication Date
2025-02-18

AI Technical Summary

Technical Problem

Existing blockchain projects use leveldb series databases to store tree structure data, resulting in slow data reading speed, because leveldb does not natively support tree structures and cannot directly understand the levels and relationships between nodes.

Method used

Through a tree structure data storage method, a file is used to store the data of all nodes of the tree structure data, and the data of the child nodes are directly positioned in the file according to the data of the parent node, thereby achieving efficient search from the root node to any child node site.

Benefits of technology

Improves the data reading speed of tree structure data, reduces file maintenance costs, and saves storage space.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119474097A_ABST
    Figure CN119474097A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of block chains, and provides a tree structure data storage method and device, electronic equipment and a computer program product. The method comprises the steps of obtaining to-be-stored tree structure data; each node except a root node is traversed in sequence from a leaf node of the tree structure data, the data of the node is written into a file, a corresponding file offset is recorded into the data of a father node of the node, and the file offset is used for representing the storage position of the data of the corresponding node in the file; and writing the data of the root node into a file, and recording index information at a specified position of the file, the index information being used for determining a storage position of the data of the root node in the file. Through the setting, the storage position of the root node in the file can be positioned according to the index information, and the storage position of the child node in the file can be positioned according to the file offset contained in the data of the father node, so that efficient addressing from the root node to the child node is realized, and the data reading speed is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of blockchain technology, and in particular to a tree structure data storage method, device, electronic device and computer program product. Background Art

[0002] In order to improve data security, blockchains usually use tree structures to store state data. Tree structure data includes leaf nodes and intermediate nodes, where leaf nodes store original key-value pairs and intermediate nodes serve as indexes. Existing blockchain projects generally choose leveldb series databases as the underlying storage solution for tree structure data. Such databases have efficient data writing performance. However, leveldb series databases do not natively support tree structures and cannot directly understand the hierarchy and relationship between nodes, which results in slower data reading speeds. Summary of the invention

[0003] In view of this, embodiments of the present application provide a tree structure data storage method, device, electronic device and computer program product, which can improve the data reading speed of tree structure data.

[0004] A first aspect of an embodiment of the present application provides a tree structure data storage method, including:

[0005] Get the tree structure data to be stored;

[0006] Starting from the leaf node of the tree structure data, traverse each node in the tree structure data except the root node in turn, write the data of the node into the first file and record the corresponding file offset into the data of the parent node of the node, where the file offset is used to indicate the storage position of the data of the corresponding node in the first file;

[0007] The data of the root node is written into the first file, and index information is recorded at a specified position of the first file, where the index information is used to determine the storage position of the data of the root node in the first file.

[0008] After obtaining the tree structure data to be stored, the technical solution of the embodiment of the present application will start from the leaf node of the tree structure data, traverse each node in the tree structure data except the root node in sequence, write the data of each node in sequence into the first file and record the corresponding file offset into the data of the parent node of the corresponding node, and finally write the data of the root node into the first file, and record the index information of the data of the root node at the specified position of the first file. The above process writes the tree structure data into the same file by traversing the nodes in post-order, so that the data of each parent node records the file offsets corresponding to all its child nodes, and the index information of the root node data is also written into the file, so that the storage position of the root node in the file can be located according to the index information, and the storage position of the child node in the file can be located according to the file offset contained in the data of the parent node, thereby realizing efficient addressing from the root node to any child node and improving the data reading speed of the tree structure data.

[0009] In an implementation of the embodiment of the present application, the tree structure data includes at least one subtree; traversing each node except the root node in the tree structure data in sequence includes:

[0010] determining a traversal order for each subtree in at least one subtree;

[0011] Traverse all nodes of each subtree in at least one subtree in turn in traversal order.

[0012] In an implementation of the embodiment of the present application, the index information is the length information of the data of the root node; the index information is recorded at a specified position of the first file, including:

[0013] The length information is recorded in the first file at a position after the data of the root node.

[0014] In another implementation of the embodiment of the present application, the index information is the file offset corresponding to the root node.

[0015] In one implementation of the embodiment of the present application, the method further includes:

[0016] When receiving a request to read the tree structure data, reading the index information from the specified position of the first file;

[0017] Read the root node data from the first file according to the index information;

[0018] From the data of the root node, read the file offsets corresponding to all child nodes of the root node;

[0019] Read data of all child nodes of the root node from the first file according to the file offsets corresponding to all child nodes of the root node;

[0020] For each child node of the root node, the file offset corresponding to all the child nodes of the child node is read from the data of the child node, and based on the file offset corresponding to all the child nodes of the child node, the data of all the child nodes of the child node is read from the first file until the data of all nodes of the tree structure data are read from the first file.

[0021] In one implementation of the embodiment of the present application, the tree structure data includes a plurality of contract status trees; the method further includes:

[0022] The addressing information and the corresponding contract account of each contract status tree in the first file are recorded in the database respectively, and the addressing information is used to determine the storage location of the data of the corresponding contract status tree in the first file.

[0023] In one implementation of the embodiment of the present application, the method further includes:

[0024] When a data query request for a target contract account is received, the target addressing information corresponding to the target contract account is read from the database;

[0025] According to the target addressing information, read the data of the contract state tree of the target contract account from the first file.

[0026] In one implementation of the embodiment of the present application, the method further includes:

[0027] When the preset conditions are met, the data of the node whose tree structure data has changed is read from the memory;

[0028] Reading data of nodes that have not been changed in the tree structure data from the first file;

[0029] Determine updated tree structure data according to the data of the nodes that have changed and the data of the nodes that have not changed;

[0030] The updated tree structure data is written into the second file.

[0031] A second aspect of an embodiment of the present application provides a tree structure data storage device, including:

[0032] A data acquisition module, used to acquire tree structure data to be stored;

[0033] A node traversal module is used to start from the leaf node of the tree structure data, traverse each node in the tree structure data except the root node in turn, write the data of the node into the first file and record the corresponding file offset into the data of the parent node of the node, and the file offset is used to indicate the storage position of the data of the corresponding node in the first file;

[0034] The data writing module is used to write the data of the root node into the first file and record index information at a specified position of the first file, wherein the index information is used to determine the storage position of the data of the root node in the first file.

[0035] A third aspect of an embodiment of the present application provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, the tree structure data storage method provided in the first aspect of the embodiment of the present application is implemented.

[0036] A fourth aspect of the embodiments of the present application provides a computer program product. When the computer program product runs on an electronic device, the electronic device executes the tree structure data storage method provided in the first aspect of the embodiments of the present application.

[0037] A fifth aspect of the embodiments of the present application provides a computer-readable storage medium, which stores a computer program. When the computer program is executed by a processor, it implements the tree structure data storage method provided in the first aspect of the embodiments of the present application.

[0038] It can be understood that the beneficial effects of the second to fifth aspects mentioned above can be found in the relevant description of the first aspect mentioned above, and will not be repeated here. BRIEF DESCRIPTION OF THE DRAWINGS

[0039] Figure 1 This is a flowchart of a tree structure data storage method provided by an embodiment of the present application;

[0040] Figure 2 This is a schematic diagram of the operation principle of the tree structure data storage method provided by an embodiment of the present application;

[0041] Figure 3 This is another schematic diagram of the operation principle of the tree structure data storage method provided by an embodiment of the present application;

[0042] Figure 4 This is a schematic diagram of the operating principle of the state tree data change in the blockchain application scenario provided by the embodiment of the present application;

[0043] Figure 5 It is a schematic diagram of the operating principle of rewriting a state tree snapshot file provided by an embodiment of the present application;

[0044] Figure 6 This is a schematic diagram of the operation principle of storing a contract state tree using the tree structure data storage method provided in an embodiment of the present application;

[0045] Figure 7 It is a structural diagram of a tree structure data storage device provided in an embodiment of the present application;

[0046] Figure 8 It is a schematic diagram of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0047] In the following description, for the purpose of illustration rather than limitation, specific details such as specific system structures, technologies, etc. are proposed, so as to thoroughly understand the embodiments of the present application. However, it should be clear to those skilled in the art that the present application can also be implemented in other embodiments without these specific details. In other cases, the detailed description of well-known systems, devices, circuits and methods is omitted to prevent unnecessary details from hindering the description of the present application. In addition, in the description of the present application specification and the attached claims, the terms "first", "second", "third" etc. are only used to distinguish the description, and cannot be interpreted as indicating or suggesting relative importance.

[0048] In the field of blockchain technology, in order to improve data security, a tree structure is usually used to store state data. Various types of tree structures distinguish between leaf nodes and intermediate nodes, where leaf nodes store original key-value pairs, while intermediate nodes serve as indexes. Existing blockchain projects generally choose the leveldb series database as the underlying storage solution for tree structure data. This type of database has efficient data writing performance but slow data reading speed, and there is a problem of "fast writing and slow reading". Specifically, the leveldb series database does not natively support tree structures and cannot directly understand the hierarchy and relationship between nodes. This non-native storage method will lead to inefficient data query. Even if the parent node logically knows the key of the child node, the actual query still needs to go through the database interface, which is heavily dependent on the query performance of the database, and the query performance of the leveldb series database is insufficient.

[0049] In view of the above problems existing in the use of leveldb series databases, the embodiment of the present application proposes a native storage method for tree structure data, which uses a file to store the data of all nodes of the tree structure data, and can directly locate the storage position of the child node data in the file according to the data of the parent node, thereby effectively improving the data reading speed of the tree structure data. For more specific technical implementation details of the embodiment of the present application, please refer to the various method embodiments described below.

[0050] It should be understood that the execution subjects of the various method embodiments proposed in this application can be various types of electronic devices, such as mobile phones, tablet computers, wearable devices, learning machines, augmented reality (AR) / virtual reality (VR) devices, laptops, ultra-mobile personal computers (UMPC), netbooks, personal digital assistants (PDA), large-screen TVs, etc. The embodiments of this application do not impose any restrictions on the specific types of the electronic devices. For example, if applied to the scenario of blockchain state tree data storage, the electronic device can be any blockchain node.

[0051] See also Figure 1 , shows a tree structure data storage method provided by an embodiment of the present application, including:

[0052] 101. Obtain tree structure data to be stored;

[0053] First, the tree structure data to be stored is obtained. The tree structure data can be any type of tree structure data, for example, it can be the MPT (Merkle Patricia Tree) state tree or JMT (Jellyfish Merkle Tree) state tree commonly used in blockchain systems, etc. The tree structure data includes a number of intermediate nodes and a number of leaf nodes. The embodiment of the present application does not impose any restrictions on the number of intermediate nodes, the number of leaf nodes, and the node connection relationship of the stored tree structure data.

[0054] 102. Starting from a leaf node of the tree structure data, traverse each node in the tree structure data except the root node in turn, write the data of the node into the first file and record the corresponding file offset into the data of the parent node of the node, where the file offset is used to indicate the storage position of the data of the corresponding node in the first file;

[0055] After obtaining the tree structure data to be stored, the data of all nodes of the tree structure data are recursively written into the same file by the post-order traversal method, which is recorded as the first file. Specifically, starting from the leaf node of the tree structure data, each node in the tree structure data except the root node is traversed in turn, the data of the node is written into the first file and the corresponding file offset is recorded into the data of the parent node of the node. The file offset is used to indicate the storage position of the data of the corresponding node in the first file.

[0056] During this operation, since the leaf node is the node at the bottom of the tree structure and has no child nodes, it is not necessary to record the file offset of the child nodes in its data, so the data of the leaf node can be written to the first file first. Since the intermediate node has child nodes, it is necessary to wait for the data of all the child nodes it has to be written to the first file and the file offset corresponding to each child node to be stored in the data of the intermediate node before the data of the intermediate node can be written to the first file. The root node is the node at the top of the tree structure, and the data of the root node is finally written to the first file. Since the root node has no parent node, the file offset corresponding to the root node cannot be stored using the parent node, so it is necessary to record the corresponding index information additionally to help locate the storage position of the root node data in the first file, and the specific description can be referred to the relevant description below. Under the premise of satisfying the above-mentioned operating principle, all nodes of the tree structure data can be traversed one by one in any order, and the embodiment of the present application does not limit the specific node traversal order.

[0057] According to the above operating principle, an optional node traversal order is to traverse all leaf nodes first, then traverse all intermediate nodes of the level connected to the leaf nodes, and then traverse all intermediate nodes of the next level, and so on until all nodes are traversed. However, since the file offset of the child node needs to be temporarily stored in the memory before writing the data of the parent node, if the number of nodes is too large, it will cause the problem of excessive memory usage. Another optional node traversal order does not require traversing all nodes level by level. As long as the data of all child nodes of a certain intermediate node have been written to the first file and the corresponding file offsets are obtained, these file offsets can be written to the data of the intermediate node, and then the data of the intermediate node can be written to the first file without waiting for all nodes of the next level of the intermediate node to be traversed. This node traversal order does not need to temporarily store the file offsets of a large number of nodes in the memory, so it can avoid the problem of excessive memory usage. The following describes an implementation method for traversing nodes in sequence according to subtrees.

[0058] In an implementation of the embodiment of the present application, the tree structure data includes at least one subtree; traversing each node except the root node in the tree structure data in sequence includes:

[0059] (1) determining the traversal order of each subtree in at least one subtree;

[0060] (2) Traverse all nodes of each subtree in at least one subtree in order.

[0061] When traversing the nodes of the tree structure data, the traversal order of each subtree of the tree structure data can be determined first, and then all the nodes of each subtree can be traversed in sequence according to the traversal order. For example, if the tree structure data has a left subtree and a right subtree, all the nodes of the left subtree can be traversed first, and then all the nodes of the right subtree can be traversed, and finally the root node can be accessed; all the nodes of the right subtree can also be traversed first, and then all the nodes of the left subtree can be traversed, and finally the root node can be accessed. For another example, if the tree structure data has subtree 1, subtree 2 and subtree 3, all the nodes of subtree 1 can be traversed first, and then all the nodes of subtree 2 can be traversed, and then all the nodes of subtree 3 can be traversed, and finally the root node can be accessed, and so on. Among them, when traversing all the nodes of a certain subtree, starting from the leaf node of the subtree, traverse up level by level until traversing to the middle node of the highest level of the subtree. Adopting this implementation method of traversing nodes in sequence according to the subtree, there is no need to temporarily store the file offsets of a large number of nodes in the memory, which can avoid the problem of excessive memory usage.

[0062] For leaf nodes, their data may include the node's key value, hash value, and other fields stored according to business needs. When traversing to the leaf node, the data of the leaf node will be written to the first file, and the corresponding file offset will be recorded. For intermediate nodes, their original data also includes the node's key value, hash value, and other fields stored according to business needs. On the basis of the original data, the file offsets of all child nodes of the intermediate node are additionally added to form new data. The new data is written to the first file, and the corresponding file offset is also recorded when writing the new data. For example, assuming that an intermediate node node0 has child nodes node1 and node2, and both child nodes node1 and node2 are leaf nodes, the data kv1 of child node node1 can be written to the first file first, and the file offset offset1 can be recorded, where offset1 indicates the storage position of kv1 in the first file; then, the data kv2 of child node node2 can be written to the first file, and the file offset offset2 can be recorded, where offset2 indicates the storage position of kv2 in the first file; finally, offset1 and offset2 can be written to the data of the intermediate node node0. Assuming that the original data of node0 is kv0, new data [offset1, offset2, kv0] will be obtained after adding the file offset, and the data written to the first file is [offset1, offset2, kv0]. At the same time, the storage position offset3 of the data [offset1, offset2, kv0] in the first file is recorded, and the data of the parent node of node0 will be written after offset3, and so on. By setting in this way, the data of each parent node will record the file offsets corresponding to all its child nodes, so that the storage position of the child node data in the file can be located according to the file offsets contained in the parent node data.

[0063] 103. Write the data of the root node into the first file, and record index information at a specified position of the first file, where the index information is used to determine a storage position of the data of the root node in the first file.

[0064] After the data of all nodes except the root node in the tree structure data are written into the first file according to the node traversal method described above, the file offsets corresponding to all child nodes of the root node are written into the data of the root node, and then the data of the root node is also written into the first file. Since the root node has no parent node, the parent node cannot be used to store the file offset corresponding to the root node, so it is necessary to record an index information at a specified position in the first file, and the index information is used to determine the storage position of the root node data in the first file. The specified position can be any position in the first file where the node data is not stored, for example, it can be a file header position or a file end position, etc.

[0065] In an implementation of the embodiment of the present application, the index information is the length information of the data of the root node; the index information is recorded at a specified position of the first file, including:

[0066] The length information is recorded in the first file at a position after the data of the root node.

[0067] This implementation records the length information of the data of the root node in the position after the data of the root node in the first file. Since the data of the root node is written to the first file last, the length information is recorded at the end of the first file. By setting it in this way, when it is necessary to read the data of the root node from the first file, the length information of the data of the root node can be read from the end of the first file, and the storage position of the data of the root node in the first file can be located according to the length information, so as to read the data of the root node. For example, assuming that the length information of the data of the root node is xB, which is recorded at the end position of the first file, the data of the length of xB can be read from the end position forward, i.e., the data of the root node.

[0068] In another implementation of the embodiment of the present application, the index information is the file offset corresponding to the root node.

[0069] This implementation method records the file offset corresponding to the root node at a specified position in the first file, and the file offset is used to indicate the storage position of the root node data in the first file. For example, when writing the root node data to the first file, the corresponding file offset offsetN can be recorded, and the file offset offsetN can be written to the end position of the first file. When the root node data needs to be read from the first file, the file offset offsetN can be read from the end of the first file, and then the data in the storage position corresponding to the file offset offsetN can be read from the first file, that is, the root node data.

[0070] After the data of all nodes of the tree structure data are written into the first file in the above manner, the following describes how to read the tree structure data from the first file.

[0071] In one implementation of the embodiment of the present application, the method further includes:

[0072] (1) when receiving a request to read tree structure data, reading index information from a specified position of the first file;

[0073] (2) Read the data of the root node from the first file according to the index information;

[0074] (3) Read the file offsets corresponding to all child nodes of the root node from the data of the root node;

[0075] (4) Reading data of all child nodes of the root node from the first file according to the file offsets corresponding to all child nodes of the root node;

[0076] (5) For each child node of the root node, the file offset corresponding to all the child nodes of the child node is read from the data of the child node, and based on the file offset corresponding to all the child nodes of the child node, the data of all the child nodes of the child node is read from the first file, until the data of all the nodes of the tree structure data are read from the first file.

[0077] When a request to read tree structure data is received, the index information is first read from the specified position of the first file, for example, the length information of the data of the root node or the file offset corresponding to the root node can be read from the end position of the first file, and the data of the root node can be located and read from the first file according to the index information; then, the data of the root node is parsed, and the parsing can be performed according to the insertion position of the file offset and the encoding rule of the node data, so as to obtain the file offsets corresponding to all the child nodes of the root node contained therein, as well as other data contents of the root node; then, according to the file offsets corresponding to all the child nodes of the root node, the data of all the child nodes of the root node can be located and read from the first file; then, for each child node of the root node, the file offsets corresponding to all the child nodes of the child node are read from the data of the child node, and according to the file offsets corresponding to all the child nodes of the child node, the data of all the child nodes of the child node are read from the first file. By analogy, the data of the nodes of the next level can be located and read step by step from the first file, and finally the data of all the leaf nodes of the bottom layer can be read from the first file, so that the data of all the nodes of the tree structure data can be read.

[0078] As an example, Figure 2 FIG. 1 is a schematic diagram of the operation principle of the tree structure data storage method provided by the embodiment of the present application. Figure 2The tree structure data shown on the left is the simplest binary tree containing 3 nodes, including the middle node node0, and two leaf nodes node1 and node2, where node0 is the root node. When storing the tree structure data, first traverse the leaf node node1 on the lower left, write its data kv1 to the file and record the file offset offset1; then, traverse the leaf node node2 on the lower right, write its data kv2 to the file and record the file offset offset2; then, write the file offset offset1 and the file offset offset2 to the data of the parent node node0, so that the data of node0 is [offset1, offset2, kv0]; finally, write the data [offset1, offset2, kv0] of node0 to the file, and write the length information node0len of the data of node0 to the end of the file. The specific file storage content is as follows: Figure 2 As shown in the upper right corner. When reading tree structure data, first Figure 2 Read the length information node0len of the end position in the upper right file. According to the length information node0len, the data of the root node node0 can be located and read from the file by offsetting forward [offset1, offset2, kv0]. At this time, the data kv0 of the root node node0 is obtained, and offset1 and offset2 are extracted. Offset1 represents the starting position of kv1 stored in the file. Therefore, according to the encoding rules of the node data, the data kv1 of the node node1 can be read from the position corresponding to offset1 in the file. Similarly, offset2 represents the starting position of kv2 stored in the file. Therefore, according to the encoding rules of the node data, the data kv2 of the node node2 can be read from the position corresponding to offset2 in the file. At this point, the data of all nodes of the tree structure data are successfully read from the file.

[0079] In another implementation, when the data of node0 [offset1, offset2, kv0] is written to the file, the corresponding file offset offset3 is recorded, and offset3 is written to the end of the file. The specific file storage content is as follows: Figure 2 As shown in the lower right. When reading tree structure data, first Figure 2The file offset offset3 of the end position is read from the file in the lower right corner. Offset3 indicates the starting position of the data of node0 stored in the file. Therefore, the data of the root node node0 [offset1, offset2, kv0] can be located and read from the file according to offset3. At this time, the data kv0 of the root node node0 is obtained, and offset1 and offset2 are extracted. After that, the specific method of reading the data kv1 of node node1 and the data kv2 of node node2 is the same as above.

[0080] As another example, Figure 3 FIG. 2 is another schematic diagram of the operation principle of the tree structure data storage method provided in the embodiment of the present application. Figure 3The tree structure data shown above is a binary tree including 7 nodes, where node7 is the root node, the left subtree includes leaf node node1, leaf node node2 and intermediate node node5, and the right subtree includes leaf node node3, leaf node node4 and intermediate node node6. When storing the tree structure data, first traverse the left subtree, starting from the leaf node node1, write its data kv1 to the file and record the file offset offset1, and similarly write the data kv2 of the leaf node node2 to the file and record the file offset offset2; write offset1 and offset2 to the data of the intermediate node node5, so that the data of node5 is [offset1, offset2, kv5]; write the data [offset1, offset2, kv5] of node5 to the file and record the file offset offset3, and the left subtree is traversed. Then traverse the right subtree, starting from the leaf node node3, write its data kv3 to the file and record the file offset offset4. Similarly, write the data kv4 of the leaf node node4 to the file and record the file offset offset5. Write offset4 and offset5 to the data of the intermediate node node6, so that the data of node6 is [offset4, offset5, kv6]. Write the data of node6 [offset4, offset5, kv6] to the file and record the file offset offset6. The right subtree is traversed. Finally, visit the root node node7, first write the file offset offset3 corresponding to its child node node5 and the file offset offset6 corresponding to the child node node6 to the data of the root node node7, so that the data of node7 is [offset3, offset6, kv7]. Then write the data of node7 [offset3, offset6, kv7] to the file, and write the length information of the data of node7 node7len to the end of the file. The specific file storage content is as follows: Figure 3 When reading tree structure data, first Figure 3The length information node7len of the end position is read from the file below. According to the length information node7len, the data of the root node node7 can be located and read from the file by offsetting forward [offset3, offset6, kv7]. At this time, kv7 is obtained, and offset3 and offset6 are extracted; according to offset3, the data of the intermediate node node5 can be located and read from the file [offset1, offset2, kv5]. At this time, kv5 is obtained, and offset1 and offset2 are extracted. According to offset1, the data kv1 of the leaf node node1 can be located and read from the file, and according to offset2, the data kv2 of the leaf node node2 can be located and read from the file; similarly, according to offset6, the data [offset4, offset5, kv6] of the intermediate node node6 can be located and read from the file, and kv6 is obtained, and offset4 and offset5 are extracted. According to offset4, the data kv3 of the leaf node node3 can be located and read from the file, and according to offset5, the data kv4 of the leaf node node4 can be located and read from the file. At this point, the data of all nodes of the tree structure data are successfully read from the file.

[0081] according to Figure 2 and Figure 3 The operation principle shown can be extended to native storage of any multi-branch tree, which has high versatility. It only needs to record the index information of the root node data in the file, and record the file offset of all its child nodes in the data of each parent node, so as to achieve efficient addressing from the root node to any child node and improve the data reading speed. In addition, this processing can store the tree structure data in the same file, and the storage method is compact, which can effectively save storage space and reduce file maintenance costs.

[0082] In application scenarios such as blockchain, the state tree data involved is constantly changing. Figure 4 This is a schematic diagram of the operating principle of state tree data changes in blockchain application scenarios. Figure 4 In the state tree snapshot file, after the transaction of each block is executed, a node change set will be generated. This set will be persisted to the pre-write log file first, and the pre-write log file can be used to recover after a crash. In addition, the set will be loaded into the memory, so that the data of the nodes that have changed will be saved in the memory, while the data of the nodes that have not changed will still be saved in the state tree snapshot file. The state tree snapshot file is rewritten every several blocks by default, covering the snapshot of the entire state tree.

[0083] According to the tree structure data storage method provided in the embodiment of the present application, the state tree data can be written into the state tree snapshot file, in which case the state tree data is used as the tree structure data to be stored, and the state tree snapshot file is used as the first file described above. In one implementation of the embodiment of the present application, the method further includes:

[0084] (1) When the preset conditions are met, the data of the node whose tree structure data has changed is read from the memory;

[0085] (2) reading data of nodes that have not been changed in the tree structure data from the first file;

[0086] (3) determining updated tree structure data based on the data of the nodes that have changed and the data of the nodes that have not changed;

[0087] (4) Write the updated tree structure data into the second file.

[0088] When the tree structure data is updated, the data of the changed nodes will be loaded into the memory, while the data of the unchanged nodes will still be saved in the first file. First, determine whether the preset conditions are met, for example, every several blocks (the specific number of blocks can be reasonably set according to the block transaction volume and the hard disk storage performance) can be regarded as meeting the conditions, or every certain time period can be regarded as meeting the conditions. When the preset conditions are met, the data of the nodes that have changed in the tree structure data are read from the memory, and the data of the nodes that have not changed in the tree structure data are read from the first file, and then the updated tree structure data is determined based on the data of the changed nodes and the data of the unchanged nodes. Specifically, this process starts from the root node, traverses the nodes that have changed in the memory and the nodes that have not changed in the first file, and combines the two parts of the nodes to form the current version of the tree structure data. Finally, the updated tree structure data is written into a new file, recorded as the second file, and the specific method of writing the tree structure data into the second file is the same as writing into the first file. The old version of the memory data and the first file can be deleted, and then wait to enter the next data update process.

[0089] As an example, Figure 5 FIG. 1 is a schematic diagram of the operation principle of rewriting a state tree snapshot file provided by an embodiment of the present application. Figure 5 In the blockchain system, by default, a new state tree snapshot file is created in the background every 10,000 blocks, and the full state tree of the current version is rewritten. This process starts from the root node, traverses the nodes that have changed in the memory and the nodes that have not changed in the state tree snapshot file, and combines the two parts of the nodes to form the complete state tree of the current version. After the rewrite is completed, the system deletes the old state tree snapshot file and the old version of the memory data. After that, this operation process will be repeated every 10,000 blocks.

[0090] In the field of blockchain technology, each contract account is associated with a contract state tree. When storing the contract state tree, the existing technology usually adds an account prefix to the key in the key-value pair data to distinguish different contract state trees, and then stores the contract state tree in the KV (Key-Value) database. This method requires adding prefixes such as account addresses used to distinguish contract state trees to the key value, which has the problem of large storage space usage and reduces data query efficiency.

[0091] The tree structure data storage method provided in the embodiment of the present application is applicable to various types of tree structure data and can be used as a native storage solution for the contract state tree. In one implementation of the embodiment of the present application, the tree structure data includes multiple contract state trees; the method also includes:

[0092] The addressing information and the corresponding contract account of each contract status tree in the first file are recorded in the database respectively, and the addressing information is used to determine the storage location of the data of the corresponding contract status tree in the first file.

[0093] The first file can store multiple different contract status trees, and each contract status tree can be written into different areas of the first file according to the storage method of the tree structure data described above. In order to locate the storage position of each contract status tree in the first file and to facilitate the query of the contract status tree corresponding to a certain contract account, the addressing information of each contract status tree in the first file and the corresponding contract account can be recorded in the database. Among them, the addressing information is used to determine the storage position of the data of the corresponding contract status tree in the first file, which can specifically be the offset of the end position of the data of the contract status tree in the first file.

[0094] In one implementation of the embodiment of the present application, the method further includes:

[0095] (1) When a data query request for a target contract account is received, the target addressing information corresponding to the target contract account is read from the database;

[0096] (2) According to the target addressing information, read the data of the contract status tree of the target contract account from the first file.

[0097] If a data query request for a target contract account is received, the target addressing information corresponding to the target contract account is first read from the database, and then the data of the contract status tree of the target contract account is located and read from the first file according to the target addressing information. The specific method of reading the contract status tree can refer to the method of reading the tree structure data from the first file described above, which will not be repeated here.

[0098] As an example, Figure 6This is a schematic diagram of the operation principle of storing a contract status tree using the tree structure data storage method provided in the embodiment of the present application. Figure 6 In the KV database, the addressing information of each contract state tree in the file and the corresponding contract account, for example, the addressing information offset1 corresponding to the contract 1 account, the addressing information offset2 corresponding to the contract 2 account, and the addressing information offset3 corresponding to the contract 3 account. Among them, offset1 represents the end position of the contract 1 state tree in the file, offset2 represents the end position of the contract 2 state tree in the file, and offset3 represents the end position of the contract 3 state tree in the file. If you need to read the data of the contract 2 account from the file, first search for the addressing information offset2 corresponding to the contract 2 account from the KV database, and then find the position corresponding to offset2 from the file, which is the end position of the contract 2 state tree in the file. The index information of the root node of the contract 2 state tree can be read through this position, and then the data of the root node of the contract 2 state tree can be located and read from the file. After that, the data of all nodes of the contract 2 state tree can be read in sequence according to the method of reading the tree structure data from the first file described above.

[0099] By using the tree structure data storage method provided in the embodiment of the present application to store the contract status tree, there is no need to add prefix information such as account address for distinguishing each key-value pair of each contract, so it can effectively save storage space. Moreover, the data of different contract status trees do not affect each other, which can improve data query efficiency.

[0100] After obtaining the tree structure data to be stored, the technical solution of the embodiment of the present application will start from the leaf node of the tree structure data, traverse each node in the tree structure data except the root node in sequence, write the data of each node in sequence into the first file and record the corresponding file offset into the data of the parent node of the corresponding node, and finally write the data of the root node into the first file, and record the index information of the data of the root node at the specified position of the first file. The above process writes the tree structure data into the same file by traversing the nodes in post-order, so that the data of each parent node records the file offsets corresponding to all its child nodes, and the index information of the root node data is also written into the file, so that the storage position of the root node in the file can be located according to the index information, and the storage position of the child node in the file can be located according to the file offset contained in the data of the parent node, thereby realizing efficient addressing from the root node to any child node and improving the data reading speed of the tree structure data.

[0101] In summary, the embodiments of the present application provide a native storage solution for tree-structured data, which has many advantages such as strong versatility, fast data reading speed, high space utilization and low file maintenance cost.

[0102] It should be understood that the size of the serial numbers of the steps in the above embodiments does not mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present application.

[0103] A tree structure data storage method is mainly described above, and a tree structure data storage device will be described below.

[0104] See also Figure 7 , shows a tree structure data storage device provided by an embodiment of the present application, including:

[0105] The data acquisition module 701 is used to acquire the tree structure data to be stored;

[0106] A node traversal module 702 is used to traverse each node in the tree structure data except the root node in turn, starting from the leaf node of the tree structure data, write the data of the node into the first file and record the corresponding file offset into the data of the parent node of the node, where the file offset is used to indicate the storage position of the data of the corresponding node in the first file;

[0107] The data writing module 703 is used to write the data of the root node into the first file and record index information at a specified position of the first file, where the index information is used to determine the storage position of the data of the root node in the first file.

[0108] In an implementation of the embodiment of the present application, the tree structure data includes at least one subtree; the node traversal module includes:

[0109] An order determination unit, used to determine a traversal order of each subtree in at least one subtree;

[0110] The node traversal unit is used to traverse all nodes of each subtree in at least one subtree in sequence according to a traversal order.

[0111] In one implementation of the embodiment of the present application, the index information is the length information of the data of the root node; the data writing module includes:

[0112] The length information recording unit is used to record the length information at a position after the data of the root node in the first file.

[0113] In another implementation of the embodiment of the present application, the index information is the file offset corresponding to the root node.

[0114] In one implementation of the embodiment of the present application, the tree structure data storage device further includes:

[0115] An index reading module, configured to read index information from a specified position of the first file when receiving a request to read tree structure data;

[0116] A root node data reading module, used to read the root node data from the first file according to the index information;

[0117] The file offset reading module is used to read the file offsets corresponding to all child nodes of the root node from the data of the root node;

[0118] A child node data reading module, used for reading data of all child nodes of the root node from the first file according to the file offsets corresponding to all child nodes of the root node;

[0119] A node addressing reading module is used to read, for each child node of the root node, the file offset corresponding to all the child nodes of the child node from the data of the child node, and read the data of all the child nodes of the child node from the first file according to the file offset corresponding to all the child nodes of the child node, until the data of all nodes of the tree structure data are read from the first file.

[0120] In one implementation of the embodiment of the present application, the tree structure data includes a plurality of contract status trees; and the tree structure data storage device further includes:

[0121] The contract information recording module is used to record the addressing information and the corresponding contract account of each contract status tree in the first file in the database, and the addressing information is used to determine the storage location of the data of the corresponding contract status tree in the first file.

[0122] In one implementation of the embodiment of the present application, the tree structure data storage device further includes:

[0123] An addressing information reading module, configured to read target addressing information corresponding to a target contract account from a database when a data query request for the target contract account is received;

[0124] The state tree reading module is used to read the data of the contract state tree of the target contract account from the first file according to the target addressing information.

[0125] In one implementation of the embodiment of the present application, the tree structure data storage device further includes:

[0126] A memory reading module is used to read the data of the node that has changed in the tree structure data from the memory when a preset condition is met;

[0127] A file reading module, used for reading data of unchanged nodes in the tree structure data from the first file;

[0128] A data updating module, used for determining updated tree structure data according to data of nodes that have changed and data of nodes that have not changed;

[0129] The file rewriting module is used to write the updated tree structure data into the second file.

[0130] An embodiment of the present application further provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the tree structure data storage method described in any of the above embodiments is implemented.

[0131] An embodiment of the present application also provides a computer program product. When the computer program product is run on an electronic device, the electronic device executes the tree structure data storage method described in any of the above embodiments.

[0132] Figure 8 is a schematic diagram of an electronic device provided by an embodiment of the present application. Figure 8 As shown, the electronic device 8 of this embodiment includes: a processor 80, a memory 81, and a computer program 82 stored in the memory 81 and executable on the processor 80. When the processor 80 executes the computer program 82, the steps in the above-mentioned tree structure data storage method embodiments are implemented, such as Figure 1 Alternatively, when the processor 80 executes the computer program 82, the functions of each module / unit in the above-mentioned device embodiments are realized, for example, Figure 7 Functions of modules 701 to 703 of the illustrated apparatus.

[0133] The computer program 82 may be divided into one or more modules / units, which are stored in the memory 81 and executed by the processor 80 to complete the present application. The one or more modules / units may be a series of computer program instruction segments capable of completing specific functions, which are used to describe the execution process of the computer program 82 in the electronic device 8.

[0134] The processor 80 may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor may be a microprocessor or any conventional processor, etc.

[0135] The memory 81 may be an internal storage unit of the electronic device 8, such as a hard disk or memory of the electronic device 8. The memory 81 may also be an external storage device of the electronic device 8, such as a plug-in hard disk, a smart media card (SMC), a secure digital (SD) card, a flash card, etc. equipped on the electronic device 8. Further, the memory 81 may also include both an internal storage unit and an external storage device of the electronic device 8. The memory 81 is used to store the computer program and other programs and data required by the electronic device. The memory 81 may also be used to temporarily store data that has been output or is to be output.

[0136] The technicians in the relevant field can clearly understand that for the convenience and simplicity of description, only the division of the above-mentioned functional units and modules is used as an example for illustration. In practical applications, the above-mentioned function allocation can be completed by different functional units and modules as needed, that is, the internal structure of the device can be divided into different functional units or modules to complete all or part of the functions described above. The functional units and modules in the embodiment can be integrated in a processing unit, or each unit can exist physically separately, or two or more units can be integrated in one unit. The above-mentioned integrated unit can be implemented in the form of hardware or in the form of software functional units. In addition, the specific names of the functional units and modules are only for the convenience of distinguishing each other, and are not used to limit the scope of protection of this application. The specific working process of the units and modules in the above-mentioned system can refer to the corresponding process in the aforementioned method embodiment, which will not be repeated here.

[0137] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.

[0138] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described or recorded in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.

[0139] Those of ordinary skill in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.

[0140] In the embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the system embodiments described above are only schematic. For example, the division of the modules or units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be an indirect coupling or communication connection through some interfaces, devices or units, which can be electrical, mechanical or other forms.

[0141] The units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the embodiments of the present application.

[0142] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.

[0143] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the present application implements all or part of the processes in the above-mentioned embodiment method, and can also be completed by instructing the relevant hardware through a computer program. The computer program can be stored in a computer-readable storage medium, and the computer program can implement the steps of the above-mentioned various method embodiments when executed by the processor. Among them, the computer program includes computer program code, and the computer program code can be in source code form, object code form, executable file or some intermediate form. The computer-readable medium may include: any entity or device capable of carrying the computer program code, recording medium, U disk, mobile hard disk, disk, optical disk, computer memory, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), electric carrier signal, telecommunication signal and software distribution medium. It should be noted that the content contained in the computer-readable medium can be appropriately increased or decreased according to the requirements of legislation and patent practice in the jurisdiction. For example, in some jurisdictions, according to legislation and patent practice, computer-readable media do not include electric carrier signals and telecommunication signals.

[0144] The embodiments described above are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, a person skilled in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some of the technical features may be replaced by equivalents. Such modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application, and should all be included in the protection scope of the present application.

Claims

1. A tree structure data storage method, characterized in that: include: Get the tree structure data to be stored; Starting from a leaf node of the tree structure data, traverse each node in the tree structure data except the root node in turn, write the data of the node into the first file and record the corresponding file offset into the data of the parent node of the node, wherein the file offset is used to indicate the storage position of the data of the corresponding node in the first file; The data of the root node is written into the first file, and index information is recorded at a specified position of the first file, where the index information is used to determine the storage position of the data of the root node in the first file.

2. The method according to claim 1, characterized in that The tree structure data includes at least one subtree; and sequentially traversing each node in the tree structure data except the root node includes: Determining a traversal order for each subtree in the at least one subtree; All nodes of each subtree in the at least one subtree are traversed in sequence according to the traversal order.

3. The method according to claim 1, characterized in that The index information is the length information of the data of the root node; and recording the index information at the designated position of the first file includes: The length information is recorded in the first file at a position after the data of the root node.

4. The method according to claim 1, characterized in that The index information is the file offset corresponding to the root node.

5. The method according to claim 1, characterized in that Also includes: When receiving a request to read the tree structure data, reading the index information from a specified position of the first file; Reading the data of the root node from the first file according to the index information; Read the file offsets corresponding to all child nodes of the root node from the data of the root node; Reading data of all child nodes of the root node from the first file according to the file offsets corresponding to all child nodes of the root node; For each child node of the root node, the file offset corresponding to all the child nodes of the child node is read from the data of the child node, and based on the file offset corresponding to all the child nodes of the child node, the data of all the child nodes of the child node is read from the first file, until the data of all nodes of the tree structure data are read from the first file.

6. The method according to claim 1, characterized in that The tree structure data includes a plurality of contract status trees; the method further includes: The addressing information and the corresponding contract account of each contract status tree in the first file are recorded in the database respectively, and the addressing information is used to determine the storage location of the data of the corresponding contract status tree in the first file.

7. The method according to claim 6, characterized in that Also includes: When receiving a data query request for a target contract account, reading target addressing information corresponding to the target contract account from the database; According to the target addressing information, read the data of the contract status tree of the target contract account from the first file.

8. The method according to any one of claims 1 to 7, characterized in that Also includes: When a preset condition is met, data of a node in the tree structure data that has changed is read from the memory; Reading data of unchanged nodes in the tree structure data from the first file; Determining the updated tree structure data according to the data of the changed nodes and the data of the unchanged nodes; The updated tree structure data is written into the second file.

9. A tree structure data storage device, characterized in that: include: A data acquisition module, used to acquire tree structure data to be stored; A node traversal module, which is used to start from the leaf node of the tree structure data, traverse each node in the tree structure data except the root node in turn, write the data of the node into the first file and record the corresponding file offset into the data of the parent node of the node, wherein the file offset is used to indicate the storage position of the data of the corresponding node in the first file; A data writing module is used to write the data of the root node into the first file and record index information at a specified position of the first file, wherein the index information is used to determine the storage position of the data of the root node in the first file.

10. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the tree structure data storage method according to any one of claims 1 to 8 is implemented.

11. A computer program product, characterized in that When the computer program product is run on an electronic device, the electronic device executes the tree structure data storage method according to any one of claims 1 to 8.

Citation Information

Cited By

  • Segmented data storage method and device, computer equipment and storage medium

    CN120872260A