Method and device for acquiring primary key

文档序号:1952645 发布日期:2021-12-10 浏览:34次 中文

阅读说明:本技术 主键的获取方法和装置 (Method and device for acquiring primary key ) 是由 王博 于 2021-01-26 设计创作,主要内容包括:本发明公开了一种主键的获取方法和装置,涉及计算机技术领域。该主键的获取方法的一具体实施方式包括:若当前可用主键列表中的列表余量达到更新阈值,则确定本批次主键的更新数量,所述更新数量是根据历史批次的主键更新数据确定的;获取当前可用主键列表的主键使用记录;若当前可用主键列表的主键使用记录不为空,则根据当前可用主键列表的主键使用记录获取所述更新数量个主键,并更新所述主键使用记录。该实施方式可以自动计算和实时更新(自适应)每批更新主键数量,可以降低出现线上事故的风险,可以解决现有获取方式需要根据主键的使用情况手动且频繁修改主键的更新数量的问题。(The invention discloses a method and a device for acquiring a primary key, and relates to the technical field of computers. One specific implementation of the method for acquiring the primary key includes: if the list allowance in the current available primary key list reaches an update threshold, determining the update quantity of the primary keys in the current batch, wherein the update quantity is determined according to the primary key update data of the historical batch; acquiring a primary key use record of a current available primary key list; and if the primary key use record of the current available primary key list is not empty, acquiring the updated number of primary keys according to the primary key use record of the current available primary key list, and updating the primary key use record. The implementation method can automatically calculate and update (self-adaptively) the number of the updated main keys in each batch, can reduce the risk of online accidents, and can solve the problem that the existing acquisition mode needs to manually and frequently modify the updated number of the main keys according to the use conditions of the main keys.)

1. A method for acquiring a primary key, comprising:

if the list allowance in the current available primary key list reaches an update threshold, determining the update quantity of the primary keys in the current batch, wherein the update quantity is determined according to the primary key update data of the historical batch;

acquiring a primary key use record of a current available primary key list;

and if the primary key use record of the current available primary key list is not empty, acquiring the updated number of primary keys according to the primary key use record of the current available primary key list, and updating the primary key use record.

2. The method of claim 1, wherein the primary key update data comprises at least: the number of primary key updates for the historical batch and the primary key update time for the historical batch.

3. The method of claim 2, wherein determining the updated number of primary keys for the batch comprises:

acquiring a first time of updating a primary key of a first historical batch, wherein the first historical batch is a previous batch adjacent to the current batch;

if the first time is not empty, calculating a second time spent by the first historical batch main key;

acquiring a third time taken by a primary key of a second historical batch to be consumed, wherein the second historical batch is a previous batch adjacent to the first historical batch;

if the third time is not empty, acquiring the updated number of the primary keys of the first historical batch;

and determining the updating number of the main keys of the batch according to the second time, the third time and the updating number of the main keys of the first historical batch.

4. The method of claim 3, wherein determining the updated number of the primary keys of the current batch according to the second time, the third time and the updated number of the primary keys of the first historical batch comprises:

calculating a ratio of the second time to the third time;

and if the update quantity of the primary keys of the first historical batch is not null, determining the product of the update quantity of the primary keys of the first historical batch and the ratio as the update quantity of the primary keys of the current batch.

5. The method of claim 1, wherein the primary key usage record comprises at least: the system comprises a primary record used by one primary key and a plurality of non-primary records, wherein the primary record is used for indicating the number of used primary keys, and the non-primary records are used for indicating available primary key value intervals.

6. The method of claim 5, wherein the data storage structure of the primary key usage record includes at least the following information: the table name, the used primary key minimum, the used primary key maximum, whether it is the primary record of the current available primary key list, the data valid flag, and the update time are stored.

7. The method of claim 5, wherein obtaining the updated number of primary keys and updating the primary key usage record comprises:

acquiring a valid non-master record of a current available master key list according to a time positive sequence;

if the obtained result of the non-main record is null, obtaining a main record corresponding to the current available main key list;

and acquiring the updated number of main keys according to the current main record, and updating the maximum value of the used main keys of the current main record.

8. The method of claim 7, wherein updating the primary key usage record for the list of available primary keys further comprises:

if the obtained result of the non-primary record is not null and the difference value between the maximum value of the primary keys and the minimum value of the primary keys in the non-primary record is less than the updating number of the primary keys, obtaining the primary keys with the updating number according to the primary key value of the current non-primary record and updating the current non-primary record into an invalid record; then, repeatedly executing the step of obtaining an effective non-main record in the current available main key list according to the time positive sequence until the non-main record meets the requirement;

if the obtained result of the non-main record is not null and the difference value between the maximum value of the main keys and the minimum value of the main keys in the non-main record is greater than or equal to the updating number of the main keys, starting to obtain the main keys with the updating number from the minimum value of the main keys; if the current non-main record has the residual main keys, updating the minimum value of the main keys of the current non-main record; and if the current non-main record has no residual main keys, updating the current non-main record into an invalid record.

