A method, device, equipment and storage medium for processing data in a tree structure

By using a combination method of node lock and data row lock in a tree structure, the performance problem of locking mechanism in the prior art in high concurrency scenarios is solved, and higher concurrency performance and lower performance consumption are achieved.

CN112306991BActive Publication Date: 2025-02-14WEBANK (CHINA)
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202011192840.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-10-30
Publication Date
2025-02-14
Estimated Expiration
2040-10-30

AI Technical Summary

Technical Problem

The locking mechanism in the existing financial technology field leads to increased performance consumption in high concurrency scenarios and coarse control granularity, resulting in low concurrency performance.

Method used

The data processing method in the tree structure is adopted to migrate data by activating the node lock of the target node, and fine-grained control is used in the update operation to reduce the number of locks and performance consumption.

Benefits of technology

Improves the concurrency performance of data processing in the tree structure, avoids high-performance consumption caused by coarse-grained lock operations, and reduces performance losses caused by OS thread hangs.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN112306991B_ABST
    Figure CN112306991B_ABST
Patent Text Reader

Abstract

An embodiment of the present application discloses a method and apparatus, device, and storage medium for processing data in a tree structure, wherein the method includes: obtaining the key value of the primary key of an operation request; comparing the key value of the primary key with the key value of a node in the tree structure to obtain a target node; when the operation request is a split operation request, migrating the data of the target node by activating the node lock of the target node; and when the operation request is an update operation request, processing the data row of the target node by activating the data row lock of the target node.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The embodiments of the present application relate to but are not limited to information technology of financial technology (Fintech), and in particular to a method and apparatus, device, and storage medium for processing data in a tree structure. Background Art

[0002] With the development of computer technology, more and more technologies are applied in the financial field. The traditional financial industry is gradually transforming to financial technology (Fintech). However, due to the security and real-time requirements of the financial industry, Fintech has also put forward higher requirements for technology. In the field of Fintech, shared locks (i.e. read locks, read-read will not cause operation blocking, read-write will cause operation blocking), mutex locks (i.e. write locks, read-write will cause operation blocking, write-write will cause operation blocking) and shared mutex locks (i.e. locks between read locks and write locks, read-write will not cause operation blocking, and can be upgraded to mutex locks when there is a write concurrency conflict) cooperate with each other to reduce the blocking waiting of thread concurrent operations to a certain extent. However, the locks involved in the relevant technology have the following problems: First, when accessing the target node, the read and write operations need to lock and unlock all the nodes on the target node path in sequence. Here, the target node path is the route from the root node to the target node. In a high concurrency scenario, this frequent locking and unlocking operation will cause higher and higher performance consumption as the amount of data increases, thereby reducing the processing performance of the memory index; second, the control granularity of the three locks is node level, and the tree structure has the property that one node contains multiple elements, which results in the coarser control granularity and the lower concurrency performance; in addition, the three locks are implemented based on the suspension of the operating system thread, and the thread suspension operation will cause the user state and kernel state of the operating system to switch, resulting in high performance consumption. Summary of the invention

[0003] In view of this, the embodiments of the present application provide a method, device, equipment, and storage medium for processing data in a tree structure to solve at least one problem existing in the related art.

[0004] The technical solution of the embodiment of the present application is implemented as follows:

[0005] On the one hand, an embodiment of the present application provides a method for processing data in a tree structure, the method comprising: obtaining the key value of the primary key of an operation request; comparing the key value of the primary key with the key value of a node in the tree structure to obtain a target node; when the operation request is a split operation request, migrating the data of the target node by activating the node lock of the target node; when the operation request is an update operation request, processing the data row of the target node by activating the data row lock of the target node.

[0006] On the other hand, an embodiment of the present application provides a device for processing data in a tree structure, the device comprising: an acquisition module, used to acquire the key value of a primary key of an operation request; a comparison module, used to compare the key value of the primary key with the key value of a node in the tree structure to obtain a target node; a migration module, used to migrate data of the target node by activating the node lock of the target node when the operation request is a split operation request; and a processing module, used to process data rows of the target node by activating the data row lock of the target node when the operation request is an update operation request.

[0007] On the other hand, an embodiment of the present application provides a data processing device in a tree structure, including a memory and a processor, wherein the memory stores a computer program that can be executed on the processor, and the processor implements the steps in the above method when executing the program.

[0008] On the other hand, an embodiment of the present application provides a computer-readable storage medium having a computer program stored thereon, and the computer program implements the steps in the above method when executed by a processor.

[0009] The method for processing data in a tree structure provided by an embodiment of the present application, on the one hand, when the operation request is a split operation request, the data of the target node is migrated by activating the node lock of the target node, so that the data security of the data split operation on the node can be guaranteed by adding a node lock to the node. On the other hand, when the operation request is an update operation request, the data row of the target node is processed by activating the data row lock of the target node, so that in the scenario of operating the data of the target node, compared with the node lock, the data row lock is a lightweight and fine-grained lock, and the data row locking operation is fine-grained, avoiding the coarse-grained lock operation of node locking, thereby reducing the number of locks. Further, the use of the data row lock avoids the high-performance consumption caused by the coarse-grained lock operation when operating a large amount of data, thereby improving the concurrent performance of data processing in the tree structure, avoiding the switching of user mode and kernel mode caused by the thread suspension in the operating system when using non-lightweight locks, and further avoiding the performance consumption when switching kernels. BRIEF DESCRIPTION OF THE DRAWINGS

[0010] Figure 1A This is a schematic diagram of the implementation flow of the method for processing data in a tree structure according to an embodiment of the present application;

[0011] Figure 1B This is a schematic diagram of the data structure of a node in a T-type tree structure according to an embodiment of the present application;

[0012] Figure 1C Schematic diagram of three different forms of tree structures of the embodiments of the present application;

[0013] Figure 1D This is a schematic diagram of the data structure of a node in the tree structure of an embodiment of the present application;

[0014] Figure 2A This is a schematic diagram of the implementation flow of the method for processing data in a tree structure according to an embodiment of the present application;

[0015] Figure 2B This is a schematic diagram of the implementation flow of the method for processing data in a tree structure according to an embodiment of the present application;

[0016] Figure 2C This is a schematic diagram of the implementation flow of the method for processing data in a tree structure according to an embodiment of the present application;

[0017] Figure 3A It is a schematic diagram of the T-tree node data structure in the related art;

[0018] Figure 3B It is a schematic diagram of a T-tree structure in the related art;

[0019] Figure 4 A schematic diagram of the lock control granularity of the method for processing data in a tree structure according to an embodiment of the present application;

[0020] Figure 5 A schematic diagram of a node data structure of a method for processing data in a tree structure according to an embodiment of the present application;

[0021] Figure 6 A schematic diagram of the structure of a device for processing data in a tree structure according to an embodiment of the present application;

[0022] Figure 7 A schematic diagram of a hardware entity of a computer device in an embodiment of the present application. DETAILED DESCRIPTION

[0023] In order to make the purpose, technical solutions and advantages of the present application clearer, the technical solutions of the present application are further elaborated in detail below in conjunction with the drawings and embodiments. The described embodiments should not be regarded as limiting the present application. All other embodiments obtained by ordinary technicians in the field without making creative work are within the scope of protection of the present application.

[0024] In the following description, reference is made to “some embodiments”, which describe a subset of all possible embodiments, but it will be understood that “some embodiments” may be the same subset or different subsets of all possible embodiments and may be combined with each other without conflict.

[0025] If similar descriptions of "first / second" appear in the application documents, the following instructions are added. In the following description, the terms "first\second\third" involved are merely used to distinguish similar objects and do not represent a specific ordering of the objects. It can be understood that "first\second\third" can be interchanged in a specific order or sequence where permitted, so that the embodiments of the present application described herein can be implemented in an order other than that illustrated or described herein.

[0026] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as those commonly understood by those skilled in the art to which this application belongs. The terms used herein are only for the purpose of describing the embodiments of this application and are not intended to limit this application.

[0027] The technical solution of the present application is further described in detail below with reference to the accompanying drawings and embodiments.

[0028] The present application provides a method for processing data in a tree structure. Figure 1A Schematic diagram of the implementation flow of the method for processing data in the tree structure of the embodiment of the present application, such as Figure 1A As shown, the method includes:

[0029] Step S101, obtaining the key value of the primary key of the operation request;

[0030] Here, the operation request may be a read operation request or a write operation request. In some embodiments, the write operation request may be a split operation performed because the node space to be written is full, or a data update operation performed on the data row of the node to be written.

[0031] In some embodiments, the data to be written or read is often stored in the form of key-value pairs. Therefore, when executing the operation request, it is necessary to perform the operation according to the key value.

[0032] Step S102, comparing the key value of the primary key with the key value of the node in the tree structure to obtain a target node;

[0033] Here, the tree structure may be a T-tree, which is a variant of a balanced binary tree. The balance condition is consistent with that of a balanced binary tree, that is, the height difference between the left and right subtrees cannot exceed 1. The difference from a balanced binary tree is that a node of a balanced binary tree contains one element, while a node of a T-tree contains a linked list that can contain more elements. Here, the elements are stored in the form of key values.

[0034] In some embodiments, the key value of the node may be a range, and the key value of the primary key may be compared with the key value of the node to determine whether the key value of the primary key is within the range of the key value of the node. If the key value of the primary key is within the range of the key value of the node, the corresponding data may be queried in the linked list of the node, and therefore, the node is determined to be the target node to be operated by the operation request.

[0035] In some other embodiments, the tree structure may be a balanced binary tree, the node contains one element, and the comparison of the key value of the primary key with the key value of the node may be to determine whether the key value of the primary key is equal to the key value of the node. If the key value of the primary key is equal to the key value of the node, the node is determined to be the target node to be operated by the operation request.

