Method, device, equipment and medium for processing elastic hash table
By introducing new and old hash buckets and load factors into the hash table, dynamic capacity management and hash conflict resolution are achieved, which solves the problems of poor scalability and degradation of search performance in traditional hash tables, and improves the scalability and search of hash tables efficiency.
Patent Information
- Application Number
- CN202411081377.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-08
- Publication Date
- 2024-12-06
- Estimated Expiration
- 2044-08-08
AI Technical Summary
Traditional hash tables are difficult to scale after the initial capacity is determined, and the search performance is degraded when the hash conflict is serious, resulting in the length of the linked list being too long and degenerating into linked list traversal.
Using a combination of new hash buckets and old hash buckets, dynamic capacity management and hash conflict resolution of elastic hash tables are realized by calculating hash values and load factors, including search, insertion and deletion operations.
Improve the scalability and search performance of hash tables, avoid linked list traversal, and optimize hash conflict handling.
Smart Images

Figure CN118606327B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of data processing technology, and in particular to a method, device, equipment and medium for processing an elastic hash table. Background Art
[0002] Hash Table: Also often called a hash table, it is a data structure that directly accesses the data storage location based on the key, and it provides fast data insertion and search functions. The hash table uses the key and the hash function (Hash (Key)) to calculate the corresponding hash value to achieve fast search of values. However, hash collision is an inevitable phenomenon in the hash table. It occurs when two different keys get the same index after being calculated by the hash function.
[0003] At present, common methods for handling hash conflicts include: chain address method, open address method, rehashing method, and double hashing method. Since the above processing methods use a combination of arrays and linked lists to solve hash conflicts, the following problems still exist: 1) It is difficult to expand after the initial capacity is determined. When the hash table is filled, the search performance deteriorates severely; 2) When the hash conflict is serious, the length of a certain linked list will be too long, and the hash table search will degenerate into a linked list traversal. Therefore, an effective solution is urgently needed to solve the above problems. Summary of the invention
[0004] In view of the above problems, the present invention is proposed to provide a method, device, equipment and medium for processing an elastic hash table that overcomes the above problems or at least partially solves the above problems.
[0005] To achieve the above-mentioned purpose and other related purposes, the present invention provides a method for processing an elastic hash table, wherein the elastic hash table includes a new hash bucket and an old hash bucket, wherein the old hash bucket is a hash bucket used by the elastic hash table before an expansion or contraction operation is performed; and the new hash bucket is a hash bucket used by the elastic hash table when an expansion or contraction operation is performed. The method includes:
[0006] Input the key value to be processed, and calculate the new and old hash values of the key value to be processed respectively according to the key value to be processed, the hash factors of the new and old hash buckets, and the hash function;
[0007] According to the new and old hash values, search, insert or delete in the new and old hash buckets;
[0008] When an insertion or deletion operation is detected, a load factor and a hash coefficient of the elastic hash table are calculated, and the state of the elastic hash table is updated using the load factor or the hash coefficient.
[0009] Optionally, before the step of inputting the key value to be processed, the method further includes:
[0010] An elastic hash table is created, and at the same time, a new hash bucket is created in the elastic hash table; wherein the new hash bucket is composed of a plurality of hash bucket pointers for storing a specified number of elements.
[0011] Optionally, searching in the new and old hash buckets according to the new and old hash values includes:
[0012] Mapping the old hash value to the old hash bucket and determining the corresponding first index position;
[0013] Traversing the hash bucket and the overflow bucket pointed to by the first index position, and comparing the key values to be processed using a preset comparison function;
[0014] When the key value to be processed exists, a search success indication is returned and the search operation ends.
[0015] Optionally, after the step of traversing the hash bucket and the overflow bucket pointed to by the first index position and comparing the key value to be processed by using a preset comparison function, the method further includes:
[0016] When the key value to be processed does not exist, the new hash bucket is searched;
[0017] Mapping the new hash value to the new hash bucket, and determining a corresponding second index position;
[0018] Traversing the hash bucket and the overflow bucket pointed to by the second index position, and comparing the key values to be processed by using a preset comparison function;
[0019] When the key value to be processed exists, a search success indication is returned and the search operation ends.
[0020] Optionally, inserting into the new and old hash buckets according to the new and old hash values includes:
[0021] According to the new and old hash values, searching in the elastic hash table whether the key value to be processed exists;
[0022] When the key value to be processed does not exist, checking the current state of the elastic hash table;
[0023] When it is detected that the elastic hash table is in a target state and the old hash bucket is empty, the old hash bucket is pointed to a new hash bucket;
[0024] Mapping the new hash value to the new hash bucket, and determining a corresponding second index position;
[0025] Loading the key value to be processed into the hash bucket pointed to by the second index position;
[0026] A determined number of old hash buckets are traversed, and data in the old hash buckets are migrated to the new hash buckets.
[0027] Optionally, deleting in the new and old hash buckets according to the new and old hash values includes:
[0028] According to the new and old hash values, searching the key value to be processed in the elastic hash table to obtain a search result;
[0029] Deleting the key value to be processed from the elastic hash table according to the search result;
[0030] When it is detected that the elastic hash table is in the target state, the data in the old hash bucket is migrated to the new hash bucket.
[0031] Optionally, deleting the key value to be processed from the elastic hash table according to the query result includes:
[0032] If it is detected that the key value to be processed is located in the overflow bucket, the key value to be processed is deleted from the overflow bucket, and when it is detected that there is no other data in the overflow bucket, the overflow bucket memory is released;
[0033] If it is detected that the key value to be processed is in the hash bucket, the key value to be processed is deleted from the hash bucket, and when it is detected that there is no other data in the hash bucket, the hash bucket memory is released.
[0034] In a second aspect, the present invention further provides a processing device for an elastic hash table, wherein the elastic hash table includes a new hash bucket and an old hash bucket, wherein the old hash bucket is a hash bucket used by the elastic hash table before an expansion or contraction operation is performed; and the new hash bucket is a hash bucket used by the elastic hash table when an expansion or contraction operation is performed, wherein the device includes:
[0035] A calculation module, used for inputting a key value to be processed, and calculating the new and old hash values of the key value to be processed according to the key value to be processed, the hash factors of the new and old hash buckets, and the hash function;
[0036] A processing module, used for searching, inserting or deleting in the new and old hash buckets according to the new and old hash values;
[0037] The update module is used to calculate the load factor and hash coefficient of the elastic hash table when an insertion or deletion operation is detected, and update the state of the elastic hash table using the load factor or hash coefficient.
[0038] In a third aspect, the present invention provides an electronic device, comprising: a memory and a processor; the memory is used to store a computer program; the processor is used to execute the computer program stored in the memory, so that the electronic device performs the steps of the elastic hash table processing method as described above.
[0039] In a fourth aspect, the present invention further provides a computer-readable storage medium having a computer program stored thereon, which, when executed by an electronic device, implements the steps of the elastic hash table processing method as described above.
[0040] The above one or more technical solutions provided by the present invention may have the following advantages or at least achieve the following technical effects:
[0041] The present invention discloses a processing method, device, equipment and medium for an elastic hash table, wherein the elastic hash table comprises a new hash bucket and an old hash bucket, wherein the old hash bucket is the hash bucket used by the elastic hash table before performing an expansion or contraction operation; and the new hash bucket is the hash bucket used by the elastic hash table when performing an expansion or contraction operation. The method comprises: inputting a key value to be processed, and calculating the new and old hash values of the key value to be processed respectively according to the key value to be processed, the hash factors of the new and old hash buckets and the hash function; searching, inserting or deleting in the new and old hash buckets according to the new and old hash values; and calculating the load factor and hash coefficient of the elastic hash table when an insertion or deletion operation is detected, and updating the state of the elastic hash table using the load factor or hash coefficient.
[0042] Therefore, the present invention realizes dynamic management of the capacity of the elastic hash table by combining new hash buckets and old hash buckets for the elastic hash table; at the same time, the load factor and hash coefficient are introduced into the elastic hash table, which solves the technical problems that the traditional hash table is difficult to expand and the data search degenerates into a traversal linked list. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] Figure 1 Shown is a flow chart of a method for processing an elastic hash table according to an embodiment of the present invention;
[0044] Figure 2 Shown is a schematic diagram of the structure of an elastic hash table in one embodiment of the present invention;
[0045] Figure 3 Shown is a schematic diagram of a flow chart of a key value to be searched in one embodiment of the present invention;
[0046] Figure 4 Shown is a schematic diagram of a flow chart of a key value to be inserted in one embodiment of the present invention;
[0047] Figure 5Shown is a schematic diagram of a flow chart of a key value to be deleted in one embodiment of the present invention;
[0048] Figure 6 It is a schematic diagram showing the functional modules of a processing device for an elastic hash table according to an embodiment of the present invention;
[0049] Figure 7 Shown is a schematic diagram of an electronic device according to an embodiment of the present invention.
[0050] The realization of the purpose, functional features and advantages of the present invention will be further explained in conjunction with embodiments and with reference to the accompanying drawings. DETAILED DESCRIPTION
[0051] The following describes the embodiments of the present invention by specific examples, and those skilled in the art can easily understand other advantages and effects of the present invention from the contents disclosed in this specification. The present invention can also be implemented or applied through other different specific embodiments, and the details in this specification can also be modified or changed in various ways based on different viewpoints and applications without departing from the spirit of the present invention. It should be noted that the following embodiments and features in the embodiments can be combined with each other without conflict.
[0052] It should be noted that the illustrations provided in the following embodiments are only schematic illustrations of the basic concept of the present invention, and thus the drawings only show components related to the present invention rather than being drawn according to the number, shape and size of components in actual implementation. In actual implementation, the type, quantity and proportion of each component may be changed arbitrarily, and the component layout may also be more complicated.
[0053] In the following description, numerous details are discussed to provide a more thorough explanation of the embodiments of the present invention. However, it is obvious to those skilled in the art that the embodiments of the present invention can be implemented without these specific details. In other embodiments, well-known structures and devices are shown in the form of block diagrams rather than in detail to avoid making the embodiments of the present invention difficult to understand.
[0054] The terms "first", "second", etc. in the specification and claims of the disclosed embodiments and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the terms used in this way can be interchanged where appropriate, so that the embodiments of the disclosed embodiments described herein. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions.
[0055] Unless otherwise stated, the term "plurality" means two or more.
[0056] In the embodiment of the present disclosure, the character " / " indicates that the preceding and following objects are in an "or" relationship. For example, A / B indicates: A or B.
[0057] The term "and / or" is a description of the association relationship between objects, indicating that three relationships can exist. For example, A and / or B means: A or B, or, A and B.
[0058] Before further describing the present invention in detail, the nouns and terms involved in the embodiments of the present invention are explained. The nouns and terms involved in the embodiments of the present invention are applicable to the following interpretations:
[0059] <1> ,Key is the value used to uniquely identify each data element in the hash table. The key is converted into an index value through a hash function, and this index value determines the storage location of the key-value pair in the hash table.
[0060] Value: the data associated with a key, that is, the data pointed to by the key.
[0061] <2> A hash function is a mathematical function that receives an input (the key value to be processed) and returns a fixed-size string, usually a byte array. The returned string is called a hash value.
[0062] Hash factor can be used to refer to certain factors or parameters that affect the performance of a hash function, such as hash seed, hash table size, or the computational complexity of a hash function.
[0063] <3> , Index refers to the value calculated by the hash function, which determines the specific storage location of the key-value pair in the hash table array.
[0064] <4> , Hash Bucket refers to a data structure in a hash table used to store all elements with the same hash value. In a hash table, each hash bucket can contain one or more elements whose keys produce the same index value after being calculated by the hash function.
[0065] <5> , Load Factor, which is the ratio of the number of hash table key values to the number of hash buckets; it can be a key indicator for hash table performance management, affecting the expansion and contraction strategy and conflict resolution mechanism. When the load factor exceeds a certain threshold, the hash table is expanded; when the load factor is lower than a certain threshold, the hash table is contracted.
[0066] Lower load factor: This means there are more empty slots in the hash table, which can reduce conflicts and improve search efficiency;
[0067] Higher load factor: As the load factor increases, the likelihood of conflicts increases, which can lead to degraded performance and require more time to resolve conflicts.
[0068] <6> , Overflow bucket, which can be a linked list, stack or other data structure, is used to store elements that cannot be found in the main hash table. The use of overflow buckets can reduce hash table conflicts, but it may also affect performance because search, insertion and deletion operations may require traversing the overflow bucket.
[0069] <7> , Hash Coefficient, which is the ratio of the number of overflow buckets to the number of hash buckets; it can reflect the filling degree of the hash table. When the hash coefficient is too high, it means that the probability of hash table collision increases, which will affect the performance of the hash table. Therefore, when the hash coefficient reaches a certain threshold, the hash table will be rehashed to solve the problem that the search degenerates into a linked list traversal due to severe hash collision.
[0070] <8> , tophash refers to a value stored in each hash bucket, which is usually the high 8 bits of the hash value of the key.
[0071] The technical solutions in the embodiments of the present invention will be described in detail below in conjunction with the accompanying drawings in the embodiments of the present invention.
[0072] See also Figure 1 An embodiment of the present invention provides a method for processing an elastic hash table, wherein the elastic hash table includes a new hash bucket and an old hash bucket, wherein the old hash bucket is a hash bucket used by the elastic hash table before an expansion or contraction operation is performed; and the new hash bucket is a hash bucket used by the elastic hash table during an expansion or contraction operation. The method may include the following steps:
[0073] Step S10, inputting the key value to be processed, and calculating the new and old hash values of the key value to be processed according to the key value to be processed, the hash factors of the new and old hash buckets, and the hash function.
[0074] Among them, the old hash bucket (oldbuckets) can be the hash bucket used by the elastic hash table before expansion or reduction; it can be used to store all key-value pairs in the current elastic hash table. Each array element may point to a hash bucket to store key-value pairs with the same hash value.
[0075] New hash buckets can be hash buckets used by the elastic hash table when the elastic hash table is expanded or reduced. Their size may be different from the old hash bucket pointer array (usually twice the size of the old hash bucket pointer array or a fixed multiple); in order to accommodate more elements or reduce conflicts, the new hash buckets are used to re-store all key-value pairs in the elastic hash table.
[0076] The key value to be processed may be a key value to be searched, a key value to be inserted, or a key value to be deleted.
[0077] The new hash value may be a hash value determined by the key value to be processed, the hash function of the new hash bucket, and the hash factor.
[0078] The old hash value may be a hash value determined by the key value to be processed, the hash function of the old hash bucket, and the hash factor.
[0079] It should be noted that, in the present invention, the new and old hash buckets use different hash factors and hash functions.
[0080] In a specific implementation, the key value to be processed can be input into the hash function of the new and old hash buckets, and then the new and old hash values corresponding to the key value to be processed can be calculated according to the hash factors of the new and old hash buckets.
[0081] Step S20: searching, inserting or deleting in the new and old hash buckets according to the new and old hash values.
[0082] In a specific implementation, after obtaining the new and old hash values, the key value to be processed can be searched, inserted, or deleted in the new and old hash buckets according to the new and old hash values.
[0083] As an example, when the key value to be processed is the key value to be searched, after obtaining the new and old hash values, the key value to be processed can be searched from the old hash bucket according to the old hash value. If the key value to be processed exists in the old hash bucket, a search success indication is returned and the search operation ends.
[0084] Furthermore, if the key value to be processed does not exist in the old hash bucket, the new hash bucket is entered, and the key value to be processed is searched from the new hash bucket according to the new hash value. If the key value to be processed exists in the new hash bucket, a search success indication is returned and the search operation is ended; if the key value to be processed does not exist in the new hash bucket, a search failure indication is returned and the search operation is ended.
[0085] Step S30, when the insertion or deletion operation is detected, the load factor and hash coefficient of the elastic hash table are calculated, and the state of the elastic hash table is updated using the load factor or hash coefficient.
[0086] The elastic hash table state may include an expansion state, a contraction state, a rehashing state or other states.
[0087] The expansion state can be used to expand the elastic hash table to increase the capacity of the new hash bucket pointer array.
[0088] As an example, when the load factor of the elastic hash table is greater than a first predetermined threshold (eg, 6.5), an expansion operation is performed on the elastic hash table to expand the capacity of the new hash bucket pointer array to twice the original capacity.
[0089] In the shrinking state, the elastic hash table may be shrunk to reduce the capacity of the new hash bucket pointer array.
[0090] As an example, when the load factor of the elastic hash table is less than a second predetermined threshold (eg, 0.5), a shrinking operation is performed on the elastic hash table to reduce the capacity of the new hash bucket pointer array to half of the original capacity.
[0091] The rehashing state may be to rehash the elastic hash table to increase the number of hash buckets of the new hash bucket pointer array (ie, an expansion operation).
[0092] As an example, when the hash coefficient of the elastic hash table exceeds a third predetermined threshold (eg, 2), the elastic hash table is rehashed.
[0093] In addition, rehashing is usually performed when the size of the hash table is not enough to support the current number of elements to avoid frequent hashing operations. Therefore, the number of rehashing should be minimized when designing a flexible hash table.
[0094] In a specific implementation, when an insertion or deletion operation is detected in the new or old hash bucket, the load factor and hash coefficient of the elastic hash table can be calculated; and then the load factor or hash coefficient can be used to update the elastic hash table state.
[0095] Furthermore, in one embodiment, before step S10, the method may further include the following step S40:
[0096] Step S40, creating an elastic hash table, and at the same time, creating a new hash bucket in the elastic hash table; wherein the new hash bucket is composed of a plurality of hash bucket pointers for storing a specified number of elements.
[0097] The new hash bucket may be composed of multiple hash bucket pointers for storing a specified number of elements.
[0098] In a specific implementation, an elastic hash table may be created first, and when creating the elastic hash table, a pointer array method may be used instead of creating a hash bucket, so that a new hash bucket may be created in the elastic hash table; wherein the new hash bucket is composed of multiple hash bucket pointers for storing a specified number of elements.
[0099] As an example, when the load factor of the elastic hash table exceeds a certain threshold, a new hash bucket can be assigned to the old hash bucket, and then a new hash bucket can be created in the elastic hash table to migrate the data in the old hash bucket to the new hash bucket. The elastic hash table uses a combination of new and old hash buckets to achieve dynamic management of the elastic hash table capacity, solving the problem of traditional hash tables being difficult to expand.
[0100] See also Figure 2 , Figure 2 This is a schematic diagram of the structure of the elastic hash table. Figure 2 As can be seen in the figure, the elastic hash table includes old hash buckets (oldbuckets) and new hash buckets (buckets). In the elastic hash table, the old and new hash buckets provide a method for quickly locating a specific hash bucket, so that search, insertion, or deletion operations can be performed in a specific hash bucket and overflow bucket.
[0101] It should be noted that when data needs to be inserted into the corresponding hash bucket, the corresponding hash bucket can be created in real time. When the amount of data in the elastic hash table is small, this method can save memory space.
[0102] In this embodiment, by inputting the key value to be processed, and according to the key value to be processed, the hash factors of the new and old hash buckets and the hash function, the new and old hash values of the key value to be processed are calculated respectively; according to the new and old hash values, search, insert or delete are performed in the new and old hash buckets; when the insertion or deletion operation is detected, the load factor and hash coefficient of the elastic hash table are calculated, and the state of the elastic hash table is updated using the load factor or hash coefficient. The present invention realizes the dynamic management of the capacity of the elastic hash table by combining the new hash bucket and the old hash bucket for the elastic hash table; at the same time, the load factor and hash coefficient are introduced in the elastic hash table, which solves the technical problems that the traditional hash table is difficult to expand and the data search degenerates into a traversal linked list.
[0103] Based on the above embodiments, a second embodiment of the processing method of the elastic hash table of the present invention is proposed. Figure 3 In this embodiment, step S20 may further include the following sub-steps A10 to A70:
[0104] Sub-step A10, mapping the old hash value to the old hash bucket, and determining the corresponding first index position.
[0105] Among them, the first index position (oldbuckets[idx]) can be used as the subscript of the pointer array of the old hash bucket to access the corresponding pointer.
[0106] As an example, the first index position may directly correspond to one of the old hash buckets (each pointer points to one hash bucket).
[0107] In a specific implementation, the old hash value may be mapped to the first index position of the old hash bucket, and then the first index position may be used to achieve fast access.
[0108] Sub-step A20, traversing the hash bucket and the overflow bucket pointed to by the first index position, and using a preset comparison function to compare the key value to be processed.
[0109] Among them, the comparison function can be used to determine whether two keys are equal.
[0110] As an example, in a hash bucket, a comparison function is used to traverse the stored key-value pairs and compare whether each key is equal to the key value to be processed.
[0111] It should be noted that, in the present invention, the new hash bucket and the old hash bucket use the same comparison function.
[0112] In a specific implementation, after obtaining the first index position, the hash bucket and overflow bucket pointed to by the first index position may be traversed; and then, in the hash bucket and overflow bucket, a comparison function may be used to compare whether each key is equal to the key value to be processed.
[0113] Sub-step A30, when the key value to be processed exists, returns a search success indication and ends the search operation.
[0114] In a specific implementation, when a key identical to the key value to be processed is found in the old hash bucket, it indicates that the key value to be processed exists in the old hash bucket; then a search success indication may be returned, and the search operation ends.
[0115] Furthermore, in one embodiment, after sub-step A20, the method further includes the following sub-steps A40 to A70:
[0116] Sub-step A40, when the key value to be processed does not exist, enter the new hash bucket to search.
[0117] In a specific implementation, when a key identical to the key value to be processed is not found in the old hash bucket, it indicates that the key value to be processed does not exist in the old hash bucket; then the new hash bucket is entered to search whether there is a key identical to the key value to be processed.
[0118] Step A50, mapping the new hash value to the new hash bucket, and determining the corresponding second index position.
[0119] Among them, the second index position (buckets[idx]) can be used as the subscript of the pointer array of the new hash bucket to access the corresponding pointer.
[0120] In a specific implementation, the new hash value may be mapped to the second index position of the new hash bucket, and then the second index position may be used to achieve fast access.
[0121] Step A60, traversing the hash bucket and the overflow bucket pointed to by the second index position, and using a preset comparison function to compare the key value to be processed.
[0122] In a specific implementation, after obtaining the second index position, the hash bucket and overflow bucket pointed to by the second index position may be traversed; and then, in the hash bucket and overflow bucket, a comparison function may be used to compare whether each key is equal to the key value to be processed.
[0123] Step A70, when the key value to be processed exists, returns a search success indication and ends the search operation.
[0124] In a specific implementation, when a key identical to the key value to be processed is found in the new hash bucket, it indicates that the key value to be processed exists in the new hash bucket; then a search success indication may be returned, and the search operation ends.
[0125] In this embodiment, the elastic hash table adopts a method of combining the new hash bucket and the old hash bucket, and performs a search operation on the key value to be processed in the new and old hash buckets, thereby improving data search performance.
[0126] Based on the above embodiments, a third embodiment of the processing method of the elastic hash table of the present invention is proposed. Figure 4 In this embodiment, step S20 may include the following sub-steps B10 to B60:
[0127] Sub-step B10: searching, according to the old and new hash values, whether the key value to be processed exists in the elastic hash table.
[0128] In a specific implementation, based on the new and old hash values, it is possible to quickly find out whether there is a key value to be processed in the new and old hash buckets of the elastic hash table.
[0129] It should be noted that when the key value to be processed exists, an existing indication is returned and the insertion operation ends.
[0130] In a specific implementation, when a key identical to the key value to be processed is found in the elastic hash table, it indicates that the key value to be processed already exists in the elastic hash table; thus, an existing indication may be returned and the insertion operation is terminated.
[0131] Sub-step B20, when the key value to be processed does not exist, checking the current state of the elastic hash table.
[0132] In a specific implementation, when a key identical to the key value to be processed is not found in the elastic hash table, it indicates that the key value to be processed does not exist in the elastic hash table; and then the current state of the elastic hash table is detected.
[0133] Sub-step B30, when it is detected that the elastic hash table is in the target state and the old hash bucket is empty, point the old hash bucket to the new hash bucket.
[0134] The target state refers to whether the current elastic hash table is in an expansion state, a contraction state, or a rehashing state.
[0135] In a specific implementation, when it is detected that the elastic hash table is in an expansion state, a contraction state, or a rehashing state, and the old hash bucket is empty, the old hash bucket can be pointed to the new hash bucket, that is, an insertion operation can be performed in the corresponding hash bucket in the new hash bucket.
[0136] Sub-step B40, mapping the new hash value to the new hash bucket, and determining the corresponding second index position.
[0137] In a specific implementation, the new hash value may be mapped to the second index position of the new hash bucket, and then the second index position may be used to achieve fast access.
[0138] Sub-step B50: loading the key value to be processed into the hash bucket pointed to by the second index position.
[0139] In a specific implementation, a specific hash bucket may be located according to the second index position, and then the key value to be processed may be loaded into the hash bucket.
[0140] As an example, if the hash bucket pointed to by the second index position is empty, a new hash bucket may be created, and the key value to be processed may be loaded into the new hash bucket.
[0141] As another example, if there is no data in the hash bucket pointed to by the second index position, the key value to be processed is loaded into the hash bucket.
[0142] As another example, if data already exists in the hash bucket pointed to by the second index position, a free cell is searched in the overflow bucket array unit, and the key value to be processed is filled into the free cell.
[0143] Sub-step B60, traversing a determined number of the old hash buckets, and migrating the data in the old hash buckets to the new hash buckets.
[0144] In a specific implementation, after the insertion operation is completed, a determined number of old hash buckets may be traversed, and the data in the old hash buckets may be migrated to the hash bucket corresponding to the new hash bucket according to sub-step B50.
[0145] It should be noted that if there is no data in the old hash bucket, the memory of the old hash bucket will be released and the elastic hash table state will be reset. If the current elastic hash table is in the expansion state, contraction state or rehashing state, the elastic hash table state will not be set. Otherwise, the load factor and hash coefficient of the current elastic hash table are calculated, and then the corresponding state is set according to the load factor and hash coefficient of the elastic hash table.
[0146] In this embodiment, the data search performance is improved by inserting the key value to be processed into the new hash bucket; and the load factor and hash coefficient are introduced into the elastic hash table to solve the technical problems that the traditional hash table is difficult to expand and the data search degenerates into a linked list.
[0147] Based on the above embodiments, a fourth embodiment of the processing method of the elastic hash table of the present invention is proposed. Figure 5 In this embodiment, step S20 may further include the following sub-steps C10 to C30:
[0148] Sub-step C10: searching the key value to be processed in the elastic hash table according to the new and old hash values to obtain a search result.
[0149] In a specific implementation, based on the new and old hash values, it is possible to quickly search whether the key value to be processed already exists in the new and old hash buckets of the elastic hash table, and obtain the corresponding search result.
[0150] Sub-step C20, deleting the key value to be processed from the elastic hash table according to the search result.
[0151] The query result may be the location of the key value to be processed.
[0152] As an example, the key value to be processed may be located in an overflow bucket, or the key value to be processed may be located in a hash bucket.
[0153] In a specific implementation, when a key identical to the key value to be processed is found in the new and old hash buckets of the elastic hash table, it indicates that the key value to be processed exists in the elastic hash table; then the result can be queried to delete the key value to be processed from the elastic hash table.
[0154] It should be noted that when the key value to be processed does not exist, an indication of non-existence is returned and the deletion operation ends.
[0155] In a specific implementation, when a key identical to the key value to be processed is not found in the new and old hash buckets of the elastic hash table, it indicates that the key value to be processed does not exist in the elastic hash table; thus, a non-existence indication may be returned, and the deletion operation ends.
[0156] Sub-step C30, when it is detected that the elastic hash table is in the target state, migrating the data in the old hash bucket to the new hash bucket.
[0157] In this embodiment, after the deletion operation is completed, the current elastic hash table state is detected; when it is detected that the current elastic hash table is in an expansion state, a contraction state, or a rehashing state, the data in the old hash bucket is migrated to the new hash bucket.
[0158] Furthermore, in one embodiment, sub-step C20 may also include the following sub-steps C201-C202:
[0159] Sub-step C201, if it is detected that the key value to be processed is located in the overflow bucket, the key value to be processed is deleted from the overflow bucket, and when it is detected that there is no other data in the overflow bucket, the overflow bucket memory is released.
[0160] Sub-step C202: if it is detected that the key value to be processed is in the hash bucket, the key value to be processed is deleted from the hash bucket, and when it is detected that there is no other data in the hash bucket, the memory of the hash bucket is released.
[0161] In a specific implementation, if it is detected that the key value to be processed is in the overflow bucket, the key value to be processed is deleted from the overflow bucket; and then the overflow bucket memory can be released when it is detected that there is no other data in the overflow bucket. If it is detected that the key value to be processed is in the hash bucket, the key value to be processed is deleted from the hash bucket, and then the hash bucket memory can be released when it is detected that there is no other data in the hash bucket.
[0162] In this embodiment, by deleting the key values to be processed in the new and old hash buckets, data search performance is improved; and the load factor and hash coefficient are introduced into the elastic hash table to solve the technical problems that the traditional hash table is difficult to expand and the data search degenerates into a linked list.
[0163] Based on the same inventive concept, the fifth embodiment of the present invention also provides an elastic hash table processing device corresponding to the elastic hash table processing method of the previous embodiment. Since the principle of solving the problem by the device in the fifth embodiment of the present invention is similar to the elastic hash table processing method of the previous embodiment of the present invention, the implementation of the system can refer to the implementation of the method, and the repeated parts will not be repeated. Figure 6 , the processing device of the elastic hash table of the present invention may include:
[0164] The calculation module 10 is used to input the key value to be processed, and calculate the new and old hash values of the key value to be processed respectively according to the key value to be processed, the hash factors of the new and old hash buckets, and the hash function;
[0165] A processing module 20, configured to search, insert or delete in the new and old hash buckets according to the new and old hash values;
[0166] The updating module 30 is used to calculate the load factor and hash coefficient of the elastic hash table when an insertion or deletion operation is detected, and update the state of the elastic hash table using the load factor or hash coefficient.
[0167] In addition, the present invention also provides a computer-readable storage medium on which a computer program is stored, and when the program is executed by a processor, the steps of the above-mentioned elastic hash table processing method are implemented.
[0168] Figure 7 is a schematic block diagram of an electronic device provided in an embodiment of the present application. Figure 7 As shown, the electronic device includes: at least one processor 401, a memory 402, at least one network interface 403 and a user interface 405. The various components in the electronic device are coupled together through a bus system 404. It can be understood that the bus system 404 is used to realize the connection and communication between these components. In addition to the data bus, the bus system 404 also includes a power bus, a control bus and a status signal bus. However, for the sake of clarity, Figure 7 In the specification, various buses are labeled as bus systems.
[0169] The user interface 405 may include a display, a keyboard, a mouse, a trackball, a click gun, keys, buttons, a touch pad or a touch screen.
[0170] It is understood that the memory 402 can be a volatile memory or a non-volatile memory, and can also include both volatile and non-volatile memories. Among them, the non-volatile memory can be a read-only memory (ROM), a programmable read-only memory (PROM), which is used as an external cache. By way of example but not limitation, many forms of RAM are available, such as static random access memory (SRAM), synchronous static random access memory (SSRAM). The memory described in the embodiments of the present invention is intended to include but is not limited to these and any other suitable categories of memory.
[0171] The memory 402 in the embodiment of the present invention is used to store various categories of data to support the operation of the electronic device 400. Examples of these data include: any executable program for operating on the electronic device 400, such as an operating system 4021 and an application 4022; the operating system 4021 includes various system programs, such as a framework layer, a core library layer, a driver layer, etc., for implementing various basic services and processing hardware-based tasks. The application 4022 may include various applications, such as a media player (Media Player), a browser (Browser), etc., for implementing various application services. The method for determining the true height of a web page provided in the embodiment of the present invention may be included in the application 4022.
[0172] The method disclosed in the above embodiment of the present invention can be applied to the processor 401, or implemented by the processor 401. The processor 401 may be an integrated circuit chip with signal processing capabilities. In the implementation process, each step of the above method can be completed by the hardware integrated logic circuit in the processor 401 or the instruction in the form of software. The above processor 401 may be a general processor, a digital signal processor (DSP, Digital Signal Processor), or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components, etc. The processor 401 can implement or execute the methods, steps and logic block diagrams disclosed in the embodiment of the present invention. The general processor 401 can be a microprocessor or any conventional processor, etc. In combination with the steps of the method for determining the real height of a web page provided in the embodiment of the present invention, it can be directly embodied as a hardware decoding processor to perform, or it can be performed by a combination of hardware and software modules in the decoding processor. The software module can be located in a storage medium, which is located in a memory, and the processor reads the information in the memory and completes the steps of the above method in combination with its hardware.
[0173] In an exemplary embodiment, the electronic device 400 may be implemented by one or more application specific integrated circuits (ASIC), DSP, programmable logic device (PLD), complex programmable logic device (CPLD) to execute the aforementioned method.
[0174] The above is only a specific implementation 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.
[0175] In summary, the present invention realizes the ability of dynamic change of the capacity of the elastic hash table by combining new hash buckets and old hash buckets in the elastic hash table; at the same time, the load factor and hash coefficient are introduced into the elastic hash table to solve the technical problems that the traditional hash table is difficult to expand and the data search degenerates into a traversal linked list.
[0176] The above embodiments are merely illustrative of the principles and effects of the present invention, and are not intended to limit the present invention. Anyone familiar with the art may modify or alter the above embodiments without departing from the spirit and scope of the present invention. Therefore, all equivalent modifications or alterations made by a person of ordinary skill in the art without departing from the spirit and technical concept disclosed by the present invention shall still be covered by the claims of the present invention.
Claims
1. A method for processing an elastic hash table, It is characterized in that The elastic hash table includes a new hash bucket and an old hash bucket, wherein the old hash bucket is a hash bucket used by the elastic hash table before the expansion or contraction operation is performed; and the new hash bucket is a hash bucket used by the elastic hash table when the expansion or contraction operation is performed. The method includes: Input the key value to be processed, and calculate the new and old hash values of the key value to be processed respectively according to the key value to be processed, the hash factors of the new and old hash buckets, and the hash function; According to the new and old hash values, search, insert or delete in the new and old hash buckets; When an insertion or deletion operation is detected, a load factor and a hash coefficient of the elastic hash table are calculated, and a state of the elastic hash table is updated using the load factor or the hash coefficient; The searching in the new and old hash buckets according to the new and old hash values includes: Mapping the old hash value to the old hash bucket and determining the corresponding first index position; Traversing the hash bucket and the overflow bucket pointed to by the first index position, and comparing the key values to be processed using a preset comparison function; When the key value to be processed exists, a search success indication is returned and the search operation ends.
2. The method according to claim 1, It is characterized in that Before the step of inputting the key value to be processed, the method further includes: An elastic hash table is created, and at the same time, a new hash bucket is created in the elastic hash table; wherein the new hash bucket is composed of a plurality of hash bucket pointers for storing a specified number of elements.
3. The method according to claim 1, It is characterized in that After the step of traversing the hash bucket and the overflow bucket pointed to by the first index position and comparing the key value to be processed by using a preset comparison function, the method further includes: When the key value to be processed does not exist, the new hash bucket is searched; Mapping the new hash value to the new hash bucket, and determining a corresponding second index position; Traversing the hash bucket and the overflow bucket pointed to by the second index position, and comparing the key values to be processed using a preset comparison function; When the key value to be processed exists, a search success indication is returned and the search operation ends.
4. The method according to claim 1, It is characterized in that The inserting into the new hash bucket according to the new hash value includes: According to the new and old hash values, searching in the elastic hash table whether there is the key value to be processed; When the key value to be processed does not exist, checking the current state of the elastic hash table; When it is detected that the elastic hash table is in a target state and the old hash bucket is empty, the old hash bucket is pointed to the new hash bucket; Mapping the new hash value to the new hash bucket, and determining a corresponding second index position; Loading the key value to be processed into the hash bucket pointed to by the second index position; A determined number of old hash buckets are traversed, and data in the old hash buckets are migrated to the new hash buckets.
5. The method according to claim 1, It is characterized in that Deleting in the new and old hash buckets according to the new and old hash values includes: According to the new and old hash values, searching the key value to be processed in the elastic hash table to obtain a search result; Deleting the key value to be processed from the elastic hash table according to the search result; When it is detected that the elastic hash table is in the target state, the data in the old hash bucket is migrated to the new hash bucket.
6. The method according to claim 5, It is characterized in that The deleting the key value to be processed from the elastic hash table according to the search result includes: If it is detected that the key value to be processed is in the overflow bucket, the key value to be processed is deleted from the overflow bucket, and when it is detected that there is no other data in the overflow bucket, the memory of the overflow bucket is released; If it is detected that the key value to be processed is in the hash bucket, the key value to be processed is deleted from the hash bucket, and when it is detected that there is no other data in the hash bucket, the hash bucket memory is released.
7. A processing device for an elastic hash table, It is characterized in that The elastic hash table includes an old hash bucket and a new hash bucket, wherein the old hash bucket is a hash bucket used by the elastic hash table before the expansion or contraction operation is performed; and the new hash bucket is a hash bucket used by the elastic hash table when the expansion or contraction operation is performed. The device includes: A calculation module, used for inputting a key value to be processed, and respectively calculating the new and old hash values of the key value to be processed according to the key value to be processed, the hash factors of the new and old hash buckets, and the hash function; A processing module, used to search, insert or delete in the new and old hash buckets according to the new and old hash values; The processing module is further used to map the old hash value to the old hash bucket and determine the corresponding first index position; Traversing the hash bucket and the overflow bucket pointed to by the first index position, and comparing the key values to be processed by using a preset comparison function; When the key value to be processed exists, a search success indication is returned and the search operation is terminated; The update operation is used to calculate the load factor and hash coefficient of the elastic hash table when an insert or delete operation is detected, and to update the state of the elastic hash table using the load factor or hash coefficient.
8. An electronic device, It is characterized in that The electronic device comprises: a memory and a processor, wherein the memory is used to store a computer program, and the processor is used to implement the steps of the method according to any one of claims 1 to 6 when executing the computer program.
9. A computer-readable storage medium, It is characterized in that The computer-readable storage medium stores a program, and when the program is run, it is used to implement the steps of the method according to any one of claims 1 to 6.