9. The method of claim 5, wherein updating the primary key usage record for the list of available primary keys comprises:

and when the mobile terminal is restarted or is on line, generating a non-main record according to the related information of the remaining main keys so as to store the remaining main keys into a database.

10. A primary key acquisition apparatus, comprising:

the determining module is used for determining the updating quantity of the main keys in the batch if the list allowance in the current available main key list reaches an updating threshold, wherein the updating quantity is determined according to the main key updating data of the historical batch;

the first acquisition module is used for acquiring a primary key use record of a current available primary key list;

and the second obtaining module is used for obtaining the updated number of the main keys according to the main key use record of the current available main key list and updating the main key use record if the main key use record of the current available main key list is not empty.

11. An electronic device, comprising:

one or more processors;

a storage device for storing one or more programs,

when executed by the one or more processors, cause the one or more processors to implement the method of any one of claims 1-9.

12. A computer-readable medium, on which a computer program is stored, which, when being executed by a processor, carries out the method according to any one of claims 1-9.

Technical Field

The invention relates to the technical field of computers, in particular to a method and a device for acquiring a main key.

Background

Generally, in the process of acquiring the primary key, the number of the primary keys to be updated in each batch needs to be manually set in advance, that is, the primary keys can be manually adjusted according to the online service request volume in daily operation and maintenance, the primary keys need to be manually adjusted again according to estimated flow during activity promotion, the parameters of the acquired primary keys need to be frequently adjusted, and the adjustment work depends on experience. And if the number of the main keys in the local memory is not used up, the system is on line or restarted, and the main keys in the local memory are abandoned. When the system has a very large carrying flow, the number of the online machines is very large, the number of the primary keys stored in the memory of each machine is also very large, and a large number of primary keys are wasted finally, so that the exhaustion of the primary key resources is accelerated. If the primary key is used for business purposes such as order number, the limited order number segment will also lead to a great waste and rapid exhaustion of order number resources.

Disclosure of Invention

In view of this, embodiments of the present invention provide a method and an apparatus for acquiring a primary key, which can solve the problem that the existing acquisition method needs to manually and frequently modify the update quantity of the primary key according to the use condition of the primary key and the problem of a large amount of waste of primary key resources caused by restart and online, and reduce the online accident risk.

To achieve the above object, according to an aspect of an embodiment of the present invention, there is provided a method for acquiring a primary key.

The method for acquiring the main key comprises the following steps:

if the list allowance in the current available primary key list reaches an update threshold, determining the update quantity of the primary keys in the current batch, wherein the update quantity is determined according to the primary key update data of the historical batch;

acquiring a primary key use record of a current available primary key list;

and if the primary key use record of the current available primary key list is not empty, acquiring the updated number of primary keys according to the primary key use record of the current available primary key list, and updating the primary key use record.

Optionally, the primary key update data includes at least: the number of primary key updates for the historical batch and the primary key update time for the historical batch.

Optionally, determining the updated number of the primary keys of the batch includes:

acquiring a first time of updating a primary key of a first historical batch, wherein the first historical batch is a previous batch adjacent to the current batch;

if the first time is not empty, calculating a second time spent by the first historical batch main key;

acquiring a third time taken by a primary key of a second historical batch to be consumed, wherein the second historical batch is a previous batch adjacent to the first historical batch;

if the third time is not empty, acquiring the updated number of the primary keys of the first historical batch;

and determining the updating number of the main keys of the batch according to the second time, the third time and the updating number of the main keys of the first historical batch.

Optionally, determining the updated number of the primary keys of the current batch according to the second time, the third time and the updated number of the primary keys of the first historical batch comprises:

calculating a ratio of the second time to the third time;

and if the update quantity of the primary keys of the first historical batch is not null, determining the product of the update quantity of the primary keys of the first historical batch and the ratio as the update quantity of the primary keys of the current batch.

Optionally, the primary key usage record includes at least: one primary key uses a primary record indicating the number of used primary keys and a plurality of non-primary records indicating the available primary key value intervals.

Optionally, the data storage structure of the primary key usage record includes at least the following information: the table name, the used primary key minimum, the used primary key maximum, whether it is the primary record of the current available primary key list, the data valid flag, and the update time are stored.

Optionally, the obtaining the updated number of primary keys, and the updating the primary key usage record includes:

acquiring a valid non-master record of a current available master key list according to a time positive sequence;

if the obtained result of the non-main record is null, obtaining a main record corresponding to the current available main key list;

and acquiring the updated number of main keys according to the current main record, and updating the maximum value of the used main keys of the current main record.

Optionally, updating the primary key usage record of the available primary key list further comprises:

if the obtained result of the non-primary record is not null and the difference value between the maximum value of the primary keys and the minimum value of the primary keys in the non-primary record is less than the updating number of the primary keys, obtaining the primary keys with the updating number according to the primary key value of the current non-primary record and updating the current non-primary record into an invalid record; then, repeatedly executing the step of obtaining an effective non-main record in the current available main key list according to the time positive sequence until the non-main record meets the requirement;