[0036] Step S103: when the operation request is a split operation request, data migration is performed on the data of the target node by activating the node lock of the target node;

[0037] Here, the split operation is an operation of splitting the data on the node. Through the split operation, the data on the node can be split into two nodes, that is, the data of the node is split into the node and the split node.

[0038] Here, the node lock is used to lock the node, which is a coarse-grained lock that controls the operation of each node and includes a mutex lock to ensure data security during concurrent operations.

[0039] Step S104: When the operation request is an update operation request, data processing is performed on the data row of the target node by activating the data row lock of the target node.

[0040] Here, the object of the update operation is the data row of the node. When operating on the data row, in order to ensure data consistency when multiple threads read the same object data, it is necessary to activate the data row lock of the target node before the update operation is performed on the data row.

[0041] Here, it should be noted that the control granularity of data row lock and node lock increases successively.

[0042] The method for processing data in a tree structure provided by an embodiment of the present application, on the one hand, when the operation request is a split operation request, the data of the target node is migrated by activating the node lock of the target node, so that the data security of the data split operation on the node can be guaranteed by adding a node lock to the node. On the other hand, when the operation request is an update operation request, the data row of the target node is processed by activating the data row lock of the target node, so that in the scenario of operating the data of the target node, compared with the node lock, the data row lock is a lightweight and fine-grained lock, and the data row locking operation is fine-grained, avoiding the coarse-grained lock operation of node locking, thereby reducing the number of locks. Further, the use of the data row lock avoids the high-performance consumption caused by the coarse-grained lock operation when operating a large amount of data, thereby improving the concurrent performance of data processing in the tree structure, avoiding the switching of user mode and kernel mode caused by the thread suspension in the operating system when using non-lightweight locks, and further avoiding the performance consumption when switching kernels.

[0043] The present invention provides a method for processing data in a tree structure, the method comprising:

[0044] Step S110, obtaining the key value of the primary key of the operation request;

[0045] Step S120, comparing the key value of the primary key with the key value of the node in the tree structure to obtain a target node;

[0046] Step S130: when the operation request is a split operation request, data migration is performed on the data of the target node by activating the node lock of the target node;

[0047] Step S140: When the operation request is an update operation request, data processing is performed on the data row of the target node by activating the data row lock of the target node.

[0048] The data row of the target node includes a thread ID and an operation in progress status, and the data row lock includes at least: a partial lock, a spin lock, and a wait lock.

[0049] Here, the data row of the node is stored in the data domain of the node, and the data row may include multiple fields, for example, may include: thread ID (thread_id), operation in-progress state (on_way) and data (data), etc.

[0050] Here, the partial lock is referred to as B lock. It records the ID of a thread through the target data row and declares that the data row belongs to this thread. After that, the thread can operate on this data row without locking it. It is the lightest lock and its performance consumption can be ignored.

[0051] Here, the spin lock is referred to as O lock. It controls the operation of multiple threads on the target data row at the same time through Compare and Swap (CAS) + version number. Each time the control of the target data row is seized, the corresponding operation is performed. If the operation is successful, the version number of the target data row is incremented by 1. If the operation fails, the target data row data and the latest version number are re-acquired, and the operation is performed again according to the latest version number. The thread that fails to seize control will poll and wait outside the data row. It is a medium-level lock and consumes a small amount of CPU performance.

[0052] Here, the waiting lock is referred to as W lock, which suspends the writing thread through the underlying operating system and waits until it wakes up and then tries to continue the operation. This will cause the operating system to switch between user mode and kernel mode, and the performance overhead is relatively large.

[0053] In some embodiments, the step S140, performing data processing on the data row of the target node by activating the data row lock of the target node, includes:

[0054] Step S141, when the thread ID is empty and the operation in progress state field is no, the data row of the target node is processed through the activated partial lock;

[0055] In some embodiments, the thread ID is empty, indicating that no thread is currently using the data row, that is, the data row is not used to record data generated by any thread; the operation in progress status field is no, indicating that no thread is currently operating on the data row, so the data row can be directly operated at this time. By default, the state at this time is the partial lock activation state. After determining that it is the partial lock activation state, the thread ID of the current thread is recorded in the thread_id field of the data row through CAS, that is, the data row is declared to belong to this thread ID, and the on_way field is set to Y at the same time, indicating that a thread is currently operating on the data row. After the update operation is completed, the on_way field of the data row is set to N, indicating that no thread is currently operating on the data row in the main memory.

[0056] In some embodiments, when the thread ID is not empty, it is determined whether the thread_id of the data row in the main memory is the same as the thread ID of the current thread. 1) When the thread_id of the data row in the main memory is the same as the thread ID of the current thread, the operation is directly performed on the data row without any locking operation, and the on_way field is set to Y, indicating that a thread is currently operating on the data row. After the operation is completed, the on_way field of the data row is set to N, indicating that no thread is currently operating on the data row.

[0057] 2) When the thread_id of the data row in the main memory is different from the thread ID of the current thread, it is determined whether the on_way field of the data row in the main memory is N. If it is N, it means that no thread is operating on the data row at this time, then partial locking redirection is performed, and the thread_id of the data row in the main memory is updated to the thread ID of the current thread through CAS, and the on_way field is set to Y at the same time, indicating that a thread is operating on the data row at this time. After the update operation is completed, the on_way field of the data row is set to N, indicating that no thread is operating on the data row at this time.

[0058] Step S142, when the thread ID of the data row of the target node is different from the ID of the request primary key value and the operation in progress state field is yes, the data row of the target node is processed through the activated spin lock;

[0059] Here, the operation in progress state field indicates that a thread is currently operating the target data row, and a lock upgrade is performed on the data row, using a spin lock operation.

[0060] Here, to implement the spinlock operation, it is necessary to add a tag field version in the data row to implement the spinlock. The spinlock is based on the comparison of the actual version number of the data row in the main memory with the expected version number of the data row in the operation thread. If the match is the same, the operation is performed on the data row in the main memory, and the data row version number is incremented by 1. If the version number match is not the same, the latest content and the latest version number of the data row in the main memory are re-acquired. The latest content is used as the basic content of the operation, and the latest version number is used as the new expected version number of the data row in the operation thread. The operation is matched and executed again. The thread that fails to grab the spinlock polls and waits outside the target data row, and a small amount of CPU performance is consumed during the loop.

[0061] In some embodiments, after the spin lock operation is adopted, the thread that wants to operate this data row needs to continuously poll the target data row in the main memory until the on_way field of the target data row becomes N, and then start competing for the target data row. Once a thread first sets the on_way field of the target data row to Y and updates the thread_id to its own thread ID, it means that this thread has obtained control of the target data row. At this time, the actual version number of the target data row in the main memory is compared with the expected version number of the target data row in the thread that currently obtains control. If the match is the same, the operation is performed on the data row in the main memory, and the data row version number is incremented by 1. If the version number does not match, the latest value and the latest version number of the data row in the main memory are re-queried. The latest value is used as the original value of the update operation, and the latest version number is used as the new expected version number of the data row in the operating thread. Then a match is made again to perform the update operation.

[0062] In some embodiments, after the operation on the data row is completed, the on_way field of the target data row needs to be set to N, indicating that no thread is currently operating on the data row.

[0063] In some embodiments, after the update operation is executed, the on_way field is set to N, the spin lock is released, the operation is terminated, and other threads that may be in a suspended waiting state and want to operate this data row are awakened, so that the threads can seize the control of the data row. When all concurrent threads are processed, the lock of the target data row is automatically downgraded to a partial lock, and the lock type is converted from a medium-weight spin lock to a lightweight partial lock, saving performance consumption.

[0064] Step S143: When the number of times the spin lock polls the data row exceeds a preset threshold, the data row of the target node is processed by the activated wait lock.

[0065] In some embodiments, since the spin lock is a medium-sized lock, and it will wait for operation opportunities by polling at the cost of a small amount of CPU performance, if the concurrency of a certain data row is in an abnormally high state, and the thread operation currently being processed is relatively time-consuming, a large number of request threads will be piled up, and the CPU will be wasted waiting, thereby filling up the CPU load, which will cause performance loss that is not worth the gain. Therefore, when the number of times the spin lock polls the data row exceeds a preset threshold, the spin lock needs to be upgraded to a waiting lock. For example, when a thread polls the target data row more than 20 times, it can be upgraded to a waiting lock. When the waiting lock is activated, the write thread can be put into a suspended waiting state through the operating system to release CPU resources until the thread being processed releases the control of the target data row, and wakes up the thread in the waiting state to participate in a new round of competition for the control of the target data row. Avoid keeping the spin lock in a polling state, which results in a large amount of CPU resources being occupied.

[0066] In some embodiments, when a thread in the awakened waiting state participates in a new round of competition for control of a target data row, the waiting lock is downgraded to a spin lock, and the thread that obtains control enters the target data row to perform operations, while other awakened competing threads restart polling and waiting; when all threads have been processed, the lock of the target data row is downgraded to a partial lock again.

[0067] Here, it should be noted that since the triggering of the wait lock will cause the switching of the operating system user state and kernel state, it will cause a large resource overhead, so it is a heavyweight lock and is only enabled when the multi-threaded concurrency is the most serious. In the actual system operation, such extreme cases are generally rare. Most of the time, the multi-threaded concurrency safety problem can be solved at the cost of a small performance overhead through partial locks and spin locks, thereby improving the concurrency performance of data processing in the improved tree structure.

[0068] Here, it should be noted that the performance consumption levels of partial lock, spin lock, and wait lock increase in sequence.

