Method, device, electronic device and storage medium for generating database primary key
By utilizing the self-increase mechanism of current system time and sequence identification in distributed databases, the primary key duplication problem caused by system clock callback is solved, the uniqueness and stability of the primary key are achieved, and the robustness of the database is improved.
Patent Information
- Application Number
- CN202011613299.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-12-30
- Publication Date
- 2025-08-19
- Estimated Expiration
- 2040-12-30
AI Technical Summary
In complex distributed systems, the primary key generated when the system clock is called back is easily repeated with the generated primary key, and the prior art is difficult to effectively solve this problem.
By obtaining the current system time of the database server, determine the index position of the primary key in the loop array, determine whether the index position is empty, if it is empty, generate the primary key and store it. Otherwise, compare the timestamp and self-increment sequence identification to generate a new primary key to ensure the uniqueness and incrementality of the primary key.
It effectively avoids the problem of primary key duplication during system clock callback, improves the stability and robustness of database primary key generation, and ensures the trend increment of primary keys.
Smart Images

Figure CN114691673B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of distributed data processing and databases, and in particular to a method, device, electronic device, and storage medium for generating a database primary key. Background Art
[0002] A primary key (identification, ID), also known as a primary key symbol, refers to an identifier that identifies a target object in a computer system. Taking a file as an example, the primary key of a file can be the file path and file name. However, when the file path or file name changes, it is difficult to obtain the file based on the original primary key. Therefore, an auto-increment primary key generated by a database can usually be used as the primary key of a file.
[0003] Currently, traditional databases mostly use auto-increment identifiers to identify large amounts of data and messages. However, this approach is not suitable for complex distributed systems. For complex distributed systems, a composite primary key is often used to generate primary keys, such as the commonly used Snowflake algorithm. However, in practice, it has been found that because computer system clocks periodically set back, and the Snowflake algorithm is strongly dependent on the computer system clock, in complex distributed database systems, the primary keys generated when the system clock sets back are prone to duplicates with previously generated primary keys. Summary of the Invention
[0004] The purpose of the embodiments of the present application is to provide a database primary key generation method, device, electronic device and storage medium, which are used to improve the problem that the primary key generated when the system clock is dialed back is likely to be repeated with the primary key that has already been generated.
[0005] An embodiment of the present application provides a method for generating a database primary key, comprising: when a new piece of data needs to be added to the database, obtaining the current system time of the server where the database is located, determining the index position of the primary key of the data to be generated in a circular array based on the current system time, judging whether the content at the index position is empty, and if so, generating the primary key of the data according to a set algorithm and storing it at the index position, and storing the primary key as the primary key of the data in the database; otherwise, obtaining the last stored primary key from the index position in the circular array, extracting the timestamp, machine identifier and sequence identifier in the primary key, and comparing the current system time with the timestamp in the last stored primary key; if the current system time is less than the timestamp in the last stored primary key, incrementing the sequence identifier to obtain the incremented sequence identifier, and generating a new primary key based on the timestamp, machine identifier and incremented sequence identifier in the last stored primary key, and then storing the new primary key in the index position of the circular array, and storing the new primary key as the primary key of the data in the database. In the above implementation process, when the content in the circular array corresponding to the index position determined by the current system time is empty, the generated primary key is stored in the content in the circular array corresponding to the index position; then, when the content in the circular array corresponding to the index position is not empty, the current system time is compared with the timestamp in the last stored primary key to determine whether the computer system clock has been dialed back. If dialed back, the sequence identifier is incremented, a new primary key is generated based on the incremented sequence identifier, and the new primary key is used as the primary key for data stored in the database, thereby effectively improving the problem that the primary key generated when the system clock is dialed back is easily duplicated with the already generated primary key.
[0006] Optionally, in an embodiment of the present application, after comparing the current system time with the timestamp in the last stored primary key, the method further includes: if the current system time is equal to the timestamp in the last stored primary key, then incrementing the sequence identifier to obtain the incremented sequence identifier, and generating a new primary key based on the current system time, the machine identifier and the incremented sequence identifier, and then storing the new primary key in the index position of the circular array, and storing the new primary key as the primary key of the data in the database.
[0007] Optionally, in an embodiment of the present application, after comparing the current system time with the timestamp in the last stored primary key, the method further includes: if the current system time is greater than the timestamp in the last stored primary key, generating a new primary key based on the current system time, the machine identifier, and the preset initial sequence, storing the new primary key in the index position of the circular array, and storing the new primary key as the primary key of the data in the database. In the above implementation process, by generating a new primary key based on the preset initial sequence when the current system time is greater than the timestamp in the last stored primary key, the situation where all the binary bits of the sequence identifier are used up due to the continuous self-increment of the sequence identifier is avoided, effectively improving the problem of generating a new primary key that is duplicated with an already generated primary key.
[0008] Optionally, in an embodiment of the present application, obtaining the last stored primary key from the index position in the circular array includes: taking the modulus of the difference between the current system time and the preset system initial time and the length of the circular array to obtain the index position of the primary key in the circular array; and obtaining the last stored primary key from the circular array according to the index position.
[0009] Optionally, in an embodiment of the present application, a new primary key is generated based on the timestamp, machine identifier, and self-incremented sequence identifier in the last stored primary key, including: determining whether the sequence identifier in the primary key processed at the current moment is detected to be a preset value, the preset value indicating that the values of all binary bits of the sequence identifier are one; if so, the timestamp in the primary key is increased by an integer multiple of the length of the cyclic array, and the self-incremented sequence identifier is flipped to obtain the flipped sequence identifier, and then the primary key is generated based on the timestamp, machine identifier, and flipped sequence identifier after increasing the length of the cyclic array. In the above implementation process, when it is detected that the sequence identifiers in all primary keys of the cyclic array are all preset values, the primary key is generated based on the timestamp, machine identifier, and flipped sequence identifier after increasing the length of the cyclic array, thereby avoiding the problem that the binary bits of the sequence identifiers in all primary keys of the cyclic array are all 1 due to the large clock back length, thereby ensuring the incremental trend of the generated primary key, and effectively improving the stability and robustness of the database primary key generation method.
[0010] Optionally, in an embodiment of the present application, it further includes: self-incrementing the sequence identifier, and generating a fill-in primary key value based on a preset initial timestamp, a machine identifier, and a sequence identifier after self-increment; storing the fill-in primary key value in a circular array until all elements of the circular array are not empty. In the above implementation process, by generating a fill-in primary key value based on a preset initial timestamp, a machine identifier, and a sequence identifier after self-increment; storing the fill-in primary key value in a circular array until all elements of the circular array are not empty, the circular array is effectively initialized before it is used, avoiding the problem of storing the primary key generated according to the callback timestamp in the uninitialized elements in the circular array, thereby ensuring the incremental trend of the generated primary key and effectively improving the stability and robustness of the database primary key generation method.
[0011] Optionally, in an embodiment of the present application, the database primary key generation method further includes: if a new primary key is generated, using the new primary key to identify a file or folder in a distributed file system, or using the new primary key to identify a data record in a distributed database. In the above implementation process, by using the new primary key to identify a data record, file, or folder in a distributed environment, the problem of obtaining duplicate data records, files, or folders through the primary key in a distributed environment is avoided, effectively improving the stability and robustness of the database primary key generation method.
[0012] The embodiment of the present application also provides a database primary key generation device, including: an index content judgment module, which is used to obtain the current system time of the server where the database is located when the database needs to add a new piece of data, determine the index position of the primary key of the data to be generated in the circular array based on the current system time, and determine whether the content at the index position is empty. If the content at the index position is empty, generate the primary key of the data according to a set algorithm and store it at the index position, and store the primary key as the primary key of the data in the database; a primary key acquisition and extraction module, which is used to obtain the last stored primary key from the index position in the circular array if the content at the index position is not empty, extract the timestamp, machine identifier and sequence identifier in the primary key, and compare the current system time with the timestamp in the last stored primary key; a new primary key generation module, which is used to increment the sequence identifier to obtain the incremented sequence identifier if the current system time is less than the timestamp in the last stored primary key, and generate a new primary key based on the timestamp, machine identifier and incremented sequence identifier in the last stored primary key, and then store the new primary key in the index position of the circular array, and store the new primary key as the primary key of the data in the database.
[0013] An embodiment of the present application further provides an electronic device, including: a processor and a memory, wherein the memory stores machine-readable instructions executable by the processor, and when the machine-readable instructions are executed by the processor, the method described above is performed.
[0014] An embodiment of the present application further provides a storage medium having a computer program stored thereon. When the computer program is executed by a processor, the method described above is executed. BRIEF DESCRIPTION OF THE DRAWINGS
[0015] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the following is a brief introduction to the drawings required for use in the embodiments of the present application. It should be understood that the following drawings only show certain embodiments of the present application and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other relevant drawings can be obtained based on these drawings without creative work.
[0016] Figure 1 A schematic diagram of a process for generating a database primary key according to an embodiment of the present application is shown;
[0017] Figure 2 A schematic diagram of a primary key in a circular array provided by an embodiment of the present application is shown;
[0018] Figure 3 A schematic diagram of the structure of a database primary key generation device provided in an embodiment of the present application is shown;
[0019] Figure 4 A schematic structural diagram of an electronic device provided in an embodiment of the present application is shown. DETAILED DESCRIPTION
[0020] The technical solutions in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application.
[0021] Before introducing the database primary key generation method provided by the embodiment of the present application, some concepts involved in the embodiment of the present application are first introduced:
[0022] A distributed database is a logically unified database created by connecting multiple physically dispersed database units using a computer network. Each connected database unit is called a site or node. Distributed databases are managed by a unified database management system, called a distributed database management system.
[0023] It should be noted that the database primary key generation method provided in the embodiment of the present application can be executed by an electronic device, where the electronic device refers to a device terminal or server with the function of executing a computer program, and the device terminal is, for example: a smart phone, a personal computer (PC), a tablet computer, a personal digital assistant (PDA), a mobile Internet device (MID), a network switch or a network router, etc.
[0024] Before introducing the database primary key generation method provided in an embodiment of the present application, the application scenarios to which the database primary key generation method is applicable are first introduced. The application scenarios here include but are not limited to: using the primary key generated by the database primary key generation method to identify data records in a data table of a distributed database system, or using the primary key generated by the database primary key generation method to identify files, folders or memory objects in a distributed system, or using the primary key generated by the database primary key generation method to identify data records in a data table of a traditional database, etc.; wherein the data tables here include but are not limited to: user information table, order information table and coupon information table, etc.
[0025] See Figure 1 The flowchart of the method for generating a database primary key provided by an embodiment of the present application is shown. The main idea of the method for generating a database primary key is to store the generated primary key in the content of the circular array corresponding to the index position determined by the current system time when the content in the circular array corresponding to the index position is empty; then, when the content in the circular array corresponding to the index position is not empty, compare the current system time with the timestamp in the last stored primary key to determine whether the computer system clock has been dialed back. If dialed back has occurred (i.e., the current system time is less than the timestamp in the last stored primary key), the sequence identifier is incremented, a new primary key is generated based on the incremented sequence identifier, and the new primary key is stored as the primary key of the data in the database, thereby effectively improving the problem that the primary key generated when the system clock is dialed back is easily duplicated with the already generated primary key. The above-mentioned method for generating a database primary key may include:
[0026] Step S110: When the database needs to add a new piece of data, the current system time of the server where the database is located is obtained, and based on the current system time, the index position of the primary key of the data to be generated in the circular array is determined, and it is judged whether the content at the index position of the circular array is a null value (NULL) or an initialization value. If the content at the index position is a null value (NULL) or an initialization value, the primary key of the data is generated according to the set algorithm and stored at the index position, and the primary key is used as the primary key of the data stored in the database.
[0027] The algorithm mentioned above may be a Snowflake algorithm, and specifically a 64-bit Snowflake algorithm (Snowflake-64bit) may be used.
[0028] See Figure 2 A schematic diagram of the primary key in the circular array provided by an embodiment of the present application is shown; the first field of the circular array from left to right in the figure is the index position of the circular array, and the second field is the element of the circular array, and the index position is used to identify the storage location of the element; the primary key with index position 1 in the figure is all 0, which is a specific value set here to illustrate the format of the primary key. In specific use, the primary key with all 0s can be used as the initialization value. The format of the above-mentioned primary key can be the format of the 64-bit snowflake algorithm (Snowflake-64bit), of course, the format of the 32-bit snowflake algorithm can also be used according to the specific situation.
[0029] The elements in the circular array are recycled. For example, when multiple primary keys generated by the snowflake algorithm have been stored in elements with index positions 1 to 11, if the next primary key generated needs to be stored, the primary key can be stored in the element with index position 1. Similarly, the next primary key can be stored in the element with index position 2, thereby achieving circular use. Of course, the length of the circular array can also be set according to the number of primary keys that need to be generated within 1 millisecond. The length of the circular array refers to the maximum number of elements that the circular array can accommodate. For example, the length of the circular array in the figure is 11. Of course, the length can also be set to 20, 100 or 1000, etc.
[0030] Each primary key stored in the above circular array includes three fields: timestamp, machine ID and sequence ID; the machine ID is used to distinguish different physical servers, and the sequence ID can be self-incremented so that the sequence ID maintains an increasing trend. These three fields are connected in sequence, for example: Figure 1 As shown, the first binary bit is the sign bit, which indicates the positive or negative nature of the number in the computer (0 indicates that the number is positive, and 1 indicates a negative number). Since the primary key is usually a positive number, the value of the first binary bit is 0. Next, the number of binary bits of each of the three fields, timestamp, machine ID, and sequence ID, can be set according to the specific situation. They can be set to 41, 10, and 12 in the figure, or to 40, 8, and 15, etc.; among them, since the accuracy of the timestamp must be less than or equal to milliseconds, the number of binary bits of the timestamp needs to be greater than or equal to 13 bits.
[0031] Optionally, to avoid the situation where the system clock is rolled back before there are still elements in the circular array that do not store data (i.e., not all elements store the primary key), and in this case, since the primary key generated according to the time after the rollback will be stored in the empty elements, the primary key data stored in the circular array is not incrementally trending. Specifically, for example: Suppose the primary key generated at time T2 is generated in element K, and the clock is rolled back to time T1 (T1 < T2). If element K+1 is empty, and at this time, if the primary key is generated according to time T1 and the primary key generated according to time T1 is stored in element K+1, it can be clearly deduced that the primary key in the circular array at this time is not incrementally trending. Therefore, before using the circular array, it is necessary to initialize the circular array to avoid the problem that the primary key in the circular array is not incrementally trending.
[0032] That is, before using the circular array, the circular array can also be initialized. Specifically, for example: Before step S110, increment the sequence identifier, and generate a filled primary key value according to the preset initial timestamp, machine identifier, and the incremented sequence identifier, and then store the filled primary key value in the circular array until all elements of the circular array are not empty. Specifically, for example: Figure 1 In the circular array after being filled, of course, in the specific implementation process, the millisecond difference between the current time and January 1, 1970 00:00:00 of the computer epoch can be calculated, and the millisecond difference can be used as the timestamp to generate the primary key value, and then the generated primary key value is filled into all elements of the circular array.
[0033] By generating a filled primary key value according to the preset initial timestamp, machine identifier, and the incremented sequence identifier; storing the filled primary key value in the circular array until all elements of the circular array are not empty, the circular array is effectively initialized before being used. Suppose the circular array is not initialized before being used. Specifically, for example: The data generated at time T2 is generated in element K of the circular array. When the system clock is rolled back to time T1 (i.e., T1 < T2), and the value of element K+1 in the circular array is 0, an empty string, or a null value (NULL). At this time, if the primary key is generated according to the timestamp corresponding to time T1, the generated primary key data is not incrementally trending. Therefore, initializing the circular array before it is used can avoid the problem that the primary key generated according to the rolled-back timestamp is stored in the uninitialized elements of the circular array, thus ensuring the incremental trend of the generated primary key and effectively improving the stability and robustness of the database primary key generation method.
[0034] An example of an implementation of the above-mentioned step S110 is: when the database needs to add a new piece of data, the current system time of the server where the database is located is obtained, and based on the current system time, the index position of the primary key of the data to be generated in the circular array is determined. The method of determining the index position is specifically, for example: taking the remainder of the timestamp of the current system time and the length of the circular array, and determining the obtained remainder as the index position of the primary key in the circular array. The timestamp of the current system time is the difference between the current system moment and the preset initial moment. The initial moment here can be the computer's epoch time: the millisecond value corresponding to 1970-01-01 00:00:00. Of course, other times can also be used according to actual conditions; then, determine whether the content of the element at the index position is empty. If the content of the element at the index position is empty, generate the primary key of the data according to the set Snow algorithm and store it in the element at the index position, and store the primary key as the primary key of the data in the database.
[0035] In the above process of obtaining the last stored primary key from the circular array according to the index position, if the value obtained from the circular array according to the index position is the initialization value, then the normal process can be directly processed, that is, the sequence identifier is directly incremented to obtain the incremented sequence identifier, and a new primary key is generated according to the timestamp of the current system time, the machine identifier and the incremented sequence identifier; wherein the initialization value here includes: an empty string or zero, etc.
[0036] After step S110, execute step S120: if the content at the index position in the circular array is not empty, obtain the last stored primary key from the index position in the circular array, extract the timestamp, machine ID and sequence ID in the primary key, and compare the current system time with the timestamp in the last stored primary key.
[0037] An example of an implementation of the above step S120 is: first, if the content at the index position of the circular array is not empty, the last stored primary key is obtained from the index position in the circular array, and the timestamp, machine ID and sequence ID in the primary key are extracted by displacement, and then the timestamp of the current system time is compared with the timestamp in the last stored primary key; wherein, an example of an implementation of obtaining and comparing timestamps is: the current system time can be represented by SC_TIME (SystemCurrent Time), which can be represented by JAVA language as SC_TIME = System.currentTimeMillis(), and the current time of the system can be obtained; then, the timestamp conversion function is used to convert the current system time into the timestamp of the current system time, specifically for example: calculating the difference between the current time and the computer era 1970-01-01 00:00:00, and determine the millisecond difference between the current moment and the computer epoch as the timestamp of the current system time; finally, compare the timestamp in the primary key with the timestamp of the current system time to know whether the timestamp in the primary key is greater than the timestamp of the current system time. For example, subtract the timestamp of the current system time from the timestamp in the primary key to obtain the timestamp difference; if the timestamp difference is positive, it means that the timestamp in the primary key is greater than the timestamp of the current system time; if the timestamp difference is zero, it means that the timestamp in the primary key is equal to the timestamp of the current system time; if the timestamp difference is negative, it means that the timestamp in the primary key is less than the timestamp of the current system time.
[0038] After step S120, step S130 is executed: if the current system time is less than the timestamp in the last stored primary key, the sequence identifier is incremented to obtain the incremented sequence identifier, and a new primary key is generated based on the timestamp, machine identifier and incremented sequence identifier in the last stored primary key. The new primary key is then stored in the index position of the circular array, and the new primary key is stored as the primary key of the data in the database.
[0039] The implementation method of the above step S130 includes: if the timestamp of the current system time is less than the timestamp in the last stored primary key, the sequence identifier is incremented to obtain the incremented sequence identifier, and the timestamp, machine identifier and incremented sequence identifier in the last stored primary key are sequentially connected together to obtain a new primary key, and then the new primary key is stored in the index position of the circular array; the above process is specifically for example: assuming that the machine identifier is represented by MID, the sequence identifier is represented by N, the timestamp in the primary key is the difference between the number of milliseconds corresponding to 2020-11-02 00:00:00 and the number of milliseconds corresponding to the computer era (represented by A), and the timestamp of the current system time is 2020-11-01 The difference between the number of milliseconds corresponding to 00:00:00 and the number of milliseconds corresponding to the computer epoch (represented by B). It is not difficult to know that A is greater than B. Then the timestamp of the current system time is less than the timestamp in the last stored primary key, which means that the computer's system clock has been dialed back. At this time, N is incremented, and the sequence identifier after the increment is N+1. The timestamp A, machine identifier MID and N+1 in the primary key are connected together in sequence to obtain a new primary key. Finally, the new primary key is stored in the circular array.
[0040] The above implementation method of generating a new primary key based on the timestamp, machine identifier and self-incremented sequence identifier in the last stored primary key is, for example: judging whether the sequence identifier in the primary key processed at the current moment is detected to be a preset value, the preset value indicates that the values of all binary bits of the sequence identifier are one; when the clock dial-back time span is very large, it may happen that the binary bits of the sequence identifiers in all primary keys of the circular array are all one, that is, the identifier generation logic in the circular array will enter an infinite loop. If it is detected that the sequence identifier in the primary key processed at the current moment is a preset value, that is, the binary bits of the sequence identifier in the primary key processed at the current moment are all one, then the timestamp in the primary key is increased by an integer multiple of the length of the circular array, and the self-incremented sequence identifier is flipped to obtain the flipped sequence identifier, and then the primary key is generated based on the timestamp, machine identifier and flipped sequence identifier after increasing the timestamp by an integer multiple of the length of the circular array.
[0041] The above-mentioned implementation method of flipping the self-incremented sequence identifier is specifically for example: when the self-incremented sequence identifier is flipped for the first time, the primary key in the current element also needs to be changed. The specific change method can be to increase the timestamp of the primary key in the current element by N milliseconds, where N is equal to the length of the loop array, and then set all the sequence identifiers of the primary key in the current element to zero. That is to say, the generated primary key in the current element overdraws the time of the next N milliseconds, so that when the system clock callback length is large, after flipping the sequence identifiers in all element numbers, when it comes to element number K again, the sequence identifier can be increased from sequence number 0 to 1, thereby continuing to ensure the increasing trend of generated data.
[0042] In the above implementation process, when the content in the circular array corresponding to the index position determined by the current system time is empty, the generated primary key is stored in the content corresponding to the index position; then, when the content in the circular array corresponding to the index position is not empty, the current system time is compared with the timestamp in the last stored primary key to determine whether the computer system clock has been dialed back. If dialed back occurs (that is, the current system time is less than the timestamp in the last stored primary key), the sequence identifier is incremented, a new primary key is generated based on the incremented sequence identifier, and the new primary key is used as the primary key of the data stored in the database, thereby effectively improving the problem that the primary key generated when the system clock is dialed back is easily duplicated with the already generated primary key.
[0043] Optionally, whether a clock rollback occurs can be determined by determining whether the timestamp in the primary key is greater than the timestamp of the current system time. The above step S130 handles the case where a clock rollback occurs. The following also includes two cases where a clock rollback does not occur:
[0044] After step S120, step S140 is executed: if the current system time is equal to the timestamp in the last stored primary key, the sequence identifier is incremented to obtain the incremented sequence identifier, and a new primary key is generated based on the timestamp of the current system time, the machine identifier and the incremented sequence identifier. The new primary key is then stored in the index position of the circular array, and the new primary key is stored as the primary key of the data in the database.
[0045] For example, the implementation method of the above-mentioned step S140 is as follows: if the current system time is equal to the timestamp in the last stored primary key, it means that multiple primary keys need to be generated at the same time (i.e., within the same millisecond). At this time, it is only necessary to increment the sequence identifier to obtain the incremented sequence identifier, and generate a new primary key based on the timestamp of the current system time, the machine identifier and the incremented sequence identifier. The primary key obtained in this way is globally increasing; then the new primary key is stored in the index position of the circular array, and the new primary key is used as the primary key of the data stored in the database.
[0046] After step S120, step S150 is executed: if the current system time is greater than the timestamp in the last stored primary key, a new primary key is generated based on the timestamp of the current system time, the machine ID and the preset initial sequence, and then the new primary key is stored in the index position of the circular array, and the new primary key is stored as the primary key of the data in the database.
[0047] For example, in the implementation of step S150, if the current system time is greater than the timestamp in the last stored primary key, a new primary key need only be generated normally according to the current system time. In other words, a new primary key need only be generated based on the timestamp of the current system time, the machine ID, and the preset initial sequence. The new primary key is then stored in the index position of the circular array, and the new primary key is used as the primary key for the data stored in the database. In the above implementation process, by generating a new primary key based on the preset initial sequence when the current system time is greater than the timestamp in the last stored primary key, the situation in which all the binary bits of the sequence identifier are used up due to the continued self-increment of the sequence identifier is avoided, effectively improving the problem of duplicates between the generated new primary key and the already generated primary key.
[0048] Optionally, in an embodiment of the present application, if a new primary key is generated, the new primary key can also be applied in multiple places. The specific application process may include: if a new primary key is generated, then the new primary key is used to identify files or folders in a distributed file system, or, the new primary key is used to identify data records in a distributed database, or, the new primary key is used to identify data objects generated in memory. The data objects generated in memory are specifically, for example, data objects generated by a Redis database or MemCache; wherein, the distributed databases that can be used are, for example, Hadoop subsystems HBase, MongoDB, and CouchDB. In the above implementation process, by using a new primary key to identify data records, files, or folders in a distributed environment, the problem of obtaining duplicate data records, files, or folders through the primary key in a distributed environment is avoided, thereby effectively improving the stability and robustness of the database primary key generation method.
[0049] See Figure 3 The structure diagram of the database primary key generation device provided by the embodiment of the present application is shown; the embodiment of the present application provides a database primary key generation device 200, including:
[0050] The index content determination module 210 is used to obtain the current system time of the database server when a new data item needs to be added to the database. Based on the current system time, it determines the index position of the primary key of the data to be generated in the circular array and determines whether the content at the index position is empty. If the content at the index position is empty, it generates the primary key of the data according to a set algorithm and stores it at the index position. The primary key is then used as the primary key for the data to be stored in the database.
[0051] The primary key acquisition and extraction module 220 is configured to obtain the previously stored primary key from the index position in the circular array if the content at the index position is not empty, extract the timestamp, machine ID, and sequence ID from the primary key, and compare the current system time with the timestamp in the previously stored primary key;
[0052] The new primary key generation module 230 is used to increment the sequence identifier if the current system time is less than the timestamp in the last stored primary key, obtain the incremented sequence identifier, and generate a new primary key based on the timestamp, machine identifier and incremented sequence identifier in the last stored primary key, then store the new primary key in the index position of the circular array, and store the new primary key as the primary key of the data in the database.
[0053] Optionally, in an embodiment of the present application, the database primary key generating device further includes:
[0054] The first primary key storage module is used to increment the sequence identifier if the current system time is equal to the timestamp in the last stored primary key, obtain the incremented sequence identifier, and generate a new primary key based on the timestamp of the current system time, the machine identifier and the incremented sequence identifier, and then store the new primary key in the index position of the circular array, and store the new primary key as the primary key of the data in the database.
[0055] Optionally, in an embodiment of the present application, the database primary key generating device further includes:
[0056] The second primary key storage module is used to generate a new primary key based on the timestamp of the current system time, the machine identifier and the preset initial sequence if the current system time is greater than the timestamp in the last stored primary key, store the new primary key in the index position of the circular array, and store the new primary key as the primary key of the data in the database.
[0057] Optionally, in an embodiment of the present application, the primary key acquisition and extraction module includes:
[0058] The index position obtaining module is used to obtain the index position of the primary key in the circular array by taking the modulus of the difference between the current system time and the preset system initial time and the length of the circular array.
[0059] The primary key acquisition module is used to obtain the last stored primary key from the circular array according to the index position.
[0060] Optionally, in an embodiment of the present application, the primary key acquisition and generation module includes:
[0061] The sequence identifier judgment module is used to judge whether the sequence identifier in the primary key processed at the current moment is a preset value, and the preset value indicates that the values of all binary bits of the sequence identifier are one.
[0062] A new primary key generation module is used to increase the timestamp in the primary key by an integer multiple of the length of the circular array if it is detected that the sequence identifier in the primary key processed at the current moment is a preset value, and flip the self-incremented sequence identifier to obtain the flipped sequence identifier, and then generate the primary key based on the timestamp after adding the integer multiple of the length of the circular array, the machine identifier and the flipped sequence identifier.
[0063] Optionally, in the embodiment of the present application, the database primary key generating device may further include:
[0064] The filling primary key generation module is used to auto-increment the sequence identifier and generate a filling primary key value based on the preset initial timestamp, machine identifier and the auto-incremented sequence identifier.
[0065] The loop array filling module is used to store the filled primary key value into the loop array until all elements of the loop array are not empty.
[0066] Optionally, in the embodiment of the present application, the database primary key generating device may further include:
[0067] The primary key using module is used to use the new primary key to identify files or folders in the distributed file system if a new primary key is generated, or to use the new primary key to identify data records in the distributed database.
[0068] It should be understood that the device corresponds to the above-mentioned embodiment of the method for generating a database primary key and can perform each step involved in the above-mentioned method embodiment. The specific functions of the device can be found in the description above. To avoid repetition, a detailed description is omitted here. The device includes at least one software function module that can be stored in a memory in the form of software or firmware or embedded in the operating system (OS) of the device.
[0069] See Figure 4 The electronic device 300 provided in the embodiment of the present application includes a processor 310 and a memory 320, wherein the memory 320 stores machine-readable instructions executable by the processor 310, and when the machine-readable instructions are executed by the processor 310, the method described above is performed.
[0070] The embodiment of the present application further provides a storage medium 330 , on which a computer program is stored. When the computer program is run by the processor 310 , the above method is executed.
[0071] Among them, the storage medium 330 can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, magnetic disk or optical disk.
[0072] In the several embodiments provided in the embodiments of the present application, it should be understood that the disclosed devices and methods can also be implemented in other ways. The device embodiments described above are merely illustrative. For example, the flowcharts and block diagrams in the accompanying drawings show the possible architectures, functions, and operations of the devices, methods, and computer program products according to the multiple embodiments of the embodiments of the present application. In this regard, each box in the flowchart or block diagram can represent a module, a program segment, or a portion of code, and the module, program segment, or a portion of code contains one or more executable instructions for implementing the specified logical functions. It should also be noted that in some alternative implementations, the functions marked in the box can also be different from the order of occurrence marked in the accompanying drawings. For example, two consecutive boxes can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, which is mainly based on the functions involved. It should also be noted that each box in the block diagram and / or flowchart, and the combination of boxes in the block diagram and / or flowchart, can be implemented using a dedicated hardware-based system that performs the specified function or action, or can be implemented using a combination of dedicated hardware and computer instructions.
[0073] In addition, the functional modules of each embodiment in the embodiments of the present application can be integrated together to form an independent part, or each module can exist separately, or two or more modules can be integrated to form an independent part.
[0074] In this document, relational terms such as first and second, etc. are used merely to distinguish one entity or operation from another entity or operation, but do not necessarily require or imply any actual relationship or order between these entities or operations.
[0075] The above description is only an optional implementation method of the embodiment of the present application, but the protection scope of the embodiment of the present application is not limited to this. Any technician familiar with this technical field can easily think of changes or replacements within the technical scope disclosed in the embodiment of the present application, and they should all be covered by the protection scope of the embodiment of the present application.
Claims
1. A method for generating a database primary key, characterized in that: include: When a new piece of data needs to be added to the database, the current system time of the server where the database is located is obtained, and based on the current system time, the index position of the primary key of the data to be generated in the circular array is determined, and it is judged whether the content at the index position is empty. If it is empty, the primary key of the data is generated according to a set algorithm and stored in the index position, and the primary key is used as the primary key of the data stored in the database; Otherwise, obtaining the last stored primary key from the index position in the circular array, extracting the timestamp, machine ID, and sequence ID in the primary key, and comparing the current system time with the timestamp in the last stored primary key; If the current system time is less than the timestamp in the last stored primary key, the sequence identifier is incremented to obtain the incremented sequence identifier, and a new primary key is generated according to the timestamp in the last stored primary key, the machine identifier, and the incremented sequence identifier. The new primary key is then stored in the index position of the circular array, and the new primary key is used as the primary key for storing the data in the database. The generating of a new primary key based on the timestamp, the machine identifier, and the auto-incremented sequence identifier in the last stored primary key includes: determining whether it is detected that the sequence identifier in the primary key processed at the current moment is a preset value, wherein the preset value indicates that the values of all binary bits of the sequence identifier are one; if so, increasing the timestamp in the primary key by an integer multiple of the length of the circular array, flipping the auto-incremented sequence identifier to obtain a flipped sequence identifier, and then generating a primary key based on the timestamp after being increased by an integer multiple of the length of the circular array, the machine identifier, and the flipped sequence identifier.
2. The method according to claim 1, characterized in that After comparing the current system time with the timestamp in the last stored primary key, the method further includes: If the current system time is equal to the timestamp in the last stored primary key, the sequence identifier is incremented to obtain the incremented sequence identifier, and a new primary key is generated based on the current system time, the machine identifier and the incremented sequence identifier. The new primary key is then stored in the index position of the circular array, and the new primary key is used as the primary key for storing the data in the database.
3. The method according to claim 1, characterized in that After comparing the current system time with the timestamp in the last stored primary key, the method further includes: If the current system time is greater than the timestamp in the last stored primary key, a new primary key is generated based on the current system time, the machine ID and the preset initial sequence, the new primary key is stored in the index position of the circular array, and the new primary key is used as the primary key for storing the data in the database.
4. The method according to claim 1, wherein The obtaining the last stored primary key from the index position in the loop array includes: The modulo of the length of the circular array is the difference between the current system time and the preset system initial time, to obtain the index position of the primary key in the circular array; Get the last stored primary key from the loop array based on the index position.
5. The method according to claim 1, wherein Also includes: Incrementing the sequence identifier, and generating a fill primary key value according to a preset initial timestamp, the machine identifier, and the sequence identifier after the auto-increment; The filled primary key value is stored in the circular array until all elements of the circular array are not empty.
6. The method according to any one of claims 1 to 5, characterized in that Also includes: If a new primary key is generated, the new primary key is used to identify a file or folder in a distributed file system, or the new primary key is used to identify a data record in a distributed database.
7. A database primary key generation device, characterized in that: include: An index content determination module is configured to, when a new piece of data needs to be added to the database, obtain the current system time of the server where the database is located, determine the index position of the primary key of the data to be generated in the circular array based on the current system time, determine whether the content at the index position is empty, and if so, generate the primary key of the data according to a set algorithm and store it at the index position, and use the primary key as the primary key of the data stored in the database; a primary key acquisition and extraction module, configured to, if the content at the index position is not empty, obtain the last stored primary key from the index position in the circular array, extract the timestamp, machine identifier, and sequence identifier from the primary key, and compare the current system time with the timestamp in the last stored primary key; a new primary key generation module, configured to, if the current system time is less than the timestamp in the last stored primary key, increment the sequence identifier to obtain the incremented sequence identifier, generate a new primary key based on the timestamp in the last stored primary key, the machine identifier, and the incremented sequence identifier, then store the new primary key in the index position of the circular array, and use the new primary key as the primary key for storing the data in the database; The generating of a new primary key based on the timestamp, the machine identifier, and the auto-incremented sequence identifier in the last stored primary key includes: determining whether it is detected that the sequence identifier in the primary key processed at the current moment is a preset value, wherein the preset value indicates that the values of all binary bits of the sequence identifier are one; if so, increasing the timestamp in the primary key by an integer multiple of the length of the circular array, flipping the auto-incremented sequence identifier to obtain a flipped sequence identifier, and then generating a primary key based on the timestamp after being increased by an integer multiple of the length of the circular array, the machine identifier, and the flipped sequence identifier.
8. An electronic device, characterized in that: include: A processor and a memory, wherein the memory stores machine-readable instructions executable by the processor, and when the machine-readable instructions are executed by the processor, the method according to any one of claims 1 to 6 is performed.
9. A storage medium, characterized in that: The storage medium stores a computer program, which, when executed by a processor, executes the method according to any one of claims 1 to 6.
Citation Information
Patent Citations
Distributed database main key generation method and apparatus
CN108241715A
Time series data storage method and device, server and storage medium
CN111125089A