if the obtained result of the non-main record is not null and the difference value between the maximum value of the main keys and the minimum value of the main keys in the non-main record is greater than or equal to the updating number of the main keys, starting to obtain the main keys with the updating number from the minimum value of the main keys; if the current non-main record has the residual main keys, updating the minimum value of the main keys of the current non-main record; and if the current non-main record has no residual main keys, updating the current non-main record into an invalid record.

Optionally, updating the primary key usage record of the available primary key list comprises:

and when the mobile terminal is restarted or is on line, generating a non-main record according to the related information of the remaining main keys so as to store the remaining main keys into a database.

To achieve the above object, according to another aspect of an embodiment of the present invention, there is provided a primary key acquisition apparatus.

The main key acquisition device of the embodiment of the invention comprises:

the determining module is used for determining the updating quantity of the main keys in the batch if the list allowance in the current available main key list reaches an updating threshold, wherein the updating quantity is determined according to the main key updating data of the historical batch;

the first acquisition module is used for acquiring a primary key use record of a current available primary key list;

and the second obtaining module is used for obtaining the updated number of the main keys according to the main key use record of the current available main key list and updating the main key use record if the main key use record of the current available main key list is not empty.

To achieve the above object, according to another aspect of an embodiment of the present invention, there is provided an electronic apparatus.

The electronic device of the embodiment of the invention comprises:

one or more processors;

a storage device for storing one or more programs,

when executed by the one or more processors, cause the one or more processors to implement the method as described above.

To achieve the above object, according to another aspect of an embodiment of the present invention, there is provided a computer-readable medium.

A computer-readable medium of an embodiment of the invention has stored thereon a computer program which, when executed by a processor, implements the method as described above.

One embodiment of the above invention has the following advantages or benefits:

in the embodiment of the invention, the main keys can be obtained according to the updating quantity when being obtained, the updating quantity is determined according to the updating data of the main keys of historical batches, the quantity of the main keys updated in each batch can be automatically calculated and updated (self-adaptive), the risk of online accidents can be reduced, the problem that the updating quantity of the main keys needs to be manually and frequently modified according to the using condition of the main keys in the existing obtaining mode can be solved, and the problem of large waste of main key resources caused by restarting and online can be solved.

Further effects of the above-mentioned non-conventional alternatives will be described below in connection with the embodiments.

Drawings

The drawings are included to provide a better understanding of the invention and are not to be construed as unduly limiting the invention. Wherein:

FIG. 1 is a schematic flow chart of a conventional method for acquiring a primary key;

FIG. 2 is a schematic flow chart of a conventional method for updating an available primary key list;

FIG. 3 is a flowchart illustrating a method for acquiring a primary key according to a first embodiment of the present invention;

FIG. 4 is a flowchart illustrating a method for acquiring a primary key according to a second embodiment of the present invention;

FIG. 5 is a flowchart illustrating the updating of a primary key usage record according to an embodiment of the present invention;

FIG. 6 is a flow diagram of constructing a non-primary record according to an embodiment of the present invention;

FIG. 7 is a block diagram of a device for acquiring a primary key according to an embodiment of the present invention;

FIG. 8 is an exemplary system architecture diagram in which embodiments of the present invention may be employed;

fig. 9 is a schematic structural diagram of a computer system suitable for implementing a terminal device or a server according to an embodiment of the present invention.

Detailed Description

Exemplary embodiments of the present invention are described below with reference to the accompanying drawings, in which various details of embodiments of the invention are included to assist understanding, and which are to be considered as merely exemplary. Accordingly, those of ordinary skill in the art will recognize that various changes and modifications of the embodiments described herein can be made without departing from the scope and spirit of the invention. Also, descriptions of well-known functions and constructions are omitted in the following description for clarity and conciseness.

With the increasing demand of high flow rate and high concurrency, a distributed database key acquisition method is generally adopted when a transaction system acquires a key at present. Referring to fig. 1, the procedure of the acquisition method roughly includes the following steps S101 to S105.

Step S101: and acquiring an available primary key list from a local memory.

Step S102: and judging whether the list allowance in the available primary key list reaches an updating threshold value. If yes, go to step S103. Otherwise, step S104 is executed.

In step S102, the update threshold refers to a ratio of the number of remaining available primary keys to the number of primary keys updated in each batch when the update operation is triggered. For example: when less than 10 primary keys remain, the update operation is initiated each time 100 primary keys are updated into the local memory, where the update threshold is 90%.

Step S103: the available primary key list is updated and then execution proceeds to step S101.

Step S104: an available primary key is obtained from the list of available primary keys.

Step S105: the currently acquired primary key is deleted from the list of available primary keys.

Referring to fig. 2, a specific process of updating the available primary key list includes steps S1031 to S1035 as follows.

Step S1031: a primary key usage record of a list of currently available primary keys is obtained.