[0069] The method for processing data in a tree structure provided by an embodiment of the present application, on the one hand, processes the data row of the target node through the data row lock of the activated target node, so that the ID of the thread can be recorded through the target data row using a partial lock, and the target data row can be declared to belong to this thread, so when the target data row encounters a thread with the same thread ID again, it can be directly operated without any locking operation. Moreover, the partial lock is extremely low in terms of performance consumption and can be basically ignored. On the other hand, when the thread ID of the data row of the target node is different from the ID of the requested primary key value and the operation in transit status field is yes, the data row of the target node is processed through the activated spin lock, so that when there is a thread operating on the target data row, the partial lock can be upgraded to a spin lock, and the target data row is continuously polled. When there is no thread operating on the target data row, the target data row is started to compete to ensure the security of the data. In addition, when the number of times the spin lock polls the data row exceeds a preset threshold, the data row of the target node is processed through the activated waiting lock, so that the thread can enter a suspended waiting state through the operating system, releasing CPU resources until the thread in the processing releases the control right of the target data row, and the thread in the waiting state is awakened to participate in a new round of competition for the control right of the target data row. In this way, lock processing with different levels of performance consumption can be performed according to the different states of the target data row being accessed by the thread, saving the performance overhead of the lock and CPU resources, and improving the processing performance of data in the tree structure.

[0070] The present invention provides a method for processing data in a tree structure, the method comprising:

[0071] Step S210, obtaining the key value of the primary key of the operation request;

[0072] Step S220, comparing the key value of the primary key with the key value of the node in the tree structure to obtain a target node, wherein the data structure of each node in the tree structure includes at least: a first-level index; wherein the first-level index storage space is an integer multiple of the CPU cache line storage space;

[0073] Here, the cache line (CACHE_LINE) is the minimum storage unit and operation unit of the CPU cache. The cache line is an integer power of 2 consecutive bytes, generally ranging from 32 to 256 bytes. For example, the default cache line size in the Linux system is 64 bytes.

[0074] It should be noted here that in the related technology, there may be a situation where data of at least two nodes are stored in one cache line. Therefore, when data in a cache line is processed by a multi-core CPU, one of the cores locks and unlocks the data in the cache line. When updating the data, it will cause the cache data of other cores to be inconsistent with the updated data, thereby causing the data cache in the cache of other cores to fail. After the cache fails frequently, the data validity of the cache will be low, the cache function cannot be effectively utilized, and the system performance will be seriously degraded.

[0075] Here, the first-level index storage space is an integer multiple of the CPU cache line storage space, which can ensure that the data to be given to the node is stored in one cache line, and can avoid the problem that the cache function cannot be effectively utilized when the data of at least two nodes is stored in one cache line, causing a serious decline in system performance.

[0076] In some embodiments, the primary index further includes: a minimum key value and a maximum key value. The step S220 of comparing the key value of the primary key with the key value of the node in the tree structure to obtain the target node includes:

[0077] Here, the minimum key value represents the key value corresponding to the minimum key in the node, and the maximum key value represents the key value corresponding to the maximum key in the node.

[0078] Step S221A, comparing the key value of the primary key with the minimum key value in the primary index of the node to obtain a first comparison result;

[0079] Here, the first comparison result indicates whether the key value of the primary key is less than the minimum key value;

[0080] Step S222A, determining a target node according to the first comparison result;

[0081] In some embodiments, the step S222A, determining the target node according to the first comparison result, includes:

[0082] Step S2221A, when the first comparison result is less than the minimum key value in the primary index of the node and the node has a left subtree, the left subtree node of the node is used as the node to be compared;

[0083] In some embodiments, if the key value of the primary key is less than the minimum key value and the current node has a left subtree, the left subtree is entered to continue searching.

[0084] Step S2222A, comparing the key value of the primary key with the minimum key value and the maximum key value in the primary index of the node to be compared, to obtain a third comparison result;

[0085] Here, the node to be compared is a left subtree node, and the maximum key value corresponding to the left subtree node is smaller than the minimum key value of the node.

[0086] Step S2223A: When the third comparison result indicates that the key value of the primary key belongs to the key value range of the node to be compared, determine the node to be compared as the target node.

[0087] In some embodiments, the primary index further includes a primary key field, and the data structure of each node in the tree structure further includes a secondary index. In step S222A, after determining the target node according to the first comparison result, the method further includes:

[0088] Here, the primary key field is stored in the form of a linked list, for example, a bidirectional linked list, and the content stored in the primary key field is the elements included in the node. Here, the secondary index is a data field, and the method for accessing the data in the secondary index is to find the corresponding data row according to the subscript and read the data record in the data.

[0089] Here, the format of the data in the data field is DATA<key,data,thread_id,on_way,version> , where key is the primary key, data is the actual stored data, thread_id records the thread ID, on_way records whether a thread is currently operating on the data row, and version is the version number of the actual data. Figure 1B Schematic diagram of the data structure of the nodes in the T-tree structure of the embodiment of the present application, such as Figure 1B As shown, an array is used to store data. The array size is calculated based on the primary index capacity. The primary index capacity defaults to 200, which can store 400 key values. The array size is set to 400.

[0090] Step S2224A, traversing the primary key field of the target node, wherein the primary key field at least includes a key value;

[0091] Step S2225A, when the key value in the primary key field matches the key value of the primary key, determining the storage address corresponding to the key value of the primary key;

[0092] Here, determining the storage address corresponding to the key value of the primary key includes: determining the hash value corresponding to the key value of the primary key using a hash function; and performing modulo operation on the hash value and the capacity to obtain the storage address corresponding to the key value of the primary key.

[0093] For example, the formula of the hash function is as follows:

[0094] hash(key)=key[0]*31^(n-1)+key[1]*31^(n-2)+...+key[n-1]*31^(nn);

[0095] Here, key[n] is the ASCII code value of the nth character of the primary key key, n is the length of the primary key key, hash(key) is the result of calculating the key value based on the hash function, and ^ represents exponentiation.

[0096] For example, Figure 1D This is a schematic diagram of the data structure of the nodes in the tree structure of the embodiment of the present application, such as Figure 1D As shown, when the key value of the requested primary key is b, the address subscript of the secondary index for b is calculated as: [ASCII(b)*31^(1-1)]%400=98, and the target data row 11 with the secondary index address subscript 98 in the main memory is located.

[0097] Step S2226A, determining the target data row in the secondary index according to the storage address, wherein the data row at least includes: a key value and data;

[0098] Here, the storage address is used to determine the target data row, and the primary key value is used to compare with the key value of the target data row again. If they are the same, the data is read.

[0099] Step S2227A, when the key value of the target data row matches the primary key value, returns the data of the target data row; when the key value in the primary key field does not match the key value of the primary key, returns a null value to indicate that the key value of the requested primary key does not exist.

[0100] Here, in the case of mismatch, linear detection is first performed, and when a matching key value is detected, the data of the target data row is returned; otherwise, a null value is returned.

[0101] In some embodiments, the method further includes: step S223A, when the first comparison result is less than the minimum key value in the primary index of the node and the node does not have a left subtree and the key value of the primary key is less than the left pointer of the node, taking the previous node of the node as the node to be compared;

[0102] It should be noted here that when the first comparison result is less than the minimum key value in the primary index of the node and the node does not have a left subtree and the key value of the primary key is less than the left pointer of the node, the tree structure rotates and the actual requested key value cannot be obtained.

[0103] In some embodiments, the tree structure rotates when: Figure 1C Schematic diagram of three different tree structures of the embodiment of the present application, such as Figure 1C As shown in (a), assuming that the requested primary key value is actually in the target node PL, then when the read operation reads the P node and finds that the requested primary key value is smaller than the range of the P node, and is about to continue reading the left subtree PL of the P tree, another write operation simultaneously inserts the node PLRR, causing the absolute value of the height difference between the left and right subtrees of the P node of the T tree to exceed 1 (the maximum height of the left subtree PL of the P node is 3, the maximum height of the right subtree PR is 1, and the absolute value of the difference is 2), and the T tree is unbalanced. Figure 1C (b) (c) rotation transformation, the final transformation is Figure 1C In the structure (c), the left subtree of the P node becomes a PLRR node, and the PLRR node does not contain the key value of the requested primary key.

[0104] Here, in the process of in-order traversal, it is necessary to jump to the previous node of the current node according to the left pointer of the current node, and continue to search for the target node. Here, the in-order traversal is to traverse the tree structure in the order of first visiting the left subtree, then visiting the root node, and finally visiting the right subtree.

[0105] For example, Figure 1C As shown in (c), the tree structure rotates. When performing a read operation, read the left subtree PLRR node of P, determine whether the requested primary key value is less than the minimum key value of PLRR, and if PLRR does not exist in the left subtree, then determine whether the requested primary key value is less than the left pointer of PLRR. If it is greater, the value does not exist and a null value is returned; if it is less, it means that the path of the target node has been changed, and jump to the previous node PLR ​​of the in-order traversal of PLRR according to the left pointer, and then compare the requested primary key value with the maximum key value and the minimum key value of the node PLR. If it is less than the minimum key value, access the left subtree PL of the node PLR ​​to find the target node PL, and then traverse the primary key domain linked list of the PL node. If the existing primary key is hit, the secondary index is retrieved according to the hash value of the primary key, the key value is compared, the data is read and returned, and if the existing primary key is not hit, a null value is returned to indicate that the data does not exist.

[0106] Step S224A, when the key value of the primary key is greater than the left pointer of the node, returns a null value to indicate that the key value of the requested primary key does not exist.

[0107] In some other embodiments, the primary index further includes: a minimum key value and a maximum key value. The step S220 of comparing the key value of the primary key with the key value of the node in the tree structure to obtain the target node includes:

[0108] Step S221B, comparing the key value of the primary key with the maximum key value in the primary index of the node to obtain a second comparison result;

[0109] Here, the second comparison result indicates whether the key value of the primary key is greater than the maximum key value;

[0110] Step S222B, determining a target node according to the second comparison result;

[0111] In some embodiments, the step S222B, determining the target node according to the second comparison result, includes:

[0112] Step S2221B: when the second comparison result is greater than the maximum key value in the primary index of the node and the node has a right subtree node, determine the right subtree node of the node as the node to be compared;

[0113] Step S2222B, when the key value of the primary key is greater than the right pointer of the node, the next node of the node is determined as the node to be compared.

[0114] Step S2223B, comparing the key value of the primary key with the minimum key value and the maximum key value in the primary index of the node to be compared, to obtain a fourth comparison result;

[0115] Here, the node to be compared may be the right subtree node of the node in step S2221B, or may be the next node of the node in step S2222B.

[0116] Step S2224B: when the fourth comparison result indicates that the key value of the primary key belongs to the key value range of the node to be compared, determine the node to be compared as the target node.

[0117] The method for processing data in a tree structure provided by the embodiment of the present application, on the one hand, by making the first-level index storage space an integer multiple of the CPU cache line storage space, it can ensure that the data to be given to the node is stored in one cache line, which can avoid the problem that the cache function cannot be effectively utilized when the data of at least two nodes are stored in one cache line, causing the system performance to seriously decline. On the other hand, the key value of the primary key is compared with the key value of the node in the tree structure to obtain the target node; the primary key field of the target node is traversed, wherein the primary key field at least includes the key value; when the key value in the primary key field matches the key value of the primary key, the storage address corresponding to the key value of the primary key is determined; according to the storage address, the target data row is determined in the secondary index, so that lock-free reading of the tree structure is realized, which greatly improves the concurrency of the read operation of the memory index and eliminates the performance loss caused by the lock. In addition, when the first comparison result is less than the minimum key value in the primary index of the node and the node does not have a left subtree and the key value of the primary key is less than the left pointer of the node, the previous node of the node is determined as the node to be compared; when the key value of the primary key is greater than the right pointer of the node, the next node of the node is determined as the node to be compared. In this way, lock-free read operations are achieved by adding left and right pointers to the tree structure, thereby reducing the number of locks and the amount of locking and unlocking operations.

[0118] The present application provides a method for processing data in a tree structure. Figure 2A Schematic diagram of the implementation flow of the method for processing data in the tree structure of the embodiment of the present application, such as Figure 2A As shown, the method includes:

[0119] Step S201, obtaining the key value of the primary key of the operation request;

[0120] Step S202, comparing the key value of the primary key with the minimum key value in the primary index of the node to obtain a first comparison result, wherein the data structure of each node in the tree structure at least includes: a primary index and a secondary index, and the primary index also includes: a minimum key value, a maximum key value and a primary key field; wherein the primary index storage space is an integer multiple of the CPU cache line storage space;

[0121] Step S203: determine the target node according to the first comparison result:

[0122] Step S204, traversing the primary key field of the target node, wherein the primary key field at least includes a key value;

[0123] Step S205, when the key value in the primary key field does not match the key value of the primary key, add the key value of the primary key to the primary index of the target node and determine whether the primary index is full;

[0124] For example, when the key value in the primary key field does not match the key value of the primary key, the key value of the primary key is added to the primary index, and at the same time, it is determined whether the primary index is full.

[0125] Step S206, when the primary index is not full, adding the value corresponding to the key value of the primary key to the secondary index of the target node to complete the data writing operation;

[0126] Step S207: When the primary index is full, determine that the operation request is a split operation request.

[0127] In some embodiments, the method also includes: step S205A, when the key value of the primary key field of the target node matches the key value of the primary key, updating the data of the target data row corresponding to the key value of the primary key to the data corresponding to the key value of the primary key to complete the data writing operation.

[0128] In some embodiments, the method further includes: step S208A, activating a node lock of the target node if the operation request is a split operation request;

[0129] For example, if the first-level index is full, the node lock mutex of the target node is activated and the node is split. At this time, all other threads that want to read and write the target node are suspended and waiting.

[0130] Step S209A, when the target node does not have a right subtree node, create a right subtree node and activate the node lock of the right subtree node;

[0131] For example, determine whether the target node has a right subtree. If not, create a new right subtree and activate the node lock mutex of the new right subtree.

[0132] Step S210A: storing the migration data on the established right subtree node, and releasing the node lock to complete the data migration.

[0133] In some embodiments, the method further includes: step S211A, adding the numerical value corresponding to the key value of the primary key to the secondary index of the right subtree node after the data migration to complete the data writing operation.

[0134] In some embodiments, the method further includes: step S209B, if the target node has a right subtree, activating a node lock of the right subtree;

[0135] Step S210B, dividing the data of the primary index of the target node into retained data and migrated data, wherein the retained data is retained in the target node;

[0136] Step S211B: store each data in the migration data on the split node of the right subtree and release the node lock to complete the data migration.

[0137] For example, if the right subtree exists, the node lock mutex of the right subtree is activated, and then according to the primary index of the target node, such as Figure 2B , n is the maximum number of elements that a primary index can hold, the default is 400, 75% of the node data from left to right is taken as the retained data, and the remaining data is taken as the migration data. According to the right subtree address of the target node, it is migrated to its right subtree. While migrating, it is determined whether the capacity of the right subtree node is full. If not, the node splitting operation is completed, and the node lock mutex of the above nodes is released at the same time, waking up the external suspended waiting thread.

[0138] If there is a right subtree and the node capacity of the right subtree is full, such as Figure 2C , then continue to determine whether the right subtree node of the right subtree exists. If not, create a new right subtree of the right subtree and activate its node lock mutex. If it exists, activate the node lock mutex of the right subtree of the right subtree, and at the same time take 75% of the data from left to right in the right subtree node as the reserved data, and the remaining data as the migration data, and migrate to the right subtree node of the right subtree as a whole. This cycle is repeated until all data is migrated to the right subtree layer by layer, and all nodes are in a non-full state, so as to complete the data migration.

[0139] In some embodiments, after completing the data migration, the method further includes: determining whether the improved T-tree is in a balanced state, and if it is unbalanced, performing a rotation operation to rebalance the improved T-tree, and finally, releasing the node lock X-lock of the node activated in the above steps.

[0140] In some embodiments, the method further includes: step S212B, adding the numerical value corresponding to the key value of the primary key to the secondary index of the split node to complete the data writing operation.

[0141] In some embodiments, after the input write operation is completed, a preamble is further used to wake up the external suspended waiting thread.

[0142] The method for processing data in a tree structure provided in an embodiment of the present application completes the data migration by storing each data in the migrated data on the split node of the right subtree and releasing the node lock. In this way, data loss caused by data overflow due to full storage space during the process of completing data writing can be avoided.

[0143] In the related art, taking T-tree as an example, the process of using T-tree to build memory index and T-tree index concurrency control is as follows:

[0144] (1) Use T-tree to build memory index:

[0145] According to the key value of the requested primary key, traverse and load the T-tree index nodes in memory until the key value is equal to the key value in a certain T-tree node, then return the corresponding data, otherwise return empty to indicate that the data does not exist.

[0146] Figure 3A is a schematic diagram of the T-tree node data structure in the related art, such as Figure 3A As shown, the data structure of the T-tree node includes a parent node address, node control information, a left subtree address, a right subtree address, and a data field storing an actual data list.

[0147] (2) T-tree index concurrency control

[0148] Here, the T-tree index concurrency control includes at least: read concurrency control and write concurrency control:

[0149] Among them, the nodes involved in read concurrency control can be seen in Figure 3B As shown, the steps of read concurrency control include:

[0150] Step S11, read the root node P of the T tree, and add an S lock (shared lock) to the P node;

[0151] Step S12: According to the requested primary key key, traverse downward from the root node P and compare. If the key value is smaller than the P node range, the next node PL is obtained, and the S lock is added to the PL node, while the S lock is released from the P node.

[0152] Step S13, continue to traverse downwards for comparison, assuming that the key value is greater than the PL node range, then obtain the next node PLR, add an S lock to the PLR ​​node, and release the S lock on the PL node;

[0153] Step S14, continue to traverse downwards for comparison. Assuming that the key belongs to the PLR ​​node range, traverse the data list in the PLR ​​node, compare the key value, and obtain the corresponding data value. If the key value exists in the list, return the corresponding data. If not, return null. At the same time, release the S lock on the PLR ​​node.

[0154] In step S15, the S locks on the nodes can be repeatedly superimposed without causing operation blocking, but a node with an S lock cannot be superimposed with an X lock, which will cause operation blocking waiting. Therefore, when the node is read by a thread, other threads cannot modify the target node, and the target node path will not change due to the modification operation, and the actual value will not be found.

[0155] Among them, the nodes involved in write concurrency control can also be seen Figure 3B As shown, the steps for writing concurrency control are:

[0156] Step S21, read the root node P of the T tree, and add an SX lock (shared mutex lock) to the P node;

[0157] Step S22: according to the requested primary key key, traverse downward from the root node P and compare. If the key is smaller than the P node range, the next node PL is obtained, and the SX lock is added to the PL node. At the same time, it is determined whether the PL node space is full. If not, the SX lock of the P node is released. Otherwise, the SX lock of the P node is not released.

[0158] Step S23, continue to traverse downwards for comparison, assuming that the key is smaller than the PL node range, then obtain the next node PLL, add an SX lock to the PLL node, and determine whether the PLL node space is full. If not, release the SX lock on the PL node, otherwise, do not release the SX lock on the PL node;

