Data processing method, device, equipment, readable storage medium and program product
By optimizing lock usage through a multi-level hash table architecture and hash functions, the problem of low blockchain cache efficiency is solved, and the effect of fast data writing and reading is achieved.
Patent Information
- Application Number
- CN202210631400.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-06-06
- Publication Date
- 2025-10-17
- Estimated Expiration
- 2042-06-06
AI Technical Summary
In the existing technology, the writing and reading efficiency of the blockchain cache is low, resulting in high complexity of cache update time and long lock holding time, which affects the efficiency of blockchain data processing.
A multi-level hash table architecture is adopted, including the first-level hash table, the second-level hash table and the third-level hash table. The key-value pairs are stored in the cache through the hash function, and the use of locks is optimized during writing and reading to reduce the lock holding time.
It improves the write and read efficiency of the blockchain cache, reduces lock preemption time, and increases the speed of data processing.
Smart Images

Figure CN117235122B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of computers, in particular, the present application relates to a data processing method and device, equipment, readable storage medium and program product. BACKGROUND
[0002] The blockchain is a new application mode of distributed data storage, peer-to-peer transmission, consensus mechanism, encryption algorithm and other computer technologies. The block of the blockchain contains multiple KV (Key-Value, key-value pair), and the prior art directly writes the multiple KV in the block into the cache of the blockchain; for example, there are 10 blocks in the blockchain, each block has 10,000 transactions, and each transaction has 100 KV, so the 10 blocks have a total of 10^7 KV, and each time a block is written into the cache, since a block has to write 10^6 KV, only 10^6 KV can be serially written into the cache, resulting in that each time a block is written into the cache, the update time complexity of the cache is as high as O(n^3), the time holding the lock is long, and the efficiency of writing a block into the cache each time is low. Since the time holding the lock is long, the efficiency of reading KV from the cache of the blockchain is low. SUMMARY
[0003] The present application proposes a data processing method, device, equipment, computer readable storage medium and computer program product to solve the problem of how to improve the efficiency of reading and writing the cache of the blockchain.
[0004] Obtaining a target block or a target key of the blockchain;
[0005] Writing multiple key-value pairs in the target block into a block cache of the memory of the blockchain; or reading a target value corresponding to the target key from the block cache;
[0006] The target key and the target value constitute a key-value pair; the block cache is a first-level hash table, the first-level hash table includes a block height of each block of the at least two blocks of the blockchain and a second-level hash table corresponding to the block height of each block, the block height of each block of the at least two blocks of the blockchain and the second-level hash table corresponding to the block height of each block constitute a key-value pair, and the second-level hash table includes multiple third-level hash tables, and each key-value pair in all key-value pairs of each block is stored in any one of the multiple third-level hash tables.
[0007] In one embodiment, writing multiple key-value pairs in the target block into the block cache of the memory of the blockchain includes:
[0008] Obtaining a new second-level hash table;
[0009] writing the plurality of key-value pairs in the target block into the new second-level hash table;
[0010] writing the new second-level hash table into the block cache of the memory of the blockchain.
[0011] In one embodiment, obtaining the new second-level hash table comprises:
[0012] obtaining the new second-level hash table based on the object pool in the memory of the blockchain.
[0013] In one embodiment, writing the plurality of key-value pairs in the target block into the new second-level hash table comprises:
[0014] writing the plurality of key-value pairs in the target block into a plurality of third-level hash tables corresponding to the new second-level hash table by a hash function, each of the plurality of key-value pairs being stored in any one of the plurality of third-level hash tables corresponding to the new second-level hash table.
[0015] In one embodiment, writing the new second-level hash table into the block cache of the memory of the blockchain comprises:
[0016] locking the first-level hash table, and writing the new second-level hash table and the block height of the target block into the first-level hash table, the new second-level hash table and the block height of the target block forming a key-value pair;
[0017] releasing the lock of the first-level hash table.
[0018] In one embodiment, writing the new second-level hash table and the block height of the target block into the first-level hash table comprises:
[0019] deleting the second-level hash table corresponding to the smallest block height of the at least two block heights, and writing the new second-level hash table and the block height of the target block into the first-level hash table.
[0020] In one embodiment, reading the target value corresponding to the target key from the block cache comprises:
[0021] locking the block cache, and sorting the block heights of the at least two blocks to obtain an ordered array, the ordered array including the key-value pairs of each of the at least two blocks, the block heights of the at least two blocks being sorted in descending order in the ordered array;
[0022] reading the target value corresponding to the target key from the block cache based on the ordered array;
[0023] releasing the lock of the block cache.
[0024] In an embodiment, the target value corresponding to the target key is read from the block cache based on the ordered array, including:
[0025] The first block height corresponding to the target key is determined based on the ordered array.
[0026] The second-level hash table corresponding to the first block height is determined.
[0027] The target value corresponding to the target key is read from the second-level hash table corresponding to the first block height.
[0028] In an embodiment, the first block height corresponding to the target key is determined based on the ordered array, including:
[0029] At least two keys identical to the target key are determined from the ordered array.
[0030] The block height in front of the at least two block heights corresponding to the at least two keys respectively is determined as the first block height corresponding to the target key.
[0031] In a second aspect, the present application provides a data processing device, including:
[0032] The first processing module is configured to obtain a target key of a target block or a block chain.
[0033] The second processing module is configured to write a plurality of key-value pairs in the target block into a block cache of a memory of the block chain, or read a target value corresponding to the target key from the block cache.
[0034] The target key and the target value constitute a key-value pair; the block cache is a first-level hash table, the first-level hash table includes a block height of each block of at least two blocks of the block chain and a second-level hash table corresponding to the block height of each block, the block height of each block of the at least two blocks of the block chain and the second-level hash table corresponding to the block height of each block constitute a key-value pair, and the second-level hash table includes a plurality of third-level hash tables, each key-value pair in all key-value pairs of each block is stored in any one of the plurality of third-level hash tables.
[0035] In a third aspect, the present application provides an electronic device, including a processor, a memory and a bus.
[0036] The bus is configured to connect the processor and the memory.
[0037] The memory is configured to store operation instructions.
[0038] The processor is configured to execute the data processing method of the first aspect of the present application by calling the operation instructions.
[0039] In a fourth aspect, the present application provides a computer readable storage medium storing a computer program, the computer program being used to execute the data processing method of the first aspect of the present application.
[0040] In a fifth aspect, the present application provides a computer program product comprising a computer program, the computer program being executed by a processor to implement the steps of the data processing method of the first aspect of the present application.
[0041] The technical scheme provided by the embodiments of the present application has at least the following beneficial effects:
[0042] The target key of the target block or the block chain is obtained, the plurality of key-value pairs in the target block are written into the block cache of the memory of the block chain, or the target value corresponding to the target key is read from the block cache, wherein the target key and the target value constitute a key-value pair, the block cache is a first-level hash table, the first-level hash table comprises the block height of each block of the at least two blocks of the block chain and the second-level hash table corresponding to the block height of each block, the block height of each block of the at least two blocks of the block chain and the second-level hash table corresponding to the block height of each block constitute a key-value pair, and the second-level hash table comprises a plurality of third-level hash tables, and each key-value pair in all key-value pairs of each block is stored in any one of the plurality of third-level hash tables; in this way, based on the cache multi-level architecture, i.e. the first-level hash table, the second-level hash table and the third-level hash table, the writing cache or the reading cache is performed, the plurality of key-value pairs in the target block can be quickly written into the cache, or the target value corresponding to the target key can be quickly read from the cache, and the lock preemption is reduced, i.e. the holding time of the lock is reduced, thereby improving the efficiency of the writing cache and the efficiency of the reading cache. BRIEF DESCRIPTION OF DRAWINGS
[0043] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings needed in the description of the embodiments of the present application will be briefly introduced.
[0044] Figure 1 is an optional structure schematic diagram of a distributed system provided by the embodiments of the present application applied to a block chain system;
[0045] Figure 2 is an optional schematic diagram of a block structure provided by the embodiments of the present application;
[0046] Figure 3 is an architecture schematic diagram of a data processing system provided by the embodiments of the present application;
[0047] Figure 4 is a flow schematic diagram of a data processing method provided by the embodiments of the present application;
[0048] Figure 5 is a schematic diagram of data processing provided by the embodiments of the present application;
[0049] Figure 6 A flowchart of another data processing method provided by an embodiment of the present application is shown in FIG. 6.
[0050] Figure 7 A flowchart of data processing provided by an embodiment of the present application is shown in FIG. 5.
[0051] Figure 8 A structure diagram of a data processing device provided by an embodiment of the present application is shown in FIG. 4.
[0052] Figure 9 A structure diagram of an electronic device provided by an embodiment of the present application is shown in FIG. 3. DETAILED DESCRIPTION
[0053] Embodiments of the present application will be described below in conjunction with the accompanying drawings. It should be understood that the embodiments described below in conjunction with the accompanying drawings are exemplary descriptions of the technical solutions of the embodiments of the present application, and do not limit the technical solutions of the embodiments of the present application.
[0054] Those skilled in the art can understand that, unless specifically stated otherwise, the singular forms "a", "an" and "the" used herein can also include the plural forms. It should be further understood that the terms "comprise" and "include" used in the embodiments of the present application mean that the corresponding features can be implemented as the presented features, information, data, steps, operations, elements and / or components, but do not exclude other features, information, data, steps, operations, elements, components and / or combinations thereof supported by the present technology. It should be understood that when we say that an element is "connected" or "coupled" to another element, the element can be directly connected or coupled to the other element, or it can mean that the element and the other element are connected through an intermediate element. In addition, "connected" or "coupled" used herein can include wireless connection or wireless coupling. The term "and / or" used herein means that at least one of the items defined by the term, for example, "A and / or B" means that A is implemented, or B is implemented, or A and B are implemented.
[0055] It can be understood that in the specific embodiments of the present application, data related to data processing is involved, and when the above embodiments of the present application are applied to specific products or technologies, user permission or consent is required, and the collection, use and processing of related data need to comply with relevant laws, regulations and standards of relevant countries and regions.
[0056] In order to make the purpose, technical solutions and advantages of the present application clearer, the embodiments of the present application will be described in further detail below in conjunction with the accompanying drawings.
[0057] The embodiment of the application is a data processing method provided by a data processing system, and relates to the fields of blockchain technology, storage and cloud technology. Illustratively, the cache for reading and writing the blockchain in the embodiment of the application can read and write the cache for the blockchain through the blockchain technology.
[0058] The system related to the embodiment of the application can be a distributed system formed by connecting clients, a plurality of nodes (any form of computing devices in an access network, such as servers and user terminals) and the network communication.
[0059] Taking the distributed system as a blockchain system, referring to Figure 1 , Figure 1 is an optional structural schematic diagram of the distributed system 100 provided by the embodiment of the application applied to the blockchain system, which is formed by a plurality of nodes (any form of computing devices in an access network, such as servers and user terminals) and clients, the nodes form a peer-to-peer (P2P, Peer To Peer) network, and the P2P protocol is an application layer protocol running on a transmission control protocol (TCP, Transmission Control Protocol) protocol. In the distributed system, any machine such as a server or a terminal can join to become a node, and the node includes a hardware layer, an intermediate layer, an operating system layer and an application layer.
[0060] Referring to Figure 1 the functions of the nodes in the blockchain system, the functions involved include:
[0061] 1) Routing, a basic function of the node, used to support the communication between the nodes.
[0062] In addition to the routing function, the node can also have the following functions:
[0063] 2) Application, used to be deployed in the blockchain, to implement specific services according to actual business requirements, to record the data related to the implementation function to form record data, to carry a digital signature in the record data to represent the source of the task data, to send the record data to other nodes in the blockchain system, and to add the record data to a temporary block when the other nodes successfully verify the source and integrity of the record data.
[0064] For example, the services implemented by the application include:
[0065] 2.1) Wallet, used to provide the function of conducting electronic currency transactions, including initiating a transaction (that is, sending the transaction record of the current transaction to other nodes in the blockchain system, and storing the record data of the transaction in the temporary block of the blockchain as a response to acknowledging the validity of the transaction after the other nodes successfully verify; of course, the wallet also supports querying the remaining electronic currency in the electronic currency address;
[0066] 2.2) Shared Ledger, which provides the function of storing, querying and modifying the account data, sends the record data of the operation on the account data to other nodes in the blockchain system, and after the other nodes verify the validity, as a response to acknowledging the validity of the account data, stores the record data in the temporary block, and can also send a confirmation to the node that initiated the operation.
[0067] 2.3) Smart Contract, a computerized protocol that can execute the terms of a certain contract, implemented through code deployed on the shared ledger for execution when certain conditions are met, according to actual business needs, the code is used to complete automated transactions, such as querying the logistics status of the goods purchased by the buyer, and transferring the buyer's electronic currency to the merchant's address after the buyer signs for the goods; of course, smart contracts are not limited to executing contracts for transactions, but can also execute contracts that process received information.
[0068] 3) Blockchain, which includes a series of blocks connected to each other in chronological order of generation, once a new block is added to the blockchain, it will not be removed, and the block records the record data submitted by the nodes in the blockchain system.
[0069] Referring to Figure 2 , Figure 2 is an optional schematic diagram of the block structure provided by the embodiments of the present application, each block includes the hash value of the transaction record stored in the block (the hash value of the block) and the hash value of the previous block, and the blocks are connected by the hash values to form a blockchain. In addition, the block can also include information such as the time stamp when the block is generated. Blockchain, in essence, is a decentralized database, which is a series of data blocks associated using cryptographic methods, each data block contains relevant information, which is used to verify the validity of the information (anti-fake) and generate the next block.
[0070] In order to better understand and illustrate the scheme of the embodiments of the present application, some technical terms involved in the embodiments of the present application are simply explained below.
[0071] Go language: Go language is a static strong type and compiled language; the syntax of Go language is similar to C, and the syntax functions of Go language include memory safety, GC (garbage collection), structure form, CSP-style concurrent computing, etc.
[0072] Hash table: Hash table is also called Hash table, which is a data structure that can be accessed directly according to the key value; that is, Hash table can access records by mapping key values to a location in the table to speed up search; the mapping function is called Hash function, and the array where the records are stored is called Hash table.
[0073] Object pool sync.pool: A basic package in Go language, which is used to cache some objects in Go language, and can reuse these objects to reduce the creation of objects, so as to reduce the cost of GC (garbage collection) and improve performance.
[0074] Hash function: After a string string is operated by a Hash function, it is converted into another string or integer data.
[0075] Time complexity: The time complexity of an algorithm is a function, which qualitatively describes the running time of the algorithm; it is a function of the length of the input value string; time complexity is often expressed by big O symbol, which does not include the low order term and the first term coefficient of the function; when using this way, the time complexity can be called asymptotic, that is, the case when the size of input value tends to infinity; for example, O (N^3) represents n*n*n times.
[0076] Leveldb: Leveldb is a very efficient KV database, and the version 1.2 of Leveldb can support data volume of billions of level, and has very high performance at this level.
[0077] WAL: WAL (Write Ahead Log) is a common means in database system, which is used to ensure the atomicity and persistence of data operation; WAL in Leveldb exists to prevent data loss after writing to memory, that is, data must be written to disk first, and then to memory.
[0078] MVCC: MVCC (Multi-Version Concurrency Control) is a method of concurrent control, which is generally used in database management system to realize concurrent access to database; in programming language, it realizes transaction memory; MVCC maintains multiple versions of data to avoid conflict between read and write operations.
[0079] The solutions provided in the embodiments of this application involve blockchain technology and cloud technology. The technical solutions of this application are described in detail below using specific embodiments. The following specific embodiments can be combined with each other, and the same or similar concepts or processes may not be described in detail in some embodiments. The embodiments of this application will be described below in conjunction with the accompanying drawings.
[0080] In order to better understand the solution provided by the embodiment of the present application, the solution is described below in conjunction with a specific application scenario.
[0081] In one embodiment, Figure 3 FIG. 1 shows a schematic diagram of the architecture of a data processing system applicable to an embodiment of the present application. It can be understood that the data processing method provided in the embodiment of the present application can be applied to, but not limited to, Figure 3 In the application scenario shown.
[0082] In this example, Figure 3 As shown, the architecture of the data processing system in this example may include but is not limited to a user terminal 10, a server 20, a network 30, and a database 40. The user terminal 10 and the server 20 can interact via the network 30, and the user terminal 10 and the database 40 can interact via the network 30; wherein the user terminal 10 can be a terminal, a server, etc., and the server 20 can be a server, and the server 20 has a blockchain. The client 10 sends the target block to the server 20 via the network 30. The server 20 obtains the target block and writes multiple key-value pairs in the target block to the block cache of the blockchain memory. The client 10 sends the target key of the blockchain to the server 20 via the network 30. The server 20 obtains the target key of the blockchain. The server 20 reads the target value corresponding to the target key from the block cache and sends the target key and target value to the client 10. The target key and target value constitute a key-value pair. The block cache is a first-level hash table. The first-level hash table includes the block height of each block in at least two blocks of the blockchain and a second-level hash table corresponding to the block height of each block. The block height of each block in at least two blocks of the blockchain and the second-level hash table corresponding to the block height of each block constitute a key-value pair. The second-level hash table includes multiple third-level hash tables. Each key-value pair of all key-value pairs of each block is stored in any one of the multiple third-level hash tables. The client 10 stores the target block, target key, and target value in the database 40 .
[0083] It is understood that the above is only an example and is not limited to this embodiment.
[0084] The terminal can be a smartphone (such as an Android phone, an iOS phone, etc.), a mobile phone simulator, a tablet computer, a notebook computer, a digital broadcast receiver, a MID (Mobile Internet Device), a PDA (Personal Digital Assistant), etc. The server can be a standalone physical server, a server cluster or a distributed system composed of multiple physical servers, a cloud server or a server cluster providing cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, CDNs (Content Delivery Networks), and basic cloud computing services such as big data and artificial intelligence platforms.
[0085] Cloud computing is a computing mode that distributes computing tasks on a resource pool composed of a large number of computing devices, so that various application systems can obtain computing power, storage space, and information services as needed. The network that provides resources is called a "cloud". The resources in the "cloud" can be infinitely expanded in the eyes of the user, and can be obtained at any time, used on demand, expanded at any time, and paid according to use.
[0086] As a basic capability provider of cloud computing, a cloud computing resource pool (referred to as a cloud platform, generally referred to as an IaaS (Infrastructure as a Service) platform) is established, and multiple types of virtual resources are deployed in the resource pool for external customers to select and use. The cloud computing resource pool mainly includes computing devices (virtualized machines containing operating systems), storage devices, and network devices.
[0087] According to logical function division, the PaaS (Platform as a Service) layer can be deployed on the IaaS (Infrastructure as a Service) layer, and the SaaS (Software as a Service) layer is deployed above the PaaS layer, or the SaaS can be directly deployed on the IaaS. The PaaS is a platform for software running, such as a database, a web container, etc. The SaaS is various business software, such as a web portal website, an SMS mass sender, etc. Generally, the SaaS and the PaaS are upper layers relative to the IaaS.
[0088] The so-called artificial intelligence cloud service is also commonly referred to as AIaaS (AI as a Service). It is a mainstream service mode of an artificial intelligence platform, specifically, the AIaaS platform splits several common AI services and provides independent or packaged services in the cloud. This service mode is similar to opening an AI theme mall: all developers can access one or more artificial intelligence services provided by the platform through an API interface, and some experienced developers can also use the AI framework and AI infrastructure provided by the platform to deploy and maintain their own cloud artificial intelligence services.
[0089] The network can include, but is not limited to, a wired network including a local area network, a metropolitan area network, and a wide area network, and a wireless network including Bluetooth, Wi-Fi, and other networks that enable wireless communication. The actual application scenario requirements can also be determined, and this is not limited.
[0090] Referring to Figure 4 , Figure 4 A flowchart of a data processing method provided by an embodiment of the application is shown, wherein the method can be executed by any electronic device, such as a server. As an optional embodiment, the method can be executed by a server. For the convenience of description, the server will be taken as an example to illustrate the execution of the method in the description of some optional embodiments below. As shown in Figure 4 The data processing method provided by the embodiment of the application includes the following steps:
[0091] S201, obtaining a target key of a target block or a block chain.
[0092] Specifically, the target block can be a block in the block chain, and the block chain is located in the server. The target block can also be a block sent by the user end to the server. A block includes a plurality of KV (Key-Value, key-value pair), and the types of Key and Value are both strings. For example, if a block has 10,000 transactions, and each transaction has 100 KV, the block includes a total of 10^6 KV. The target key of the block chain can be a Key existing in at least one block of the block chain.
[0093] S202, writing a plurality of key-value pairs in the target block into a block cache of a memory of the block chain; or reading a target value corresponding to the target key from the block cache.
[0094] The target key and the target value form a key-value pair. The block cache is a first-level hash table, the first-level hash table includes the block height of each block in the at least two blocks of the block chain and the second-level hash table corresponding to the block height of each block, the block height of each block in the at least two blocks of the block chain and the second-level hash table corresponding to the block height of each block form a key-value pair, and the second-level hash table includes a plurality of third-level hash tables. Each key-value pair in the entire key-value pair of each block is stored in any one of the plurality of third-level hash tables.
[0095] Specifically, the block cache can be a data structure that sets the key-value pair into the memory of the block chain, that is, the block cache is a memory structure; the block cache can be a data structure in the Go language, that is, the block cache is a hash table. The block cache can realize fast reading or writing of key-value pairs. The plurality of key-value pairs in the target block can be all key-value pairs in the target block, and all key-value pairs in the target block are written into the block cache of the memory of the block chain. The target value corresponding to the target key can be Value existing in the at least one block of the block chain. Based on the target key, the target value is read from the block cache.
[0096] Specifically, as shown in Figure 5 The block cache (blockCache) is a first-level hash table, and the first-level hash table includes the block height (blockHeight) of each block in the at least two blocks of the block chain and the second-level hash table (ConcurrencyMap) corresponding to the block height of each block. For example, the at least two blocks are 10 blocks, the block heights of the 10 blocks are block height 0, block height 1, block height 2, block height 3, block height 4, block height 5, block height 6, block height 7, block height 8, and block height 9, respectively, and the block heights of the 10 blocks correspond to 10 different second-level hash tables, that is, the block height of each block corresponds to a second-level hash table. The block height (blockHeight) of each block in the at least two blocks of the block chain and the second-level hash table (ConcurrencyMap) corresponding to the block height of each block form a key-value pair (Key:blockHeight, Value:ConcurrencyMap), that is, Key is blockHeight and Value is ConcurrencyMap. The blockCache can be represented as map[uint64]ConcurrencyMap, which indicates that the type of Key (blockHeight) is int64 and the Value is ConcurrencyMap.
[0097] The second-level hash table (ConcurrencyMap) includes a plurality of third-level hash tables (Map), and each key-value pair in all key-value pairs of each block is stored in any one of the plurality of third-level hash tables. One second-level hash table stores all key-value pairs in one block, and the second-level hash table uses a hash function, such as an fnv64 function or an fnv32 function, to map the Key (string type) of any key-value pair in the block to one of the plurality of third-level hash tables, and the index of the third-level hash table can be 0-N, where the Key can be a string type, N is a positive integer, for example, N is 31; the total number of the plurality of third-level hash tables can be N+1, for example, if N is 31, one second-level hash table includes 32 third-level hash tables, and all key-value pairs in the block can be distributed to the 32 third-level hash tables, and the key-value pairs distributed to each third-level hash table in the 32 third-level hash tables can be different, and the 32 third-level hash tables store all key-value pairs in the block, and each third-level hash table in the 32 third-level hash tables can store a plurality of key-value pairs in the block. The key-value pairs in the block can also not be distributed to some third-level hash tables in the plurality of third-level hash tables, that is, there are no key-value pairs in some third-level hash tables.
[0098] In the embodiments of the present application, a target key of a target block or a block chain is obtained; a plurality of key-value pairs in the target block are written into a block cache of a memory of the block chain; or a target value corresponding to the target key is read from the block cache; wherein the target key and the target value constitute a key-value pair; the block cache is a first-level hash table, the first-level hash table includes a block height of each block in at least two blocks of the block chain and a second-level hash table corresponding to the block height of each block, the block height of each block in the at least two blocks of the block chain and the second-level hash table corresponding to the block height of each block constitute a key-value pair, and the second-level hash table includes a plurality of third-level hash tables, and each key-value pair in all key-value pairs of each block is stored in any one of the plurality of third-level hash tables; in this way, based on the cache multi-level architecture, that is, the first-level hash table, the second-level hash table and the third-level hash table, the writing cache or the reading cache is performed, the plurality of key-value pairs in the target block can be quickly written into the cache, or the target value corresponding to the target key can be quickly read from the cache, and the lock preemption is reduced, that is, the holding time of the lock is reduced, thereby improving the efficiency of the writing cache and the efficiency of the reading cache.
[0099] In one embodiment, writing a plurality of key-value pairs in a target block into a block cache of a memory of a block chain includes steps A1-A3:
[0100] Step A1, obtaining a new second-level hash table.
[0101] Specifically, one block corresponds to one second-level hash table (ConcurrencyMap), and to write multiple key-value pairs in a target block to the block cache in the memory of the block chain, a second-level hash table needs to be obtained, i.e., a new second-level hash table, which corresponds to the target block.
[0102] In one embodiment, obtaining the new second-level hash table comprises:
[0103] Obtaining the new second-level hash table based on the object pool in the memory of the block chain.
[0104] Specifically, the new second-level hash table (ConcurrencyMap) is created in real time based on the object pool in the memory of the block chain, and the new second-level hash table can also be called from the object pool in the memory of the block chain. For example, the object pool in the memory of the block chain can be the object pool sync.pool in the Go language.
[0105] Step A2, write multiple key-value pairs in the target block to the new second-level hash table.
[0106] Specifically, multiple key-value pairs in the target block are written to the new second-level hash table (ConcurrencyMap) through the function concurrencyMapA.Set, and the new second-level hash table establishes a mapping relationship between the key (Key) of each key-value pair in the target block and one of the third-level hash tables (Map) through a hash function, such as fnv64 function or fnv32 function, i.e., the key (Key) is mapped to one of the third-level hash tables, so that multiple key-value pairs in the target block can be written to the multiple third-level hash tables included in the new second-level hash table.
[0107] In one embodiment, writing multiple key-value pairs in the target block to the new second-level hash table comprises:
[0108] Writing multiple key-value pairs in the target block to the multiple third-level hash tables corresponding to the new second-level hash table through a hash function, and each key-value pair in the multiple key-value pairs is stored in any one of the multiple third-level hash tables corresponding to the new second-level hash table.
[0109] Specifically, the hash function can be fnv, fnv36, fnv64, etc. The total number of the plurality of third-level hash tables corresponding to the new second-level hash table, i.e., the total number of the plurality of third-level hash tables included in the new second-level hash table can be M, M being a positive integer, for example, M being 32, 64, etc. When M is 32, the 32 third-level hash tables can be distinguished by index numbers, and the index numbers of the 32 third-level hash tables are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 and 32, respectively. All the key-value pairs in the target block can be uniformly distributed into the 32 third-level hash tables, and all the key-value pairs in the target block can also be distributed into part of the 32 third-level hash tables, for example, 30 third-level hash tables. The two third-level hash tables in the 32 third-level hash tables other than the 30 third-level hash tables can not be assigned key-value pairs.
[0110] Step A3, write the new second-level hash table to the block cache of the memory of the block chain.
[0111] Specifically, the block cache is a first-level hash table, and the number of second-level hash tables included in the first-level hash table is limited, for example, the first-level hash table includes 10 second-level hash tables. Therefore, writing the new second-level hash table to the block cache of the memory of the block chain, i.e., writing the new second-level hash table to the first-level hash table, the new second-level hash table will replace one second-level hash table in the first-level hash table.
[0112] In one embodiment, writing the new second-level hash table to the block cache of the memory of the block chain includes:
[0113] Locking the first-level hash table, and writing the new second-level hash table and the block height of the target block into the first-level hash table; the new second-level hash table and the block height of the target block form a key-value pair;
[0114] Releasing the lock of the first-level hash table.
[0115] Specifically, the process of writing the new second-level hash table into the block cache of the memory of the blockchain includes: locking the first-level hash table, and writing the new second-level hash table and the block height of the target block into the first-level hash table; the new second-level hash table and the block height of the target block form a key-value pair; and releasing the lock of the first-level hash table. The time for holding the lock in this process is very short, which is only a simple hashset operation (writing the new second-level hash table and the block height of the target block into the first-level hash table), and therefore, the time complexity of this process is O(1), which means that the number of operations is 1, indicating that the process is fast and can quickly write the new second-level hash table into the block cache of the memory of the blockchain, reducing the lock contention, i.e. reducing the holding time of the lock, thereby improving the efficiency of the write cache.
[0116] In one embodiment, writing the new second-level hash table and the block height of the target block into the first-level hash table includes:
[0117] deleting the second-level hash table corresponding to the smallest block height among the block heights of the at least two blocks and the smallest block height, and writing the new second-level hash table and the block height of the target block into the first-level hash table.
[0118] Specifically, the block height of a block can refer to the position of the block in the blockchain, and the smaller the block height, the longer the storage time of the data (key-value pair) in the block; for example, the block heights of two blocks are 2 and 3 respectively, the data in the block with a block height of 2 is stored in January 2021, and the data in the block with a block height of 3 is stored in March 2021, that is, the data in the block with a block height of 3 is updated on the basis of the data in the block with a block height of 2.
[0119] For example, the at least two blocks are 10 blocks, and the block heights of the 10 blocks are block height 0, block height 1, block height 2, block height 3, block height 4, block height 5, block height 6, block height 7, block height 8 and block height 9 respectively, and the block heights of the 10 blocks correspond to 10 different second-level hash tables, i.e. each block height corresponds to a second-level hash table; wherein block height 0 is the smallest block height among the block heights of the 10 blocks, and block height 0 and the second-level hash table corresponding to block height 0 are deleted, and the new second-level hash table and the block height of the target block are written into the first-level hash table.
[0120] In one embodiment, reading the target value corresponding to the target key from the block cache includes:
[0121] locking the block cache, and sorting the block heights of the at least two blocks to obtain an ordered array, the ordered array including a key-value pair of each of the at least two blocks, the block heights of the at least two blocks being sorted in descending order in the ordered array;
[0122] reading, based on the ordered array, a target value corresponding to a target key from the block cache;
[0123] releasing the lock on the block cache.
[0124] Specifically, when reading all keys in the block cache, the block cache is locked, where the all keys are block heights of at least two blocks, and each of the all keys is a block height of a block. The all keys in the block cache are read to obtain an array blockHeightArr including the block heights of the at least two blocks. For example, the at least two blocks are 10 blocks, and the 10 blocks correspond to 10 block heights, where one block corresponds to one block height, and the array blockHeightArr includes the 10 block heights of the 10 blocks. However, the 10 block heights included in the array blockHeightArr are not sorted. Because there are only 10 block heights, the holding time of the lock is very short, reducing the lock preemption, i.e., reducing the holding time of the lock, thereby improving the efficiency of reading the cache.
[0125] The 10 block heights in the array blockHeightArr are sorted in descending order to obtain an ordered array sortBlockHeightArr. The sorting method can be quick sorting. For example, the at least two blocks are 10 blocks, and the block heights of the 10 blocks are sorted in descending order by quick sorting. The block heights in the ordered array sortBlockHeightArr are sorted in descending order as follows: block height 9, block height 8, block height 7, block height 6, block height 5, block height 4, block height 3, block height 2, block height 1, and block height 0.
[0126] According to the MVCC, the ordered array sortBlockHeightArr is traversed in descending order of block height, for example, block height 9, block height 8, block height 7, block height 6, block height 5, block height 4, block height 3, block height 2, block height 1, block height 0, and if it is determined that the second-level hash table (ConcurrencyMap) corresponding to block height 8, the second-level hash table corresponding to block height 6, and the second-level hash table corresponding to block height 2 all have the same key (Key) as the target key, since block height 8 is the maximum block height among block height 8, block height 6, and block height 2, the value corresponding to the key (Key) that is the same as the target key in block height 8 is determined as the target value corresponding to the target key; the second-level hash table (ConcurrencyMap) corresponding to block height 8, for example, concurrencyMapA, can read the value corresponding to the target key (inputKey) from concurrencyMapA by calling the function concurrencyMapA.get(“inputKey”), that is, the target value.
[0127] It should be noted that the key-value pairs included in two or more blocks can be the same, that is, the key-value pairs are multi-versioned. For example, the second-level hash table corresponding to block height 8, the second-level hash table corresponding to block height 6, and the second-level hash table corresponding to block height 2 all have the same key (Key) as the target key, that is, the key-value pair formed by the target key and the target value is multi-versioned; block height 8 is the maximum block height among block height 8, block height 6, and block height 2, and the value corresponding to the target key in the second-level hash table corresponding to block height 8 is read, that is, the target value.
[0128] In one embodiment, based on the ordered array, the target value corresponding to the target key is read from the block cache, comprising:
[0129] Based on the ordered array, the first block height corresponding to the target key is determined;
[0130] The second-level hash table corresponding to the first block height is determined;
[0131] The target value corresponding to the target key is read from the second-level hash table corresponding to the first block height.
[0132] Specifically, according to the MVCC, the ordered array sortBlockHeightArr is traversed in descending order of block height, for example, from block height 9, block height 8, block height 7, block height 6, block height 5, block height 4, block height 3, block height 2, block height 1, block height 0, and if it is determined that the second level hash table (ConcurrencyMap) corresponding to block height 8, the second level hash table corresponding to block height 6 and the second level hash table corresponding to block height 2 all have the same key (Key) as the target key, since block height 8 is the maximum block height among block height 8, block height 6 and block height 2, block height 8 is the first block height; therefore, the value (Value) corresponding to the key (Key) which is the same as the target key in block height 8 is determined as the target value corresponding to the target key; the second level hash table (ConcurrencyMap) corresponding to block height 8, for example, concurrencyMapA, that is, concurrencyMapA is the second level hash table corresponding to the first block height, and the value corresponding to the target key (inputKey) can be read from concurrencyMapA by calling the function concurrencyMapA.get(“inputKey”) in the memory, that is, the target value.
[0133] In one embodiment, based on the ordered array, the first block height corresponding to the target key is determined, comprising:
[0134] At least two keys which are the same as the target key are determined from the ordered array.
[0135] The block height in front of the at least two keys is determined as the first block height corresponding to the target key.
[0136] Specifically, for example, from block height 9, block height 8, block height 7, block height 6, block height 5, block height 4, block height 3, block height 2, block height 1, block height 0, and if it is determined that the second level hash table (ConcurrencyMap) corresponding to block height 8, the second level hash table corresponding to block height 6 and the second level hash table corresponding to block height 2 all have the same key (Key) as the target key, since block height 8 is the maximum block height among block height 8, block height 6 and block height 2, block height 8 is the first block height.
[0137] The application embodiment has at least the following beneficial effects:
[0138] Based on the multi-level cache architecture, i.e., the first-level hash table, the second-level hash table and the third-level hash table, the write cache or the read cache is performed, the multiple key-value pairs in the target block can be quickly written into the cache, or the target value corresponding to the target key can be quickly read from the cache, and the lock preemption is reduced, i.e., the holding time of the lock is reduced, so that the efficiency of the write cache and the efficiency of the read cache are improved.
[0139] In order to better understand the method provided by the embodiments of the present application, the scheme of the embodiments of the present application will be further described below in combination with examples of specific application scenarios.
[0140] The method provided by the embodiments of the present application can be applied to a blockchain scenario, such as reading and writing a cache of a blockchain.
[0141] In one embodiment, as shown in Figure 6 In a blockchain, a block has many KV (key-value pairs), and these KVs are written into a block Cache to provide read acceleration and write acceleration when flushing the cache to a db (database). Write acceleration is the ability to quickly write KVs from the cache to the db; without a blockCache, the data needs to be read from the WAL again and then written to the db, so that a write operation is very slow.
[0142] The process of writing a block to a db includes: writing the KVs in the block into a write-ahead log (WAL), then writing the KVs in the block into a block Cache, and finally writing the KVs to the db through the block Cache, where the db is, for example, a Leveldb; the amount of KVs is large, for example, a block has 10,000 transactions, and each transaction has 100 KVs, so the total number of KVs is 10,000*100=10^6. During the writing process, reading also occurs, i.e., other operations need to read KVs from the block Cache.
[0143] In one embodiment, the BenchMark of adding a block to a cache is shown in Table 1, the BenchMark of deleting a block from a cache is shown in Table 2, and the BenchMark of concurrent Get (reading a KV from a cache) and Set (writing all KVs in a block to a cache) is shown in Table 3.
[0144] Table 1: BenchMark of adding a block to a cache
[0145]
[0146] wherein, blockCache is the block cache in the method provided by the embodiment of the application; AddBlock-counts represents the number of times of adding blocks to the cache within a preset time, for example, 5290111 times of adding blocks to the cache, that is, 5290111 blocks are added to the cache. AddBlock-ops represents the time required for each operation, for example, 208 ns / op represents the time required for each operation is 208 ns (nanosecond).
[0147] As shown in Table 1, for the AddBlock operation (adding a block to the cache), the performance of blockCache is the best, for example, the performance of blockCache is 24600 times that of mapCache, and the performance of blockCache is 55565 times that of syncMapCache.
[0148] Table 2: BenchMark of deleting a block from the cache
[0149]
[0150] As shown in Table 2, for the DelBlock operation (deleting a block from the cache), the performance of blockCache is the best, and the time required for each operation is 24.4 ns, for example, the performance of blockCache is 75448 times that of mapCache, and the performance of blockCache is 80666 times that of syncMapCache.
[0151] Table 3: BenchMark of concurrent Get and Set
[0152] Cache type counts ops Performance vs. blockCache 103902 300 354 ns / op 1 mapCache 402 359 707 9 ns / op 1 / 11 syncMapCache 291 446 824 5 ns / op 1 / 14
[0153] As shown in Table 3, for the Set (AddBlock) operation and the Get operation performed by concurrent mixing, on average, each operation (including Set and Get, that is, reading and writing), blockCache only needs 300354 ns, the performance of blockCache is 11 times that of mapCache, and the performance of blockCache is 14 times that of syncMapCache.
[0154] In one specific application scenario embodiment, for example, a cache scenario of reading and writing a blockchain, see Figure 7, shows a processing flow of a data processing method, wherein the method can be executed by any electronic device, such as a server. As an optional implementation, the method can be executed by a server. For the convenience of description, in the description of some optional embodiments below, the server will be used as an example to illustrate the execution subject of the method. Figure 7 As shown, the processing flow of the data processing method provided in the embodiment of the present application includes the following steps:
[0155] S701, build a multi-level block cache in the blockchain memory.
[0156] Specifically, the block cache of the multi-level architecture may be a block cache of the three-level architecture, for example, Figure 5 As shown, the three-level architecture includes a first-level hash table (blockCache), a second-level hash table (ConcurrencyMap), and a third-level hash table (Map); wherein the block cache (blockCache) is a first-level hash table, and the first-level hash table includes the block height (blockHeight) of each block in at least two blocks of the blockchain and the second-level hash table (ConcurrencyMap) corresponding to each block's block height. The block height (blockHeight) of each block in at least two blocks of the blockchain and the second-level hash table (ConcurrencyMap) corresponding to each block's block height constitute a key-value pair (Key:blockHeight, Value:ConcurrencyMap), that is, the Key is blockHeight and the Value is ConcurrencyMap. The second-level hash table (ConcurrencyMap) includes multiple third-level hash tables (Maps), and each key-value pair of all key-value pairs of each block is stored in any of the multiple third-level hash tables. A second-level hash table stores all key-value pairs in a block. The second-level hash table uses a hash function, such as the fnv64 function or the fnv32 function, to map the key (string type) of any key-value pair in the block to one of the third-level hash tables.
[0157] It should be noted that the ConcurrencyMap uses the fnv64 function or the fnv32 function for mapping. Since the fnv64 function or the fnv32 function can guarantee very strong randomness and very low collision probability, it can be guaranteed that the lock conflict is very low in the concurrent update of the ConcurrencyMap. If there are 64 threads (each thread corresponds to a third-level hash table, that is, 64 threads correspond to 64 third-level hash tables), it can be guaranteed that 64 calculations can be completed simultaneously in parallel and there is no lock competition. By reducing the lock competition, the lock holding time is reduced, thereby ensuring the efficiency of reading and writing.
[0158] S702, write all key-value pairs in block A to the pre-write log.
[0159] Specifically, as shown in Figure 6 illustrated, the downtime will cause the key-value pairs (KV) in the block cache to be lost. In order to prevent the loss of key-value pairs, all key-value pairs in block A can be written to the pre-write log (WAL) first.
[0160] S703, write all key-value pairs in block A to the block cache.
[0161] Specifically, all key-value pairs in a block (block A, i.e. target block) are written to the block cache (blockCache). Based on the object pool in the memory of the block chain, a second-level hash table (ConcurrencyMap) is created in real time; a second-level hash table can also be called from the object pool in the memory of the block chain. For example, the object pool can be the object pool sync.pool in the Go language. Lock the first-level hash table, write all key-value pairs in the block to the second-level hash table, and release the lock of the first-level hash table; lock the first-level hash table, write the second-level hash table to the block cache, and release the lock of the first-level hash table.
[0162] It should be noted that the time complexity of updating the first-level hash table corresponding to writing a block (block) to the block cache (blockCache) is O(1), so the time of holding the lock of the first-level hash table is very short, that is, the lock holding time is reduced, thereby improving the efficiency of writing cache.
[0163] S704, read key-value pairs from the block cache.
[0164] Specifically, the block cache is locked, and the block heights of 10 blocks in the block chain are sorted to obtain an ordered array, the ordered array including a key-value pair of each of the 10 blocks, and the block heights of the 10 blocks are sorted in descending order in the ordered array; based on the ordered array, a target value corresponding to a target key is read from the block cache; the lock of the block cache is released; wherein the key-value pair formed by the target key and the target value is the key-value pair read from the block cache.
[0165] It should be noted that, in the process of reading, only the block cache (blockCache) needs to be locked or the lock needs to be released, and since there are only 10 elements (block heights of 10 blocks) in the block cache, it is very fast to traverse the 10 elements once. In most cases, there is no lock competition between writing and reading, and even if there is lock competition between writing and reading, the lock competition time is very short; by reducing the lock competition, the lock holding time is reduced, thereby ensuring the high efficiency of reading and writing.
[0166] S705, write all key-value pairs of block A in the block cache to the database.
[0167] Specifically, as shown in Figure 5 , all key-value pairs of block A in the block cache are written to db, such as Leveldb.
[0168] The application embodiment has at least the following beneficial effects:
[0169] Based on the multi-level architecture of the block cache, all key-value pairs in the block can be quickly written to the block cache, or the key-value pairs can be quickly read from the cache, the lock competition is reduced, the lock holding time is reduced, and the efficiency of writing the cache and the efficiency of reading the cache are improved.
[0170] The application embodiment also provides a data processing apparatus, and a structure diagram of the data processing apparatus is as shown in Figure 8 The data processing apparatus 90 includes a first processing module 901 and a second processing module 902.
[0171] The first processing module 901 is configured to obtain a target key of a target block or a block chain.
[0172] The second processing module 902 is configured to write a plurality of key-value pairs in the target block to a block cache of a memory of the block chain, or read a target value corresponding to the target key from the block cache.
[0173] The target key and the target value form a key-value pair; the block cache is a first-level hash table, the first-level hash table includes a block height of each block of the at least two blocks of the block chain and a second-level hash table corresponding to the block height of each block, the block height of each block of the at least two blocks of the block chain and the second-level hash table corresponding to the block height of each block form a key-value pair, and the second-level hash table includes a plurality of third-level hash tables, and each key-value pair in all key-value pairs of each block is stored in any one of the plurality of third-level hash tables.
[0174] In one embodiment, the second processing module 902 is specifically configured to:
[0175] Obtain a new second-level hash table;
[0176] Write the plurality of key-value pairs in the target block into the new second-level hash table;
[0177] Write the new second-level hash table into the block cache of the memory of the block chain.
[0178] In one embodiment, the second processing module 902 is specifically configured to:
[0179] Obtain the new second-level hash table based on the object pool in the memory of the block chain.
[0180] In one embodiment, the second processing module 902 is specifically configured to:
[0181] Write the plurality of key-value pairs in the target block into a plurality of third-level hash tables corresponding to the new second-level hash table through a hash function, and each key-value pair in the plurality of key-value pairs is stored in any one of the plurality of third-level hash tables corresponding to the new second-level hash table.
[0182] In one embodiment, the second processing module 902 is specifically configured to:
[0183] Lock the first-level hash table, and write the new second-level hash table and the block height of the target block into the first-level hash table; the new second-level hash table and the block height of the target block form a key-value pair;
[0184] Release the lock of the first-level hash table.
[0185] In one embodiment, the second processing module 902 is specifically configured to:
[0186] Delete a second-level hash table corresponding to a minimum block height of the block heights of the at least two blocks, and write the new second-level hash table and the block height of the target block into the first-level hash table.
[0187] In an embodiment, the second processing module 902 is specifically configured to:
[0188] locking the block cache, and sorting the block heights of the at least two blocks to obtain an ordered array, the ordered array including the key-value pair of each of the at least two blocks, and the block heights of the at least two blocks being sorted in descending order in the ordered array;
[0189] reading, based on the ordered array, the target value corresponding to the target key from the block cache;
[0190] releasing the lock on the block cache.
[0191] In an embodiment, the second processing module 902 is specifically configured to:
[0192] determining, based on the ordered array, the first block height corresponding to the target key;
[0193] determining the second-level hash table corresponding to the first block height;
[0194] reading, from the second-level hash table corresponding to the first block height, the target value corresponding to the target key.
[0195] In an embodiment, the second processing module 902 is specifically configured to:
[0196] determining, from the ordered array, at least two keys identical to the target key;
[0197] determining, as the first block height corresponding to the target key, the block height in front of the block heights corresponding to the at least two keys respectively.
[0198] By applying the embodiments of the present application, at least the following beneficial effects are achieved:
[0199] The target key of the target block or the block chain is acquired; a plurality of key-value pairs in the target block are written into a block cache of a memory of the block chain; or a target value corresponding to the target key is read from the block cache; wherein the target key and the target value constitute a key-value pair; the block cache is a first-level hash table, the first-level hash table comprises a block height of each block of at least two blocks of the block chain and a second-level hash table corresponding to the block height of each block, the block height of each block of the at least two blocks of the block chain and the second-level hash table corresponding to the block height of each block constitute a key-value pair, and the second-level hash table comprises a plurality of third-level hash tables, and each key-value pair in all key-value pairs of each block is stored in any one of the plurality of third-level hash tables; in this way, based on the cache multi-level architecture, that is, the first-level hash table, the second-level hash table and the third-level hash table, the write cache or the read cache is performed, the plurality of key-value pairs in the target block can be quickly written into the cache, or the target value corresponding to the target key can be quickly read from the cache, and the lock preemption is reduced, that is, the holding time of the lock is reduced, so that the efficiency of the write cache and the efficiency of the read cache are improved.
[0200] The electronic device 4000 shown in FIG. 4A includes a processor 4001 and a memory 4003. Figure 9 The electronic device 4000 shown in FIG. 4A includes a processor 4001 and a memory 4003. Figure 9 The electronic device 4000 shown in FIG. 4A includes a processor 4001 and a memory 4003.
[0201] The processor 4001 can be a CPU (Central Processing Unit), a general-purpose processor, a DSP (Digital Signal Processor), an ASIC (Application Specific Integrated Circuit), an FPGA (Field Programmable Gate Array), or other programmable logic devices, transistor logic devices, hardware components, or any combination thereof. It can implement or execute various exemplary logical blocks, modules and circuits described in combination with the disclosure. The processor 4001 can also be a combination of computing functions, such as one or more microprocessor combinations, combinations of DSP and microprocessor, etc.
[0202] The bus 4002 can include a path that transmits information between the above components. The bus 4002 can be a PCI (Peripheral Component Interconnect) bus or an EISA (Extended Industry Standard Architecture) bus, or the like. The bus 4002 can be divided into an address bus, a data bus, a control bus, and the like. For convenience of representation, Figure 9 Only one thick line is used in the figure, but it does not mean that there is only one bus or only one type of bus.
[0203] The memory 4003 can be a ROM (Read Only Memory) or other type of static storage device that can store static information and instructions, a RAM (Random Access Memory) or other type of dynamic storage device that can store information and instructions, an EEPROM (Electrically Erasable Programmable Read Only Memory), a CD-ROM (Compact Disc Read Only Memory) or other optical disk storage, a magnetic disk storage medium, other magnetic storage device, or any other medium that can be used to carry or store computer programs and that can be read by a computer, without limitation.
[0204] The memory 4003 is used to store a computer program for executing the embodiments of the present application, and is controlled by the processor 4001 to execute. The processor 4001 is used to execute the computer program stored in the memory 4003 to realize the steps shown in the foregoing method embodiments.
[0205] The electronic device includes, but is not limited to, a server, and the like.
[0206] By applying the embodiments of the present application, at least the following beneficial effects are achieved:
[0207] The target key of the target block or the blockchain is obtained; a plurality of key-value pairs in the target block are written into a block cache of a memory of the blockchain; or a target value corresponding to the target key is read from the block cache; wherein the target key and the target value constitute a key-value pair; the block cache is a first-level hash table, the first-level hash table comprises a block height of each block of at least two blocks of the blockchain and a second-level hash table corresponding to the block height of each block, the block height of each block of the at least two blocks of the blockchain and the second-level hash table corresponding to the block height of each block constitute a key-value pair, and the second-level hash table comprises a plurality of third-level hash tables, each key-value pair in all key-value pairs of each block is stored in any one of the plurality of third-level hash tables; in this way, based on the cache multi-level architecture, that is, the first-level hash table, the second-level hash table and the third-level hash table, the write cache or the read cache is performed, the plurality of key-value pairs in the target block can be quickly written into the cache, or the target value corresponding to the target key can be quickly read from the cache, and the lock preemption is reduced, that is, the holding time of the lock is reduced, so that the efficiency of the write cache and the efficiency of the read cache are improved.
[0208] The embodiment of the present application provides a computer readable storage medium, and the computer readable storage medium stores a computer program. The computer program is executed by a processor to implement the steps and corresponding contents of the foregoing method embodiments.
[0209] The embodiment of the present application further provides a computer program product, which comprises a computer program. The computer program is executed by a processor to implement the steps and corresponding contents of the foregoing method embodiments.
[0210] Based on the same principle as the method provided in the embodiments of the present application, the embodiments of the present application further provide a computer program product or a computer program, which comprises computer instructions stored in a computer readable storage medium. A processor of a computer device reads the computer instructions from the computer readable storage medium, and the processor executes the computer instructions to enable the computer device to perform the method provided in any of the optional embodiments of the present application.
[0211] It should be understood that although each operation step in the flowchart of the embodiments of the present application is indicated by an arrow, the implementation order of the steps is not limited to the order indicated by the arrow. Unless explicitly stated herein, in some implementation scenarios of the embodiments of the present application, the implementation steps in each flowchart can be executed in other orders as required. In addition, part or all of the steps in each flowchart can include multiple sub-steps or multiple stages based on the actual implementation scenario. Part or all of these sub-steps or stages can be executed at the same time, and each of these sub-steps or stages can also be executed at different times. In the scenario where the execution times are different, the execution order of these sub-steps or stages can be flexibly configured as required, and the embodiments of the present application do not limit this.
[0212] The above is only an optional implementation of some implementation scenarios of the present application. It should be pointed out that, for ordinary skilled persons in the technical field, other similar implementation means based on the technical idea of the present application without departing from the technical concept of the present application also belong to the protection scope of the embodiments of the present application.
Claims
1. A data processing method, characterized in that: include: Get the target key of the target block or blockchain; Writing multiple key-value pairs in the target block to the block cache of the memory of the blockchain; or reading a target value corresponding to the target key from the block cache; The target key and the target value constitute a key-value pair; the block cache is a first-level hash table, the first-level hash table includes the block height of each block in the at least two blocks of the blockchain and the second-level hash table corresponding to the block height of each block, the block height of each block in the at least two blocks of the blockchain and the second-level hash table corresponding to the block height of each block constitute a key-value pair, the second-level hash table includes multiple third-level hash tables, and each key-value pair of all key-value pairs of each block is stored in any one of the multiple third-level hash tables.
2. The method according to claim 1, characterized in that Writing the multiple key-value pairs in the target block to the block cache of the memory of the blockchain includes: Get a new second-level hash table; Writing a plurality of key-value pairs in the target block into the new second-level hash table; The new second-level hash table is written to the block cache of the blockchain memory.
3. The method according to claim 2, characterized in that The obtaining of a new second-level hash table includes: Based on the object pool in the memory of the blockchain, a new second-level hash table is obtained.
4. The method according to claim 2, characterized in that Writing the multiple key-value pairs in the target block into the new second-level hash table includes: Through a hash function, multiple key-value pairs in the target block are written into multiple third-level hash tables corresponding to the new second-level hash table; each key-value pair in the multiple key-value pairs is stored in any third-level hash table in the multiple third-level hash tables corresponding to the new second-level hash table.
5. The method according to claim 2, characterized in that Writing the new second-level hash table into the block cache of the memory of the blockchain includes: Locking the first-level hash table, and writing the new second-level hash table and the block height of the target block into the first-level hash table; the new second-level hash table and the block height of the target block constitute a key-value pair; Release the lock of the first-level hash table.
6. The method according to claim 5, characterized in that Writing the new second-level hash table and the block height of the target block into the first-level hash table includes: The smallest block height among the block heights of the at least two blocks and the second-level hash table corresponding to the smallest block height are deleted, and the new second-level hash table and the block height of the target block are written into the first-level hash table.
7. The method according to claim 1, characterized in that The reading the target value corresponding to the target key from the block cache includes: Locking the block cache and sorting the block heights of the at least two blocks to obtain an ordered array, the ordered array including a key-value pair for each of the at least two blocks, and the block heights of the at least two blocks being sorted from largest to smallest in the ordered array; Based on the ordered array, reading a target value corresponding to the target key from the block cache; The lock of the block cache is released.
8. The method according to claim 7, characterized in that The step of reading the target value corresponding to the target key from the block cache based on the ordered array includes: Determine, based on the ordered array, a first block height corresponding to the target key; Determine a second-level hash table corresponding to the first block height; Read a target value corresponding to the target key from a second-level hash table corresponding to the first block height.
9. The method according to claim 8, characterized in that Determining the first block height corresponding to the target key based on the ordered array includes: Determine at least two keys from the ordered array that are identical to the target key; The block height that is sorted first among the block heights corresponding to the at least two keys is determined as the first block height corresponding to the target key.
10. A data processing device, characterized in that: include: A first processing module is used to obtain a target key of a target block or blockchain; A second processing module is configured to write the multiple key-value pairs in the target block into a block cache of the memory of the blockchain; or reading a target value corresponding to the target key from the block cache; The target key and the target value constitute a key-value pair; the block cache is a first-level hash table, the first-level hash table includes the block height of each block in the at least two blocks of the blockchain and the second-level hash table corresponding to the block height of each block, the block height of each block in the at least two blocks of the blockchain and the second-level hash table corresponding to the block height of each block constitute a key-value pair, the second-level hash table includes multiple third-level hash tables, and each key-value pair of all key-value pairs of each block is stored in any one of the multiple third-level hash tables.
11. An electronic device comprising a memory, a processor, and a computer program stored in the memory, wherein: The processor executes the computer program to implement the steps of the method according to any one of claims 1 to 9.
12. 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 9 are implemented.
13. A computer program product comprising a computer program, 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 9 are implemented.
Citation Information
Patent Citations
Dynamic blockchain system and method for providing efficient and secure distributed data access, data storage and data transport
US20190386817A1
Cache data location system
US20200341909A1