Step S1032: and judging whether the main key use record is empty or not. If yes, go to step S1033; otherwise, step S1034 is performed.

Step S1033: a primary key usage record for the list of currently available primary keys is created.

In step S1033, the primary key usage record refers to a piece of data stored in the database, and the storage structure of the usage record data is as follows:

name of field Type of field Whether or not it can empty Remarks for note
table_name varchar(100) Whether or not Primary key for storing table name
used_quantity bigint(20) Whether or not Number of used primary keys

Each table stores a piece of usage record data, wherein a table _ name field is used for storing a table name, and a used _ quality field is used for storing the number of used primary keys.

When the system needs to acquire a new batch of main keys according to the use record, firstly acquiring used _ quality according to the table _ name, then accumulating a certain value, and finally returning the main keys in the interval before and after accumulation; if the used _ quality cannot be acquired, a table usage record is first inserted, and then the above procedure of acquiring the primary key is repeated.

For example: the teacher table needs to obtain 100 primary keys, if the corresponding used _ quality is found to be 3500 through query, the used _ quality is updated to be 3600, and then 100 primary keys of which the used _ quality is 3501 to 3600 are returned.

Step S1034: a specified number of primary keys are acquired, and a primary key usage record is updated.

In step S1034, the specified number is typically configured manually. The specified quantity may be configured smaller when the rate of daily use is lower and larger when the promotional activity is highly concurrent. If the primary key is updated for a long time due to the fact that the designated number is configured too much, the difference of the primary key values in different machines is large, potential safety hazards exist, and the default use rule is that the primary key generated later is larger. If the specified quantity is set too small when the QPS performance is large, the machine needs to request the database to update the primary key frequently, and the QPS performance is affected finally.

Step S1035: and updating the acquired primary key list to a local memory.

In order to solve the problem that the existing acquisition mode needs to manually and frequently modify the update quantity of the main keys according to the use conditions of the main keys, based on the above embodiments, the embodiment of the invention provides the main key acquisition method, which can automatically calculate and update (self-adaptively) the quantity of the main keys updated in each batch, and can reduce the risk of online accidents. Fig. 3 is a flowchart illustrating a method for acquiring a primary key according to an embodiment of the present invention, and as shown in fig. 3, the method for acquiring a primary key may include steps S301 to S303 as follows.

Step S301: and if the list allowance in the current available primary key list reaches an update threshold, determining the update quantity of the primary keys in the current batch, wherein the update quantity is determined according to the primary key update data of the historical batch.

In step S301, the update threshold refers to a ratio of the number of remaining available primary keys to the number of primary keys updated per batch when an update operation is triggered. The update quantity refers to the target quantity of the primary keys obtained from the database, the update quantity is related to the primary key update data of the historical batches with the preset quantity adjacent to the batch, and the specific value of the preset quantity can be determined according to actual needs. For example: the preset number is two, that is, the update number of the primary key can be obtained according to the primary key update data of two historical batches adjacent to the batch. The primary key updating data refers to historical parameter data of each primary key acquisition process, and at least comprises the following steps: the updating quantity of the primary keys of the historical batches and the updating time of the primary keys of the historical batches are determined, and the time spent by the primary keys of the historical batches is determined according to the updating time of the primary keys of the historical batches.

Further, when the updated number of the primary keys is obtained according to the primary key update data of two historical batches adjacent to the current batch, the specific process of obtaining the updated number of the primary keys is roughly as follows: firstly, acquiring first time for updating a primary key of a first historical batch, wherein the first historical batch is a previous batch adjacent to the current batch; if the first time is not empty, calculating a second time spent by the first historical batch main key; then acquiring a third time taken by a main key of a second historical batch to be consumed, wherein the second historical batch is a previous batch adjacent to the first historical batch; if the third time is not empty, acquiring the updated number of the primary keys of the first historical batch; and finally, determining the updated number of the primary keys of the batch according to the second time, the third time and the updated number of the primary keys of the first historical batch. For example: a ratio of the second time to the third time may be calculated; and if the update quantity of the primary keys of the first historical batch is not null, determining the product of the update quantity of the primary keys of the first historical batch and the ratio as the update quantity of the primary keys of the current batch.

Step S302: a primary key usage record of a list of currently available primary keys is obtained.

Wherein the data storage structure of the primary key usage record includes at least the following information: the table name, the used primary key minimum, the used primary key maximum, whether it is the primary record of the current available primary key list, the data valid flag, and the update time are stored. The data storage structure of the primary key usage record is shown in the following table:

it should be noted that the primary key usage record at least includes: one primary key uses a primary record indicating the number of used primary keys and a plurality of non-primary records indicating the available primary key value intervals. It will be appreciated that each table has a primary key using a primary record and a plurality of non-primary records, the primary record being positively valid, some of the non-primary records being valid and some of the non-primary records being invalid. The used _ quality _ begin of the primary record must be equal to 0, the used _ quality _ end has the same meaning as the used _ quality, and the non-primary record records the usable primary key interval through the used _ quality _ begin and the used _ quality _ end. Wherein the combined unique index is table _ name + isMaster + isValid + used _ quality _ begin.