[0159] Step S24, continue to traverse downward and compare. Assuming that the key is within the range of the PLL node, add an X lock to the node PLL, and determine whether the PLL node space is full. If not, directly insert the new value, and then release the X lock (mutually exclusive lock) of the node PLL, and release the SX lock of the node holding the SX lock in the above step; if it is full, upgrade the node holding the SX lock in the above step to an X lock, and then insert the new value into the PLL node, and add a right child node PLLR to PLL, and migrate the overflowed data to the PLLR node. Finally, determine whether the T tree is balanced. If it is unbalanced, the T tree structure is rotated to achieve balance.

[0160] Step S25: After the data addition and the T-tree structure adjustment are completed, the X locks of the nodes upgraded to X locks in step 4 are uniformly released.

[0161] Step S26: X locks on nodes cannot be overlapped repeatedly. For a node holding an X lock, the read and write operations of other threads will be blocked outside the node and wait.

[0162] It can be seen that there are two shortcomings in the related technology:

[0163] (1) The three types of locks (S lock, X lock, and SX lock) work together to reduce the blocking and waiting of concurrent thread operations to a certain extent. However, the disadvantages are also obvious. First, when accessing the target node, read and write operations need to lock and unlock all nodes on the target node path in sequence. In high-concurrency scenarios, this frequent locking and unlocking operation will cause higher and higher performance consumption as the amount of data increases, thereby reducing the processing performance of the memory index. Second, the control granularity of the three locks is at the node level, and the T-tree itself has the property that one node contains multiple elements. The coarser the control granularity, the lower the concurrency performance. In addition, the three locks are implemented based on the suspension of the operating system's threads, and the thread suspension operation will cause the operating system to switch between the user state and the kernel state. Performance consumption is also a major shortcoming.

[0164] (2) In today's multi-core CPU processors, each core has a private cache, but the T-tree node is designed without considering the cache. When the data in a cache line is read by multiple cores, one of the cores locks and unlocks the data in the cache line, which will cause the data in the cache of other cores to frequently fail. The cache cannot be utilized, and the system performance is seriously degraded.

[0165] To solve the above two problems, the present application provides a method for processing data in a tree structure, which reduces the performance loss of the data in the tree structure in a high-concurrency scenario and ensures multi-threaded concurrency safety. The method for processing data in a tree structure provided by the present application is implemented through a data row lock 41 and a node lock 42. Figure 4 FIG. 1 is a schematic diagram of the lock control granularity of the method for processing data in a tree structure according to an embodiment of the present application. Figure 4 As shown, they are divided into two categories based on the control granularity. One is the data row lock 41, which is a fine-grained lock that controls the operation of each piece of data, including a partial lock (B lock), a spin lock (O lock), and a wait lock (W lock); the other is the node lock 42, which is a coarse-grained lock that controls the operation of each node and only includes a mutex lock (X lock).

[0166] Figure 5 Schematic diagram of the node data structure of the method for processing data in the tree structure of the embodiment of the present application, such as Figure 5As shown, the thread_id and on_way marking fields are added to the data row of data domain 51 to implement partial lock. The partial lock records the thread ID through the target data row and declares that the target data row belongs to this thread. Then, when the target data row encounters a thread with the same thread ID again, it can be directly operated without any locking operation, and the performance consumption can be ignored. The version marking field is added to the data row to implement the spin lock. The spin lock is based on the actual version number of the data row in the main memory and the expected version number of the data row in the operation thread. If the match is the same, the operation is performed on the data row in the main memory, and the data row version number is incremented by 1. If the version number match is not the same, the latest content and the latest version number of the data row in the main memory are re-acquired. The latest content is used as the basic content of the operation, and the latest version number is used as the new expected version number of the data row in the operation thread. The matching operation is performed again. The thread that does not grab the spin lock polls and waits outside the target data row, and a small amount of CPU performance is consumed during the loop. The waiting lock is to wait according to the operating system to suspend the thread to ensure the safety of multi-threaded concurrent operations. The performance consumption levels of partial lock, spin lock, and wait lock increase in sequence, and the control granularity of data row lock and node lock increases in sequence. Figure 5 As shown in the figure, a left pointer, a right pointer, and a node lock mark are added to each node in the tree structure. The left pointer and the right pointer are used to implement lock-free read operations, reducing the number of locks and the amount of lock and unlock operations. In this way, performance optimization and concurrency safety can be achieved by controlling the number, granularity, and performance consumption of locks. Figure 5 As shown in the figure, the data structure of the node is divided into a primary index and a secondary index. The primary index is an integer multiple of the CPU buffer row size and stores frequently used data. When the CPU reads data from the memory in a whole row, the data loaded into the cache is frequently used data, so that the cache can play the maximum performance role. The secondary index stores the actual data value, and the usage frequency is lower than that of the data in the primary index. It does not need to be fully loaded into the cache every time to ensure the effectiveness of the cache.

[0167] An embodiment of the present application provides a method for processing data in a tree structure. In some embodiments, the first-level index size of a node is an integer multiple of a CPU cache buffer line, and includes three parts: an index header, a boundary line, and a key value line.

[0168] Table 1 is an index header structure of a node in a tree structure provided in an embodiment of the present application. As shown in Table 1, each field occupies a fixed-size space, and the total occupied space is 64 bytes, which is exactly the space of a cache line.

[0169] Table 1 Index header structure of nodes in the tree structure

[0170]

[0171] The correspondence between Chinese characters and characters in Table 1 is as follows: node address is represented by NODE_ADDR, parent node address is represented by PARENT_ADDR, left subtree address is represented by LEFT_ADDR, right subtree address is represented by RIGHT_ADDR; balance factor is represented by BALANCE_FACTOR, lock flag is represented by LOCK_FLAG, total length of primary key is represented by KEY_LENGTH, and balance factor is represented by BALANCE_FACTOR.

[0172] Table 2 is a node boundary row structure in a tree structure provided by an embodiment of the present application. As shown in Table 2, each field occupies a fixed size of space, and the total occupied space is 128 bytes, which is exactly the space of two cache lines. Figure 1B As shown, the left pointer points to the previous node of the in-order traversal, and the right pointer points to the next node of the in-order traversal.

[0173] Table 2 Boundary row structure of nodes in tree structure

[0174]

[0175] The correspondence between Chinese characters and characters in Table 2 is as follows: the minimum key value is represented by MIN_KEY; the maximum key value is represented by MAX_KEY; the left pointer is represented by LEFT_POINT; and the right pointer is represented by RIGHT_POINT.

[0176] Table 3 is a key value row structure of a node in a tree structure provided in an embodiment of the present application. As shown in Table 3, the key value row structure includes: a primary key field and a padding field.

[0177] Table 3 Key value row structure of nodes in the tree structure

[0178]

[0179] The correspondence between Chinese characters and characters in Table 3 is as follows: the primary key field is represented by KEYS; the padding field is represented by PADDING.

[0180] In some embodiments, the primary index capacity is an adjustable parameter, which means a positive integer multiple of cache lines (the default cache line size is 64 bytes), ranging from 100 to 300, with a default of 200, i.e., 200 buffer lines. The default primary index size of each node is 200*64byte=12800byte. A key occupies 32 bytes, so a buffer line can store 2 keys, and a node with 200 buffer lines can store 400 keys. A 15-layer improved T-tree has 16,384 nodes, which can store 6,553,600 keys, which is approximately equal to 6.55 million data items. Users can weigh and adjust the primary index capacity according to the data requirements of the actual project to achieve the optimal choice.

[0181] The first-level index is strictly aligned to an integer multiple of the buffer line size, making the data boundary more obvious. When the CPU reads and operates the target data in units of buffer lines, it will not interfere with other data and invalidate the cache of other data, thereby improving the effectiveness of the cache.

[0182] The secondary index mainly includes the data field. Here, the format of the data in the data field is DATA<key,data,thread_id,on_way,version> , where key is the primary key, data is the actual stored data, thread_id records the thread ID, on_way records whether a thread is currently operating on the data row, and version is the version number of the actual data. Figure 1B Schematic diagram of the data structure of the nodes in the T-tree structure of the embodiment of the present application, such as Figure 1B As shown, an array is used to store data. The array size is calculated based on the primary index capacity. The primary index capacity defaults to 200, which can store 400 key values. The array size is set to 400. hash(key) is the result of calculating the key value based on the hash function. The hash function formula is as follows:

[0183] hash(key)=key[0]*31^(n-1)+key[1]*31^(n-2)+...+key[n-1]*31^(nn);

[0184] Here, key[n] is the ASCII code value of the nth character of the primary key key, n is the length of the primary key key, hash(key) is the result of calculating the key value based on the hash function, and ^ represents exponentiation.

[0185] Here, the storage uses an open addressing method. The hash value of the primary key is calculated, and then the modulus is taken by the array capacity (400) to obtain the address index of the data in the array, thereby improving the retrieval efficiency. The requested key value is then compared with the key value in the table to see if they are equal. If they are equal, the actual value is returned. If they are not equal, a linear detection is performed. If a matching key is detected, the specific data is returned, otherwise it is returned empty.

[0186] The present application provides a method for processing data in a tree structure, taking lock-free concurrency control of a read operation of a T-tree as an example, the method includes:

[0187] Step S31, read the root node, and determine whether the key value of the primary key of the read operation request is within the range of the root node;

[0188] Here, if Figure 1C As shown, read the root node P of the T tree, compare the key value of the requested primary key (key value) with the minimum key and maximum key of node P to determine whether the requested key value is within the node range. Assuming that the key is smaller than the minimum key of P, traverse to the left subtree PL.

[0189] Step S32, reading the left subtree node, and determining whether the key value of the primary key of the read operation request is within the range of the left subtree node;

