Method for realizing concurrent reading and writing of multi-index memory data
1. A method for realizing concurrent reading and writing of multi-index memory data is characterized by comprising the following steps:
s1, dividing a data structure into an index area and a data area;
s2, writing data by adopting a linux read-write lock API and a global write lock;
s3, reading data, and starting not adding a global lock;
s4, setting an index function, calculating a value as an index value according to the data content, wherein the index value is the key of the index table;
and S5, after the data are read through the index, checking the data to ensure that the data are matched with the index value.
2. The method for implementing concurrent read/write of multi-index memory data as claimed in claim 1, wherein in S1, the index area comprises a plurality of indexes, each index is implemented based on a hash table, key of the hash table is an index field value, and value is a value.
3. The method for implementing concurrent reading and writing of multi-index memory data according to claim 1, wherein: in S5: and calculating a key value by using the index function set in the S4, comparing the key value with the index value transmitted during data query, if the key value is the same as the index value, indicating that the data is correct, otherwise, circularly reading for 3 times, if the key value is incorrect, adding a global read lock, and reading again.
Background
The multi-index memory data means that a plurality of indexes can be established for the same data, and the data can be accessed through any index. The map is a one-to-one special case, and the safety and the correctness in a concurrent scene need to be ensured when the map is read and written; at present, a problem of concurrent control exists in many-to-one data structures, and most of the concurrent control is to add a global lock to control concurrent reading and writing, so that the concurrent reading and writing performance is sharply reduced.
Disclosure of Invention
The technical problem to be solved by the invention is to provide a method for realizing concurrent reading and writing of multi-index memory data, eliminate global locks as much as possible and improve concurrent reading and writing performance on the premise of ensuring the concurrent security.
The method for realizing the concurrent reading and writing of the multi-index memory data is realized by the following technical scheme: the method specifically comprises the following steps:
s1, dividing a data structure into an index area and a data area;
s2, writing data by adopting a linux read-write lock API and a global write lock;
s3, reading data, and starting not adding a global lock;
s4, setting an index function, calculating a value as an index value according to the data content, wherein the index value is the key of the index table;
s5, after the data are read through the index, a check is carried out on the data to ensure that the data are matched with the index value;
as a preferred technical solution, in S1, the index area includes a plurality of indexes, each index is implemented based on a hash table, key of the hash table is an index field value, and value is a numerical value.
As a preferred technical solution, in S5, the key value is calculated by using the index function set in S4, and then compared with the index value transmitted when the data is queried, if the key value is the same as the index value, it indicates that the data is correct, otherwise, the data needs to be read circularly 3 times, if the key value is incorrect, a global read lock is added, and then the key value is read again.
The invention has the beneficial effects that: the invention can eliminate the global lock as much as possible and improve the concurrent read-write performance on the premise of ensuring the concurrent security.
Drawings
In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings used in the description of the embodiments or the prior art will be briefly described below, it is obvious that the drawings in the following description are only some embodiments of the present invention, and for those skilled in the art, other drawings can be obtained according to the drawings without creative efforts.
Fig. 1 is an overall framework diagram of the present invention.
Detailed Description
All of the features disclosed in this specification, or all of the steps in any method or process so disclosed, may be combined in any combination, except combinations of features and/or steps that are mutually exclusive.
Any feature disclosed in this specification (including any accompanying claims, abstract and drawings), may be replaced by alternative features serving equivalent or similar purposes, unless expressly stated otherwise. That is, unless expressly stated otherwise, each feature is only an example of a generic series of equivalent or similar features.
In the description of the present invention, it is to be understood that the terms "one end", "the other end", "outside", "upper", "inside", "horizontal", "coaxial", "central", "end", "length", "outer end", and the like, indicate orientations or positional relationships based on those shown in the drawings, and are used only for convenience in describing the present invention and for simplicity in description, and do not indicate or imply that the device or element being referred to must have a particular orientation, be constructed in a particular orientation, and be operated, and thus, should not be construed as limiting the present invention.
Further, in the description of the present invention, "a plurality" means at least two, e.g., two, three, etc., unless specifically limited otherwise.
The use of terms such as "upper," "above," "lower," "below," and the like in describing relative spatial positions herein is for the purpose of facilitating description to describe one element or feature's relationship to another element or feature as illustrated in the figures. The spatially relative positional terms may be intended to encompass different orientations of the device in use or operation in addition to the orientation depicted in the figures. For example, if the device in the figures is turned over, elements described as "below" or "beneath" other elements or features would then be oriented "above" the other elements or features. Thus, the exemplary term "below" can encompass both an orientation of above and below. The device may be otherwise oriented and the spatially relative descriptors used herein interpreted accordingly.
In the present invention, unless otherwise explicitly specified or limited, the terms "disposed," "sleeved," "connected," "penetrating," "plugged," and the like are to be construed broadly, e.g., as a fixed connection, a detachable connection, or an integral part; can be mechanically or electrically connected; they may be directly connected or indirectly connected through intervening media, or they may be connected internally or in any other suitable relationship, unless expressly stated otherwise. The specific meanings of the above terms in the present invention can be understood by those skilled in the art according to specific situations.
As shown in fig. 1, a method for implementing concurrent reading and writing of multi-index memory data of the present invention specifically includes the following steps:
s1, dividing a data structure into an index area and a data area;
s2, writing data by adopting a linux read-write lock API and a global write lock; the concurrent writing is limited, and the safety of data structures such as indexes is ensured.
S3, reading data, and starting not adding a global lock; where read data is concurrency enabled and not blocked by writes.
S4, setting an index function, calculating a value as an index value according to the data content, wherein the index value is the key of the index table; taking C + + language as an example (the method is not limited to C + +), its index function prototype:
typedef int(*IndexKeyCallFunc)(const void*value,int value_len,char*key,int&key_len)
wherein value, value _ len is input parameter, representing data, key, key _ len is output parameter, representing key content after calculating;
for example, the data is a structure:
an index function is specified, and an id field is used as a main key index:
this indicates that the index value can be calculated by the data content in reverse; each index corresponds to an index function.
S5, after the data are read through the index, a check is carried out on the data to ensure that the data are matched with the index value;
in this embodiment, in S1, the index area includes a plurality of indexes, each index is implemented based on a hash table, a key of the hash table is an index field value, and a value is a numerical value; the value may be used to address the data field, being the number of data in the data field; the data area holds actual data.
In this embodiment, in S5, the key value is calculated by using the index function set in S4, and compared with the index value transmitted when the data is queried, if the key value is the same as the index value, it indicates that the data is correct, otherwise, the data needs to be read circularly 3 times, if the key value is incorrect, a global read lock is added at this time, and the key value is read again; at this time, a correct result must be obtained; under high concurrency, the check with extremely low probability does not pass, so that the probability of adding the global lock can be greatly reduced, and the concurrent read-write performance is greatly improved.
The above description is only an embodiment of the present invention, but the scope of the present invention is not limited thereto, and any changes or substitutions that are not thought of through the inventive work should be included in the scope of the present invention. Therefore, the protection scope of the present invention shall be subject to the protection scope defined by the claims.
- 上一篇:石墨接头机器人自动装卡簧、装栓机
- 下一篇:多表分页查询方法、装置、设备及存储介质