Step S303: and if the primary key use record of the current available primary key list is not empty, acquiring the updated number of primary keys according to the primary key use record of the current available primary key list, and updating the primary key use record.

In step S303, before acquiring the updated number of primary keys, a valid non-primary record of the currently available primary key list may be acquired in a time positive order, and then according to whether a result of the acquired non-primary record is empty, the acquiring process is divided into the following cases:

the first situation is as follows: the result of the non-primary record being obtained is null.

If the obtained result of the non-main record is null, obtaining a main record corresponding to the current available main key list; and acquiring the updated number of main keys according to the current main record, and updating the maximum value of the used main keys of the current main record.

Case two: and the obtained result of the non-main record is not null, and the difference value between the maximum value of the main key and the minimum value of the main key in the non-main record is less than the updating number of the main key.

If the obtained result of the non-primary record is not null and the difference value between the maximum value of the primary keys and the minimum value of the primary keys in the non-primary record is less than the updating number of the primary keys, obtaining the primary keys with the updating number according to the primary key value of the current non-primary record and updating the current non-primary record into an invalid record; and then repeatedly executing the step of acquiring a valid non-master record in the current available master key list according to the time positive sequence until the non-master record meets the requirement.

Case three: and the obtained result of the non-main record is not null, and the difference value between the maximum value of the main key and the minimum value of the main key in the non-main record is greater than or equal to the updating number of the main key.

If the obtained result of the non-main record is not null and the difference value between the maximum value of the main keys and the minimum value of the main keys in the non-main record is greater than or equal to the updating number of the main keys, starting to obtain the main keys with the updating number from the minimum value of the main keys; if the current non-main record has the residual main keys, updating the minimum value of the main keys of the current non-main record; and if the current non-main record has no residual main keys, updating the current non-main record into an invalid record.

It should be emphasized that, in order to solve the problem of a large amount of waste of the primary key resources caused by restart and online, when the system is restarted or online, a non-primary record is generated according to the related information of the remaining primary keys, so as to store the remaining primary keys in the database.

After step S303, the updated number of the primary keys in the current batch, the updated time of the primary keys in the current batch, and the time spent by the primary keys in the first historical batch may be saved in the memory.

In the embodiment of the invention, the main keys can be acquired according to the updating number when being acquired, the updating number is determined according to the updating data of the main keys in historical batches, the updating number of the main keys in each batch can be automatically calculated and updated (self-adaptive), the risk of online accidents can be reduced, and the problem that the updating number of the main keys needs to be manually and frequently modified according to the use condition of the main keys in the conventional acquisition mode can be solved.

In order to solve the problem that the existing acquisition mode needs to manually and frequently modify the update quantity of the main keys according to the use conditions of the main keys, based on the above embodiments, the embodiment of the invention provides the main key acquisition method, which can automatically calculate and update (self-adaptively) the quantity of the main keys updated in each batch, and can reduce the risk of online accidents. Fig. 4 is a flowchart illustrating a method for acquiring a primary key according to an embodiment of the present invention, and as shown in fig. 4, the method for acquiring a primary key may include steps S401 to S413 as follows.

Step S401: acquiring a first time of updating a primary key of a first historical batch, wherein the first historical batch is a previous batch adjacent to the current batch.

Step S402: and judging whether the first time is empty or not. If the first time is empty, executing step S403 to step S404; if the first time is not empty, step S405 is executed.

Step S403: and if the first time is empty, storing the updating time of the main key of the batch into a memory.

Step S404: returning to a default value, and then ending the flow; the default values may be preset, for example: the default value is 100.

Step S405: and if the first time is not empty, calculating a second time spent by the first history batch main key.

Step S406: and acquiring a third time taken for a second historical batch to be consumed by the main key, wherein the second historical batch is a previous batch adjacent to the first historical batch.

Step S407: judging whether the third time is empty or not; if the third time is empty, step S408 to step S410 are executed. If the third time is not empty, step S411 is executed.

Step S408: and if the third time is empty, storing the updating time of the main key of the batch into a memory.

Step S409: and saving the time spent by the primary key of the first historical batch to a memory.

Step S410: and returning to the default value, and then ending the flow.

Step S411: if the third time is not empty, calculating a ratio m between the second time and the third time, and then executing step S412.

Step S412: and acquiring the updated number of the primary keys of the first historical batch.

Step S413: and judging whether the updating quantity of the primary keys of the first historical batch is null or not. If the updated number of the primary keys of the first history batch is null, step S414 to step S417 are executed. If the updated number of the primary keys of the first history batch is not null, step S418 is executed.

Step S414: and if the updating quantity of the primary keys of the first historical batch is empty, storing the updating time of the primary keys of the batch in a memory.

Step S415: and saving the time spent by the primary key of the first historical batch to a memory.

Step S416: and storing the updated number of the main keys of the batch in a memory.