[0190] Here, the left subtree node PL is read, and the requested key value is compared with the minimum key and the maximum key of the node PL to determine whether the requested key value is within the node range.

[0191] Step S33: When the key value of the request is within the range of the left subtree node, determine that the left subtree node is the target node.

[0192] For example, if the left subtree node PL is determined to be the target node, the primary key domain linked list of the PL node is traversed. If the existing primary key is hit, the secondary index retrieval address is used according to the hash value of the primary key, and the read data is returned directly. If the existing primary key is not hit, a null value is returned to indicate that the data does not exist.

[0193] In some embodiments, since the read operation no longer locks and unlocks the nodes on the target node path, when searching for the target node, if there are other write operations to insert new values, the T tree loses balance and rotates, and the path of the target node changes, so that the key value of the actual request cannot be obtained. Here, the T tree loses balance when the absolute value of the height difference between the left and right subtrees of a node exceeds 1. For example, PLR is the target node, P->PL->PLR is the target node path, and the P and PL nodes are nodes on the target node path.

[0194] For example, Figure 1CAs shown in the figure, assuming that the requested primary key is actually in the target node PL, when the read operation reads the P node and finds that the requested primary key is smaller than the range of the P node, and is about to continue reading the left subtree PL of the P tree, another write operation inserts the node PLRR at the same time, causing the absolute value of the height difference between the left and right subtrees of the P node of the T tree to exceed 1 (the maximum height of the left subtree PL of the P node is 3, the maximum height of the right subtree PR is 1, and the absolute value of the difference is 2), and the T tree becomes unbalanced and undergoes a rotation transformation, as shown in Figure 1C As shown in (b) and (c), the final transformation is Figure 1C In the structure (c), the left subtree of the P node becomes a PLRR node, which does not contain the requested primary key, so it returns empty and the actual data in the T tree cannot be queried.

[0195] In some embodiments, in the case of the T tree rotation, this problem can be well solved by the newly added left pointer and right pointer in the primary index. After the tree structure is transformed, the read operation reads the left subtree PLRR node of P, and determines whether the requested primary key key is less than the minimum key value of PLRR. At the same time, PLRR does not exist in the left subtree, and then determines whether the requested primary key key is less than the left pointer of PLRR. If it is greater, the value does not exist and a null value is returned; if it is less, it means that the path of the target node has been changed, and jumps to the previous node PLR ​​of the in-order traversal of PLRR according to the left pointer, and then compares the maximum key value and the minimum key value of the requested primary key key with the node PLR. If it is less than the minimum key value, access the left subtree PL of the node PLR ​​to find the target node PL, and then traverse the primary key domain linked list of the PL node. If the existing primary key is hit, the secondary index is retrieved according to the hash value of the primary key, the key value is compared, the data is read and returned, and if the existing primary key is not hit, a null value is returned to indicate that the data does not exist.

[0196] The lock-free concurrency control algorithm for read operations can be summarized into the following three cases: 1) If the requested primary key is less than the minimum key value of the current node, and the current node has no left subtree, and the requested primary key is less than the left pointer of the current node, it means that the path of the target node has been transformed by the balancing operation, and according to the left pointer, jump to the previous node of the in-order traversal to continue looking for the target node; 2) If the requested primary key is greater than the maximum key value of the current node, and the current node has no right subtree, and the requested primary key is greater than the right pointer of the current node, it means that the path of the target node has been transformed by the balancing operation, and according to the right pointer, jump to the next node of the in-order traversal to continue looking for the target node; 3) If it does not belong to the above two scenarios, This means that the path to the target node has not been changed. Normally, the requested primary key key is compared with the minimum key value and the maximum key value of the current node. There are four sub-scenarios: (1) If the key is less than the minimum key value and the current node has a left subtree, then enter the left subtree to continue searching; (2) If the key is less than the minimum key value and the current node does not have a left subtree, and the key is greater than the left pointer of the current node, then the value does not exist and returns null; (3) If the key is greater than the maximum key value and the current node has a right subtree, then enter the right subtree to continue searching; (4) If the key is greater than the maximum key value and the current node does not have a right subtree, and the key is less than the right pointer of the current node, then the value does not exist and returns null.

[0197] The method for processing data in a tree structure provided by the embodiment of the present application reads the root node and determines whether the key value of the primary key of the read operation request is within the range of the root node; reads the left subtree node and determines whether the key value of the primary key of the read operation request is within the range of the left subtree node; and determines that the left subtree node is the target node when the key value of the request is within the range of the left subtree node. In this way, lock-free concurrent control of data in a tree structure is achieved, the concurrency of read operations of memory indexes is improved, and the performance loss caused by locks is eliminated.

[0198] The present application embodiment provides a method for processing data in a tree structure, taking multi-level lock concurrency control of write operations of a T-tree as an example, the method includes:

[0199] Step S41, according to the primary key of the write operation request and the lock-free read operation, determine the target node corresponding to the primary key of the request;

[0200] Step S42, adding the requested primary key key into the primary index, and determining whether the primary index is full;

[0201] Step S43, when the primary index is not full, adding the value corresponding to the key value of the primary key to the secondary index of the target node to complete the data writing operation;

[0202] For example, after determining the target node, if the requested primary key does not exist in the first-level index of the node, the primary key is added to the first-level index, and at the same time, it is determined whether the first-level index is full. If not, the value corresponding to the requested primary key is stored in the second-level index to complete the initial data insertion operation.

[0203] Step S44: when the primary index is full, determining that the operation request is a split operation request;

[0204] Step S45, when the operation request is a split operation request, activating the node lock of the target node;

[0205] In some embodiments, if the primary index is full, the node lock X lock of the target node is activated to split the node, and other threads that want to read and write the target node are all suspended and waiting. At the same time, it is determined whether the target node has a right subtree.

[0206] Step S46, when the target node does not have a right subtree node, create a right subtree node and activate the node lock of the right subtree node;

[0207] In some embodiments, if the target node does not have a right subtree node, a new right subtree is created and the node lock X lock of the new right subtree is activated. If the target node has a right subtree node, the node lock X lock of the right subtree is activated, and then according to the primary index of the target node, such as Figure 2B , n is the maximum number of elements that a primary index can hold, the default value is 400, 75% of the data of the node is taken from left to right as the reserved data, and the remaining data is migrated to the right subtree according to the right subtree address of the target node. While migrating, it is determined whether the capacity of the right subtree node is full. If not, the node split operation is completed, and the node lock X lock of the above node is released, and the external suspended waiting thread is awakened. If it is full, such as Figure 2C , then continue to determine whether the right subtree node of the right subtree exists. If not, create a new right subtree of the right subtree and activate its node lock X lock. If it exists, activate the node lock X lock of the right subtree of the right subtree, and at the same time take 75% of the data from left to right in the right subtree node as the reserved data, and migrate the remaining data to the right subtree node of the right subtree as a whole. This cycle is repeated until all data is migrated to the right subtree layer by layer, and all nodes are in a non-full state, and then determine whether the improved T tree is in a balanced state. If it is unbalanced, perform a rotation operation to rebalance the improved T tree.

[0208] Step S47, completing the node splitting and data writing operations.

[0209] In some embodiments, the node with the node lock X-lock activated is released, the initial data insertion operation is completed, and the external suspended waiting thread is awakened. Since the locking order of the node lock X-lock is from top to bottom and from left to right, deadlock will not occur.

[0210] In some embodiments, after determining the target node, if the primary key key exists in the primary index of the node, an update operation is performed on the data row in the node. Figure 1D , assuming that the requested primary key is b, then the address subscript of the secondary index for b is: [ASCII(b)*31^(1-1)]%400=98, so the target data row 11 with the secondary index address subscript 98 in the main memory is located. The current thread reads the target data row 11, first determines whether the thread_id field of the target data row is empty and the on_way field is N. If so, a partial lock is performed, and the thread ID of the current thread is recorded in the thread_id field of the target data row in the main memory through CAS, that is, the data row is declared to belong to this thread ID, and the on_way field is set to Y, indicating that a thread is currently operating on the target data row. After the update operation is completed, the on_way field of the target data row is set to N, indicating that no thread is currently operating on the target data row in the main memory.

[0211] In some embodiments, if the thread_id field of the target data row is not empty, it is determined whether the thread_id of the target data row in the main memory is the same as the thread ID of the current thread. If they are the same, the operation is performed directly on the target data row without any locking operation. If they are not the same, it is determined whether the on_way field of the target data row in the main memory is N. If it is N, it means that no thread is currently operating on the target data row. Then, partial lock redirection is performed, and the thread_id of the target data row in the main memory is updated to the thread ID of the current thread through CAS. At the same time, the on_way field is set to Y, indicating that a thread is currently operating on the target data row. After the update operation is completed, the on_way field of the target data row is set to N, indicating that no thread is currently operating on the target data row.

[0212] In some embodiments, if the thread_id of the target data row in the main memory is different from the thread ID of the current thread, and the on_way field is Y, which means that a thread is operating on the target data row at this time, then a lock upgrade is performed on the target data row, and the partial lock is upgraded to a spin lock. At this time, all threads that want to operate this data row first continuously poll the target data row in the main memory until the on_way field of the target data row becomes N, and then start competing for the target data row. Once a thread first sets the on_way field of the target data row to Y and updates the thread_id to its own thread ID, it means that this thread has obtained control of the target data row. The actual version number of the target data row in the main memory is compared with the expected version number of the target data row in the thread that currently obtains control. If they match, the operation is performed on the data row in the main memory, and the data row version number is incremented by 1. If the version number does not match, the latest value and the latest version number of the data row in the main memory are queried again. The latest value is used as the original value of the update operation, and the latest version number is used as the new expected version number of the data row in the operation thread. Then, the match is performed again, and the update operation is performed. Finally, the on_way field is set to N, the spin lock is released, the operation is ended, and other threads that may be in a suspended waiting state and want to operate this data row are awakened, and they start a new round of control grabbing. When all concurrent threads are processed, the lock of the target data row is automatically downgraded to a partial lock, and the operation is performed according to the process of determining whether the thread_id of the target data row in the main memory is the same as the thread ID of the current thread if the thread_id field of the target data row is not empty.

