Detailed Description
In order to make the objects, technical solutions and advantages of the embodiments of the present invention more apparent, the embodiments of the present invention will be described in detail with reference to the accompanying drawings. However, it will be appreciated by those of ordinary skill in the art that in various embodiments of the invention, numerous technical details are set forth in order to provide a better understanding of the present application. However, the technical solution claimed in the present application can be implemented without these technical details and various changes and modifications based on the following embodiments. The following embodiments are divided for convenience of description, and should not constitute any limitation to the specific implementation manner of the present invention, and the embodiments may be mutually incorporated and referred to without contradiction.
A first embodiment of the present invention relates to a data synchronization method, which is applied to a server in a data synchronization system, where the data synchronization system further includes a source database and a target database respectively connected to the server, and a specific process is shown in fig. 1, and includes:
and step 101, deducing local service data according to the current updating operation.
Specifically, in this embodiment, the number of the servers in the data synchronization system is one, and the update operation is stored in a local cache area of the server, where the update operation at least includes one of the following operations or any combination thereof: deleting operation, adding operation and modifying operation, wherein the updating operation at least comprises data type, data content and version number, the version number of the updating operation recorded in the same comparison process is the same, and the version number of the new updating operation recorded in the comparison process is larger than the version number of the new updating operation recorded in the comparison process at the last time. The server may deduce local service data through the current update operation in the cache, for example: the current update operations include: the new [ ID ] [ a ], [ name ] [ A ], [ age ] [16], version [1], new [ ID ] [ B ], [ name ] [ B ], [ age ] [17], version [1], new [ ID ] [ C ], [ name ] [ C ], [ age ] [18], version [1], delete [ ID ] [ a ], version [2], modified [ ID ] [ C ], [ age ] [19], version [2], new [ ID ] [ D ], [ name ] [ D ], [ age ] [19], version [2], is deduced according to the current update operation, and [ ID ] is [ a ] which comprises two update operations: adding [ ID ] [ a ], [ name ] [ A ], [ age ] [16], version [1], deleting [ ID ] [ a ], and version [2], and knowing that the data content is deleted; [ ID ] is [ b ] includes an update operation: newly adding [ ID ] [ B ], [ name ] [ B ], [ age ] [17] and version [1], thus obtaining a piece of local data content [ ID ] [ B ], [ name ] [ B ], [ age ] [17 ]; [ ID ] is [ c ] includes two update operations: newly adding [ ID ] [ C ], [ name ] [ C ], [ age ] [18], version [1], modifying [ ID ] [ C ], [ age ] [19] and version [2], thus obtaining a piece of local data content [ ID ] [ C ], [ name ] [ C ], [ age ] [19 ]; [ ID ] is [ c ] includes an update operation: adding [ ID ] [ D ], [ name ] [ D ], [ age ] [19] and version [1], obtaining a piece of local data content [ ID ] [ D ], [ name ] [ D ], [ age ] [19], and deducing local service data, wherein the step of adding [ ID ] [ D ], [ name ] [ D ], [ age ] [19], comprises the following steps: [ ID ] [ B ], [ name ] [ B ], [ age ] [17], [ ID ] [ C ], [ name ] [ C ], [ age ] [19], [ ID ] [ D ], [ name ] [ D ], [ age ] [19 ]. In an example, the server may also update the local service data in real time according to the new update operation when recording the new update operation, and store the local service data, so that the local service data can be directly used when needed. In one example, the update operation includes one or any combination of the following in addition to the data type, data content and version number: a hash of the time and bytes is generated. In one example, the update operation is stored in memory at the server side, which may enable caching and cache retrieval as compared to storing the update operation to disk. In one example, the update operations for different forms may be stored in the same cache or may be stored in different caches.
And 102, comparing the local service data with the service data of the source database, and recording new updating operation according to the comparison result.
Specifically, after the local service data is derived, the server may compare the local service data with the service data of the source database in real time, or compare the local service data with the service data of the source database according to a preset period, where the preset period may be set according to actual needs, and this embodiment is not limited specifically. In the process of data comparison, the server records the updating operation in the process of data comparison to obtain a new updating operation.
In one example, the new update operation includes a delete operation, comparing the local service data with the service data in the source database, and recording the new update operation according to the comparison result, including: traversing the data content in the local service data, searching the currently traversed data content in the service data of the source database, and recording the deletion operation of deleting the currently traversed data content if the currently traversed data content is not searched.
In one example, if the data content in the service data has the primary key, in searching the currently traversed data content, the primary key in the currently traversed data content is searched, and if the primary key in the currently traversed data content is not searched, it indicates that the currently traversed data content is not searched. Specifically, the primary key is data content for identifying an Identity, i.e., the primary key is an Identity card (ID), and has uniqueness. As will be described in more detail below with reference to an example, the current update operation includes: adding [ ID ] [ a ], [ name ] [ A ], [ age ] [16], version [1], adding [ ID ] [ B ], [ name ] [ B ], [ age ] [17], version [1], adding [ ID ] [ C ], [ name ] [ C ], [ age ] [18] and version [1], deducing local service data as shown in table one, wherein table two is the service data of the source database; traversing data contents in local service data, acquiring a main key 'a' when traversing the data contents 'a, A and 16', searching 'a' in the service data of a source database, and recording deletion operations of 'deleting [ ID ] [ a ], version [2 ]' when 'a' which is not searched indicates that the currently traversed data contents are not searched; when the data content 'B, B, 17' is traversed, the main key 'B' is obtained, the 'B' is searched in the service data of the source database, and the data is ignored when the 'B' is searched at the moment; when traversing to the data content 'C, 18', acquiring the primary key 'C', searching 'C' in the service data of the source database, if the data is ignored when the primary key 'C' is found, acquiring a new updating operation as follows: "delete [ ID ] [ a ], version [2 ]".
ID
|
Name (I)
|
Age (age)
|
a
|
A
|
16
|
b
|
B
|
17
|
c
|
C
|
18 |
Table one: local service data
ID
|
Name (I)
|
Age (age)
|
b
|
B
|
17
|
c
|
C
|
19 |
Table two: service data of source database
In one example, in the searching of the currently traversed data content, all data contents in the currently traversed data content are searched, and if all data contents in the currently traversed data content are not searched, it indicates that the currently traversed data content is not searched. Specifically, as will be described in more detail below with reference to an example, the current update operation includes, in a second example: the name [ A ], [ age ] [16], the version [1], [ name ] [ B ], [ age ] [17], the version [1], [ name ] [ C ], [ age ] [18], and the version [1], then local business data is deduced as shown in the third table, and the fourth table is the business data of the source database; traversing each data content in the local service data, searching for the data content A, 16 in the source database when traversing the data content A, 16, not searching for the data content A, 16 at the moment, and recording deletion operations of deleting the name, the age and the version 2; when the data content 'B, 17' is traversed, searching 'B, 17' in the source database, and if the 'B, 17' is searched, ignoring the data; when traversing to the data content "C, 18", searching for "C, 18" in the source database, at this time, not searching for "C, 18", recording the deletion operation of "delete [ name ] [ C ], [ age ] [18], version [2 ]", and then obtaining a new update operation as: "delete [ name ] [ A ], [ age ] [16], version [2 ]" and "delete [ name ] [ C ], [ age ] [18], version [2 ]".
Name (I)
|
Age (age)
|
A
|
16
|
B
|
17
|
C
|
18 |
Table three: local service data
Name (I)
|
Age (age)
|
B
|
17
|
C
|
19 |
Table four: service data of source database
In one example, the new update operation comprises an add operation; comparing the local service data with the service data in the source database, and recording new updating operation according to the comparison result, wherein the updating operation comprises the following steps: traversing the data content in the service data of the source database, searching the currently traversed data content in the local service data, and recording the newly-added operation of newly-added currently traversed data content if the currently traversed data content is not searched.
In one example, if the data content in the service data has the primary key, in searching the currently traversed data content, the primary key in the currently traversed data content is searched, and if the primary key in the currently traversed data content is not searched, it indicates that the currently traversed data content is not searched. Specifically, as will be described in detail below with reference to an example, in a third example, the current update operation includes: newly adding [ ID ] [ a ], [ name ] [ A ], [ age ] [16], version [1], newly adding [ ID ] [ B ], [ name ] [ B ], [ age ] [17], version [1], newly adding [ ID ] [ C ], [ name ] [ C ], [ age ] [18], version [1], deducing local service data as shown in table one, wherein table five is service data of a source database, at the moment, the service data of the source database does not have a version control column, traversing all data contents in the service data of the source database, acquiring a main key "B" when traversing to the data contents "B, B and 17", searching "B" in the local service data, and ignoring the data when searching "B" at the moment; when data content 'C, C, 19' is traversed, a main key 'C' is obtained, the 'C' is searched in local service data, and the data is ignored when the 'C' is searched; when traversing the data content 'D, D, 19', acquiring the primary key 'D', searching the local service data for 'D', not searching the local service data for 'D', recording the new operations of 'newly adding [ ID ] [ D ], [ name ] [ D ], [ age ] [19], and version [2],' and then acquiring new updating operations as follows: "newly added [ ID ] [ D ], [ name ] [ D ], [ age ] [19], version [2 ]".
ID
|
Name (I)
|
Age (age)
|
b
|
B
|
17
|
c
|
C
|
19
|
d
|
D
|
19 |
Table five: service data of source database
In one example, in the searching of the currently traversed data content, all data contents in the currently traversed data content are searched, and if all data contents in the currently traversed data content are not searched, it indicates that the currently traversed data content is not searched. Specifically, as will be described in detail below with reference to an example, in a fourth example, the current update operation includes: the name [ A ], [ age ] [16], the version [1], [ name ] [ B ], [ age ] [17], the version [1], [ name ] [ C ], [ age ] [18], and the version [1], then local business data is deduced as shown in the third table, and the sixth table is the business data of the source database; when the service data of the source database does not have the version control column, traversing all data contents in the service data of the source database, and when the data content 'B, 17' is traversed, finding the 'B, 17' at the moment, ignoring the data; when traversing to the data content "C, 19", the new operation of "new [ name ] [ C ], [ age ] [19], version [2] is not found at this time, the new operation of" new [ name ] [ D ], [ age ] [19], version [2] is recorded when traversing to the data content "D, 19", and then the new update operation is obtained: "New [ name ] [ C ], [ age ] [19], version [2 ]" and "New [ name ] [ D ], [ age ] [19], version [2 ]".
Name (I)
|
Age (age)
|
B
|
17
|
C
|
19
|
D
|
19 |
Table six: service data of source database
In one example, if the data content in the service data respectively has a primary key, the new update operation further includes a modification operation; comparing the local service data with the service data in the source database, and recording new updating operation according to the comparison result, wherein the updating operation comprises the following steps: traversing the data content in the local service data, searching the main key in the currently traversed data content in the service data of the source database, and if the main key in the currently traversed data content is found and the data content in the service data of the source database, which has the same main key as the currently traversed data content, is not completely consistent with the currently traversed data content, recording and modifying operation according to the data content in the service data of the source database, which has the same main key as the currently traversed data content.
Specifically, as will be described in detail below with reference to an example, in the fifth example, if [ ID ] [ a ], [ name ] [ a ], [ age ] [16], version [1], new [ ID ] [ B ], [ name ] [ B ], [ age ] [17], version [1], new [ ID ] [ C ], [ name ] [ C ], [ age ] [18], and version [1] are added, local service data is derived as shown in table one, and table two is service data of the source database; traversing data contents in local service data, searching for 'a' in the service data of the source database when traversing the data contents 'a, A and 16', acquiring a main key 'a', and ignoring the data when not searching for 'a'; when the data content 'B, B, 17' is traversed, searching 'B' in the service data of the source database, acquiring a primary key 'B', searching 'B' at the moment, wherein the data content with the same primary key 'B' in the service data of the source database is 'B, B, 17', which is completely consistent with the currently traversed data content, and ignoring the data; when traversing the data content 'C, 18', acquiring the primary key 'C', searching the 'C' in the service data of the source database, searching the 'C', wherein the data content with the same primary key 'C' in the service data of the source database is 'C, 19', which is not completely consistent with the currently traversed data content, and then modifying the [ ID ] [ C ] name [ C ] age [19] and the version [2] according to the record of 'C, 19', and obtaining a new updating operation as follows: "Modify [ ID ] [ C ] name [ C ] age [19], version [2 ]". In one example, if only the modified data content is included in the modification operation, then the modification operation of "modify [ ID ] [ c ] age [19], version [2] is recorded.
In one example, if the data content in the service data respectively has a primary key, the new update operation further includes a modification operation; comparing the local service data with the service data in the source database, and recording new updating operation according to the comparison result, wherein the updating operation comprises the following steps: and traversing the data content in the service data of the source database, searching the main key in the currently traversed data content in the data content of the local service data, and recording modification operation according to the currently traversed data content if the main key in the currently traversed data content is found and the data content of the local service data, which has the same main key as the currently traversed data content, is not completely consistent with the currently traversed data content.
Specifically, as will be described in detail below with reference to an example, in the sixth example, if [ ID ] [ a ], [ name ] [ a ], [ age ] [16], version [1], new [ ID ] [ B ], [ name ] [ B ], [ age ] [17], version [1], new [ ID ] [ C ], [ name ] [ C ], [ age ] [18], and version [1] are added, local service data is derived as shown in table one, and table five is service data of the source database; traversing data content in service data of a source database, acquiring a main key 'B' when traversing the data content 'B, B, 17', searching 'B' in local service data, searching 'B' at the moment, wherein the data content with the same main key 'B' in the local service data is 'B, B, 17', which is completely consistent with the currently traversed data content, and ignoring the data; when traversing the data content ' C, 19 ', acquiring the primary key ' C ', searching ' C ' in the service data of the source database, searching ' C ' at the moment, wherein the data content with the same primary key ' C ' in the service data of the source database is ' C, 18 ', which is not completely consistent with the currently traversed data content, acquiring the primary key ' D ' according to the ' C, 19 ' record ' modification of the [ ID ] [ C ] name [ C ] age [19], version [2], when traversing the data content ' D, D, 19 ', acquiring the primary key ' D ', not searching ' D ' in the local service data, not searching ' D ' at the moment, and ignoring the data to obtain a new updating operation as follows: "Modify [ ID ] [ C ] name [ C ] age [19], version [2 ]". In one example, if only the modified data content is included in the modification operation, then the modification operation of "modify [ ID ] [ c ] age [19], version [2] is recorded.
In one example, if the service data of the source database has a version control column, the data content in the service data of the source database is determined by the following method: inquiring the latest version number of the current updating operation to obtain a first latest version number; and searching the data content corresponding to the version number greater than the first latest version number from the version control column of the service data of the source database to obtain a first search result, and taking the first search result as the data content in the service data of the source database.
Specifically, the service data of the source database records a version number of the data content through a version control column, where table seven is the service data of the source database, a fourth column in table seven is a version control column, the version control column is not the data content of the service data and is only used to identify the version number of the data content of the service data, when the data content of the source database is subjected to a new addition and modification operation, the version number of the data content subjected to the new addition and modification operation changes, and the version numbers of the service data subjected to the new addition and modification operation in the same update process are the same, and the version number of the update operation recorded in the update process is greater than the version number of the update operation recorded in the previous update process, for example: the version number corresponding to "B, 17" in the service data of the source database is 1, and the version number corresponding to "C, 18" is 1; in the first updating process, "18" in "C" is modified to "19", and for the data content subjected to the modifying operation, the version number corresponding to "C, 19" is 2, and "D, 19" is newly added, and for the data content subjected to the adding operation, the version number corresponding to "D, 19" is 2, as shown in table eight, it is the service data of the source database after the first updating, in the second updating process, "19" in "B" is modified to "20", as shown in table nine, the version number corresponding to "B, 20" is 3, as shown in table nine, it is the service data of the source database after the second updating. In the process of data comparison, the latest version number of the current updating operation is inquired, and if the current updating operation has: adding 'a, A, 16', the version number is 1; newly adding 'B, B, 17', wherein the version number is 1; and if the service data of the source database is shown as table eight, searching the data content corresponding to the version number with the version number greater than 1 from the version control column of the service data of the source database according to the table eight to obtain search results of "C, 19" and "D, 19", and traversing "C, 19" and "D, 19". The server compares the local service data with the service data of the source database, and records a new update operation according to the comparison result, where the version number of the new update operation record is consistent with the version number of the version control column, for example: when data content ' D, D, 19 ' is traversed, a primary key ' D ' is obtained, the ' D ' is searched in local service data, the ' D ' is not searched at the moment, and new operations of ' newly adding [ ID ] [ D ], [ name ] [ D ], [ age ] [19], [ version number ] [2] are recorded; the setting rule of the version number may be set according to actual needs, and may be represented by an arabic number or a letter, which is not specifically limited in this embodiment. By the method, if the service data of the source database has the version control column, which data content in the source database is updated can be known according to the version control column, the data content corresponding to the version number greater than the first latest version number is searched from the version control column of the service data in the source database, and at this time, the data content in the service data of the source database, i.e., the data content of the service data which is updated, only needs to be searched in a traversal manner, so that the local workload can be reduced, and after the new adding operation is recorded, only the recorded new adding operation needs to be sent to the target database, so that the network transmission quantity between the server and the target database is reduced, and the synchronization speed is also improved.
ID
|
Name (I)
|
Age (age)
|
Version control
|
a
|
A
|
16
|
1
|
b
|
B
|
17
|
1
|
c
|
C
|
18
|
1 |
TABLE VII: service data of source database
ID
|
Name (I)
|
Age (age)
|
Version control
|
b
|
B
|
17
|
1
|
c
|
C
|
19
|
2
|
d
|
D
|
19
|
2 |
Table eight: service data of source database after first updating
ID
|
Name (I)
|
Age (age)
|
Version control
|
b
|
B
|
20
|
3
|
c
|
C
|
19
|
2
|
d
|
D
|
19
|
2 |
Table nine: the service data of the source database after the second update
In one example, if the data content in the service data respectively has a primary key, traversing the data content in the local service data, searching the primary key in the currently traversed data content in the service data of the source database, if the primary key in the currently traversed data content is not found, indicating that the currently traversed data content is not found, recording a deletion operation for deleting the currently traversed data content, and if the primary key in the currently traversed data content is found and the data content in the service data of the source database, which has the same primary key as the currently traversed data content, is not completely consistent with the currently traversed data content, recording and modifying the traversal operation according to the data content in the service data of the source database, which has the same primary key as the currently traversed data content; and then, local service data is deduced according to the current updating operation, the data content in the service data of the source database is traversed, the main key in the currently traversed data content is searched in the local service data, and if the main key in the currently traversed data content is not searched, the newly added operation of newly adding the currently traversed data content is recorded.
Specifically, as will be described in detail below with reference to an example, in a seventh example, the current update operation includes: adding [ ID ] [ a ], [ name ] [ A ], [ age ] [16], version [1], adding [ ID ] [ B ], [ name ] [ B ], [ age ] [17], version [1], adding [ ID ] [ C ], [ name ] [ C ], [ age ] [18] and version [1], deducing local service data as shown in table one, wherein table five is the service data of the source database; traversing data contents in local service data, acquiring a main key 'a' when traversing the data contents 'a, A and 16', searching 'a' in the service data of a source database, and recording deletion operations of 'deleting [ ID ] [ a ], version [2 ]' when 'a' which is not searched indicates that the currently traversed data contents are not searched; when the data content 'B, B, 17' is traversed, acquiring the primary key 'B', searching 'B' in the service data of the source database, searching 'B' at the moment, wherein the data content with the same primary key 'B' in the service data of the source database is 'B, B, 17', and the data is ignored if the data content is completely consistent with the data content traversed currently; when traversing to the data content "C, 18", acquiring the primary key "C", searching "C" in the service data of the source database, and at this time, searching "C", wherein the data content having the same primary key "C" in the service data of the source database is "C, 19", which is not completely consistent with the currently traversed data content, and then modifying the modification operations of [ ID ] [ C ] age [19] and version [2] according to the record of "C, 19", and then the new updating operation includes: delete [ ID ] [ a ], version [2], modify [ ID ] [ C ] name [ C ] age [19], version [2], so the current update operation: adding [ ID ] [ a ], [ name ] [ A ], [ age ] [16], version [1], adding [ ID ] [ B ], [ name ] [ B ], [ age ] [17], version [1], adding [ ID ] [ C ], [ name ] [ C ], [ age ] [18], version [1], deleting [ ID ] [ a ], version [2], modifying [ ID ] [ C ] age [19], version [2], deducing local service data as shown in a table, traversing data content in the service data of a source database, acquiring a primary key "B" when traversing the data content "B, B and 17", searching "B" in the local service data, and ignoring the data when searching "B" at the moment; when data content 'C, C, 19' is traversed, a main key 'C' is obtained, the 'C' is searched in local service data, and the data is ignored when the 'C' is searched; when traversing the data content "D, 19", acquiring the primary key "D", searching "D" in the local service data, not searching "D" at this time, recording the new adding operation of "newly adding [ ID ] [ D ], [ name ] [ D ], [ age ] [19], version [2 ]", and then all the new updating operations include: delete [ ID ] [ a ], version [2], modify [ ID ] [ c ] age [19], version [2], add [ ID ] [ D ], [ name ] [ D ], [ age ] [19], version [2 ]. By the method, due to the judgment of the deletion operation and the modification operation which are carried out firstly, the data content of the local service data is reduced when the deletion operation exists, so that the comparison of the data content can be reduced when the judgment of the new addition operation is carried out subsequently, and the synchronization speed is further improved.
ID
|
Name (I)
|
Age (age)
|
b
|
B
|
17
|
c
|
C
|
19 |
TABLE Ten: local service data
In one example, if the data content in the service data has a primary key, traversing the data content in the local service data, searching the primary key in the currently traversed data content in the service data of the source database, if the primary key in the currently traversed data content is not found, indicating that the currently traversed data content is not found, recording a deletion operation for deleting the currently traversed data content; and then, local service data is deduced according to the current updating operation, data content in the service data of the source database is traversed, a primary key in the currently traversed data content is searched in the local service data, if the primary key in the currently traversed data content is not searched, the newly-added operation of newly-added currently-traversed data content is recorded, and if the primary key in the currently-traversed data content is searched and the data content in the local service data, which has the same primary key as the currently-traversed data content, is not completely consistent with the currently-traversed data content, the modification operation is recorded according to the currently-traversed data content.
In one example, if the data content in the service data respectively has a primary key, traversing the data content in the service data of the source database, searching the primary key in the currently traversed data content in the local service data, if the primary key in the currently traversed data content is not found, indicating that the currently traversed data content is not found, and recording a new adding operation for adding the currently traversed data content; and then, pushing local service data according to the current updating operation, traversing the data content in the local service data, searching the main key in the currently traversed data content in the service data of the source database, recording deletion operation for deleting the currently traversed data content if the main key in the currently traversed data content is not searched, and recording modification operation according to the data content in the service data of the source database, which has the same main key as the currently traversed data content, if the main key in the currently traversed data content is searched and the data content in the service data of the source database, which has the same main key as the currently traversed data content, is not completely consistent with the currently traversed data content.
Specifically, as will be described in detail below with reference to an example, in an eighth example, the current update operation includes: adding [ ID ] [ a ], [ name ] [ A ], [ age ] [16], version [1], adding [ ID ] [ B ], [ name ] [ B ], [ age ] [17], version [1], adding [ ID ] [ C ], [ name ] [ C ], [ age ] [18] and version [1], deducing local service data as shown in table one, wherein table five is the service data of the source database; firstly, traversing data content in service data of a source database, acquiring a main key 'B' when traversing the data content 'B, B, 17', searching 'B' in local service data, and ignoring the data when searching 'B' at the moment; when data content 'C, C, 19' is traversed, a main key 'C' is obtained, the 'C' is searched in local service data, and the data is ignored when the 'C' is searched; when traversing the data content 'D, D, 19', acquiring the primary key 'D', searching the local service data for 'D', not searching the local service data for 'D', recording the new operations of 'newly adding [ ID ] [ D ], [ name ] [ D ], [ age ] [19], and version [2],' and then acquiring new updating operations as follows: "newly added [ ID ] [ D ], [ name ] [ D ], [ age ] [19], version [2 ]"; and traversing the data content in the local service data, so that the current updating operation: adding [ ID ] [ a ], [ name ] [ A ], [ age ] [16], version [1], adding [ ID ] [ B ], [ name ] [ B ], [ age ] [17], version [1], adding [ ID ] [ C ], [ name ] [ C ], [ age ] [18], version [1], adding [ ID ] [ D ], [ name ] [ D ], [ age ] [19], version [2], deducing local service data as shown in a table eleven, traversing data contents in the local service data, acquiring a primary key "a" when traversing data contents "a, A and 16", searching "a" in service data of a source database, wherein the condition that the "a" is not searched at the moment indicates that the currently traversed data content is not searched, and recording deletion operation of "deleting [ ID ] [ a ], version [2 ]; when the data content 'B, B, 17' is traversed, acquiring the primary key 'B', searching 'B' in the service data of the source database, searching 'B' at the moment, wherein the data content with the same primary key 'B' in the service data of the source database is 'B, B, 17', and the data is ignored if the data content is completely consistent with the data content traversed currently; when the data content 'C, C, 18' is traversed, the primary key 'C' is obtained, the 'C' is searched in the service data of the source database, the data content with the same primary key 'C' in the service data of the source database is 'C, C, 19', which is not completely consistent with the data content traversed currently, then according to the 'C, C, 19' record 'modify [ ID ] [ C ] age [19], version [2 ]' modification operation, when the data content 'D, D, 19' is traversed, the primary key 'D' is obtained, searching for 'D' in the service data of the source database, searching for 'D' at the moment, wherein the data content with the same primary key 'D' in the service data of the source database is 'D, D, 19', if the data content is completely consistent with the data content traversed currently, the data is ignored, and a new update operation is obtained as follows: delete [ ID ] [ a ], version [2], modify [ ID ] [ c ] age [19], version [2], then all new update operations include: delete [ ID ] [ a ], version [2], modify [ ID ] [ c ] age [19], version [2], add [ ID ] [ D ], [ name ] [ D ], [ age ] [19], version [2 ].
ID
|
Name (I)
|
Age (age)
|
a
|
A
|
16
|
b
|
B
|
17
|
c
|
C
|
18
|
d
|
D
|
19 |
Table eleven: local service data
In one example, if the data content in the service data respectively has a primary key, traversing the data content in the service data of the source database, searching the primary key in the currently traversed data content in the local service data, if the primary key in the currently traversed data content is not found, indicating that the currently traversed data content is not found, recording an addition operation for adding the currently traversed data content, and if the primary key in the currently traversed data content is found and the data content in the local service data, which has the same primary key as the currently traversed data content, is not completely consistent with the currently traversed data content, recording a modification operation according to the currently traversed data content; and then, local service data is deduced according to the current updating operation, data contents in the local service data are traversed, a primary key in the currently traversed data contents is searched in the service data of the source database, if the primary key in the currently traversed data contents is not searched, the currently traversed data contents are not searched, and deletion operation for deleting the currently traversed data contents is recorded.
In one example, traversing data content in local service data, searching currently traversed data content in service data of a source database, and recording deletion operation for deleting the currently traversed data content if the currently traversed data content is not found; and then, local service data is deduced according to the current updating operation, the data content in the service data of the source database is traversed, the currently traversed data content is searched in the local service data, and if the currently traversed data content is not searched, the newly added operation of newly adding the currently traversed data content is recorded.
In one example, firstly traversing data content in service data of a source database, searching currently traversed data content in local service data, and recording a new adding operation for adding the currently traversed data content if the currently traversed data content is not searched; and then, local service data is deduced according to the current updating operation, data contents in the local service data are traversed, the currently traversed data contents are searched in the service data of the source database, and if the currently traversed data contents are not searched, deleting operation for deleting the currently traversed data contents is recorded.
In one example, if the data content in the service data respectively has a primary key, traversing the data content in the local service data, searching the primary key in the currently traversed data content in the service data of the source database, if the primary key in the currently traversed data content is not found, indicating that the currently traversed data content is not found, recording a deletion operation for deleting the currently traversed data content, and if the primary key in the currently traversed data content is found and the data content in the service data of the source database, which has the same primary key as the currently traversed data content, is not completely consistent with the currently traversed data content, recording and modifying operation according to the data content in the service data of the source database, which has the same primary key as the currently traversed data content.
In one example, if the data content in the service data respectively has a primary key, traversing the data content in the service data of the source database, searching the primary key in the currently traversed data content in the local service data, if the primary key in the currently traversed data content is not found, indicating that the currently traversed data content is not found, recording an addition operation for adding the currently traversed data content, and if the primary key in the currently traversed data content is found and the data content in the local service data having the same primary key as the currently traversed data content is not completely consistent with the currently traversed data content, recording a modification operation according to the currently traversed data content.
It should be noted that, when determining whether the new update operation includes a delete operation, an add operation, and a modify operation according to the comparison result, the order of the determination is not limited, that is, when determining the operation type, any combination may be performed, including but not limited to the following: determining a delete operation and a modify operation before determining a new operation, determining a delete operation before determining a new operation and a modify operation, determining a new operation before determining a delete and a modify operation, determining a delete operation before determining a new operation, determining a new operation before determining a delete operation, determining a delete operation and a modify operation, determining a new operation and a modify operation, and the like.
And 103, sending the new updating operation to the target database, so that the target database can perform data synchronization according to the new updating operation.
Specifically, in this embodiment, the number of the target databases is one, and the server sends a new update operation to the target databases, so that the target databases perform data synchronization according to the new update operation. In one example, the number of the target databases is more than one, the server sends a new update operation to all the target databases, so that the target databases perform data synchronization according to the new update operation, at this time, although the number of the target databases to be synchronized is more than one, the server only compares the local service data with the service data of the source database once to obtain a new update operation, and directly sends the new update operation to all the target databases, so that the data synchronization of all the target databases according to the new update operation can be realized. In one example, if the target database needs to be bridged to the server through the corresponding client, the server sends a new update operation to the client, and the client synchronizes data of the corresponding target database according to the new update operation.
In one example, after comparing the local service data with the service data of the source database and recording a new update operation according to the comparison result, the method further includes: and sending the number of the new updating operations to the target database, so that the target database can verify whether the synchronous data content is complete according to the number of the new updating operations. Specifically, the server may send the number of new update operations first, and then send the new update operations; or sending new updating operation first and then sending the number of the new updating operation; the new update operation and the number of new update operations may also be sent simultaneously. The target database carries out data synchronization after receiving new updating operations, the number of the updated updating operations can be obtained when the data synchronization is finished, if the number of the updated updating operations in the target database is not consistent with the number of the new updating operations sent by the server, the incomplete updating, namely the incomplete synchronized data content is shown, the target database requests the server for the new updating operations again, and if the number of the updated updating operations in the target database is consistent with the number of the new updating operations sent by the server, the complete updating, namely the complete synchronized data content is shown. For example: the number of new update operations is 10, data synchronization is performed after the new update operations are received, incomplete update is described if the number of update operations updated in the target database is 5 when data synchronization is completed, data synchronization is performed after the new update operations are received, and complete update is described if the number of update operations updated in the target database is 10 when data synchronization is completed. By the method, the server side also sends the number of the new updating operations to the target database, so that the target database can verify whether the synchronized data content is complete according to the number of the new updating operations, thereby verifying whether the data synchronization is complete, improving the integrity of the data content in the service data of the target database in the data synchronization process and improving the accuracy of the data synchronization. In one example, the number of the service ends in the data synchronization system is more than one, all the service ends form a service end cluster, each service end is deployed in a distributed architecture mode, and a distributed lock is adopted to ensure that only one service end performs the same data synchronization task in the service end cluster and the service end with the best current performance performs the task, if the service end fails, the data synchronization task is continuously performed by other service ends, namely, the other service ends deduce local service data according to the current updating operation; comparing the local service data with the service data of the source database, recording new updating operation according to the comparison result, and sending the new updating operation to the target database, so that the target database can perform data synchronization according to the new updating operation until the execution is successful or all the service terminals in the service terminal cluster are in failure; the server side in the server side cluster finds and communicates with each other through a Transmission Control Protocol (TCP) or an Internet Protocol (IP) built in the server side, and the connection and work of the server side in the server side cluster are automatically realized.
In this embodiment, local service data is derived according to the current update operation; comparing the local service data with the service data of the source database, and recording new updating operation according to the comparison result; and sending the new updating operation to the target database for the target database to carry out data synchronization according to the new updating operation. By the method, the network transmission quantity of the target database and the server only comprises the step that the server sends the new updating operation to the server, and the server does not need to send all the service data of the source database acquired from the source database to the target database, so that the network transmission quantity between the server and the target database is reduced, the transmission speed is higher as the network transmission quantity is smaller, the synchronization speed can be improved, the server does not need to compare the local service data with the service data of the source data after the target database sends a data synchronization request, and the server can immediately send the new updating operation to the target database according to the comparison result, and the synchronization speed is also improved.
A second embodiment of the present invention relates to a data synchronization method, and is substantially the same as the first embodiment, with the main difference that: receiving a monitoring request sent by a target database, where a specific flowchart is shown in fig. 2, and the specific flowchart includes:
step 201, receiving a monitoring request sent by a target database, where the monitoring request is used to characterize that when a server monitors that a new update operation exists, the server sends the new update operation to the target database.
Specifically, the target database sends a monitoring request to the server to monitor the update operation of the server, and the server sends a new update operation to the target database when monitoring that the new update operation exists. In one example, the target database needs to be bridged to the server side through the corresponding client side, the client side sends a monitoring request to the server side, the server side sends a new updating operation to the client side when monitoring that the new updating operation exists, and the client side enables the corresponding target database to perform data synchronization according to the new updating operation.
And step 202, deducing local service data according to the current updating operation.
Step 203, comparing the local service data with the service data of the source database, and recording a new updating operation according to the comparison result.
And step 204, sending the new updating operation to the target database, so that the target database can carry out data synchronization according to the new updating operation.
Step 202-204 is similar to step 101-103, and will not be described herein.
In this embodiment, the server does not need to forward the request for data synchronization of the target database to the source database, and the server only sends the new update operation to the target database when the new update operation exists, so that network transmission between the server and the source database can be reduced, and overall network transmission is reduced.
A third embodiment of the present invention relates to a data synchronization method, and is substantially the same as the first embodiment, with the main difference that: and sending the second search result to the target database for the target database to perform data synchronization according to the second search result, where a specific flowchart is shown in fig. 3 and includes:
step 301, querying the latest version number of the data content in the service data of the target database to obtain a second latest version number.
Step 302, if the data content corresponding to the second latest version number in the service data of the target database is judged to be synchronous and complete, searching for the updating operation corresponding to the version number greater than the second latest version number from the current updating operation to obtain a second searching result; and if the synchronization of the data content corresponding to the second latest version number in the service data of the target database is judged to be incomplete, searching for the updating operation corresponding to the version number not less than the second latest version number from the current updating operation, and obtaining a second searching result.
And step 303, sending the second search result to the target database, so that the target database performs data synchronization according to the second search result.
Specifically, during one data synchronization, a problem that a target database fails to receive a complete new update operation in the data synchronization process, or an interruption occurs during the data synchronization process, so that the data synchronization cannot be completely performed, or when a newly added target database exists, the newly added target database does not receive a new update operation before the data synchronization process, and so on, which may cause a part of the update operations to be unsuccessfully sent to the target database, so that, each time a client performs data synchronization, the server first queries the latest version number of data content in the service data of the target database to obtain a second latest version number, and determines the first version number according to the number of update operations corresponding to the second latest version number in the server and the number of update operations corresponding to the second latest version number in the target database, where the second latest version number is updated Whether the data contents corresponding to the two latest version numbers are synchronous and complete or not is judged, if the number of the updating operations corresponding to the second latest version number in the server side is equal to the number of the updating operations of the second latest version number in the target database, the data contents corresponding to the second latest version number in the target database are complete, whether the updating operations corresponding to the version number larger than the second latest version number can be found in the current updating operations of the server side is determined, if the updating operations corresponding to the version number larger than the second latest version number are found, a second searching result is obtained, if the target database is not updated to the latest version number, the obtained second searching result is sent to the target database, if the number of the updating operations corresponding to the second latest version number in the server side is larger than the number of the updating operations of the second latest version number in the target database, if the data content corresponding to the second latest version number in the target database is incomplete, searching for the updating operation corresponding to the version number not less than the second latest version number from the current updating operation to obtain a second searching result, and sending the second searching result to the target database; the server may send the second search result to the target database all at once, or may send the second search result to the target database multiple times according to the version number, for example: the second latest version number is 2, if the number of the updated update operations corresponding to the second latest version number in the target database is 5, and the number of the update operations corresponding to the second latest version number at the server is 5, the data content corresponding to the second latest version number is complete, and the update operation with the version number of 3 and the update operation with the version number of 4 are found from the current update operation, the update operation with the version number of 3 and the update operation with the version number of 4 can be completely sent to the target database once, or the update operation with the version number of 3 can be newly sent to the target database, and then the update operation with the version number of 4 is sent to the target database, so that the target database performs data synchronization according to the second search result; and if the number of the update operations of the second latest version number, which are updated, in the target database is 5, and the number of the update operations of the second latest version number, which are updated, in the server is 10, the data content corresponding to the second latest version number is incomplete, and the update operation of the version number 2, the update operation of the version number 3, and the update operation of the version number 4 are found from the current update operation.
In an example, the server may also enter step 301 after receiving a data request including the latest version of the data content in the service data of the target database sent by the target database, where the server may know the latest version number of the data content in the service data of the target database by querying the content in the data request.
And step 304, deducing local service data according to the current updating operation.
And 305, comparing the local service data with the service data of the source database, and recording a new updating operation according to the comparison result.
And step 306, sending the new updating operation to the target database, so that the target database can perform data synchronization according to the new updating operation.
The steps 304-306 are similar to the steps 101-103 in the first embodiment, and are not described herein again.
It should be noted that, the data transmission between the server and the target database is realized by the subscription service, and the data transmission between the server and the target can be performed at any time after the subscription service is established between the server and the target database, so that the step 301 and 303 can be performed in any step, that is, the step 301 and 303 can be performed after the step 304, or after the step 305, or after the step 306.
In the embodiment, all the updating operations can be sent to the target database, so that the intermediate updating operations are avoided from being missed, and the integrity of the updating operations received by the target database is ensured, thereby ensuring the integrity of data contents in the service data of the target database in the data synchronization process and improving the accuracy of data synchronization.
A fourth embodiment of the present invention relates to a data synchronization method, which is applied to a target database in a data synchronization system, where the data synchronization system further includes a server connected to the target database and the target database connected to the server, and a specific flow is shown in fig. 4, and includes:
step 401, receiving a new update operation sent by a server; the new updating operation is obtained by the server comparing the local service data with the service data of the source database and recording according to the comparison result;
step 402, data synchronization is performed according to the new update operation.
The steps of the above methods are divided for clarity, and the implementation may be combined into one step or split some steps, and the steps are divided into multiple steps, so long as the same logical relationship is included, which are all within the protection scope of the present patent; it is within the scope of the patent to add insignificant modifications to the algorithms or processes or to introduce insignificant design changes to the core design without changing the algorithms or processes.
A fifth embodiment of the present invention is directed to a data synchronization system, including: a source database 501, a server 502 and a target database 503;
a source database 501 for providing service data of source data;
the server 502 is configured to deduce local service data according to the current update operation; comparing the local service data with the service data of the source database, and recording new updating operation according to the comparison result; sending the new updating operation to a target database for the target database to carry out data synchronization according to the new updating operation;
and the target database 503 is configured to receive a new update operation sent by the server, and perform data synchronization according to the new update operation.
In one example, the number of the service terminals in the data synchronization system is more than one, all the service terminals form a service terminal cluster, each service terminal is deployed in a distributed architecture mode, and a distributed lock is adopted to ensure that only one service terminal in the service terminal cluster performs the same data synchronization task and the service terminal with the best current performance performs the same task, if the service terminal fails, the data synchronization task is continuously performed by other service terminals, the updating operation in the data synchronization process is recorded, and the updating operation is sent to a target database for the target database to perform data synchronization until the data synchronization is successfully performed or all the service terminals in the service terminal cluster fail; the server side in the server side cluster finds and communicates with each other through a Transmission Control Protocol (TCP) or an Internet Protocol (IP) built in the server side, and the connection and work of the server side in the server side cluster are automatically realized.
In one example, the number of the target databases is more than one, the server sends a new update operation to all the target databases to synchronize the data of the target databases, at this time, although the number of the target databases to be synchronized is more than one, the server only compares the local service data with the service data of the source database once to obtain a new update operation, and directly sends the new update operation to all the target databases, so that the data synchronization of all the target databases according to the new update operation can be realized.
In one example, if the target database needs to be bridged to the server side through the corresponding client side, the server side sends the update operation to the client side, and the client side synchronizes data of the corresponding target database according to the update operation.
It should be understood that the present embodiment is a system embodiment corresponding to the first, second, and third embodiments, and the present embodiment can be implemented in cooperation with the first, second, and third embodiments. The related technical details mentioned in the first, second and third embodiments are still valid in this embodiment, and are not described herein again in order to reduce repetition. Accordingly, the related-art details mentioned in the present embodiment can also be applied to the first embodiment, the second embodiment, and the third embodiment.
A sixth embodiment of the invention is directed to a server, as shown in fig. 6, comprising at least one processor 602; and, a memory 601 communicatively coupled to the at least one processor; the memory 601 stores instructions executable by the at least one processor 602, and the instructions are executed by the at least one processor 602 to enable the at least one processor 602 to perform the data synchronization method.
Where the memory 601 and the processor 602 are coupled by a bus, the bus may comprise any number of interconnected buses and bridges that couple one or more of the various circuits of the processor 602 and the memory 601 together. The bus may also connect various other circuits such as peripherals, voltage regulators, power management circuits, and the like, which are well known in the art, and therefore, will not be described any further herein. A bus interface provides an interface between the bus and the transceiver. The transceiver may be one element or a plurality of elements, such as a plurality of receivers and transmitters, providing a means for communicating with various other apparatus over a transmission medium. Data processed by processor 602 is transmitted over a wireless medium through an antenna, which receives the data and transmits the data to processor 602.
The processor 602 is responsible for managing the bus and general processing and may also provide various functions including timing, peripheral interfaces, voltage regulation, power management, and other control functions. And memory 601 may be used to store data used by processor 602 in performing operations.
A seventh embodiment of the present invention relates to a computer-readable storage medium storing a computer program. The computer program realizes the above-described method embodiments when executed by a processor.
That is, as can be understood by those skilled in the art, all or part of the steps in the method for implementing the embodiments described above may be implemented by a program instructing related hardware, where the program is stored in a storage medium and includes several instructions to enable a device (which may be a single chip, a chip, or the like) or a processor (processor) to execute all or part of the steps of the method described in the embodiments of the present application. And the aforementioned storage medium includes: a U-disk, a removable hard disk, a Read-Only Memory (ROM), a Random Access Memory (RAM), a magnetic disk or an optical disk, and other various media capable of storing program codes.
It will be understood by those of ordinary skill in the art that the foregoing embodiments are specific embodiments for practicing the invention, and that various changes in form and details may be made therein without departing from the spirit and scope of the invention in practice.