Step S417: and returning to the default value, and ending the flow.

Step S418: and if the update number of the primary keys of the first historical batch is not null, determining the product of the update number of the primary keys of the first historical batch and the ratio m as the update number of the primary keys acquired from the database for the batch.

Step S419: and storing the updating time of the main key of the batch into a memory.

Step S420: and saving the time spent by the primary key of the first historical batch to a memory.

Step S421: and storing the updated number of the main keys of the batch in a memory.

Step S422: and returning the updated number of the main keys of the batch.

In the embodiment of the invention, when the updating number is determined, the updating number can be determined based on the updating data of the main keys of the historical batches with the preset number adjacent to the batch, the updating number of the main keys in each batch can be automatically calculated and updated (self-adaptive), the risk of online accidents can be reduced, and the problem that the updating number of the main keys needs to be manually and frequently modified according to the use condition of the main keys in the conventional acquisition mode can be solved.

In order to solve the problem of a large amount of waste of the main key resources caused by restart and online, on the basis of the above embodiment, the embodiment of the invention provides a method for acquiring the main key, which realizes a processing flow through a new data storage structure of the main key use record, so that the main key which is not used up during each restart can be stored in a Database (DB), and the main key is returned and continuously used when the main key is acquired next time, thereby solving the problem of a large amount of waste of the main key resources caused by restart and online. Fig. 5 is a flowchart illustrating a method for acquiring a primary key according to an embodiment of the present invention, and as shown in fig. 5, the method for acquiring a primary key may include steps S501 to S513 as follows.

The data storage structure of the primary key use record is shown as the following table:

name of field Type of field Whether or not it can empty Remarks for note
table_name varchar(100) Whether or not Storing table names
used_quantity_begin bigint(20) Whether or not Used primary key minimum
used_quantity_end bigint(20) Whether or not Maximum value of used primary key
isMaster tinyint(4) Whether or not Whether to present the main record of the table
isValid tinyint(4) Whether or not Data valid flag bit
updateTime datetime Whether or not Update time

It will be appreciated that each table has a primary key using a primary record and a plurality of non-primary records, the primary record being positively valid, some of the non-primary records being valid and some of the non-primary records being invalid. The used _ quality _ begin of the primary record must be equal to 0, the used _ quality _ end has the same meaning as the used _ quality, and the non-primary record records the usable primary key interval through the used _ quality _ begin and the used _ quality _ end. Wherein the combined unique index is table _ name + isMaster + isValid + used _ quality _ begin.

Step S501: a valid non-primary record in the currently available primary key list is obtained in chronological order.

Step S502: judging whether the obtained result of the non-main record is empty or not; if yes, go to step S503 to step S505; otherwise, step S506 is executed.

Step S503: and if the obtained result of the non-main record is null, obtaining the main record corresponding to the specified data table.

Step S504: and recording the updated number of the main keys in the current main record into a return result so as to acquire the updated number of the main keys according to the current main record.

Step S505: the used primary key maximum value of the current primary record is updated.

Step S506: and if the obtained result of the non-main record is not null, judging whether the difference value between the maximum value of the main keys and the minimum value of the main keys in the non-main record is greater than or equal to the updating number of the main keys. If yes, go to step S507 to step S510. Otherwise, step S511 to step S512 are executed.

Step S507: if the obtained result of the non-main record is not null and the difference between the maximum value of the main keys in the non-main record and the minimum value of the main keys is greater than or equal to the update number of the main keys, recording the main keys with the update number into the return result from the minimum value of the main keys so as to obtain the main keys with the update number from the minimum value of the main keys, and then executing the steps S508 to S510.

Step S508: judging whether the current non-main record has residual main keys; if yes, go to step S510; otherwise, step S511 is performed.

Step S509: and if the current non-main record has the remaining main keys, updating the minimum value of the main keys of the current non-main record, and then returning the result.

Step S510: and if the current non-main record has no residual main keys, updating the current non-main record into an invalid record, and then returning the result.

Step S511: if the difference value between the maximum value of the primary keys and the minimum value of the primary keys in the non-primary record is smaller than the updating number of the primary keys, acquiring the primary keys with the updating number according to the primary key values of the current non-primary record

Step S512: the current non-primary record is updated to an invalid record. Then, step S501 is repeatedly executed until the non-primary record satisfies the requirement.

The embodiment of the invention designs a data storage structure of a primary key use record, so that the primary key which is not used up during each restart can be stored in a Database (DB), and the primary key is returned and continuously used when the primary key is acquired next time, thereby solving the problem of large waste of primary key resources caused by restart and online.

On the basis of the embodiment shown in fig. 5, in order to solve the problem of a large amount of waste of the primary key resources caused by restart and online, a non-primary record may be generated according to the related information of the remaining primary keys when the key is restarted or online, so as to store the remaining primary keys in the database.