[0213] Here, the spin lock belongs to a medium-sized lock. It waits for operation opportunities by polling at the cost of a small amount of CPU performance. However, if the concurrency of a certain data row is in an abnormally high state, and the thread operation currently being processed is relatively time-consuming, a large number of request threads will be accumulated, and the CPU will be wasted waiting, thereby filling up the CPU load, which will cause performance loss that is not worth the gain. Therefore, when a thread polls the target data row more than 20 times, it is upgraded to a waiting lock, and the write thread is put into a suspended waiting state through the operating system to release CPU resources until the thread being processed releases the control right of the target data row, and the thread in the waiting state is awakened to participate in a new round of competition for the control right of the target data row. At this time, the waiting lock is downgraded to a spin lock, and the thread that obtains the control right enters the target data row for operation, and other awakened competing threads restart polling and waiting, and repeat the operation if the thread_id of the target data row in the main memory is different from the thread ID of the current thread, and the on_way field is Y, until all threads are processed, and the lock of the target data row is downgraded to a partial lock again. Since the triggering of the waiting lock will cause the switching of the operating system user state and kernel state, which will cause a large resource overhead, it is a heavyweight lock and is only enabled when the multi-threaded concurrency is the most serious. In actual system operation, such extreme cases are generally rare. Most of the time, the multi-threaded concurrency safety problem can be solved at the cost of a small performance overhead through partial locking and spin locking, thereby improving the concurrency performance of the improved T-tree.

[0214] The method for processing data in a tree structure provided in an embodiment of the present application implements concurrent write control through multi-level locks such as partial locks, spin locks and wait locks; the integer multiples of the alignment buffer lines make the data boundaries more obvious, and the target node index is aligned with the CPU's 1L cache. In these two aspects, the CPU cache is fully utilized, the number of locks is reduced, and the amount of locking and unlocking operations is reduced, the lock granularity is refined, the lock performance overhead is reduced, the concurrent performance optimization of the tree structure is achieved, and the security of multi-threaded processing is ensured.

[0215] Based on the foregoing embodiments, an embodiment of the present application provides a device for processing data in a tree structure, which includes the units included and the modules included in the units, and can be implemented by a processor in a data processing device in a tree structure (such as a computer device); of course, it can also be implemented by a specific logic circuit; in the implementation process, the processor can be a central processing unit (CPU), a microprocessor (MPU), a digital signal processor (DSP) or a field programmable gate array (FPGA), etc.

[0216] Figure 6 Schematic diagram of the structure of the data processing device in the tree structure of the embodiment of the present application. Figure 6As shown, the device 600 includes an acquisition module 601, a comparison module 602, a migration module 603 and a processing module 604, wherein:

[0217] The acquisition module 601 is used to acquire the key value of the primary key of the operation request;

[0218] A comparison module 602 is used to compare the key value of the primary key with the key value of the node in the tree structure to obtain a target node;

[0219] A migration module 603, configured to migrate data of the target node by activating a node lock of the target node when the operation request is a split operation request;

[0220] The processing module 604 is configured to perform data processing on the data row of the target node by activating the data row lock of the target node when the operation request is an update operation request.

[0221] In some embodiments, the data row of the target node includes a thread ID and an operation in progress status, the data row lock includes at least: a partial lock, a spin lock, and a wait lock, and the processing module is used to:

[0222] When the thread ID is empty and the operation in progress state field is no, the data row of the target node is processed through the activated partial lock;

[0223] When the thread ID of the data row of the target node is different from the ID of the request primary key value and the operation in progress state field is yes, the data row of the target node is processed through the activated spin lock;

[0224] When the number of times the spin lock polls the data row exceeds a preset threshold, the data row of the target node is processed through the activated wait lock.

[0225] In some embodiments, the data structure of each node in the tree structure includes at least: a first-level index; wherein the first-level index storage space is an integer multiple of the CPU cache line storage space.

[0226] In some embodiments, the primary index further includes: a minimum key value and a maximum key value; the comparison module includes a first comparison unit and a first determination unit, wherein: the first comparison unit is used to compare the key value of the primary key with the minimum key value in the primary index of the node to obtain a first comparison result; the first determination unit is used to determine the target node according to the first comparison result;

[0227] Alternatively, the comparison module includes a second comparison unit and a second determination unit, wherein the second comparison unit is used to compare the key value of the primary key with the maximum key value in the primary index of the node to obtain a second comparison result; and the second determination unit is used to determine the target node based on the second comparison result.

[0228] In some embodiments, the first determination unit is used to: when the first comparison result is less than the minimum key value in the primary index of the node and the node has a left subtree, use the left subtree node of the node as the node to be compared; compare the key value of the primary key with the minimum key value and the maximum key value in the primary index of the node to be compared to obtain a third comparison result; when the third comparison result indicates that the key value of the primary key belongs to the key value range of the node to be compared, determine that the node to be compared is the target node.

[0229] In some embodiments, the primary index further includes a primary key field, and the data structure of each node in the tree structure further includes a secondary index; the device further includes:

[0230] A traversal module, used for traversing the primary key field of the target node, wherein the primary key field at least includes a key value;

[0231] A first determining module, configured to determine a storage address corresponding to the key value of the primary key when the key value in the primary key field matches the key value of the primary key;

[0232] A second determination module, configured to determine the target data row in the secondary index according to the storage address, wherein the data row at least includes: a key value and data;

[0233] The first return module is used to return the data of the target data row if the key value of the target data row matches the key value of the primary key; if the key value in the primary key field does not match the key value of the primary key, return a null value to indicate that the key value of the requested primary key does not exist.

[0234] In some embodiments, the apparatus further comprises:

[0235] A third determination module is used to use the previous node of the node as the node to be compared when the first comparison result is less than the minimum key value in the primary index of the node and the node does not have a left subtree and the key value of the primary key is less than the left pointer of the node;

[0236] The second returning module is used to return a null value when the key value of the primary key is greater than the left pointer of the node, so as to indicate that the key value of the requested primary key does not exist.

[0237] In some embodiments, the second determining unit is further configured to:

[0238] If the second comparison result is greater than the maximum key value in the primary index of the node and the node has a right subtree node, the right subtree node of the node is determined as the node to be compared; if the key value of the primary key is greater than the right pointer of the node, the next node of the node is determined as the node to be compared;

[0239] Compare the key value of the primary key with the minimum key value and the maximum key value in the primary index of the node to be compared to obtain a fourth comparison result;

[0240] When the fourth comparison result indicates that the key value of the primary key belongs to the key value range of the node to be compared, the node to be compared is determined to be the target node.

[0241] In some embodiments, the apparatus further comprises:

[0242] A first adding module, configured to, if the key value in the primary key field does not match the key value of the primary key, add the key value of the primary key to the primary index of the target node and determine whether the primary index is full;

[0243] A second adding module is used to add the value corresponding to the key value of the primary key to the secondary index of the target node to complete the data writing operation when the primary index is not full;

[0244] The fourth determining module is used to determine that the operation request is a split operation request when the first-level index is full.

[0245] In some embodiments, the device also includes: an update module, which is used to update the data of the target data row corresponding to the key value of the primary key to the data corresponding to the key value of the primary key when the key value of the primary key field of the target node matches the key value of the primary key, so as to complete the data writing operation.

[0246] In some embodiments, the migration module includes:

[0247] A first activation unit, used to activate the node lock of the target node;

[0248] A creating unit, used for creating a right subtree node and activating a node lock of the right subtree node when the target node does not have a right subtree node;

[0249] The first storage unit is used to store the migration data on the established right subtree node and release the node lock to complete the data migration.

[0250] In some embodiments, the migration module further includes:

[0251] A second activation unit, configured to activate a node lock of a right subtree when a right subtree exists in the target node;

[0252] a partitioning unit, configured to partition the data of the primary index of the target node into retained data and migrated data, wherein the retained data is retained in the target node;

[0253] The second storage unit is used to store each data in the migration data on the split node of the right subtree and release the node lock to complete the data migration.

[0254] The description of the above device embodiment is similar to the description of the above method embodiment, and has similar beneficial effects as the method embodiment. For technical details not disclosed in the device embodiment of the present application, please refer to the description of the method embodiment of the present application for understanding.

[0255] It should be noted that in the embodiment of the present application, if the method for processing data in the above-mentioned tree structure is implemented in the form of a software function module and sold or used as an independent product, it can also be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the embodiment of the present application can be essentially or partly reflected in the form of a software product that contributes to the relevant technology. The computer software product is stored in a storage medium, including a number of instructions to enable a computer device (which can be a personal computer, server, etc.) to execute all or part of the methods described in each embodiment of the present application. The aforementioned storage medium includes: various media that can store program codes, such as a U disk, a mobile hard disk, a read-only memory (ROM), a disk or an optical disk. In this way, the embodiment of the present application is not limited to any specific combination of hardware and software.

[0256] Correspondingly, an embodiment of the present application provides a data processing device in a tree structure, which may be, for example, a computer device, comprising a memory and a processor, wherein the memory stores a computer program that can be executed on the processor, and the processor implements the steps in the above method when executing the program.

[0257] Correspondingly, an embodiment of the present application provides a computer-readable storage medium on which a computer program is stored, and when the computer program is executed by a processor, the steps in the above method are implemented.