Referring to fig. 6, constructing the non-master record may specifically include the steps of: firstly, setting the minimum value of the remaining primary keys as used _ quality _ begin; then setting the maximum value of the remaining primary keys as used _ quality _ end; then the isMaster is 2-non-primary record. And setting the name of the data table, and finally storing the remaining main keys into a database. It should be noted that the embodiment of the present invention does not specifically limit the setting order of the parameters.

Fig. 7 is a schematic block diagram of an acquisition apparatus for a primary key according to an embodiment of the present invention, and referring to fig. 7, the acquisition apparatus 700 for a primary key may specifically include the following modules:

a determining module 701, configured to determine, if a list margin in a current available primary key list reaches an update threshold, an update number of primary keys in a current batch, where the update number is determined according to primary key update data in a historical batch;

a first obtaining module 702, configured to obtain a primary key usage record of a current available primary key list;

a second obtaining module 703, configured to, if the primary key usage record of the current available primary key list is not empty, obtain the updated number of primary keys according to the primary key usage record of the current available primary key list, and update the primary key usage record.

Optionally, the primary key update data includes at least: the number of primary key updates for the historical batch and the primary key update time for the historical batch.

Optionally, the determining module 701 is further configured to:

acquiring a first time of updating a primary key of a first historical batch, wherein the first historical batch is a previous batch adjacent to the current batch;

if the first time is not empty, calculating a second time spent by the first historical batch main key;

acquiring a third time taken by a primary key of a second historical batch to be consumed, wherein the second historical batch is a previous batch adjacent to the first historical batch;

if the third time is not empty, acquiring the updated number of the primary keys of the first historical batch;

and determining the updating number of the main keys of the batch according to the second time, the third time and the updating number of the main keys of the first historical batch.

Optionally, the determining module 701 is further configured to:

calculating a ratio of the second time to the third time;

and if the update quantity of the primary keys of the first historical batch is not null, determining the product of the update quantity of the primary keys of the first historical batch and the ratio as the update quantity of the primary keys of the current batch.

Optionally, the primary key usage record includes at least: one primary key uses a primary record indicating the number of used primary keys and a plurality of non-primary records indicating the available primary key value intervals.

Optionally, the data storage structure of the primary key usage record includes at least the following information: the table name, the used primary key minimum, the used primary key maximum, whether it is the primary record of the current available primary key list, the data valid flag, and the update time are stored.

Optionally, the second obtaining module 703 is further configured to:

acquiring a valid non-master record of a current available master key list according to a time positive sequence;

if the obtained result of the non-main record is null, obtaining a main record corresponding to the current available main key list;

and acquiring the updated number of main keys according to the current main record, and updating the maximum value of the used main keys of the current main record.

Optionally, the second obtaining module 703 is further configured to:

if the obtained result of the non-primary record is not null and the difference value between the maximum value of the primary keys and the minimum value of the primary keys in the non-primary record is less than the updating number of the primary keys, obtaining the primary keys with the updating number according to the primary key value of the current non-primary record and updating the current non-primary record into an invalid record; then, repeatedly executing the step of obtaining an effective non-main record in the current available main key list according to the time positive sequence until the non-main record meets the requirement;

if the obtained result of the non-main record is not null and the difference value between the maximum value of the main keys and the minimum value of the main keys in the non-main record is greater than or equal to the updating number of the main keys, starting to obtain the main keys with the updating number from the minimum value of the main keys; if the current non-main record has the residual main keys, updating the minimum value of the main keys of the current non-main record; and if the current non-main record has no residual main keys, updating the current non-main record into an invalid record.

Optionally, the obtaining device of the primary key further includes:

and the generating module is used for generating a non-main record according to the related information of the remaining main keys when the mobile terminal is restarted or is on line so as to store the remaining main keys in the database.

The implementation method can automatically calculate and update (self-adaptively) the number of the updated main keys in each batch, can reduce the risk of online accidents, and can solve the problem that the existing acquisition mode needs to manually and frequently modify the updated number of the main keys according to the use conditions of the main keys.

Fig. 8 shows an exemplary system architecture 800 of a primary key acquisition method or a primary key acquisition apparatus to which embodiments of the present invention may be applied.

As shown in fig. 8, the system architecture 800 may include terminal devices 801, 802, 803, a network 804, and a server 805. The network 804 serves to provide a medium for communication links between the terminal devices 801, 802, 803 and the server 805. Network 804 may include various types of connections, such as wire, wireless communication links, or fiber optic cables, to name a few.

A user may use the terminal devices 801, 802, 803 to interact with a server 805 over a network 804 to receive or send messages or the like. Various communication client applications may be installed on the terminal devices 801, 802, 803. The terminal devices 801, 802, 803 include, but are not limited to, smart phones, tablet computers, laptop portable computers, desktop computers, and the like.

It should be noted that the method for acquiring the primary key provided by the embodiment of the present invention is generally executed by the server 805, and accordingly, the acquiring device of the primary key is generally disposed in the server 805.

It should be understood that the number of terminal devices, networks, and servers in fig. 8 is merely illustrative. There may be any number of terminal devices, networks, and servers, as desired for implementation.

Referring now to FIG. 9, shown is a block diagram of a computer system 900 suitable for use with a terminal device implementing an embodiment of the present invention. The terminal device shown in fig. 9 is only an example, and should not bring any limitation to the functions and the scope of use of the embodiments of the present invention.

As shown in fig. 9, the computer system 900 includes a Central Processing Unit (CPU)901 that can perform various appropriate actions and processes in accordance with a program stored in a Read Only Memory (ROM)902 or a program loaded from a storage section 908 into a Random Access Memory (RAM) 903. In the RAM 903, various programs and data necessary for the operation of the system 900 are also stored. The CPU 901, ROM 902, and RAM 903 are connected to each other via a bus 904. An input/output (I/O) interface 905 is also connected to bus 904.

The following components are connected to the I/O interface 905: an input portion 906 including a keyboard, a mouse, and the like; an output section 907 including components such as a Cathode Ray Tube (CRT), a Liquid Crystal Display (LCD), and the like, and a speaker; a storage portion 908 including a hard disk and the like; and a communication section 909 including a network interface card such as a LAN card, a modem, or the like. The communication section 909 performs communication processing via a network such as the internet. The drive 910 is also connected to the I/O interface 905 as necessary. A removable medium 911 such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, or the like is mounted on the drive 910 as necessary, so that a computer program read out therefrom is mounted into the storage section 908 as necessary.

In particular, according to the embodiments of the present disclosure, the processes described above with reference to the flowcharts may be implemented as computer software programs. For example, embodiments of the present disclosure include a computer program product comprising a computer program embodied on a computer readable medium, the computer program comprising program code for performing the method illustrated in the flow chart. In such an embodiment, the computer program may be downloaded and installed from a network through the communication section 909, and/or installed from the removable medium 911. The above-described functions defined in the system of the present invention are executed when the computer program is executed by a Central Processing Unit (CPU) 901.

It should be noted that the computer readable medium shown in the present invention can be a computer readable signal medium or a computer readable storage medium or any combination of the two. A computer readable storage medium may be, for example, but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination of the foregoing. More specific examples of the computer readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer diskette, a hard disk, a Random Access Memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing. In the present invention, a computer readable storage medium may be any tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device. In the present invention, however, a computer readable signal medium may include a propagated data signal with computer readable program code embodied therein, for example, in baseband or as part of a carrier wave. Such a propagated data signal may take many forms, including, but not limited to, electro-magnetic, optical, or any suitable combination thereof. A computer readable signal medium may also be any computer readable medium that is not a computer readable storage medium and that can communicate, propagate, or transport a program for use by or in connection with an instruction execution system, apparatus, or device. Program code embodied on a computer readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, fiber optic cable, RF, etc., or any suitable combination of the foregoing.

The flowchart and block diagrams in the figures illustrate the architecture, functionality, and operation of possible implementations of systems, methods and computer program products according to various embodiments of the present invention. In this regard, each block in the flowchart or block diagrams may represent a module, segment, or portion of code, which comprises one or more executable instructions for implementing the specified logical function(s). It should also be noted that, in some alternative implementations, the functions noted in the block may occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently, or the blocks may sometimes be executed in the reverse order, depending upon the functionality involved. It will also be noted that each block of the block diagrams or flowchart illustration, and combinations of blocks in the block diagrams or flowchart illustration, can be implemented by special purpose hardware-based systems which perform the specified functions or acts, or combinations of special purpose hardware and computer instructions.

As another aspect, the present invention also provides a computer-readable medium that may be contained in the apparatus described in the above embodiments; or may be separate and not incorporated into the device. The computer readable medium carries one or more programs which, when executed by a device, cause the device to comprise: if the list allowance in the current available primary key list reaches an update threshold, determining the update quantity of the primary keys in the current batch, wherein the update quantity is determined according to the primary key update data of the historical batch; acquiring a primary key use record of a current available primary key list; and if the primary key use record of the current available primary key list is not empty, acquiring the updated number of primary keys according to the primary key use record of the current available primary key list, and updating the primary key use record.

According to the technical scheme of the embodiment of the invention, the number of the updated main keys in each batch can be automatically calculated and updated (self-adaptive), the risk of online accidents can be reduced, the problem that the updating number of the main keys needs to be manually and frequently modified according to the use condition of the main keys in the conventional acquisition mode can be solved, and the problem of large waste of main key resources caused by restarting and online can be solved.

The above-described embodiments should not be construed as limiting the scope of the invention. Those skilled in the art will appreciate that various modifications, combinations, sub-combinations, and substitutions can occur, depending on design requirements and other factors. Any modification, equivalent replacement, and improvement made within the spirit and principle of the present invention should be included in the protection scope of the present invention.

22页详细技术资料下载
上一篇:一种医用注射器针头装配设备
下一篇:数据更新方法、装置、服务端、存储介质及程序产品

网友询问留言

已有0条留言

还没有人留言评论。精彩留言会获得点赞!

精彩留言,会给你点赞!