[0258] It should be noted here that the description of the above storage medium and device embodiments is similar to the description of the above method embodiments, and has similar beneficial effects as the method embodiments. For technical details not disclosed in the storage medium and device embodiments of this application, please refer to the description of the method embodiments of this application for understanding.

[0259] It should be noted that Figure 7 A schematic diagram of a hardware entity of a computer device in an embodiment of the present application is shown in FIG. Figure 7 As shown, the hardware entity of the computer device 700 includes: a processor 701, a communication interface 702 and a memory 703, wherein

[0260] Processor 701 generally controls the overall operation of computer device 700 .

[0261] The communication interface 702 may enable the computer device to communicate with other devices via a network.

[0262] The memory 703 is configured to store instructions and applications executable by the processor 701, and can also cache data to be processed or processed by the processor 701 and various modules in the computer device 700 (for example, image data, audio data, voice communication data, and video communication data), which can be implemented through flash memory (FLASH) or random access memory (Random Access Memory, RAM).

[0263] It should be understood that "one embodiment" or "an embodiment" mentioned throughout the specification means that specific features, structures or characteristics related to the embodiment are included in at least one embodiment of the present application. Therefore, "in one embodiment" or "in an embodiment" appearing throughout the specification does not necessarily refer to the same embodiment. In addition, these specific features, structures or characteristics can be combined in one or more embodiments in any suitable manner. It should be understood that in various embodiments of the present application, the size of the sequence number of the above-mentioned processes does not mean the order of execution, and 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. The above-mentioned sequence numbers of the embodiments of the present application are only for description and do not represent the advantages and disadvantages of the embodiments.

[0264] It should be noted that, in this article, the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, an element defined by the sentence "comprises a ..." does not exclude the existence of other identical elements in the process, method, article or device including the element.

[0265] In the several embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. The device embodiments described above are only schematic. For example, the division of the 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 can be integrated into another system, or some features can be ignored or not executed. In addition, the coupling, direct coupling, or communication connection between the components shown or discussed can be through some interfaces, and the indirect coupling or communication connection of the devices or units can be electrical, mechanical or other forms.

[0266] The units described above as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units; 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 present embodiment.

[0267] In addition, all functional units in the embodiments of the present application may be integrated into one processing unit, or each unit may be a separate unit, or two or more units may be integrated into one unit; the above-mentioned integrated units may be implemented in the form of hardware or in the form of hardware plus software functional units.

[0268] A person skilled in the art can understand that all or part of the steps of implementing the above method embodiment can be completed by hardware related to program instructions, and the aforementioned program can be stored in a computer-readable storage medium. When the program is executed, it executes the steps of the above method embodiment; and the aforementioned storage medium includes: mobile storage devices, read-only memories (ROM), magnetic disks or optical disks, etc., various media that can store program codes.

[0269] Alternatively, if the above-mentioned integrated unit of the present application is implemented in the form of a software function module and sold or used as an independent product, it can also be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the embodiment of the present application can be essentially or partly embodied in the form of a software product that contributes to the relevant technology. The computer software product is stored in a storage medium, including a number of instructions to enable a computer device (which can be a personal computer, a server) to execute all or part of the methods described in each embodiment of the present application. The aforementioned storage medium includes: various media that can store program codes, such as mobile storage devices, ROMs, magnetic disks, or optical disks.

[0270] The above is only an implementation method of the present application, but the protection scope of the present application is not limited thereto. Any person skilled in the art who is familiar with the present technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.

Claims

1. A method for processing data in a tree structure, characterized in that: The method comprises: Obtaining the key value of the primary key of the operation request; comparing the key value of the primary key with the key value of the node in the tree structure to obtain the target node; the data structure of each node in the tree structure includes: a primary index and a secondary index; Traversing the primary key field of the target node, wherein the primary key field at least includes a key value; If the key value in the primary key field does not match the key value of the primary key, adding the key value of the primary key to the primary index of the target node and determining whether the primary index is full; If the primary index is not full, add the value corresponding to the key value of the primary key to the secondary index of the target node to complete the data writing operation; When the primary index is full, determining that the operation request is a split operation request; In a case where the operation request is a split operation request, performing data migration on the target node by activating a node lock of the target node; In the case where the operation request is an update operation request, data processing is performed on the data row of the target node by activating the data row lock of the target node.

2. The method according to claim 1, characterized in that The data row of the target node includes a thread ID and an operation in progress state, the data row lock includes at least: a partial lock, a spin lock and a wait lock, and the data row of the target node is processed by activating the data row lock of the target node, including: When the thread ID is empty and the operation in progress state field is no, the data row of the target node is processed through the activated partial lock; When the thread ID of the data row of the target node is different from the ID of the request primary key value and the operation in progress state field is yes, the data row of the target node is processed through the activated spin lock; When the number of times the spin lock polls the data row exceeds a preset threshold, the data row of the target node is processed through the activated wait lock.

3. The method according to claim 1, characterized in that The primary index storage space is an integer multiple of the CPU cache line storage space.

4. The method according to claim 3, characterized in that The primary index also includes: a minimum key value and a maximum key value; the step of comparing the key value of the primary key with the key value of the node in the tree structure to obtain the target node includes: Compare the key value of the primary key with the minimum key value in the primary index of the node to obtain a first comparison result; determine the target node according to the first comparison result; or, Compare the key value of the primary key with the maximum key value in the primary index of the node to obtain a second comparison result; and determine the target node according to the second comparison result.

5. The method according to claim 4, characterized in that The step of determining the target node according to the first comparison result includes: When the first comparison result is less than the minimum key value in the primary index of the node and the node has a left subtree, the left subtree node of the node is used as the node to be compared; Compare the key value of the primary key with the minimum key value and the maximum key value in the primary index of the node to be compared to obtain a third comparison result; When the third comparison result indicates that the key value of the primary key belongs to the key value range of the node to be compared, the node to be compared is determined to be the target node.

6. The method according to claim 5, characterized in that After traversing the primary key field of the target node, wherein the primary key field at least includes a key value, the method further includes: In the case where the key value in the primary key field matches the key value of the primary key, determining a storage address corresponding to the key value of the primary key; According to the storage address, determining a target data row in the secondary index, wherein the target data row includes at least: a key value and data; When the key value of the target data row matches the key value of the primary key, the data of the target data row is returned; when the key value in the primary key field does not match the key value of the primary key, a null value is returned to indicate that the key value of the primary key of the operation request does not exist.

7. The method according to claim 5, characterized in that The method further comprises: When the first comparison result is less than the minimum key value in the primary index of the node and the node does not have a left subtree and the key value of the primary key is less than the left pointer of the node, the previous node of the node is used as the node to be compared; In the case where the key value of the primary key is greater than the left pointer of the node, a null value is returned to indicate that the key value of the primary key of the operation request does not exist.

8. The method according to claim 4, characterized in that The step of determining the target node according to the second comparison result includes: If the second comparison result is greater than the maximum key value in the primary index of the node and the node has a right subtree node, the right subtree node of the node is determined as the node to be compared; if the key value of the primary key is greater than the right pointer of the node, the next node of the node is determined as the node to be compared; Compare the key value of the primary key with the minimum key value and the maximum key value in the primary index of the node to be compared to obtain a fourth comparison result; When the fourth comparison result indicates that the key value of the primary key belongs to the key value range of the node to be compared, the node to be compared is determined to be the target node.

9. The method according to claim 6, characterized in that The method further comprises: When the key value of the primary key field of the target node matches the key value of the primary key, the data of the target data row corresponding to the key value of the primary key is updated to the data corresponding to the key value of the primary key to complete the data writing operation.

10. The method according to claim 1, characterized in that The step of migrating data of the target node by activating the node lock of the target node includes: Activating a node lock of the target node; If the target node does not have a right subtree node, create a right subtree node and activate a node lock of the right subtree node; The migration data is stored on the established right subtree node, and the node lock is released to complete the data migration.

11. The method according to claim 10, characterized in that The migrating the data of the target node by activating the node lock of the target node also includes: If the target node has a right subtree, activating a node lock of the right subtree; Dividing the data of the primary index of the target node into retained data and migrated data, wherein the retained data is retained in the target node; Each data in the migration data is stored on the split node of the right subtree and the node lock is released to complete the data migration.

12. A data processing device in a tree structure, characterized in that: The device comprises: The acquisition module is used to obtain the key value of the primary key of the operation request; A comparison module, used to compare the key value of the primary key with the key value of the node in the tree structure to obtain a target node; the data structure of each node in the tree structure includes: a primary index and a secondary index; A traversal module, used for traversing the primary key field of the target node, wherein the primary key field at least includes a key value; A first adding module, configured to, if the key value in the primary key field does not match the key value of the primary key, add the key value of the primary key to the primary index of the target node and determine whether the primary index is full; A second adding module is used to add the value corresponding to the key value of the primary key to the secondary index of the target node to complete the data writing operation when the primary index is not full; A fourth determination module, configured to determine, when the primary index is full, that the operation request is a split operation request; A migration module, configured to migrate data of the target node by activating a node lock of the target node when the operation request is a split operation request; The processing module is used for performing data processing on the data row of the target node by activating the data row lock of the target node when the operation request is an update operation request.

13. A device for processing data in a tree structure, comprising a memory and a processor, wherein the memory stores a computer program that can be run on the processor, characterized in that: When the processor executes the program, the steps in the method according to any one of claims 1 to 11 are implemented.

14. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 11 are implemented.

Citation Information

Patent Citations

  • Index optimization method and device based on T-tree

    CN109254962A

  • Processing method and device for index data and electronic equipment

    CN110990404A