Data synchronization method, device and equipment
By receiving synchronization requests and determining the synchronization file based on the master database, the problem of mismatch between master and slave database logs is solved, ensuring data consistency and improving the accuracy and efficiency of data synchronization.
Patent Information
- Application Number
- CN202310862040.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-07-12
- Publication Date
- 2025-01-14
AI Technical Summary
In existing technologies, there is a problem of log mismatch in the master database during the data synchronization process between the master database and the slave database, which leads to data inconsistency and makes it impossible to accurately synchronize data.
By receiving synchronization requests, the system determines the synchronization file based on the master database, including data associated with transactions and version numbers, ensures that the logs of the master and slave databases match, and sends the synchronization file for updates to maintain data consistency.
It achieves data consistency between the master and slave databases, improving the accuracy and efficiency of data synchronization.
Smart Images

Figure CN119311765A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and more specifically, to a data synchronization method, apparatus and device. Background Art
[0002] Data synchronization technology refers to ensuring the data consistency of data copies stored in different electronic devices by synchronizing data copies distributed in different electronic devices. In some scenarios, data consistency between the master database and the slave database is achieved by synchronizing data in the master database in the first electronic device to the slave database in the second electronic device.
[0003] In the conventional technology, the data consistency between the master database and the slave database is achieved by using a method based on the log of the master database (e.g., binary log), wherein the log of the master database is used to record the data operation statements of the write transaction (e.g., delete, insert, or update) executed on the master database. In the above data synchronization process, there is a phenomenon that the logs of the master database and the master database do not match, that is, the data operation statements recorded in the log of the master database cannot correctly reflect the data in the master database, so that the data in the master database that needs to be synchronized to the slave database determined according to the log of the master database is inaccurate, thereby causing the problem of inconsistent data between the master database and the slave database.
[0004] Therefore, there is an urgent need to provide a data synchronization method to maintain the data consistency between the master database and the slave database as much as possible. Summary of the invention
[0005] The present application provides a data synchronization method, apparatus and device, which maintains data consistency between a master database and a slave database as much as possible.
[0006] In a first aspect, a data synchronization method is provided, which is applied to a first electronic device, the method comprising: receiving a synchronization request from a second electronic device, the synchronization request being used to request that first data in a first database in the first electronic device be backed up to a second database in the second electronic device, the synchronization request comprising a second version number corresponding to the second database; in a case where the second version number is different from the first version number corresponding to the first database, determining a synchronization file according to the first database, the synchronization file comprising the first data and the first version number, the first data being data associated with a first transaction, the first version number being a number obtained when the first electronic device successfully updates a third database in the first electronic device according to the first transaction, and the synchronization file is updated according to the first transaction. When the log update of the third database by the transaction fails, a version number different from the third version number corresponding to the third database is determined, the log of the third database matches the third database, the first database is a database obtained after successfully executing the first transaction on the third database and updating the third version number to the first version number, the third version number is the same as the second version number, and the data in the third database is the same as the data in the second database; the synchronization file is sent to the second electronic device, so that the second electronic device updates the second database using the synchronization file to obtain a fourth database, the data in the fourth database is the same as the data in the first database, and the version number corresponding to the fourth database is the first version number.
[0007] Optionally, the first data and the first version number included in the synchronization file in the above implementation manner may also be replaced by the first database.
[0008] In the above implementation, the data in the second database is the same as the data in the third database, that is, the second database is a database obtained by backing up the third database, and the log of the third database in the first electronic device matches the third database (that is, the data operation statements recorded in the log of the third database can correctly reflect the data in the third database). Based on this, the third version number corresponding to the third database can be set to be the same as the second version number corresponding to the second database.
[0009] In the above implementation, the first version number is a version number determined by the first electronic device when the third database is successfully updated and the log update of the third database fails, which is different from the third version number in the third database. It should be understood that in the scenario where the database update is successful and the log update fails, the first electronic device includes the logs of the first database and the third database, and the log of the third database is the log of the first database. Based on this, the second version number corresponding to the second database and the first version number corresponding to the first database are also different. That is to say, in the scenario where the database update is successful and the log update fails, the third version number corresponding to the third database (or the second version number corresponding to the second database) is different from the first version number corresponding to the first database, and the logs of the first database and the third database included in the first electronic device do not match, that is, the data operation statements recorded in the log of the third database cannot correctly reflect the data in the first database.
[0010] In the above technical solution, the first version number corresponding to the first database and the second version number corresponding to the second database in the synchronization request are different, that is, the logs of the first database and the third database included in the first electronic device do not match (that is, the data operation statements recorded in the log of the third database cannot correctly reflect the data in the first database). In the case where the first electronic device determines that the first version number corresponding to the first database and the second version number in the synchronization request are different, the first electronic device determines the synchronization file according to the first database. Thereafter, the first electronic device sends the synchronization file determined according to the first database to the second electronic device, so that the second electronic device updates the second database using the synchronization file to obtain the fourth database, thereby maintaining the data consistency of the first database as the master database and the fourth database as the slave database as much as possible. In summary, the data synchronization method provided in the embodiment of the present application maintains the data consistency of the master database and the slave database as much as possible.
[0011] In one possible implementation, before receiving the synchronization request from the second electronic device, the method also includes: updating the third database according to the first transaction to obtain a fifth database; in the event that the log update of the third database according to the first transaction fails, deleting all data operation statements in the log of the third database, and updating the third version number in the log of the third database to the first version number to obtain the log of the first database, wherein all data operation statements are used to generate the data in the third database; updating the third version number in the fifth database to the first version number to obtain the first database, wherein the first version number corresponding to the first database is the first version number in the log of the first database or the first version number in the first database.
[0012] In the above technical solution, when the first electronic device successfully updates the third database according to the first transaction to obtain the fifth database, and fails to update the log of the third database according to the first transaction, resulting in the mismatch between the logs of the fifth database and the third database in the first electronic device, the first electronic device deletes all data operation statements in the log of the third database, and updates the third version number of the log of the third database to a first version number different from the third version number to obtain the log of the first database, and updates the third version number of the fifth database to a first version number different from the third version number to obtain the first database. Thereafter, when the first electronic device receives the second version number corresponding to the second database in the synchronization request, the first electronic device can determine that the first database in the first electronic device and the log of the first database (i.e., the log of the third database) do not match by comparing the first version number corresponding to the first database and the second version number in the synchronization request, so that the first electronic device determines the synchronization file according to the first database, which is conducive to maintaining the data consistency of the first database as the master database and the fourth database as the slave database as much as possible.
[0013] In another possible implementation, the log of the third database and the third database further record the second transaction identifier of the second transaction, the all data operation statements include the data operation statements of the second transaction, and the data in the third database include data determined according to the data operation statements of the second transaction; and the updating of the third database according to the first transaction to obtain the fifth database includes: updating the data in the third database according to the first transaction, and updating the second transaction identifier according to the first transaction to obtain the fifth database including the first transaction identifier; and, in the case where the updating of the log of the third database according to the first transaction fails, deleting the data in the log of the third database The method comprises: determining that the first transaction identifier in the fifth database and the second transaction identifier in the log of the third database are different when the first electronic device is powered off and then powered on again during the process of updating the log of the third database according to the first transaction; and deleting all data operation statements in the log of the third database and updating the third version number in the log of the third database to the first version number to obtain the log of the first database when the first electronic device is powered off and then powered on again; and deleting all data operation statements in the log of the third database and updating the third version number in the log of the third database to the first version number to obtain the log of the first database when the first transaction identifier in the fifth database and the second transaction identifier in the log of the third database are different.
[0014] In the above technical solution, a transaction identifier for indicating a write transaction is added to the database and the log of the database in the first electronic device. Based on this, when the first electronic device successfully updates the third database according to the first transaction to obtain the fifth database, and when the log of the third database is updated according to the first transaction, it is determined that the log of the third database is successfully updated according to the transaction identifier in the fifth database and the transaction identifier in the log of the third database, and when the third database is updated successfully according to the first transaction, the log of the third database is successfully updated according to whether the first transaction is successful. This judgment method is relatively simple. In the scenario where the first electronic device is powered on again after power failure, the first electronic device can determine that the third database is successfully updated according to the first transaction, and the log of the third database is updated unsuccessfully according to the transaction identifier in the fifth database and the transaction identifier in the log of the third database when they are different. Thereafter, the first electronic device will update the third version number in the fifth database to the first version number to obtain the first database, and delete all data operation statements in the log of the third database and update the third version number in the log of the third database to the first version number to obtain the log of the first database. Thereafter, when the first electronic device receives the second version number corresponding to the second database in the synchronization request, the first electronic device can determine that the first database in the first electronic device and the log of the first database (i.e., the log of the third database) do not match by comparing the first version number corresponding to the first database and the second version number in the synchronization request, so that the first electronic device can determine the synchronization file based on the first database. This is beneficial to maintaining the data consistency of the first database as the master database and the fourth database as the slave database as much as possible.
[0015] In another possible implementation, the synchronization request further includes a second sequence number, where the second sequence number is used to indicate a location of data requested to be synchronized to the first database by the synchronization request, and the synchronization file further includes the second sequence number.
[0016] In another possible implementation, before sending the synchronization file to the second electronic device, the method also includes: in a case where the second version number is the same as the first version number corresponding to the first database, determining the synchronization file according to a first log of the first database, and the first log matches the first database, wherein, in a case where the first electronic device successfully updates the third database according to the first transaction to obtain the first database, and successfully obtains the first log by updating the log of the third database according to the first transaction, the first version number is the same as the third version number corresponding to the third database.
[0017] In the above technical solution, the first version number corresponding to the first database and the second version number corresponding to the second database in the synchronization request are the same, that is, the first database included in the first electronic device matches the first log of the first database (that is, the data operation statement recorded in the first log can correctly reflect the data in the first database). When the first electronic device determines that the first version number corresponding to the first database is the same as the second version number in the synchronization request, the first electronic device determines the synchronization file according to the first log of the first database. It should be understood that in the case where the first log and the first database match, the determination process of the synchronization file determined by the first electronic device based on the first log is simpler and more efficient than the determination process of the synchronization file determined by the first electronic device based on the first database. Thereafter, the first electronic device sends the synchronization file determined according to the first log to the second electronic device, so that the second electronic device updates the second database using the synchronization file to obtain the fourth database, thereby maintaining the data consistency of the first database as the master database and the fourth database as the slave database as much as possible. In summary, the data synchronization method provided in the embodiment of the present application is also conducive to improving the efficiency of data synchronization while maintaining the data consistency of the master database and the slave database as much as possible.
[0018] In another possible implementation, when the second version number is the same as the first version number corresponding to the first database, determining the synchronization file according to the first log of the first database includes: when the second version number is the same as the first version number corresponding to the first database, and the first check code and the second check code are the same, determining that the first log has not been modified, the first check code is a check code of a first data operation statement at a first position recorded in the first log, the second check code is a check code generated by the first electronic device according to a second data operation statement obtained from the first position in the first log, the first data operation statement and the second data operation statement are the same, and the first data operation statement at the first position recorded in the first log is a data operation statement of the first transaction; when the first log has not been modified, determining the synchronization file according to the first sequence number of the first log and the second sequence number in the synchronization request, the synchronization file including the first data operation statement, the first sequence number being used to indicate the position of data synchronized to the first database by the first electronic device before receiving the synchronization request, and the second sequence number being used to indicate the position of data requested to be synchronized to the first database by the synchronization request.
[0019] In the above implementation, the first verification code and the second verification code are the same, that is, the first log has not been tampered with. In this implementation, the first electronic device includes a first database and a first log, and the first database and the first log match, that is, the data operation statements recorded in the first log can correctly reflect the data in the first database.
[0020] In the above technical solution, when the first electronic device determines that the first verification code and the second verification code are the same, the first database included in the first electronic device matches the first log (that is, the data operation statements recorded in the first log can correctly reflect the data in the first database). Based on this, the first electronic device determines the synchronization file according to the first log. It should be understood that when the first log and the first database match, the determination process of the synchronization file determined by the first electronic device based on the first log is simpler and more efficient than the determination process of the synchronization file determined by the first electronic device based on the first database. Thereafter, the first electronic device sends the synchronization file determined according to the first log to the second electronic device, so that the second electronic device updates the second database using the synchronization file to obtain the fourth database, thereby maintaining the data consistency of the first database as the master database and the fourth database as the slave database as much as possible. In summary, the data synchronization method provided in the embodiment of the present application is also conducive to improving the efficiency of data synchronization while maintaining the data consistency of the master database and the slave database as much as possible.
[0021] In another possible implementation manner, the synchronization file further includes the first verification code or the second verification code.
[0022] In the above technical solution, the synchronization file also includes a first check code or a second check code. Based on this, after the second electronic device receives the synchronization file including the first check code or the second check code, the second electronic device can perform data verification on the data operation statement (i.e., the first data operation statement) in the synchronization file according to the first check code or the second check code in the synchronization file to determine whether the data operation statement in the synchronization file has been tampered with during the transmission process. Only when the second electronic device determines that the data operation statement in the synchronization file has not been tampered with, will it update the second database according to the data operation statement in the synchronization file to obtain the fourth database, thereby maintaining the data consistency of the first database as the master database and the fourth database as the slave database as much as possible.
[0023] In another possible implementation, when the second version number is the same as the first version number corresponding to the first database, determining the synchronization file according to the first log of the first database includes: when the second version number is the same as the first version number corresponding to the first database, and the first verification code and the second verification code are different, determining that the first log has been modified, the first verification code is the verification code of the first data operation statement at the first position recorded in the modified first log, the second verification code is the verification code generated by the first electronic device according to the second data operation statement obtained from the first position in the modified first log, the first data operation statement and the second data operation statement are different, and the first data operation statement at the first position recorded in the modified first log is the data operation statement of the first transaction; and, when the first log is modified, determining the synchronization file according to the first database, the synchronization file including the first data.
[0024] In the above implementation, the first verification code and the second verification code are different, that is, the first log is modified to obtain a modified first log. In this implementation, the first database included in the first electronic device does not match the modified first log, that is, the data operation statements recorded in the modified first log cannot correctly reflect the data in the first database.
[0025] In the above technical solution, when the first electronic device determines that the first verification code and the second verification code are different, the first database included in the first electronic device does not match the modified first log (that is, the data operation statements recorded in the modified first log cannot correctly reflect the data in the first database). Based on this, the first electronic device needs to determine the synchronization file according to the first database. Thereafter, the first electronic device sends the synchronization file determined according to the first database to the second electronic device, so that the second electronic device updates the second database using the synchronization file to obtain a fourth database, thereby maintaining the data consistency of the first database as the master database and the fourth database as the slave database as much as possible. In summary, the data synchronization method provided in the embodiment of the present application is beneficial to improving the efficiency of data synchronization while maintaining the data consistency of the master database and the slave database as much as possible.
[0026] In another possible implementation, the first verification code is located at a second position in the first log, and the second position is associated with the first position. After receiving the synchronization request from the second electronic device, the method further includes: obtaining the first data operation statement from the first position in the first log according to the first serial number of the first log and the second serial number in the synchronization request; generating the first verification code according to the first data operation statement, and writing the first verification code to the second position of the first log.
[0027] In the above technical solution, a first verification code is added to the first log of the first database, and the first verification code is written into the first log when the first electronic device writes the data statement of the first transaction into the first log and receives a synchronization request including a second serial number sent by the second electronic device. This is beneficial for the first electronic device to determine whether the data operation statement of the first transaction recorded in the first log has changed after obtaining the first log recording the data operation statement of the first transaction according to the first verification code recorded in the first log.
[0028] In another possible implementation, the log of the third database and the second transaction identifier of the second transaction are recorded in the third database, all data operation statements in the log of the third database include data operation statements of the second transaction, and the data in the third database includes data determined according to the data operation statements of the second transaction; and, before receiving the synchronization request from the second electronic device, the method further includes: updating the data in the third database according to the first transaction, and updating the second transaction identifier in the third database according to the first transaction, to obtain the first database; updating all data operation statements in the log of the third database according to the first transaction, and updating the second transaction identifier in the log of the third database according to the first transaction identifier, to obtain the first log.
[0029] In the above technical solution, a transaction identifier for indicating a write transaction is added to the database in the first electronic device and the log of the database. Based on this, the first electronic device can determine that the first database and the first log match by comparing the transaction identifier in the first database with the transaction identifier in the first log. This determination method is relatively simple.
[0030] In another possible implementation manner, the first database and the third database are associated with a first application in the first electronic device, and the second database and the fourth database are associated with a second application in the second electronic device.
[0031] In another possible implementation manner, the first database and the second database are embedded databases.
[0032] In another possible implementation, the first electronic device includes a first database engine and a first synchronization service, and the receiving of the synchronization request from the second electronic device includes: the first synchronization service receives the synchronization request from the second electronic device; the method also includes: the first synchronization service sends the synchronization request to the first database engine; when the second version number is different from the first version number corresponding to the first database, determining the synchronization file according to the first database includes: when the second version number is different from the first version number, the first database engine determines the synchronization file according to the first database; the method also includes: the first database engine sends the synchronization file to the first synchronization service; sending the synchronization file to the second electronic device includes: the first synchronization service sends the synchronization file to the second electronic device.
[0033] In a second aspect, a data synchronization method is provided, which is applied to a first electronic device, the method comprising: receiving a synchronization request from a second electronic device, the synchronization request being used to request that first data in a first database in the first electronic device be backed up to a second database in the second electronic device, the synchronization request comprising a second version number corresponding to the second database; in a case where the second version number is the same as the first version number corresponding to the first database, determining the synchronization file according to a first log of the first database, the first log matching the first database, wherein, in a case where the first electronic device successfully obtains the first database by updating the third database according to the first transaction, and successfully obtains the first log by updating the log of the third database according to the first transaction, the first version number is the same as the third version number corresponding to the third database, the log of the third database matches the third database, the third version number is the same as the second version number, and the data in the third database is the same as the data in the second database; sending the synchronization file to the second electronic device, so that the second electronic device updates the second database using the synchronization file to obtain a fourth database, the data in the fourth database being the same as the data in the first database.
[0034] In a possible implementation, when the second version number is the same as the first version number corresponding to the first database, determining the synchronization file according to the first log of the first database includes: when the second version number is the same as the first version number corresponding to the first database, and the first check code and the second check code are the same, determining that the first log has not been modified, the first check code is a check code of a first data operation statement at a first position recorded in the first log, the second check code is a check code generated by the first electronic device according to a second data operation statement obtained from the first position in the first log, the first data operation statement and the second data operation statement are the same, and the first data operation statement at the first position recorded in the first log is a data operation statement of the first transaction; when the first log has not been modified, determining the synchronization file according to the first sequence number of the first log and the second sequence number in the synchronization request, the synchronization file including the first data operation statement, the first sequence number being used to indicate the location of data synchronized to the first database by the first electronic device before receiving the synchronization request, and the second sequence number being used to indicate the location of data requested to be synchronized to the first database by the synchronization request.
[0035] In another possible implementation, when the second version number is the same as the first version number corresponding to the first database, the synchronization file is determined according to the first log of the first database, including: when the second version number is the same as the first version number corresponding to the first database, and the first verification code and the second verification code are different, determining that the first log has been modified, the first verification code is the verification code of the first data operation statement at the first position recorded in the modified first log, the second verification code is the verification code generated by the first electronic device according to the second data operation statement obtained from the first position in the modified first log, the first data operation statement and the modified second data operation statement are different, and the first data operation statement at the first position recorded in the modified first log is the data operation statement of the first transaction; when the first log is modified, determining the synchronization file according to the first database, the synchronization file includes the first data.
[0036] It can be understood that the beneficial effects of the second aspect mentioned above can be found in the relevant description of the first aspect mentioned above, and will not be repeated here.
[0037] In a third aspect, a data synchronization method is provided, which is applied to a second electronic device, the method comprising: sending a synchronization request to a first electronic device, the synchronization request being used to request that first data in a first database in the first electronic device be backed up to a second database in the second electronic device, the synchronization request comprising a second version number corresponding to the second database; receiving a synchronization file from the first electronic device; when the synchronization file comprises the first data and the first version number corresponding to the first database, updating the data in the second database according to the first data, and updating the second version number in the second database according to the first version number, to obtain a fourth database, the data in the fourth database being the same as the data in the first database, and the version number corresponding to the fourth database being the first version number, wherein the synchronization file is determined by the first electronic device according to the first database when the second version number is different from the first version number, the first data is data associated with a first transaction, the first version number is a version number different from the third version number corresponding to the third database determined when the first electronic device successfully updates a third database in the first electronic device according to the first transaction, and fails to update a log of the third database according to the first transaction, the log of the third database matches the third database, and the first database is a database obtained after the first transaction is successfully executed on the third database and the third version number is updated to the first version number.
[0038] Optionally, the first data and the first version number included in the synchronization file in the above implementation manner may also be replaced by the first database.
[0039] In the above implementation, the data in the second database is the same as the data in the third database, that is, the second database is a database obtained by backing up the third database, and the log of the third database in the first electronic device matches the third database (that is, the data operation statements recorded in the log of the third database can correctly reflect the data in the third database). Based on this, the third version number corresponding to the third database can be set to be the same as the second version number corresponding to the second database.
[0040] In the above implementation, the first version number is a version number determined by the first electronic device when the third database is successfully updated and the log update of the third database fails, which is different from the third version number in the third database. It should be understood that in the scenario where the database update is successful and the log update fails, the first electronic device includes the logs of the first database and the third database, and the log of the third database is the log of the first database. Based on this, the second version number corresponding to the second database and the first version number corresponding to the first database are also different. That is to say, in the scenario where the database update is successful and the log update fails, the third version number corresponding to the third database (or the second version number corresponding to the second database) is different from the first version number corresponding to the first database, and the logs of the first database and the third database included in the first electronic device do not match, that is, the data operation statements recorded in the log of the third database cannot correctly reflect the data in the first database.
[0041] In the above technical solution, the first version number corresponding to the first database and the second version number corresponding to the second database in the synchronization request are different, that is, the logs of the first database and the third database included in the first electronic device do not match (that is, the data operation statements recorded in the log of the third database cannot correctly reflect the data in the first database). In the case where the first electronic device determines that the first version number corresponding to the first database and the second version number in the synchronization request are different, the first electronic device determines the synchronization file according to the first database. Thereafter, the first electronic device sends the synchronization file determined according to the first database to the second electronic device, and the second electronic device updates the second database using the synchronization file to obtain a fourth database, thereby maintaining the data consistency of the first database as the master database and the fourth database as the slave database as much as possible. In summary, the data synchronization method provided in the embodiment of the present application maintains the data consistency of the master database and the slave database as much as possible.
[0042] In another possible implementation, the method further includes: when the synchronization file includes the first data operation statement of the first transaction, executing the first data operation statement on the second database to obtain the fourth database, the data in the fourth database is the same as the data in the first database, wherein the synchronization file is determined by the first electronic device based on the first serial number of the first log and the second serial number in the synchronization request when the second version number is the same as the first version number corresponding to the first database and the first electronic device determines that the first log of the first database has not been modified based on the first verification code and the second verification code, the first log matches the first database, and the first verification code is the first data at the first position recorded in the first log. operation statement, the second verification code is a verification code generated by the first electronic device according to the second data operation statement obtained from the first position in the first log, the first data operation statement and the second data operation statement are the same, the first serial number is used to indicate the position of the data synchronized to the first database by the first electronic device before receiving the synchronization request, and the second serial number is used to indicate the position of the data requested to be synchronized to the first database by the synchronization request; and, when the first electronic device successfully obtains the first database by updating the third database according to the first transaction, and successfully obtains the first log by updating the log of the third database according to the first transaction, the first version number is the same as the third version number corresponding to the third database.
[0043] In the above technical solution, the synchronization file received by the second electronic device is a synchronization file determined according to the first log when the first electronic device determines that the first log has not been modified, that is, when the first log and the first database match, the first electronic device determines the synchronization file according to the first log. It should be understood that when the first log and the first database match, the determination process of the synchronization file determined by the first electronic device based on the first log is simpler and more efficient than the determination process of the synchronization file determined by the first electronic device based on the first database. Thereafter, the second electronic device updates the second database according to the synchronization file to obtain the fourth data, and the method maintains the data consistency of the first database as the master database and the fourth database as the slave database as much as possible. In summary, the data synchronization method provided in the embodiment of the present application is also conducive to improving the efficiency of data synchronization while maintaining the data consistency of the master database and the slave database as much as possible.
[0044] In another possible implementation, the synchronization file also includes the first verification code or the second verification code; and, in a case where the synchronization file includes the first data operation statement of the first transaction, executing the first data operation statement on the second database, before obtaining the fourth database, the method further includes: generating a third verification code based on the first data operation statement in the synchronization file; and determining that the first data operation statement in the synchronization file has passed the verification when the third verification code is the same as the verification code included in the synchronization file.
[0045] In the above technical solution, the synchronization file also includes a first check code or a second check code. Based on this, after the second electronic device receives the synchronization file including the first check code or the second check code, the second electronic device can perform data verification on the data operation statement (i.e., the first data operation statement) in the synchronization file according to the first check code or the second check code in the synchronization file to determine whether the data operation statement in the synchronization file has been tampered with during the transmission process. Only when the second electronic device determines that the data operation statement in the synchronization file has not been tampered with, will it update the second database according to the data operation statement in the synchronization file to obtain the fourth database, thereby maintaining the data consistency of the first database as the master database and the fourth database as the slave database as much as possible.
[0046] In another possible implementation, when the synchronization file includes the first data, the data in the second database is updated according to the first data to obtain the fourth database, and the data in the fourth database is the same as the data in the first database, wherein the synchronization file is determined by the first electronic device according to the first database when the second version number is the same as the first version number corresponding to the first database and the first electronic device determines that the first log of the first database is modified according to the first verification code and the second verification code, wherein the first verification code is a verification code of a first data operation statement at a first position recorded in the modified first log, the second verification code is a verification code generated by the first electronic device from a second data operation statement obtained from the first position in the modified first log, the first data operation statement is different from the second data operation statement, and the first data operation statement at the first position recorded in the modified first log is a data operation statement of the first transaction; and when the first electronic device successfully obtains the first database by updating the third database according to the first transaction, and successfully obtains the first log by updating the log of the third database according to the first transaction, the first version number is the same as the third version number corresponding to the third database.
[0047] In the above technical solution, the synchronization file received by the second electronic device is a synchronization file determined according to the first database when the first electronic device determines that the first log has been modified, that is, when the first log and the first database do not match, the first electronic device determines the synchronization file according to the first database. Thereafter, the second electronic device updates the second database according to the synchronization file to obtain the fourth data. The method maintains the data consistency of the first database as the master database and the fourth database as the slave database as much as possible.
[0048] In another possible implementation, before obtaining the fourth database, the method further includes: performing data verification on the first data in the synchronization file to determine whether the first data passes the verification.
[0049] In the above technical solution, only when the second electronic device determines that the first data in the synchronization file has passed the verification, will it update the second database according to the first data in the synchronization file to obtain the fourth database, thereby maintaining the data consistency of the first database as the main database and the fourth database as the slave database as much as possible.
[0050] In another possible implementation, the second electronic device includes a second database engine, a second synchronization service and a second application. Before sending the synchronization request to the first electronic device, the method also includes: the second synchronization service receives the synchronization request sent from the second application; the sending of the synchronization request to the first electronic device includes: the second synchronization service sends the synchronization request to the first electronic device; the receiving of the synchronization file from the first electronic device includes: the second synchronization service receives the synchronization file; the method also includes: the second synchronization service sends the synchronization file to the second database engine; the updating of the data in the second database according to the first data and the updating of the second version number in the second database according to the first version number to obtain the fourth database when the synchronization file includes the first data and the first version number corresponding to the first database, includes: the second database engine updates the data in the second database according to the first data and the updating of the second version number in the second database according to the first version number to obtain the fourth database when the synchronization file includes the first data and the first version number corresponding to the first database.
[0051] In another possible implementation manner, the first database and the third database are associated with a first application in the first electronic device, and the second database and the fourth database are associated with a second application in the second electronic device.
[0052] In another possible implementation manner, the first database and the second database are embedded databases.
[0053] In a fourth aspect, a data synchronization method is provided, which is applied to a system including a first electronic device and a second electronic device, the method comprising: the second electronic device sends a synchronization request to the first electronic device, the synchronization request is used to request to back up first data in a first database in the first electronic device to a second database in the second electronic device, the synchronization request includes a second version number corresponding to the second database; when the second version number is different from the first version number corresponding to the first database, the first electronic device determines a synchronization file based on the first database, the synchronization file includes the first data and the first version number, the first data is data associated with a first transaction, and the first version number is when the first electronic device successfully updates a third database in the first electronic device according to the first transaction, and updates the third database according to the first transaction In the case where the log update of the database fails, a version number different from the third version number corresponding to the third database is determined, the log of the third database matches the third database, the first database is a database obtained after successfully executing the first transaction on the third database and updating the third version number to the first version number, the third version number is the same as the second version number, and the data in the third database is the same as the data in the second database; the first electronic device sends the synchronization file to the second electronic device; the second electronic device updates the data in the second database according to the first data, and updates the second version number in the second database according to the first version number to obtain a fourth database, the data in the fourth database is the same as the data in the first database, and the version number corresponding to the fourth database is the first version number.
[0054] In a fifth aspect, a data synchronization device is provided, which is applied to a first electronic device and is used to execute the data synchronization method provided in the first aspect or the second aspect. Specifically, the data synchronization device may include a module for executing any possible implementation of the first aspect or the second aspect.
[0055] In a sixth aspect, a data synchronization device is provided, which is applied to a second electronic device and is used to execute the data synchronization method provided in the third aspect. Specifically, the data synchronization device may include a module for executing any possible implementation of the third aspect.
[0056] In a seventh aspect, a data synchronization system is provided, the data synchronization system comprising a first electronic device and a second electronic device, the second electronic device being used to: send a synchronization request to the first electronic device, the synchronization request being used to request that first data in a first database within the first electronic device be backed up to a second database within the second electronic device, the synchronization request comprising a second version number corresponding to the second database; the first electronic device being used to: when the second version number is different from the first version number corresponding to the first database, determine a synchronization file according to the first database, the synchronization file comprising the first data and the first version number, the first data being data associated with a first transaction, the first version number being when the first electronic device successfully updates a third database within the first electronic device according to the first transaction, and the third database is updated according to the first transaction. In the event of a log update failure, a version number different from the third version number corresponding to the third database is determined, the log of the third database matches the third database, the first database is a database obtained after successfully executing the first transaction on the third database and updating the third version number to the first version number, the third version number is the same as the second version number, and the data in the third database is the same as the data in the second database; the first electronic device is also used to: send the synchronization file to the second electronic device; the second electronic device is also used to: update the data in the second database according to the first data, and update the second version number in the second database according to the first version number to obtain a fourth database, the data in the fourth database is the same as the data in the first database, and the version number corresponding to the fourth database is the first version number.
[0057] It can be understood that the beneficial effects of the fourth to seventh aspects mentioned above can be found in the relevant descriptions of the first to third aspects mentioned above, and will not be repeated here.
[0058] In an eighth aspect, an electronic device is provided, comprising a unit for executing any one of the methods in the first aspect, the second aspect, or the third aspect. The device may be a terminal device or a chip in a terminal device. The device may include an input unit and a processing unit.
[0059] When the device is a terminal device, the processing unit may be a processor, and the input unit may be a communication interface; the terminal device may also include a memory, which is used to store computer program code. When the processor executes the computer program code stored in the memory, the terminal device executes any one of the methods in the first aspect, the second aspect or the third aspect.
[0060] When the device is a chip in a terminal device, the processing unit may be a processing unit inside the chip, and the input unit may be an output interface, a pin or a circuit, etc.; the chip may also include a memory, which may be a memory inside the chip (for example, a register, a cache, etc.) or a memory located outside the chip (for example, a read-only memory, a random access memory, etc.); the memory is used to store computer program code, and when the processor executes the computer program code stored in the memory, the chip executes any one of the methods in the first aspect, the second aspect or the third aspect.
[0061] In one possible implementation, a memory is used to store computer program code; a processor executes the computer program code stored in the memory, and when the computer program code stored in the memory is executed, the processor is used to execute any one of the methods in the first aspect, the second aspect, or the third aspect.
[0062] In the ninth aspect, a computer-readable storage medium is provided, wherein the computer-readable storage medium stores a computer program code. When the computer program code is executed by a data synchronization device, the data synchronization device executes any one of the data synchronization methods in the first aspect, the second aspect or the third aspect.
[0063] In the tenth aspect, a computer program product is provided, the computer program product comprising: a computer program code, when the computer program code is executed by a data synchronization device, the data synchronization device executes any one of the data synchronization methods in the first aspect, the second aspect or the third aspect. BRIEF DESCRIPTION OF THE DRAWINGS
[0064] Figure 1 It is a schematic diagram of an application scenario suitable for the data synchronization method provided in an embodiment of the present application.
[0065] Figure 2 It is a schematic diagram of a specific application scenario applicable to the data synchronization method provided in the embodiment of the present application.
[0066] Figure 3 It is a schematic diagram of an architecture of a first electronic device and a second electronic device that execute the data synchronization method provided in an embodiment of the present application.
[0067] Figure 4 It is a schematic diagram of a software system of an electronic device that executes the data synchronization method provided in an embodiment of the present application.
[0068] Figure 5 It is a schematic diagram of another architecture of a first electronic device and a second electronic device for executing the data synchronization method provided in an embodiment of the present application.
[0069] Figure 6 It is a schematic diagram of a data synchronization method provided in an embodiment of the present application.
[0070] Figure 7 is the above Figure 6 A schematic diagram of a third database involved in the provided data synchronization method.
[0071] Figure 8 is the above Figure 6 A schematic diagram of a log of a third database involved in the provided data synchronization method.
[0072] Fig. 9 is the above Figure 6 A schematic diagram of a fifth database involved in the data synchronization method provided.
[0073] Fig.10 is the above Figure 6 A schematic diagram of a first database involved in the provided data synchronization method.
[0074] Fig.11 is the above Figure 6 A schematic diagram of a log of a first database involved in a data synchronization method is provided.
[0075] Fig.12 is the above Figure 6 A schematic diagram of a second database and a fourth database involved in the provided data synchronization method.
[0076] Fig.13 It is a schematic diagram of another data synchronization method provided in an embodiment of the present application.
[0077] Fig.14 It is a schematic diagram of another data synchronization method provided in an embodiment of the present application.
[0078] Fig.15 It is a schematic diagram of another data synchronization method provided in an embodiment of the present application.
[0079] Fig.16 It is a schematic diagram of another data synchronization method provided in an embodiment of the present application.
[0080] Fig.17 is the above Fig.16 A schematic diagram of a first log of a first database involved in a data synchronization method is provided.
[0081] Fig.18 It is a schematic diagram of another data synchronization method provided in an embodiment of the present application.
[0082] Fig.19 It is a schematic diagram of a graphical user interface displayed by the second electronic device 1900 provided in an embodiment of the present application.
[0083] Fig. 20 It is a schematic diagram of a data synchronization device provided in an embodiment of the present application.
[0084] Fig.21 It is a schematic diagram of the structure of an electronic device 2100 provided in an embodiment of the present application. DETAILED DESCRIPTION
[0085] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0086] First, the application scenarios of the data synchronization method provided in the embodiments of the present application are introduced with reference to the accompanying drawings.
[0087] Figure 1 It is a schematic diagram of an application scenario suitable for the data synchronization method provided in an embodiment of the present application.
[0088] For example, see Figure 1 The application scenario shown includes a first electronic device 110 and at least one second electronic device 120. The first electronic device 110 and the second electronic device 120 can communicate with each other through an end-to-end communication method. The communication connection between the first electronic device 110 and the second electronic device 120 can be a short-range communication connection, such as a Wi-Fi point-to-point connection or a Bluetooth connection, etc.; the communication connection between the first electronic device 110 and the second electronic device 120 can also be a long-range communication connection, such as an Internet connection, etc., and no limitation is imposed on the specific type of the end-to-end communication method.
[0089] In the embodiment of the present application, there is no limitation on the device type of any one of the first electronic device 110 and the second electronic device 120. For example, any one of the first electronic device 110 and the second electronic device 120 may be a terminal device such as a mobile phone, a tablet computer, and a notebook computer, or may be a non-terminal device such as a server.
[0090] For example, see Figure 2 The specific schematic diagram of the application scenario provided by the embodiment of the present application is shown, and the application scenario includes a mobile phone 210 as the first electronic device 110, and a tablet 220 as the second electronic device 120. The communication connection between the mobile phone 210 and the tablet 220 can be a Wi-Fi point-to-point connection.
[0091] Based on the communication connection between the first electronic device 110 and the second electronic device 120, the second electronic device 120 can synchronize the application data generated by the application of the first electronic device 110 to the local storage space of the second electronic device 120 to achieve cross-device data access. It can be understood that the data generated by the application of the first electronic device 110 is stored in the local storage space of the first electronic device 110.
[0092] Similarly, the first electronic device 110 can also synchronize the application data generated by the application of the second electronic device 120 to the local storage space of the first electronic device 110, so that the first electronic device 110 can access the application data generated by the application of the second electronic device 120 in the local storage space to achieve cross-device data access. It is understandable that the data generated by the application of the second electronic device 120 is stored in the local storage space of the second electronic device 120.
[0093] In the embodiments of the present application, the data types accessed across devices described above are not specifically limited. For example, the data accessed across devices may be media data, such as video, audio, and pictures. For another example, the data accessed across devices may also be file data, such as PDF.
[0094] For example, in some application scenarios, Figure 2 The picture data stored in the gallery application of the tablet 220 is synchronized with the gallery application of the mobile phone 210 , so that the user can access the pictures stored in the gallery application of the tablet 220 by browsing the gallery application of the mobile phone 210 .
[0095] It should be understood that Figure 1 and Figure 2 The application scenarios shown are for illustration only and do not constitute any limitation on the application scenarios to which the data synchronization method provided in the embodiments of the present application is applicable.
[0096] Next, based on the above Figure 1 The application scenario shown, combined with the accompanying drawings, introduces the relevant module units within the electronic device involved when the electronic device executes the data synchronization method provided by the embodiment of the present application.
[0097] For example, see Figure 3 The first electronic device 110 includes a first application 111, a first database synchronization service 112, a first database engine 113, and a database in the first electronic device 110. The second electronic device 120 includes a second application 121, a second database synchronization service 122, a second database engine 123, and a database in the second electronic device 120.
[0098] Next, each module unit in the first electronic device 110 is introduced.
[0099] The first application 111 is an application program running in the first electronic device 110. The application data generated by the first application 111 can be stored in the first electronic device 110. The type of the first application 111 is not specifically limited. For example, the first application 111 can be a gallery application, and the application data generated by the first application 111 can be a picture or a video. For another example, the first application 111 can be a table application, and the application data generated by the first application 111 can be a two-dimensional data table.
[0100] The database in the first electronic device 110 is a local database of the first electronic device 110. The database in the first electronic device 110 is a database associated with the first application 111 in the first electronic device 110, that is, the first application 111 can access the database in the first electronic device 110.
[0101] See also Figure 3 , the database in the first electronic device 110 includes a first database 114. The database mode of the first database 114 is a slave database mode, and the first database 114 is used to back up the data in the second database 124 in the second electronic device 120 obtained by the first electronic device 110 to the first database 114, and obtain the updated first database ( Figure 3 (not shown) so that the data in the second database 124 is the same as the data in the updated first database, so that the purpose of data synchronization between the updated first database in the first electronic device and the second database 124 in the second electronic device can be achieved.
[0102] It should be understood that the updated first database and the first database 114 are the same database, that is, the updated first database and the first database 114 have the same database identifier, the difference is that the data stored in the updated first database and the data stored in the first database 114 are not completely the same.
[0103] In some other implementations, the database in the first electronic device 110 may also include: Figure 3 The dotted line shows the database 115 . The database mode of the database 115 is a master database mode, and the database 115 is used to store the application data generated by the first application 111 .
[0104] In the embodiment of the present application, the type of the database in the first electronic device 110 is not specifically limited. In some implementations, the database in the first electronic device 110 is an embedded database embedded in the first application 111. In this implementation, the database in the first electronic device 110 can specifically be a file stored in the memory space of the first electronic device 110, and the first application 111 can access the memory space. For example, the embedded database corresponding to the first application 111 can be, but is not limited to, a lightweight database SQLite. In other implementations, the database in the first electronic device 110 is a non-embedded database. In this implementation, the database in the first electronic device 110 can specifically be a database stored in the disk space of the first electronic device 110, and the first application 111 can access the disk space. For example, the non-embedded database can be, but is not limited to, MySQL.
[0105] The first database synchronization service 112 is used to send data (eg, synchronization request) to the second electronic device 120 , and receive data (eg, data stored in the second database 124 ) sent from the second electronic device 120 to the first electronic device 110 .
[0106] The first database engine 113 is used to perform operations (eg, query operations or update operations, etc.) on the database in the first electronic device 110 .
[0107] For example, the first database engine 113 performs an update operation on the first database 114 based on the structured update statement sent by the first application 111 to obtain the updated first database. For another example, the first database engine 113 performs a query operation on the first database 114 based on the structured query statement sent by the first application 111, and returns the obtained query result to the first application 111.
[0108] Next, each module unit in the second electronic device 120 is introduced.
[0109] The second application 121 is an application program running in the second electronic device 120. When the second electronic device 120 runs the second application 121, application data of the second application 121 will be generated. There is no specific limitation on the application type of the second application 121, and it can be selected according to actual usage requirements. For example, when the second application 121 is a gallery application, the application data generated by the second application 121 can be a picture or a video. For another example, when the second application 121 is a table application, the application data generated by the second application 121 can be a two-dimensional data table.
[0110] The database in the second electronic device 120 is a local database of the second electronic device 120. The database in the second electronic device 120 is specifically a database associated with the second application 121 in the second electronic device 120, that is, the second application 121 can access the database in the second electronic device 120.
[0111] See also Figure 3 The database in the second electronic device 120 includes a second database 124. The database mode of the second database 124 is a master database mode, and the second database 124 is used to store application data generated by the second application 121.
[0112] In some other implementations, the database in the second electronic device 120 may also include a database other than the second database 124 ( Figure 3 The database other than the second database 124 may be a database associated with the second application 121, and the database mode of the database other than the second database 124 is a slave database mode. The database other than the second database 124 backs up the data in the database 115 in the first electronic device 110 acquired by the second electronic device 120 to the database other than the second database 124.
[0113] The second database synchronization service 122 is used to send data (eg, data to be synchronized in the second database 123 ) to the first electronic device 110 , and receive data (eg, synchronization request) sent by the first electronic device 110 to the second electronic device 120 .
[0114] The second database engine 123 is used to perform operations (eg, query operations or update operations) on the database in the second electronic device 120 .
[0115] For example, the second database engine 123 performs an update operation on the second database 124 based on the structured update statement sent by the second application 121 to obtain a fourth database. For another example, the second database engine 123 performs a query operation on the second database 124 based on the structured query statement sent by the second application 121, and returns the obtained query result to the second application 121.
[0116] In the embodiment of the present application, the type of the database in the second electronic device 120 is not specifically limited. In some implementations, the database in the second electronic device 120 is an embedded database embedded in the second application 121, and the second application 121 can access the data in the database corresponding to the second application 121 through an API. In this implementation, the database in the second electronic device 120 can specifically be a file stored in the memory space of the second electronic device 120, and the second application 121 can access the memory space. For example, the embedded database corresponding to the second application 121 can be, but is not limited to, a lightweight database SQLite. In other implementations, the database in the second electronic device 120 is a non-embedded database. In this implementation, the database in the second electronic device 120 can specifically be a database stored in the disk space of the second electronic device 120, and the second application 121 can access the disk space. For example, the non-embedded database can be, but is not limited to, MySQL.
[0117] In some other implementations, the first electronic device 110 may further include: Figure 3 The first database access framework 116 is shown by the dotted line. The second electronic device 120 may also include Figure 3 The second database access framework 125 is shown in dashed lines.
[0118] In the above implementation, the first application 111 in the first electronic device 110 can implement data interaction with the first database synchronization service 112 through the first database access framework 116, and the first application 111 can implement access to the database 115 through the first database access framework 116 and the first database engine 113. It should be understood that the first database synchronization service 112 can also implement data interaction with the first database engine 113 through the first database access framework 116.
[0119] In the above implementation, the second application 121 in the second electronic device 120 can implement data interaction with the second database synchronization service 123 through the second database access framework 125, and the second application 121 can implement access to the second database 124 through the second database access framework 125 and the second database engine 123. It should be understood that the second database synchronization service 123 can also implement data interaction with the second database engine 123 through the second database access framework 125.
[0120] It should be understood that the above Figure 3 The first electronic device 110 and the second electronic device 120 shown are for illustration only and do not constitute any limitation on the first electronic device and the second electronic device that execute the data synchronization method provided in the embodiment of the present application.
[0121] As mentioned above, the data synchronization method provided in the embodiment of the present application can be applied to at least two electronic devices. The software structure of the electronic device is described in detail below in conjunction with the accompanying drawings. It can be understood that the following Figure 4 The software architecture of the electronic device shown may be the software architecture of each of the at least two electronic devices that execute the data synchronization method provided in the embodiment of the present application.
[0122] For example, Figure 4 A schematic diagram of a software system of an electronic device is shown. Figure 4 , the software system adopts a layered architecture. The layered architecture divides the software into several layers, each layer has a clear role and division of labor. The layers communicate with each other through software interfaces. In some embodiments, the Android system is divided into five layers, from top to bottom, respectively, an application layer 410, an application framework layer 420, an Android runtime (Android Runtime) and a core library layer 430, a hardware abstract layer (HAL) 440, and a kernel layer 450.
[0123] The application layer 410 may include a series of application packages. For example, the application package may include applications such as camera, gallery, chat, call, map, navigation, calendar, Bluetooth, music, video, etc.
[0124] Each of the above-mentioned application programs may include more specific functional modules. For example, a gallery may include a business module and a notification module.
[0125] The above-mentioned various application programs may be used to generate application data. For example, the gallery is used to generate photo data.
[0126] The application framework layer 420 provides an application programming interface (API) and a programming framework for the applications in the application layer. The application framework layer 420 includes some predefined functions.
[0127] like Figure 4 As shown, the application framework layer 420 may include a window manager, a notification manager, an activity manager, an input manager, a view system, a content provider, a resource manager, and the like.
[0128] The window manager provides window management services (WMS). WMS can be used for window management, window animation management, surface management, and as a transfer station for the input system.
[0129] Content providers are used to store and retrieve data and make it accessible to applications. This data can include videos, images, audio, calls made and received, browsing history and bookmarks, phone books, etc.
[0130] The view system includes visual controls, such as controls for displaying text, controls for displaying images, etc. The view system can be used to build applications. A display interface can be composed of one or more views. For example, a display interface including a text notification icon can include a view for displaying text and a view for displaying images.
[0131] The resource manager provides various resources for applications, such as localized strings, icons, images, layout files, video files, and so on.
[0132] The notification manager enables applications to display notification information in the status bar. It can be used to convey notification-type messages and can disappear automatically after a short stay without user interaction. For example, the notification manager is used to notify download completion, message reminders, etc. The notification manager can also be a notification that appears in the system top status bar in the form of a chart or scroll bar text, such as notifications of applications running in the background, or a notification that appears on the screen in the form of a dialog window. For example, a text message is displayed in the status bar, a prompt sound is emitted, an electronic device vibrates, an indicator light flashes, etc.
[0133] The activity manager can provide activity management services (AMS), which can be used to start, switch, and schedule system components (such as activities, services, content providers, and broadcast receivers) as well as manage and schedule application processes.
[0134] The input manager can provide input management services (IMS), which can be used to manage system input, such as touch screen input, key input, sensor input, etc. IMS takes events from input device nodes and distributes them to appropriate windows through interaction with WMS.
[0135] Android Runtime includes core libraries and virtual machines. Android Runtime is responsible for scheduling and management of the Android system.
[0136] The core library consists of two parts: one part is the function that the programming language (for example, Java language) needs to call, and the other part is the Android core library.
[0137] The application layer 410 and the application framework layer 420 run in a virtual machine. The virtual machine executes the programming files (e.g., java files) of the application layer 410 and the application framework layer 420 as binary files. The virtual machine is used to perform functions such as object life cycle management, stack management, thread management, security and exception management, and garbage collection.
[0138] The core library layer 430 may include multiple functional modules, such as surface manager, media framework, libc, SQLite, OpenGL ES, Webkit, etc.
[0139] The surface manager is used to manage the display subsystem and provide the fusion of two-dimensional (2D) and three-dimensional (3D) layers for multiple applications.
[0140] The media framework supports playback and recording of a variety of commonly used audio and video formats, as well as static image files, etc. The media library can support a variety of audio and video encoding formats, such as: MPEG4, H.264, MP3, AAC, AMR, JPG, PNG, etc.
[0141] The hardware abstraction layer (HAL) 440 is an interface layer located between the operating system kernel and the upper software, and its purpose is to abstract the hardware. The hardware abstraction layer is an abstract interface driven by the device kernel, which is used to implement the application programming interface that provides access to the underlying device to the higher-level Java API framework. HAL contains multiple library modules, such as camera HAL, Vendor warehouse, display, Bluetooth, audio, etc. Each library module implements an interface for a specific type of hardware component. It can be understood that the camera HAL can provide an interface for the camera FWK to access hardware components such as cameras. The Vendor warehouse can provide an interface for the media FWK to access hardware components such as encoders. When the system framework layer API requires access to the hardware of the portable device, the Android operating system will load the library module for the hardware component.
[0142] The kernel layer 450 is the foundation of the Android operating system, and the final functions of the Android operating system are all completed through the kernel layer. The kernel layer may include display drivers, camera drivers, audio drivers, and sensor drivers.
[0143] It should be noted that the application provides Figure 4The software structure diagram of the electronic device shown is only an example and does not limit the specific module division in different layers of the Android operating system. For details, please refer to the introduction of the Android operating system software structure in conventional technology. In addition, the shooting method provided in this application can also be implemented based on other operating systems (for example, IOS or Hongmeng, etc.), and this application will not give examples one by one.
[0144] Next, the above Figure 4 The software architecture of the electronic device is shown as Figure 3 The software architecture of the first electronic device 110 and the second electronic device 120 shown in FIG. Figure 5 , introduce the above Figure 3 The first electronic device 110 and the second electronic device 120 include various module units and Figure 4 Shows the relationship between software architectures.
[0145] For example, see Figure 5 , the above Figure 3 The application of the first electronic device 110 , the application of the second electronic device 120 , the first database access framework 116 , the second database access framework 115 , the first database synchronization service 112 , and the second database synchronization service 122 all belong to module units in the application layer 410 .
[0146] like Figure 5 As shown, the applications of the first electronic device 110 include a file manager, a media application and a distributed gallery, wherein the applications of the first electronic device 110 are Figure 3 A specific example of the first application 111 is shown.
[0147] The first database access framework 116 is integrated into the application in the form of a binary archive file (Android Archive, AAr) package of an Android library project. Figure 5 The first database access framework 116 includes configuration management, connection management, structured query language (SQL) statement management, transaction management and synchronization management modules.
[0148] Next, the functions of each module in the first database access framework 116 are introduced. It should be understood that Figure 5 The functions of the various modules included in the second database access framework 115 shown can refer to the description of the various modules in the first database access framework 116 below.
[0149] Configuration management is used to configure the properties of the database in the first electronic device 110. For example, the configuration management is used to configure the mode of the database in the first electronic device to be a master database mode or a slave database mode.
[0150] The connection management is used to manage the connection with the first database engine 113. In practical applications, if a connection has been established between the application of the first electronic device 110 and the first database engine 113, the connection can be stored in the connection management, so that the connection can be directly obtained from the connection management next time.
[0151] The sql statement management is used to encapsulate sql statements. For example, the sql statement management is used to encapsulate sql statements for executing insert operations, delete operations, modify operations or query operations on the database in the first electronic device 110.
[0152] Transaction management is used to manage the start, end or rollback of a transaction. Rollback refers to the act of restoring a program or data to its last correct state when a program or data processing error occurs. For example, rollback includes program rollback and data rollback.
[0153] Synchronization management is used to provide an interface for the application of the first electronic device 110 to obtain the database in the first electronic device 110, and to make a callback when the synchronization data is completed. In other words, the synchronization management in the first database access framework 116 is a bridge used to connect the first database access framework 116 and the first database synchronization service 112.
[0154] The first database synchronization service 112 is mainly used for data transmission and reception across devices. The first database synchronization service 112 may be a system application package of the first electronic device 110. Figure 5 The first database synchronization service 112 includes access management, synchronization management and data transmission and reception.
[0155] Access management is the authentication and verification of application requests.
[0156] Synchronization management is to encapsulate the message requesting synchronization and parse the received files.
[0157] The data transmission and reception is used to send data to the second electronic device, or receive data from the second electronic device.
[0158] Figure 5The first database engine 113 shown belongs to the core library layer of the first electronic device 110. In some implementations, the first database engine 113 runs in an Android application package (apk) in the form of a dynamic library file (so).
[0159] See also Figure 5 The first database engine 113 includes a storage component and a database log component. Next, the functions of each module included in the first database engine 113 are introduced. It should be understood that Figure 5 The functions of the modules included in the second database engine 123 shown can refer to the description of the modules in the first database engine 113 below.
[0160] The storage component is used to store the database in the first electronic device 110. In some implementations, the database in the first electronic device 110 includes: Figure 3 In the case of the first database 114 shown, the first database 114 may be a database associated with an application of the first electronic device 110 , that is, the first database is used to store application data generated by the associated application.
[0161] The database operation log component is used to store structured query language for performing operations (eg, inserting, deleting, or rewriting operations, etc.) on the database in the first electronic device 110 .
[0162] It should be understood that Figure 5 The first database engine 113 shown may also include other modules, and the second database engine 123 may also include other modules. Among them, the other modules included in the first database engine 113 are associated with the database type of the database in the first electronic device 110 stored by the storage component in the first database engine 113. The other modules included in the second database engine 123 are associated with the database type of the database in the second electronic device 120 stored by the storage component in the second database engine 123.
[0163] The kernel layer includes the kernel layer of the first electronic device 110 and the kernel layer of the second electronic device 120 .
[0164] In the embodiment of the present application, there is no specific limitation on the operating system type of the first electronic device 110 and the operating system type of the second electronic device 120. For example, the operating system of the first electronic device 110 may be Android, and the operating system of the second electronic device 120 may be Windows. For another example, the operating system of the first electronic device 110 and the operating system of the second electronic device 120 may both be Android systems.
[0165] Next, Figure 5 The problem existing in the data synchronization process is described by taking the synchronization of data in the distributed gallery of the first electronic device 110 to the computer manager of the second electronic device 120 as an example.
[0166] It is assumed that, in the initial stage, the data in the distributed gallery of the first electronic device 110 and the data in the gallery of the computer manager in the second electronic device 120 are the same.
[0167] In the absence of abnormalities, a new picture data is inserted into the distributed gallery of the first electronic device 110, and the first electronic device 110 will execute the following process: the first electronic device 110 uses a uniform resource identifier (URI) to access the first database engine 113 through the first database access framework 116 to open the database stored in the storage component included in the first database engine 113. Among them, the URI includes the name of the database stored in the storage component included in the first database engine 113, and the information that the database is the main database. In the case of opening the database stored in the storage component in the first database engine 113, the first electronic device 110 uses the first database access framework 116 to execute SQL statement 1, wherein SQL statement 1 can be expressed as "insert into test values ("t1");". In the case of the first database access framework 116 successfully executing SQL statement 1, a new data t1 will be added to the table test in the database file stored by the storage component in the first database engine 113, and an SQL statement 1 will be added to the binary log file stored in the database operation log component included in the first database engine 113. After executing the above steps, the data corresponding to the SQL statement 1 is successfully written into the database file of the first electronic device 110, and the SQL statement 1 is successfully written into the binary log file.
[0168] It should be noted that each SQL statement stored in the binary log file is also called an event (or transaction). Among them, each SQL statement stored in the binary log file can be a write event for performing an update operation on the database, and the write event for performing an update operation on the database can be, but not limited to, an insert event, an update event, or a delete event. It should also be understood that the binary log file is not used to record events for performing read operations on the database. For example, the event for performing a read operation on the database can be, but not limited to, a query (select) event.
[0169] Next, when the second electronic device 120 needs to synchronize the data in the distributed gallery of the first electronic device 110 to the gallery of the computer manager, the following interactive steps can be used to achieve this: First, the second electronic device 120 sends information such as the current synchronization position of the computer manager in the second electronic device 120 to the first electronic device 110 through the second database synchronization service 122. Next, when the first database synchronization service 112 of the first electronic device 110 receives the message requesting synchronization sent by the second database synchronization service 122, the first database synchronization service 112 sends the synchronization position requested by the computer manager to the first database access framework 116. Thereafter, the first database engine 113 exports the newly added SQL "insert into test values("t1");" to the incremental synchronization file according to the requested synchronization position, and sends the incremental synchronization file to the second electronic device 120 through the first database access framework 116 and the first database synchronization service 112. The second database engine 123 in the second electronic device 120 executes the SQL "insert into testvalues("t1")" in the incremental synchronization file, so that the data t1 indicated by the SQL statement can be written into the computer manager, thereby ensuring that the data in the distributed gallery of the first electronic device 110 and the data in the computer manager of the second electronic device 120 are the same.
[0170] However, during the above data synchronization process, if any of the following situations occurs, the data in the distributed gallery of the first electronic device 110 and the data in the computer manager of the second electronic device 120 will be inconsistent.
[0171] Case 1:
[0172] In the first electronic device 110, the data corresponding to SQL statement 1 is successfully written into the database stored in the storage component included in the first database engine 113, and the SQL statement 1 is failed to be written into the binary log file stored in the database operation log component included in the first database engine 113. In this way, the computer manager in the second electronic device 120 cannot execute the newly added SQL statement 1, resulting in inconsistency between the data in the distributed gallery of the first electronic device 110 and the data in the computer manager of the second electronic device 120.
[0173] Case 2:
[0174] In the first electronic device 110, the data corresponding to SQL statement 1 is successfully written into the database stored in the storage component included in the first database engine 113. Before the SQL statement 1 is written into the binary log file stored in the database operation log component included in the first database engine 113, the first electronic device 110 loses power, causing the first electronic device 110 to restart. After the first electronic device 110 loses power and restarts,
[0175] After the computer manager of the second electronic device 120 requests synchronization with the first electronic device 110 , the computer manager of the second electronic device 120 cannot execute the newly added SQL statement 1 , resulting in inconsistency between the data in the distributed gallery of the first electronic device 110 and the data in the computer manager of the second electronic device 120 .
[0176] Case 3:
[0177] In the first electronic device 110, the data corresponding to SQL statement 1 is successfully written into the database stored in the storage component included in the first database engine 113, and the SQL statement 1 is successfully written into the binary log file stored in the database operation log component included in the first database engine 113. In the case of the above successful writing (i.e., after the disk is written), the SQL statement 1 in the binary log file stored in the database operation log component included in the first database engine 113 is jumped or artificially tampered, or there is an abnormality in the sending process of the incremental synchronization file from the first electronic device 110 to the second electronic device 120, resulting in the computer manager of the second electronic device 120 requesting the synchronous execution of the newly added SQL statement 1 from "insert intotest values("t1");" to "insert into test values("t2");" or other different strings, thereby resulting in inconsistency between the data in the distributed gallery of the first electronic device 110 and the data in the computer manager of the second electronic device 120.
[0178] In order to solve the problems existing in the conventional technology, the present application provides a data synchronization method, device and equipment. Next, the data synchronization method provided by the embodiment of the present application is described in detail with reference to the accompanying drawings.
[0179] Figure 6 is a schematic diagram of a data synchronization method provided in an embodiment of the present application. The data synchronization method provided in an embodiment of the present application can be performed by a first electronic device and a second electronic device. It is understandable that any one of the first electronic device and the second electronic device can be implemented as software, or a combination of software and hardware. Exemplarily, the first electronic device in the embodiment of the present application can be Figure 1 The electronic device 110 shown, the second electronic device may be Figure 1 The electronic device 120 is shown. Figure 6 As shown, the data synchronization method provided in the embodiment of the present application includes S610 to S640. S610 to S640 are described in detail below.
[0180] S610, the second electronic device sends a synchronization request to the first electronic device, the synchronization request is used to request to back up the first data in the first database in the first electronic device to the second database in the second electronic device, and the synchronization request includes a second version number corresponding to the second database. Correspondingly, the first electronic device receives the synchronization request from the second electronic device.
[0181] In the above step S610, the synchronization request is used to request to back up the first data in the first database in the first electronic device to the second database in the second electronic device. In this data synchronization mode, the first database is the master database, the second database is the slave database, the first electronic device can be called the master device for data synchronization, and the second electronic device can be called the slave device for data synchronization. The first data requested by the synchronization request can be part of the data or all of the data in the first database, which is not specifically limited in the embodiments of the present application.
[0182] The database types of the first database and the second database are not specifically limited. In some implementations, the first database is associated with a first application in a first electronic device, and the second database is associated with a second application in a second electronic device. In this implementation, the first database and the second database may be embedded databases. For example, the first database and the second database may be lightweight databases such as SQLite databases. In other implementations, the first database and the second database may also be non-embedded databases. For example, the first database and the second database may be MySQL databases.
[0183] S620, when the second version number of the first electronic device is different from the first version number corresponding to the first database, the first electronic device determines a synchronization file according to the first database, the synchronization file includes first data and a first version number, the first data is data associated with the first transaction, the first version number is a version number different from the third version number corresponding to the third database determined when the first electronic device successfully updates the third database in the first electronic device according to the first transaction and fails to update the log of the third database according to the first transaction, the log of the third database matches the third database, the first database is a database obtained after successfully executing the first transaction on the third database and updating the third version number to the first version number, the third version number is the same as the second version number, and the data in the third database is the same as the data in the second database.
[0184] In the initial stage, the first electronic device includes a third database and a log of the third database, wherein the third database and the log of the third database match, that is, the data operation statements recorded in the log of the third database can correctly reflect the data in the third database. In other words, the data operation statements recorded in the log of the third database are used to generate data in the third database. For example, the data in the third database includes 1 and 2, and the log of the third database includes SQL statement 1 and SQL statement 2, SQL statement 1 is used to generate 1, and SQL statement 2 is used to generate 2. Based on this, it can be considered that the logs of the first database and the third database are mismatched.
[0185] It should be understood that the data in a database described in the embodiments of the present application refers to application data generated by an application stored in the database. In some possible implementations, the third database may include a header page and a data page, wherein the header page is used to store attribute information related to the third database (for example, database type, serial number or version number, etc.), and the data page is used to store application data generated by an application associated with the third database.
[0186] It should be understood that in the embodiments of the present application, the version number, serial number or transaction identifier in a database is the attribute information associated with the database, and these attribute information are not the application data generated by the application stored in the database. The version number and serial number in the log of a database are the attribute information associated with the log of the database, and these attribute information are not the data operation statements stored in the log of the database.
[0187] In the above initial stage, the data in the second database in the second electronic device is obtained by backing up the data in the third database in the first electronic device, and the data in the second database is the same as the data in the third database. Based on this, the third version number corresponding to the third database can be set to be the same as the second version number corresponding to the second database. In the embodiment of the present application, the method for synchronizing the data in the third database in the first electronic device to the data in the second database in the second electronic device is not specifically limited, but when the third database in the first electronic device matches the log of the third database, and the second database is obtained by backing up the third database, it is necessary to set the third version number corresponding to the third database to be the same as the second version number corresponding to the second database.
[0188] In the stage after the initial stage, the first electronic device receives a new write transaction (i.e., the first transaction). When the first electronic device obtains the first transaction, the first electronic device needs to perform an update operation on the third database and the log of the third database according to the first transaction. In actual applications, in order to ensure that the database and the log of the database match, the third database is usually updated successfully according to the first transaction to obtain the updated third database, and then the log of the third database is updated according to the first transaction. Based on the above update rules, after the first electronic device successfully updates the third database according to the first transaction, there is a situation where the log update of the third database fails when the log of the third database is updated according to the first transaction. In this way, the database obtained after the successful update and the log that was not successfully updated (i.e., the log of the third database) do not match. Furthermore, when data synchronization is implemented according to a log that does not match the updated database, it will cause the problem of inconsistency between the data in the master database and the data in the slave database.
[0189] To solve the above problem, the embodiment of the present application provides a data synchronization method. Below, based on the above update rule, the process of the first electronic device updating the third database and the log of the third database according to the first transaction provided by the embodiment of the present application is introduced.
[0190] Before the first electronic device executes the above-mentioned step S610, that is, before the first electronic device receives the synchronization request from the second electronic device, the method executed by the first electronic device also includes: updating the third database according to the first transaction to obtain a fifth database; in the case that the log update of the third database according to the first transaction fails, deleting all data operation statements in the log of the third database, and updating the third version number in the log of the third database to the first version number, obtaining the log of the first database, and all data operation statements are used to generate data in the third database; updating the third version number in the fifth database to the first version number, obtaining the first database, and the first version number corresponding to the first database is the first version number in the log of the first database or the first version number in the first database.
[0191] In the above steps, the first electronic device fails to update the log of the third database according to the first transaction, that is, the first data operation statement of the first transaction fails to be successfully written to the log of the third database. Exemplarily, the first transaction can be any write transaction such as deletion, update or insertion.
[0192] In the above steps, the first version number and the third version number are two different version numbers, and the presentation form of the first version number and the third version number is not specifically limited and can be set according to actual needs. In some implementations, the first version number and the third version number can be random numbers, for example, the third version number is 1 and the first version number is 2.
[0193] It should be understood that the fifth database and the third database are databases with the same database identifier, the difference being that the two databases are databases included in the first electronic device at different times, and the data stored in the two databases are not completely the same.
[0194] In the above implementation, the first electronic device obtains the fifth database by executing the first transaction on the third database, and the log update of the third database according to the first transaction fails. In this implementation, the first electronic device includes the logs of the fifth database and the third database, wherein the log of the third database is the log of the fifth database, and the log of the fifth database does not match the fifth database. For example, the data in the fifth database includes 1 and 2, and the log of the third database only includes data operation statement 1, and data operation statement 1 is used to generate 1. Based on this, it can be considered that the logs of the fifth database and the third database do not match. That is to say, in the above scenario where the database update is successful and the log update fails, the data operation statement in the log of the third database, which is the log of the fifth database, cannot correctly reflect the data in the fifth database.
[0195] In the above implementation, when the first electronic device successfully updates the third database according to the first transaction to obtain the fifth database, and fails to update the log of the third database, resulting in the mismatch between the log of the fifth database and the log of the third database in the first electronic device, the first electronic device deletes all data operation statements in the log of the third database, and updates the third version number of the fifth database and the third version number of the log of the updated third database to the first version number. The first version number is a new version number randomly generated by the first electronic device.
[0196] It should be understood that after the first electronic device executes the above steps, the first electronic device includes the logs of the first database and the updated third database, wherein the log of the updated third database is also called the log of the first database, wherein the log of the first database does not record any data operation statements. In other words, the content recorded in the log of the first database cannot correctly reflect the data in the first database.
[0197] In the embodiment of the present application, when the first electronic device successfully updates the third database according to the first transaction to obtain the fifth database, and when the log of the third database is updated according to the first transaction, the first electronic device may lose power. In this scenario, when the first electronic device is powered on again after losing power, the first electronic device cannot know whether the log of the third database is successfully updated according to the first transaction.
[0198] Next, a method for determining that a log update to a third database according to a first transaction has failed when a first electronic device successfully updates a third database according to a first transaction to obtain a fifth database, provided by an embodiment of the present application, is introduced.
[0199] In some implementations, the log of the third database and the third database also record the second transaction identifier of the second transaction, all data operation statements include the data operation statements of the second transaction, and the data in the third database include data determined according to the data operation statements of the second transaction; and, updating the third database according to the first transaction to obtain the fifth database includes: updating the data in the third database according to the first transaction, and updating the second transaction identifier according to the first transaction to obtain the fifth database including the first transaction identifier; and, in the case that the update of the log of the third database according to the first transaction fails, deleting all data operation statements in the log of the third database, and updating the third version number in the log of the third database to the first version number, and obtaining the log of the first database, including: in the process of updating the log of the third database according to the first transaction, when the first electronic device is powered off and then powered on again, determining that the first transaction identifier in the fifth database is different from the second transaction identifier in the log of the third database; in the case that the first transaction identifier in the fifth database is different from the second transaction identifier in the log of the third database, deleting all data operation statements in the log of the third database, and updating the third version number in the log of the third database to the first version number, and obtaining the log of the first database.
[0200] In the above steps, the first electronic device updates the data in the third database according to the first transaction, and updates the second transaction identifier according to the first transaction identifier of the first transaction, and obtains the fifth database including the first transaction identifier. Exemplarily, it includes: the first electronic device updates the data in the third database according to the first transaction, obtains the updated third database, and the updated third database records the second transaction identifier; the first electronic device updates the second transaction identifier recorded in the updated third database to the first transaction identifier, and obtains the fifth database including the first transaction identifier. Among them, the data in the fifth database is determined according to the first transaction and the data in the third database. For example, the data in the third database is 1, and the first transaction is to write 2 to the third database. Based on this, after the third database is updated according to the first transaction, the data in the third database includes 1 and 2. For another example, the data in the third database includes 1 and 2, and the first transaction is to modify 2 in the third database to 3. Based on this, after the third database is updated according to the first transaction, the data in the third database includes 1 and 3.
[0201] In the above steps, the first transaction identifier is used to represent the first transaction, and the second transaction identifier is used to represent the second transaction, wherein the first transaction identifier and the second transaction identifier are two different transaction identifiers. There is no specific limitation on the presentation form of the first transaction identifier and the second transaction identifier, and they can be set according to actual application scenarios and actual needs. For example, the second transaction identifier can be t0, and the first transaction identifier can be t1.
[0202] In the above steps, the location of the transaction identifier in the database and the log of the database is not specifically limited. In some implementations, the database and the log of the database include a header page, the transaction field included in the header page of the database is used to store the transaction identifier, and the transaction field included in the header page of the log of the database is used to store the transaction identifier.
[0203] In the above implementation, a transaction identifier for representing a write transaction is added to the database and the log of the database. In this way, when the first electronic device successfully updates the database according to the write transaction (i.e., the first transaction) and the power is cut off and then powered on again when the log of the database is updated according to the write transaction, it can be determined whether the log of the database is successfully updated according to the transaction identifier in the updated database and the transaction identifier in the log of the database based on the write transaction.
[0204] In the above implementation, after the first electronic device successfully updates the third database according to the first transaction to obtain the updated third database, it updates the second transaction identifier in the updated third database according to the first transaction identifier of the first transaction. In this way, it can be ensured that the first transaction identifier in the obtained third database matches the latest first write transaction executed on the third database.
[0205] In an embodiment of the present application, the first electronic device executes the above S620, that is, the first electronic device determines the synchronization file according to the first database when the second version number is different from the first version number corresponding to the first database. Based on the above version number update rule provided in the embodiment of the present application, the second version number of the second database carried in the synchronization request is different from the first version number corresponding to the first database. It can be understood that the first database in the current first electronic device and the log of the first database do not match, that is, the data operation statements recorded in the log of the first database cannot correctly reflect the data in the first database, that is, the synchronization file determined according to the log of the first database is unreliable. Based on this, in order to ensure the consistency of the data of the master database and the slave database, the first electronic device needs to determine the data included in the synchronization file according to the first database.
[0206] In some implementations, the synchronization file in the above step S620 may include first data, which is data associated with the first transaction. Optionally, the synchronization file may also include a first version number and a second sequence number, wherein the second sequence number is used to indicate the location of the data requested to be synchronized to the first database by the synchronization request.
[0207] In some other implementations, the synchronization file in the above step S620 may include the first database, wherein the first version number and the second sequence number are also recorded in the first database, wherein the second sequence number is used to indicate the location of the data requested to be synchronized to the first database by the synchronization request. In this implementation, the synchronization file includes all the data in the first database, and this data synchronization method is also called full data synchronization.
[0208] In an embodiment of the present application, the first version number corresponding to the first database is a version number stored in the first electronic device, and the location where the first version number is stored is not specifically limited. In some implementations, the first version number can be stored in the first database and the log of the first database. Specifically, the first version number can be stored in the version number field of the header page of the first database, and the first version number can be stored in the version number field of the header page of the log of the first database.
[0209] In an embodiment of the present application, the second database in the second electronic device is a database obtained by synchronizing the data of the third database in the first electronic device. Optionally, after the second electronic device synchronizes the data of the third database to the second electronic device to obtain the second database, the third database and the log of the third database will both record a first serial number, and the first serial number is used to indicate the location of the data synchronized to the third database when performing this data synchronization. Thereafter, the first electronic device executes the first transaction on the third database to obtain the first database, and the first serial number is also recorded in the first database. The log of the above-mentioned first database is obtained by performing an operation on the log of the third database including the first serial number, and the first serial number is also recorded in the log of the first database. Based on this, before the first electronic device sends a synchronization file to the second electronic device, the first electronic device can also perform the following steps: the first electronic device updates the first serial number in the first database to the second serial number; the first electronic device updates the first serial number in the log of the first database to the second serial number.
[0210] In the above, the third database, the log of the third database, the fifth database, the first database, and the log of the first database in the first electronic device are involved. Next, taking each database including a version number, a serial number, and a transaction identifier, and each database log including a version number, a serial number, and a transaction identifier as an example, the format of the third database, the log of the third database, the fifth database, the first database, and the log of the first database provided in the embodiment of the present application is described with reference to the accompanying drawings. It should be understood that in the following, Figures 7 to 11 The format of the database or the log of the database shown is for illustration only and does not constitute any limitation on the format of the database or the log of the database in the embodiments of the present application. Figures 7 to 11 The database header and the database log header shown may not include the transaction field. Figures 7 to 11 The header of the database and the header of the log of the database shown may also include other attribute fields. For example, the other attribute fields in the header of the database may be fields indicating the database type, and the database type may be a master database type or a slave database type.
[0211] The third database mentioned above can be Figure 7 The third database shown. Figure 7 The third database includes a header page and a data page, wherein the header page includes a version number field for storing a third version number, a transaction field for storing a second transaction identifier, and a serial number field for storing a first serial number, and the data page includes multiple pages, which are used to store application data generated by an application of the first electronic device (for example, the first application).
[0212] The log of the third database in the above can be as follows Figure 8 The log of the third database is shown. Figure 8 ,The log of the third database includes the header, index page and data page.
[0213] Figure 8 The header of the log of the third database shown is used to store a version number field, a transaction field and a sequence number field, wherein the value of the version number field is the third version number, the value of the transaction field is the second transaction identifier, and the value of the synchronization sequence number field is the first sequence number. The index page of the log of the third database records multiple lines of data, and each line of data in the multiple lines of data records the following fields: the sequence number of the index page, the page number of the data page, and the statement length associated with the sequence number. Among them, the sequence number of the index page increases in order from small to large. The page number of the data page represents the page number in the data page of the log of the third database. The statement length associated with the sequence number refers to the length of the SQL statement in the data page associated with the sequence number of the index page, and the unit of the statement length associated with the sequence number can be bytes. See Figure 8 , when the sequence number is 1, the sequence number is associated with SQL statement 1 in page 0, that is, xxx represents the length of SQL statement 1. When the sequence number is 2, the sequence number is associated with SQL statement 2 in page 1 and SQL statement 3 in page 2, that is, the statement length represented by yyy includes the length of SQL statement 2 and the length of SQL statement 3. When the sequence number is 3, the sequence number is associated with SQL statement 4 in page 3, that is, nnn represents the length of SQL statement 4.
[0214] Optional, Figure 8 The index page of the log of the third database shown can also record Figure 8 The dashed line shows the synchronization state. Figure 8 The value of the synchronization status shown by the dotted line indicates whether the data indicated by the page number of the data page of the corresponding row is synchronized, wherein the value of the synchronization status is "1" indicating that the data indicated by the page number of the data page of the corresponding row has been synchronized, and the value of the synchronization status is "0" indicating that the data indicated by the page number of the data page of the corresponding row has not been synchronized. In other words, the data in any data page with a page number of 0 to 3 has been synchronized to the second database. The data page in the log of the third database is used to store the SQL statement for generating the data in the data page of the third database, wherein the type of the SQL statement can be insert, delete or update.
[0215] Optional, Figure 8 The index page of the log of the third database shown can also record Figure 8 The dotted line shows the check code. Figure 8 The check code 1 shown by the dotted line is the check code generated based on SQL statement 1 in the data page corresponding to the serial number 1 of the index page, the check code 2 is the check code generated based on SQL statement 2 and SQL statement 3 in the data page corresponding to the serial number 2 of the index page, and the check code 3 is the check code generated based on SQL statement 4 in the data page corresponding to the serial number 3 of the index page.
[0216] The fifth database mentioned above can be Fig. 9 The fifth database shown. Fig. 9 The fifth database includes a header page and a data page, wherein the header page includes a version number field for storing a third version number, a transaction field for storing a first transaction identifier, and a serial number field for storing a first serial number, and the data page includes multiple pages, and the multiple pages are used to store application data generated by an application of the first electronic device (for example, a first application), and the data stored in the multiple pages include data determined according to the first transaction. It can be understood that the first transaction in this implementation can be a transaction for inserting new data into the third database.
[0217] The first database mentioned above can be Fig.10 The first database shown. Fig.10 The first database includes a header page and a data page, wherein the header page includes a version number field for storing a first version number, a transaction field for storing a first transaction identifier, and a serial number field for storing a first serial number, and the data page includes multiple pages, and the multiple pages are used to store application data generated by an application of the first electronic device (for example, a first application), and the data stored in the multiple pages include data determined according to the first transaction. It can be understood that the first transaction in this implementation can be a transaction for inserting new data into the third database.
[0218] The log of the first database above can be as follows Fig.11 The log of the first database is shown. Fig.11 , the log of the first database includes a header page, an index page, and a data page. The header of the log of the first database is used to store a version number field, a transaction field, and a sequence number field, wherein the value of the version number field is the first version number, the value of the transaction field is the first transaction identifier, and the value of the synchronization sequence number field is the first sequence number. The content stored in the index page of the log of the first database is the same as Figure 8 The contents stored in the index page in the log of the third database are the same, and the contents not described in detail here can be referred to the relevant description above. The contents in the data page in the log of the first database are empty, that is, no data operation statements are stored.
[0219] It should be noted that, in the embodiment of the present application, when the capacity of the data stored in the log of the database in the first electronic device is greater than the preset capacity, the log of the database needs to be reset to obtain a log of the regenerated database, wherein the capacity of the log of the regenerated database is greater than the preset capacity, the version number recorded in the log of the regenerated database has been updated to a new version number, and no data operation statements are recorded in the log of the regenerated database. It should be understood that when the version number recorded in the log of the regenerated database has been updated to a new version number, at the same time, the version number recorded in the database associated with the log of the regenerated database will also be updated according to the version number recorded in the log of the regenerated database.
[0220] S630: The first electronic device sends a synchronization file to the second electronic device, so that the second electronic device updates the second database using the synchronization file to obtain a fourth database. The second electronic device receives the synchronization file from the first electronic device.
[0221] In the above-mentioned step S630, the first electronic device sends a synchronization file to the second electronic device, which can be understood as the first electronic device encapsulating the data of the first database based on a universal transmission protocol, and sending the encapsulated file to the second electronic device. In some implementations, when the data stored in the first database is data generated by the first application in the first electronic device, the above-mentioned universal transmission protocol can also be used to transmit application data generated by multiple applications different from the first application, and the data formats of the application data generated by different applications are different. In this implementation, the first electronic device does not need to encapsulate the data generated by the first application (i.e., the data in the first database) in a specific protocol format (i.e., the format of the transmission protocol corresponding to the application type), so that the versatility of the data synchronization method provided in the embodiment of the present application and the efficiency of data synchronization can be guaranteed.
[0222] S640, the second electronic device updates the second database according to the synchronization file to obtain a fourth database, the data in the fourth database is the same as the data in the first database, and the version number corresponding to the fourth database is the first version number.
[0223] In the embodiment of the present application, after executing the above S610 to S630, the synchronization file obtained by the second electronic device includes the first data and the first version number. Based on this, the second electronic device updates the second database according to the synchronization file to obtain the fourth database, including: the second electronic device updates the data in the second database according to the first data, and updates the second version number in the second database according to the first version number to obtain the fourth database.
[0224] In an embodiment of the present application, the second database in the second electronic device is a database obtained by synchronizing the data of the third database in the first electronic device. Optionally, after the second electronic device synchronizes the data of the third database to the second electronic device to obtain the second database, the second database, the third database, and the log of the third database will all record a first serial number, and the first serial number is used to indicate the location of the data synchronized to the third database when performing this data synchronization. Thereafter, the first electronic device executes the first transaction on the third database to obtain the first database, and the first serial number is also recorded in the first database. The log of the above-mentioned first database is obtained by performing an operation on the log of the third database including the first serial number, and the first serial number is also recorded in the log of the first database. Based on this, after the second electronic device executes the above-mentioned S640, the second electronic device can also perform the following steps: update the first serial number in the fourth database to the second serial number.
[0225] Exemplarily, the second database mentioned above can be as follows Fig.12 The second database shown in (1) Fig.12The second database shown in (1) is Figure 7 The third database shown is the same as that shown in FIG. 1 , and the contents not described in detail here can be referred to the relevant description above. The fourth database above can be as follows Fig.12 The fourth database shown in (2) in Fig.12 The fourth database shown in (2) includes a header page and a data page, wherein the header page includes a version number field for storing a first version number, a transaction field for storing a first transaction identifier, and a serial number field for storing a first serial number, and the data page includes multiple pages, and the multiple pages are used to store application data generated by an application of the first electronic device (for example, the first application), and the data stored in the multiple pages include data determined according to the first transaction. It can be understood that the first transaction in this implementation can be a transaction for inserting new data into the third database.
[0226] It should be understood that the above Figure 6 The data synchronization method shown is for illustration only and does not constitute any limitation to the data synchronization method provided in this application.
[0227] In the embodiment of the present application, the version number and transaction identifier are added to the database and database log in the first electronic device, and the version number is added to the database in the second electronic device. This modification method is simple and easy to implement. When the first electronic device determines that the first version number corresponding to the first database is different from the second version number corresponding to the second database in the synchronization request, that is, the logs of the first database and the third database included in the first electronic device do not match (that is, the data operation statements recorded in the log of the third database cannot correctly reflect the data in the first database). When the first electronic device determines that the first version number corresponding to the first database is different from the second version number in the synchronization request, the first electronic device determines the synchronization file according to the first database. Thereafter, the first electronic device sends the synchronization file determined according to the first database to the second electronic device, so that the second electronic device updates the second database using the synchronization file to obtain the fourth database, thereby maintaining the data consistency of the first database as the master database and the fourth database as the slave database as much as possible. In summary, the data synchronization method provided in the embodiment of the present application maintains the data consistency of the master database and the slave database as much as possible. In addition, in the above-mentioned data synchronization process, only the first electronic device and the second electronic device are involved, that is, the data synchronization method provided in the embodiment of the present application is applicable to the end-to-end data synchronization scenario of electronic devices, that is, the method can maintain the data consistency of the master database and the slave database in the end-to-end scenario as much as possible.
[0228] Above, combined Figures 6 to 12 The interaction steps between the first electronic device and the second electronic device involved in the data synchronization method provided by the present application are described. Fig.13 The data synchronization method shown is combined with the accompanying drawings to introduce the internal interaction steps of the first electronic device and the internal interaction steps of the second electronic device provided by the present application.
[0229] Fig.13 is a schematic diagram of another data synchronization method provided in an embodiment of the present application. It should be understood that Fig.13 The data synchronization method shown is as follows Figure 6 A specific embodiment of the data synchronization method is shown. Fig.13 The data synchronization method provided in the embodiment of the present application is performed by a first electronic device and a second electronic device, wherein the first electronic device includes a first application, a first database engine and a first synchronization service, and the second electronic device includes a second synchronization service, a second database engine and a second application. Exemplary, Fig.13 The first electronic device shown may be Figure 3 The first electronic device 110 is shown. Fig.13 The second electronic device shown may be Figure 3 The second electronic device 120 is shown. It should be understood that Fig.13 The first synchronization service shown is Figure 3 The first database synchronization service 112 is shown. Fig.13 The second synchronization service shown is Figure 3 A second database synchronization service 122 is shown.
[0230] like Fig.13 As shown, the data synchronization method includes S1301 to S1309. S1301 to S1309 are described in detail below.
[0231] S1301: A second application of a second electronic device sends a synchronization request to a second synchronization service. Correspondingly, the second synchronization service receives the synchronization request from the second application.
[0232] In the embodiment of the present application, there is no specific limitation on the sending method of the second application to send the synchronization request to the second synchronization service. In some implementations, the second application can directly send the synchronization request to the second synchronization service. In other implementations, the second electronic device can also include a second database access framework, based on which the second application can send the synchronization request to the second synchronization service through the second database access framework.
[0233] The synchronization request in the above step S1301 is the same as the synchronization request in the above step S610. For details not described in detail here, please refer to the relevant description in the above step S610. It can be understood that the difference between the above step S1301 and the above step S610 is that the execution subject is different.
[0234] S1302: The second synchronization service of the second electronic device sends a synchronization request to the first synchronization service of the first electronic device. Correspondingly, the first synchronization service receives the synchronization request from the second synchronization service.
[0235] S1303: The first synchronization service of the first electronic device sends a synchronization request to the first database engine of the first electronic device. Correspondingly, the first database engine receives the synchronization request from the first synchronization service.
[0236] In the embodiment of the present application, there is no specific limitation on the sending method of the first synchronization service to send the synchronization request to the first database engine of the first electronic device. In some implementations, the first synchronization service can directly send the synchronization request to the first database engine. In other implementations, the first electronic device can also include a first database access framework, based on which the first synchronization service can send the synchronization request to the first database engine through the first database access framework.
[0237] S1304: When the second version number is different from the first version number corresponding to the first database, the first database engine of the first electronic device determines a synchronization file according to the first database.
[0238] The method in which the first database engine in the above step S1304 determines the synchronization file according to the first database when the second version number is different from the first version number corresponding to the first database is the same as the method in which the first electronic device in the above step S620 determines the synchronization file according to the first database when the second version number is different from the first version number corresponding to the first database. For the contents not described in detail here, please refer to the relevant description in the above step S620.
[0239] S1305: The first database engine of the first electronic device sends a synchronization file to the first synchronization service of the first electronic device. Correspondingly, the first synchronization service receives the synchronization file from the first database engine.
[0240] In the embodiment of the present application, there is no specific limitation on the sending method of the first database engine to send the synchronization request to the first synchronization service. In some implementations, the first database engine can directly send the synchronization request to the first synchronization service. In other implementations, the first electronic device can also include a first database access framework, based on which the first database engine can send the synchronization request to the first synchronization service through the first database access framework.
[0241] S1306: The first synchronization service of the first electronic device sends a synchronization file to the second synchronization service of the second electronic device. Correspondingly, the second synchronization service receives the synchronization file from the first synchronization service.
[0242] S1307: The second synchronization service of the second electronic device sends a synchronization file to the second database engine of the second electronic device. Correspondingly, the second database engine receives the synchronization file from the second synchronization service.
[0243] In the embodiment of the present application, there is no specific limitation on the sending method of the second synchronization service to send the synchronization request to the second database engine. In some implementations, the second synchronization service can directly send the synchronization request to the second database engine. In other implementations, the second electronic device can also include a second database access framework, based on which the second synchronization service can send the synchronization request to the second database engine through the second database access framework.
[0244] S1308, when the synchronization file includes the first data and the first version number corresponding to the first database, the second database engine of the second electronic device updates the data in the second database according to the first data, and updates the second version number in the second database according to the first version number, to obtain a fourth database.
[0245] In the above step S1308, the data in the fourth database is the same as the data in the first database, and the version number corresponding to the fourth database is the first version number.
[0246] The execution principle of the above step S1308 is the same as that of the above step S640. For the contents not described in detail here, please refer to the relevant description in the above step S640.
[0247] It should be understood that the above Fig.13 The data synchronization method provided in the embodiment of the present application is only for illustration and does not constitute any limitation to the embodiment of the present application.
[0248] Fig.14 It is a schematic diagram of another data synchronization method provided in an embodiment of the present application. It should be understood that Fig.14 The data synchronization method shown above is Figure 6 A specific embodiment of the data synchronization method is shown. Fig.14 The data synchronization method provided in the embodiment of the present application is performed by a first electronic device and a second electronic device, wherein the first electronic device includes a first application, a first database engine and a first synchronization service, and the second electronic device includes a second synchronization service, a second database engine and a second application. Exemplary, Fig.14 The first electronic device shown may be Figure 3 The first electronic device 110 is shown. Fig.14 The second electronic device shown may be Figure 3 The second electronic device 120 is shown. It should be understood that Fig.14 The first synchronization service shown is Figure 3The first database synchronization service 112 is shown. Fig.14 The second synchronization service shown is Figure 3 A second database synchronization service 122 is shown.
[0249] In the embodiment of the present application, in the initial stage, the first electronic device includes a third database and a log of the third database, and the third database and the log of the third database match. Specifically, the header of the third database includes a third version number and a first sequence number, the data page of the third database includes data associated with the second transaction, the header of the log of the third database includes a second version number and a first sequence number, and the data page of the third database includes a data operation statement of the second transaction.
[0250] In the embodiment of the present application, in the initial stage, the second electronic device includes a second database. The header of the second database includes a second version number and a first sequence number, the data page of the second database includes data associated with the second transaction, and the second database is a database obtained by backing up the third database.
[0251] like Fig.14 As shown, the data synchronization method includes S1401 to S1412. S1401 to S1412 are described in detail below.
[0252] S1401: A first application of a first electronic device sends a first transaction to a first database engine of the first electronic device. Correspondingly, the first database engine receives the first transaction from the first application.
[0253] The first transaction in the above step S1401 is the same as the first transaction in the above step S620. For the contents not described in detail here, please refer to the relevant description in the above step S620.
[0254] S1402, the first database engine of the first electronic device updates the data page of the third database according to the first transaction to obtain the fifth database, the header of the fifth database records the third version number and the first serial number, and the data in the data page of the fifth database is determined based on the first transaction and the data of the data page of the third database.
[0255] For the contents not described in detail in the above step S1402, please refer to the relevant description in the above step S620.
[0256] S1403, when the first database engine of the first electronic device successfully updates the third database according to the first transaction and fails to update the log of the third database according to the first transaction, the first database engine performs the following operations: delete all data operation statements in the log of the third database, and update the third version number in the log of the third database to the first version number to obtain the log of the first database; and update the third version number in the fifth database to the first version number to obtain the first database.
[0257] In an embodiment of the present application, the scenario after the first database engine of the first electronic device executes the above S1402 and S1403 is: the first database engine successfully updates the third database according to the first transaction, and fails to update the log of the third database according to the first transaction.
[0258] Optionally, before the first electronic device executes the above S1403, the second electronic device may also randomly generate a new first version number.
[0259] For the contents not described in detail in the above step S1403, please refer to the relevant description in the above step S620.
[0260] S1404: The second application of the second electronic device sends a synchronization request to the second synchronization service of the second electronic device. Correspondingly, the second synchronization service receives the synchronization file from the second application.
[0261] S1405: The second synchronization service of the second electronic device sends a synchronization request to the first synchronization service of the first electronic device. Correspondingly, the first synchronization service receives the synchronization request from the second synchronization service.
[0262] S1406: The first synchronization service of the first electronic device sends a synchronization request to the first database engine of the first electronic device. Correspondingly, the first database engine receives the synchronization file from the first synchronization service.
[0263] S1407: When determining that the second version number in the synchronization request and the first version number in the log of the first database are different, the first database engine of the first electronic device generates a synchronization file according to the first database, where the synchronization file includes the first database.
[0264] In the above step S1407, the synchronization file includes a first database, wherein the header of the first database includes a first version number and a second sequence number, and the data included in the data page of the first database is determined according to the first transaction and the data included in the data page of the third database.
[0265] The synchronization file generated after the first electronic device executes the above S1407 includes the first database, which is a full data synchronization process. That is to say, in the embodiment of the present application, in the scenario where the first database engine successfully updates the third database according to the second transaction, and the log update of the third database according to the second transaction fails, the synchronization file sent by the first electronic device to the second electronic device includes the first database, so that the second electronic device updates the second database according to the first database, which is a full data synchronization process.
[0266] S1408: The first database engine of the first electronic device updates the first serial number in the first database and the log of the first database to the second serial number.
[0267] In the embodiment of the present application, after the first electronic device generates a synchronization file according to the synchronization request sent by the second electronic device, the above-mentioned step S1408 may also be executed.
[0268] Optionally, in some other implementations, the first electronic device may execute S1408 after executing S1406 and before executing S1407.
[0269] It should be understood that after the first electronic device executes the above S1401 to S1408, the first version number and the second sequence number are recorded in the header of the first database, and the data of the data page of the first database is determined according to the first transaction and the data of the data page of the third database. The first version number and the second sequence number are recorded in the header of the log of the first database, and no data operation statement is recorded in the data page of the log of the first database.
[0270] S1409: The first database engine of the first electronic device sends a synchronization file to the first synchronization service of the first electronic device. Correspondingly, the first synchronization service receives the synchronization file from the first database engine.
[0271] S1410: A first synchronization service of a first electronic device sends a synchronization file to a second synchronization service of a second electronic device. Correspondingly, the second synchronization service receives the synchronization file from the first synchronization service.
[0272] In the above S1410, the way in which the first synchronization service sends synchronization files to the second synchronization service is the same as the way in which the first electronic device sends synchronization files to the second electronic device described in the above step S630. For contents not described in detail here, please refer to the relevant description in the above S630.
[0273] S1411, the second synchronization service of the second electronic device sends a synchronization file to the second database engine of the second electronic device. Correspondingly, the second database engine receives the synchronization file from the second synchronization service.
[0274] S1412: When the second database engine of the second electronic device determines that the first database in the synchronization file passes the integrity check, the second database is updated according to the first database to obtain a fourth database.
[0275] In the above step S1412, the second database engine updates the second database according to the first database to obtain the fourth database, including: updating the data in the data page of the second database to the data in the data page of the first database, updating the second version number in the header of the second database to the first version number in the header of the first database, and updating the first serial number in the header of the second database to the second serial number in the header of the first database to obtain the fourth database. That is, after the first electronic device executes the above step S1412, the data in the fourth database is the same as the data in the first database, the version number in the fourth database is the first version number, and the serial number in the fourth database is the second serial number.
[0276] In the above step S1412, the second electronic device will update the second database based on the first database to obtain the fourth database only when the first database in the synchronization file received from the first electronic device passes the integrity check. In this way, the inconsistency between the fourth database as a slave database and the first database as a master database can be avoided as much as possible.
[0277] Optionally, before executing the above step S1412, the second database engine may further execute the following steps: synchronize the identifier and integrity check algorithm in the header of the first database in the file to determine whether the first database passes the integrity check.
[0278] In an embodiment of the present application, the manner in which data is sent between any two modules in the first application, the first database engine, and the first synchronization service included in the first electronic device is not specifically limited. In some implementations, data can be sent directly between any two of the above modules, i.e., no intermediate module forwarding is required. In other implementations, the first electronic device also includes a first database access framework, based on which data interaction can be achieved between any two of the above modules through the first database access framework. For example, the first application sends a second transaction to the first database engine, including: the first application sends the second transaction to the first database access framework; the first database access framework sends the second transaction to the first database engine.
[0279] It should be understood that the above Fig.14 The data synchronization method shown is for illustration only and does not constitute any limitation to the data synchronization method provided in the embodiments of the present application.
[0280] Fig.15It is a schematic diagram of another data synchronization method provided in an embodiment of the present application. It should be understood that Fig.15 The data synchronization method shown above is Figure 6 A specific embodiment of the data synchronization method is shown. Fig.15 The data synchronization method provided in the embodiment of the present application is performed by a first electronic device and a second electronic device, wherein the first electronic device includes a first application, a first database engine and a first synchronization service, and the second electronic device includes a second synchronization service, a second database engine and a second application. Exemplary, Fig.15 The first electronic device shown may be Figure 3 The first electronic device 110 is shown. Fig.15 The second electronic device shown may be Figure 3 The second electronic device 120 is shown. It should be understood that Fig.15 The first synchronization service shown is Figure 3 The first database synchronization service 112 is shown. Fig.15 The second synchronization service shown is Figure 3 A second database synchronization service 122 is shown.
[0281] In the embodiment of the present application, in the initial stage, the first electronic device includes a third database and a log of the third database, and the third database and the log of the third database match. Specifically, the header of the third database includes a third version number, a first sequence number, and a second transaction identifier of the second transaction, the data page of the third database includes data associated with the second transaction, the header of the log of the third database includes the second version number, the first sequence number, and the second transaction identifier, and the data page of the third database includes a data operation statement of the second transaction.
[0282] In the embodiment of the present application, in the initial stage, the second electronic device includes a second database. The header of the second database includes a second version number, a first sequence number, and a second transaction identifier. The data page of the second database includes data associated with the second transaction. The second database is a database obtained by backing up the third database.
[0283] like Fig.15 As shown, the data synchronization method includes S1501 to S1513. S1501 to S1513 are described in detail below.
[0284] S1501: A first application of a first electronic device sends a first transaction to a first database engine of the first electronic device. Correspondingly, the first database engine receives the first transaction from the first application.
[0285] The first transaction in the above step S1501 is the same as the first transaction in the above step S620. For the contents not described in detail here, please refer to the relevant description in the above step S620.
[0286] S1502, the first database engine of the first electronic device updates the data page of the third database according to the first transaction to obtain the fifth database, the header of the fifth database records the third version number, the first serial number and the first transaction identifier, and the data in the data page of the fifth database is determined based on the first transaction and the data of the data page of the third database.
[0287] S1503, when the first electronic device is powered on again after power failure, the first database engine of the first electronic device determines that the first transaction identifier in the header of the fifth database is different from the second transaction identifier in the header of the log of the third database.
[0288] S1504, when it is determined that the first transaction identifier in the header of the fifth database and the second transaction identifier in the header of the log of the third database are different, the first database engine of the first electronic device performs the following operations: delete all data operation statements in the log of the third database, and update the third version number in the log of the third database to the first version number to obtain the log of the first database; and update the third version number in the fifth database to the first version number to obtain the first database.
[0289] The first electronic device executes the above S1503 and S1504, that is, when the first electronic device is powered on again after power failure, the first electronic device determines whether the log update of the third database is successful according to the first transaction by checking whether the first transaction identifier in the header of the fifth database and the second transaction identifier in the header of the log of the third database are the same. Wherein, when the first electronic device determines that the first transaction identifier in the header of the fifth database and the second transaction identifier in the header of the log of the third database are different, the first electronic device executes the update operation described in the above S1504. Wherein, the first electronic device determines that the first transaction identifier in the header of the fifth database and the second transaction identifier in the header of the log of the third database are different, that is, the first electronic device learns that after the update of the third database is successful according to the first transaction, the log update of the third database according to the first transaction fails.
[0290] Optionally, before the first electronic device executes the above S1504, the second electronic device may also randomly generate a new first version number.
[0291] For the contents not described in detail in the above S1502 to S1504, please refer to the relevant description in the above step S620.
[0292] S1505: The second application of the second electronic device sends a synchronization request to the second synchronization service of the second electronic device. Correspondingly, the second synchronization service receives the synchronization file from the second application.
[0293] The synchronization request in the above step S1505 is the same as the synchronization request in the above step S610. For the contents not described in detail here, please refer to the relevant description in the above step S610.
[0294] S1506: The second synchronization service of the second electronic device sends a synchronization request to the first synchronization service of the first electronic device. Correspondingly, the first synchronization service receives the synchronization request from the second synchronization service.
[0295] S1507: The first synchronization service of the first electronic device sends a synchronization request to the first database engine of the first electronic device. Correspondingly, the first database engine receives the synchronization file from the first synchronization service.
[0296] S1508: When the first database engine of the first electronic device determines that the second version number in the synchronization request and the first version number in the log of the first database are different, the first database engine generates a synchronization file according to the first database, where the synchronization file includes the first database.
[0297] In the above step S1508, the synchronization file includes a first database, wherein the header of the first database includes a first version number, a second sequence number and a first transaction identifier, and the data included in the data page of the first database is determined based on the first transaction and the data included in the data page of the third database.
[0298] The synchronization file generated by S1508 above includes the first database, which is a full data synchronization process. That is to say, in the embodiment of the present application, in the scenario where the first database engine successfully updates the third database according to the second transaction, and the log update of the third database according to the second transaction fails, the synchronization file sent by the first electronic device to the second electronic device includes the first database, so that the second electronic device updates the second database according to the first database, which is a full data synchronization process.
[0299] S1509: The first database engine of the first electronic device updates the first serial number in the first database and the log of the first database to the second serial number according to the synchronization request.
[0300] In the embodiment of the present application, after the first electronic device generates a synchronization file according to the synchronization request sent by the second electronic device, the above-mentioned step S1509 may also be executed.
[0301] It should be understood that after the first electronic device executes the above S1501 to S1509, the first version number, the second sequence number and the first transaction identifier are recorded in the header of the first database, and the data of the data page of the first database is determined according to the first transaction and the data of the data page of the third database. The first version number, the second sequence number and the first transaction identifier are recorded in the header of the log of the first database, and no data operation statement is recorded in the data page of the log of the first database.
[0302] S1510: A first database engine of a first electronic device sends a synchronization file to a first synchronization service of the first electronic device. Correspondingly, the first synchronization service receives the synchronization file from the first database engine.
[0303] S1511, the first synchronization service of the first electronic device sends a synchronization file to the second synchronization service of the second electronic device. Correspondingly, the second synchronization service receives the synchronization file from the first synchronization service.
[0304] In the above S1511, the way in which the first synchronization service sends synchronization files to the second synchronization service is the same as the way in which the first electronic device sends synchronization files to the second electronic device described in the above step S630. For contents not described in detail here, please refer to the relevant description in the above S630.
[0305] S1512: The second synchronization service of the second electronic device sends a synchronization file to the second database engine of the second electronic device. Correspondingly, the second database engine receives the synchronization file from the second synchronization service.
[0306] S1513: When the second database engine of the second electronic device determines that the first database in the synchronization file passes the integrity check, the second database is updated according to the first database to obtain a fourth database.
[0307] In the above step S1513, the second database engine updates the second database according to the first database to obtain the fourth database, including: updating the data in the data page of the second database to the data in the data page of the first database, updating the second version number in the header of the second database to the first version number in the header of the first database, and updating the first serial number in the header of the second database to the second serial number in the header of the first database to obtain the fourth database. That is, after the first electronic device executes the above step S1513, the data in the fourth database is the same as the data in the first database, the version number in the fourth database is the first version number, and the serial number in the fourth database is the second serial number.
[0308] Optionally, before executing the above step S1513, the second database engine may further execute the following steps: synchronize the identifier and integrity check algorithm in the header of the first database in the file to determine whether the first database passes the integrity check.
[0309] In an embodiment of the present application, the manner in which data is sent between any two modules in the first application, the first database engine, and the first synchronization service included in the first electronic device is not specifically limited. In some implementations, data can be sent directly between any two of the above modules, i.e., no intermediate module forwarding is required. In other implementations, the first electronic device also includes a first database access framework, based on which data interaction can be achieved between any two of the above modules through the first database access framework. For example, the first application sends a second transaction to the first database engine, including: the first application sends the second transaction to the first database access framework; the first database access framework sends the second transaction to the first database engine.
[0310] It should be understood that the above Fig.15 The data synchronization method shown is for illustration only and does not constitute any limitation to the data synchronization method provided in the embodiments of the present application.
[0311] In the above, combined Figures 6 to 15 A data synchronization method provided by the present application is described. In the data synchronization method described above, the first electronic device determines a synchronization file according to the first database when the second version number is different from the first version number corresponding to the first database.
[0312] Another data synchronization method provided by the present application is described below in conjunction with the accompanying drawings. The data synchronization method described below is: the first electronic device determines a synchronization file based on the first log as a log of the first database when the second version number is the same as the first version number corresponding to the first database.
[0313] Fig.16 is a schematic diagram of another data synchronization method provided in an embodiment of the present application. The data synchronization method provided in an embodiment of the present application can be performed by a first electronic device and a second electronic device. It is understandable that any one of the first electronic device and the second electronic device can be implemented as software, or a combination of software and hardware. Exemplarily, the first electronic device in the embodiment of the present application can be Figure 1 The electronic device 110 shown, the second electronic device may be Figure 1 The electronic device 120 is shown. Fig.16 As shown, the data synchronization method provided in the embodiment of the present application includes S1610 to S1640. S1610 to S1640 are described in detail below.
[0314] S1610, the second electronic device sends a synchronization request to the first electronic device, the synchronization request is used to request to back up the first data in the first database in the first electronic device to the second database in the second electronic device, and the synchronization request includes a second version number corresponding to the second database. Correspondingly, the first electronic device receives the synchronization request from the second electronic device.
[0315] In the above step S1610, the synchronization request is used to request to back up the first data in the first database in the first electronic device to the second database in the second electronic device. In this data synchronization mode, the first database is the master database, the second database is the slave database, the first electronic device can be called the master device for data synchronization, and the second electronic device can be called the slave device for data synchronization. The first data requested by the synchronization request can be part of the data or all of the data in the first database, which is not specifically limited in the embodiments of the present application.
[0316] The database types of the first database and the second database are not specifically limited. In some implementations, the first database is associated with a first application in a first electronic device, and the second database is associated with a second application in a second electronic device. In this implementation, the first database and the second database may be embedded databases. For example, the first database and the second database may be SQLite databases. In other implementations, the first database and the second database may also be non-embedded databases. For example, the first database and the second database may be MySQL databases.
[0317] S1620, the first electronic device determines the synchronization file according to the first log of the first database when the second version number is the same as the first version number corresponding to the first database, and the first log matches the first database, wherein, when the first electronic device successfully obtains the first database by updating the third database according to the first transaction, and successfully obtains the first log by updating the log of the third database according to the first transaction, the first version number is the same as the third version number corresponding to the third database.
[0318] In the above step S1620, the log of the third database matches the third database, and the third version number is the same as the second version number. The third database matches the log of the third database, that is, the data operation statements recorded in the log of the third database can correctly reflect the data in the third database.
[0319] In the initial stage, the data in the second database in the second electronic device is obtained by backing up the data in the third database in the first electronic device. The data in the second database is the same as the data in the third database. Based on this, the third version number corresponding to the third database can be set to be the same as the second version number corresponding to the second database.
[0320] It should be understood that the data in a database described in the embodiments of the present application refers to application data generated by an application stored in the database. In some possible implementations, the third database may include a header page and a data page, wherein the header page is used to store attribute information related to the third database (for example, database type, serial number or version number, etc.), and the data page is used to store application data generated by an application associated with the third database.
[0321] It should be understood that in the embodiments of the present application, the version number, serial number or transaction identifier in a database is the attribute information associated with the database, and these attribute information are not the application data generated by the application stored in the database. The version number and serial number in the log of a database are the attribute information associated with the log of the database, and these attribute information are not the data operation statements stored in the log of the database.
[0322] In the stage after the initial stage, the first electronic device receives a new write transaction (i.e., the first transaction). When the first electronic device obtains the first transaction, the first electronic device needs to perform an update operation on the third database and the log of the third database according to the first transaction. In actual applications, in order to ensure that the database and the log of the database match, the third database is usually updated according to the first transaction to successfully obtain the updated third database, and then the log of the third database is updated according to the first transaction. Below, based on the above-mentioned update rules, the process of the first electronic device performing an update on the third database and the log of the third database according to the first transaction provided in an embodiment of the present application is introduced.
[0323] In some implementations, the log of the third database and the second transaction identifier of the second transaction are recorded in the third database, all data operation statements in the log of the third database include data operation statements of the second transaction, and the data in the third database includes data determined according to the data operation statements of the second transaction; based on this, before the first electronic device receives a synchronization request from the second electronic device, the method executed by the first electronic device also includes: the first electronic device updates the data in the third database according to the first transaction, and updates the second transaction identifier in the third database according to the first transaction identifier, to obtain the first database; the first electronic device updates all data operation statements in the log of the third database according to the first transaction, and updates the second transaction identifier in the log of the third database according to the first transaction identifier, to obtain the first log.
[0324] In the above steps, the first transaction identifier is used to represent the first transaction, and the second transaction identifier is used to represent the second transaction, wherein the first transaction and the second transaction are different, and the first transaction identifier and the second transaction identifier are two different transaction identifiers. There is no specific limitation on the presentation form of the first transaction identifier and the second transaction identifier, and they can be set according to actual application scenarios and actual needs. For example, the second transaction identifier can be t0, and the first transaction identifier can be t1.
[0325] In the above steps, the location of the transaction identifier in the database and the log of the database is not specifically limited. In some implementations, the database and the log of the database include a header page, the transaction field included in the header page of the database is used to store the transaction identifier, and the transaction field included in the header page of the log of the database is used to store the transaction identifier.
[0326] The format of the first database in the above steps is the same as that in the above Figure 6 The format of the first database in the data synchronization method provided is the same. Exemplarily, the first database in the above steps can be as follows Fig.10 A first database is shown.
[0327] In the above implementation, after the first electronic device successfully updates the third database according to the first transaction to obtain the updated third database, it updates the second transaction identifier in the updated third database according to the first transaction identifier of the first transaction. In this way, it can be ensured that the first transaction identifier in the obtained third database matches the latest first write transaction executed on the third database.
[0328] In the above step S1620, when the first electronic device successfully obtains the first database according to the first transaction to update the third database, and successfully obtains the first log according to the log update of the third database by the first transaction, the first version number is the same as the third version number corresponding to the third database. That is, the scenario described in the above step S1620 is: the first electronic device successfully obtains the first database according to the first transaction to update the third database, and successfully obtains the first log according to the log update of the third database by the first transaction. In this scenario, the logs of the first database and the first database are matched, that is, the data operation statements recorded in the log of the first database can correctly reflect the data in the first database. Thereafter, the first electronic device can determine the synchronization file based on the log of the first database. However, in the scenario where the logs of the first database and the first database match, after the first electronic device successfully obtains the first log according to the log update of the third database by the first transaction, there may be a situation where the first log is tampered, resulting in the mismatch between the tampered first log and the first database. That is, in this scenario, the first electronic device determines the synchronization file based on the tampered first log that does not match the first database, which will cause the problem of inconsistent data in the master database and the slave database.
[0329] To solve the above technical problems, the working principle of the technical solution provided in the embodiment of the present application is as follows: when the first electronic device successfully updates the data operation statement in the log of the third database according to the first transaction and obtains the log of the updated third database, the first electronic device will write the first verification code of the data operation statement of the first transaction in the log of the updated third database. Thereafter, when the first electronic device obtains the data operation statement from the first position in the first log according to the first sequence number in the first log and the second sequence number in the synchronization request, the first electronic device will generate a second verification code for the obtained data operation statement, and compare the second verification code with the first verification code of the first data operation statement recorded in the first position in the first log to determine whether the data operation statement recorded at the first position in the first log has been tampered with after the first electronic device successfully obtains the first log according to the first transaction to update the log of the third database. In the case that the first log has not been tampered with, that is, the first log and the first database are matched, so that the first electronic device can determine the data included in the synchronization file according to the first sequence number of the first log and the second sequence number in the synchronization request. In the case that the first log has been tampered with, that is, the first log and the first database are not matched, so that the first electronic device can determine the data included in the synchronization file according to the first database.
[0330] Next, two implementation methods of the first electronic device provided in the embodiments of the present application to execute the above technical solution are introduced.
[0331] Implementation method 1:
[0332] In implementation mode 1, the first electronic device executes the above S1620, that is, the first electronic device determines the synchronization file according to the first log of the first database when the second version number is the same as the first version number corresponding to the first database, including:
[0333] When the second version number is the same as the first version number corresponding to the first database, and the first verification code and the second verification code are the same, the first electronic device determines that the first log has not been modified, the first verification code is a verification code of a first data operation statement at a first position recorded in the first log, the second verification code is a verification code generated by the first electronic device according to a second data operation statement obtained from the first position in the first log, the first data operation statement and the second data operation statement are the same, and the first data operation statement at the first position recorded in the first log is a data operation statement of the first transaction;
[0334] When the first log has not been modified, the first electronic device determines the synchronization file based on the first serial number of the first log and the second serial number in the synchronization request, where the synchronization file includes a first data operation statement, the first serial number is used to indicate the location of the data synchronized to the first database by the first electronic device before receiving the synchronization request, and the second serial number is used to indicate the location of the data requested to be synchronized to the first database by the synchronization request.
[0335] It should be understood that in the above step, only one data operation statement is stored at the first position in the first log, that is, the first data operation statement or the second data operation statement is stored at the first position in the first log.
[0336] In the above steps, the first check code is a check code of the first database operation statement at the first position recorded in the first log, and the first position is a position in the first log determined according to the first sequence number and the second sequence number for storing the first database operation statement. The second check code is a check code generated by the first electronic device according to the second database operation statement obtained from the first position in the first log.
[0337] The following describes the format of the first log in the above steps with examples in conjunction with the accompanying drawings. It should be understood that Fig.17 The format of the first log shown is only for illustration and does not constitute any limitation on the format of the first log.
[0338] Exemplarily, the first log of the first database in the above step may be Fig.17 The first log of the first database is shown. Fig.17 ,The first log includes a header, an index page and a data page.
[0339] Fig.17The header of the first log shown is used to store a version number field, a transaction field and a sequence number field, wherein the value of the version number field is the first version number, the value of the transaction field is the first transaction identifier, and the value of the synchronization sequence number field is the first sequence number.
[0340] Fig.17 The index page of the first log shown has multiple lines of data recorded in it. Each line of data in the multiple lines of data has the following fields recorded: the serial number of the index page, the page number of the data page, and the length of the statement associated with the serial number. The serial number of the index page increases in ascending order. The page number of the data page indicates the page number in the data page of the first log. The length of the statement associated with the serial number refers to the length of the SQL statement in the data page associated with the serial number of the index page. The unit of the length of the statement associated with the serial number can be bytes. See Fig.17 , when the sequence number is 1, the sequence number is associated with SQL statement 1 in page 0, that is, xxx represents the length of SQL statement 1. When the sequence number is 2, the sequence number is associated with SQL statement 2 in page 1 and SQL statement 3 in page 2, that is, the statement length represented by yyy includes the length of SQL statement 2 and the length of SQL statement 3. When the sequence number is 3, the sequence number is associated with SQL statement 4 in page 3, that is, nnn represents the length of SQL statement 4. When the sequence number is 4, the sequence number is associated with SQL statement 5 in page 4, that is, zzz represents the length of SQL statement 5.
[0341] Optionally, the index page of the first log can also record Fig.17 The dashed line shows the synchronization state. Fig.17 The value of the synchronization state shown by the dotted line indicates whether the data indicated by the page number of the data page of the corresponding row is synchronized, wherein the value of the synchronization state is "1" indicating that the data indicated by the page number of the data page of the corresponding row has been synchronized, and the value of the synchronization state is "0" indicating that the data indicated by the page number of the data page of the corresponding row has not been synchronized. In other words, the data in any one of the data pages with page numbers 0 to 3 has been synchronized to the second database, and the data in the data page with page number 4 has not been synchronized to the second database.
[0342] Optionally, the index page of the first log can also record Fig.17 The dotted line shows the check code. Fig.17 The check code 1 shown by the dotted line is a check code generated according to SQL statement 1 in the data page corresponding to the index page serial number 1. The check code 2 is a check code generated according to SQL statements 2 and 3 in the data page corresponding to the index page serial number 2. The check code 3 is a check code generated according to SQL statement 4 in the data page corresponding to the index page serial number 3. Fig.17The first check code shown by the solid line is a check code generated according to the SQL statement 5 in the data page corresponding to the serial number 4 of the index page, and the SQL statement 5 is the data operation statement of the first transaction.
[0343] Fig.17 The data page in the first log is shown to be used to store an SQL statement for generating data in the data page of the third database, wherein the type of the SQL statement may be insert, delete or update.
[0344] See also Fig.17 , the first sequence number recorded in the first log is 3, and the second sequence number in the synchronization request is 4. Based on this, it can be determined according to the first sequence number and the second sequence number that the data operation statements to be synchronized in the first log include SQL statement 5 in the data page corresponding to the sequence number 4 of the index page, where SQL statement 5 is the data operation statement of the first transaction. Fig.17 , a first check code of a data operation statement of a first transaction is recorded in a second position of an index page of a first log, and the second position is associated with the first position.
[0345] It is understandable that when Fig.17 In the case where the synchronization status corresponding to the sequence number 4 in the index page of the first log is updated from "0" to 1, Fig.17 The first sequence number in the sequence number field in the header of the first log shown also needs to be updated to the second sequence number in the synchronization request.
[0346] Optionally, the synchronization file in the above steps also includes a first verification code or a second verification code. Based on this, after the second electronic device receives the synchronization file, the second electronic device can perform data verification on the data operation statement (i.e., the first data operation statement) in the synchronization file according to the first verification code or the second verification code in the synchronization file to determine whether the data operation statement in the synchronization file has been tampered with during the transmission process.
[0347] The scenario described in the above implementation method 1 is that after the first electronic device successfully obtains the first database by updating the third database according to the first transaction, and successfully obtains the first log by updating the log of the third database according to the first transaction, the first log has not been tampered with or modified, that is, the first log and the first database are matched (that is, the data operation statements recorded in the first log can correctly reflect the data in the first database). Based on this, the first electronic device determines the data included in the synchronization file according to the first serial number of the first log and the second serial number in the synchronization request. It should be understood that in the above steps, the first electronic device determines that the first log and the first database used by the synchronization file are matched. Thereafter, the second electronic device updates the second database according to the synchronization file to obtain the fourth database, which is conducive to ensuring the data consistency in the first database as the master database and the fourth database as the slave database.
[0348] Implementation method 2:
[0349] In the second implementation method, the first electronic device determines the synchronization file according to the first log of the first database when the second version number is the same as the first version number corresponding to the first database, including: the first electronic device determines that the first log has been modified when the second version number is the same as the first version number corresponding to the first database and the first verification code and the second verification code are different, the first verification code is the verification code of the first data operation statement at the first position recorded in the modified first log, the second verification code is the verification code generated by the first electronic device according to the second data operation statement obtained from the first position in the modified first log, the first data operation statement in the modified first log is different from the second data operation statement in the modified first log, and the first data operation statement at the first position recorded in the modified first log is the data operation statement of the first transaction; when the first log is modified, the first electronic device determines the synchronization file according to the first database, and the synchronization file includes the first data.
[0350] It should be understood that in the above step, only one data operation statement is stored at the first position in the modified first log, that is, the first data operation statement or the second data operation statement is stored at the first position in the modified first log.
[0351] The modified first log in the above steps is a log obtained after the first data operation statement at the first position in the first log is modified to a second data operation statement different from the first data operation statement. That is, the difference between the first log and the modified first log is that the data operation statement stored at the first position in the first log is different from the data operation statement stored at the first position in the modified first log. The format of the modified first log can refer to the format of the first log described in the above implementation method 1, and will not be described in detail here.
[0352] The scenario described in the second implementation method is that after the first electronic device successfully updates the third database according to the first transaction to obtain the first database, and successfully obtains the first log according to the log update of the third database according to the first transaction, the data operation statement of the first transaction recorded in the first log is tampered with or modified, resulting in a mismatch between the modified first log and the first database (that is, the data operation statement recorded in the modified first log cannot correctly reflect the data in the first database). Based on this, the first electronic device determines the data included in the synchronization file according to the first database, and thereafter, the second electronic device updates the second database according to the synchronization file to obtain the fourth database, which is conducive to ensuring the data consistency between the first database as the master database and the fourth database as the slave database.
[0353] In the above implementation method 1 and implementation method 2 provided in the embodiment of the present application, both involve the first verification code in the first log or the modified first log. In the embodiment of the present application, before the first electronic device executes the above implementation method 1 or implementation method 2, the first electronic device may also execute the step of writing the first verification code of the data operation statement of the first transaction into the first log. There is no specific limitation on the timing when the first electronic device writes the first verification code into the first log.
[0354] In some implementations, the first verification code is located at a second position in the first log, and the second position is associated with the first position. After the first electronic device receives a synchronization request from the second electronic device, the method executed also includes: the first electronic device obtains a first data operation statement from the first position in the first log based on the first serial number of the first log and the second serial number in the synchronization request; the first electronic device generates a first verification code based on the first data operation statement, and writes the first verification code to the second position of the first log.
[0355] In the above steps, the first sequence number and the second sequence number are used to determine the first position in the first log.
[0356] In the above implementation, the first verification code recorded in the second position in the first log is a verification code written into the first log after the first electronic device receives the synchronization request.
[0357] In other implementations, when the second version number of the first electronic device is the same as the first version number corresponding to the first database, before determining the synchronization file according to the first log of the first database, the method executed also includes: when the first electronic device successfully obtains the first database according to the first transaction, the first electronic device updates the data operation statements in the log of the third database according to the first transaction to obtain the log of the updated third database; the first electronic device writes the verification code of the data operation statement of the first transaction to the second position of the log of the updated third database, the second position is associated with the first position, and the first log is obtained, and the verification code of the data operation statement of the first transaction is the first verification code.
[0358] In the above implementation, the first verification code recorded in the second position in the first log is a verification code written into the first log before the first electronic device receives the synchronization request, that is, when the log of the third database is updated according to the first transaction.
[0359] In the above implementation, a verification code field is added to the first log of the first database, and the first verification code of the data operation statement of the first transaction is stored in the verification code field. This is helpful for subsequent determination of whether the data operation statement of the first transaction recorded in the first log has changed after the first electronic device obtains the first log.
[0360] S1630: The first electronic device sends a synchronization file to the second electronic device, so that the second electronic device updates the second database using the synchronization file to obtain a fourth database. Correspondingly, the second electronic device receives the synchronization file from the first electronic device.
[0361] In the above-mentioned step S1630, the first electronic device sends a synchronization file to the second electronic device, which can be understood as the first electronic device encapsulating the data of the first database based on a universal transmission protocol, and sending the encapsulated file to the second electronic device. In some implementations, when the data stored in the first database is data generated by the first application in the first electronic device, the above-mentioned universal transmission protocol can also be used to transmit application data generated by multiple applications different from the first application, and the data formats of the application data generated by different applications are different. In this implementation, the first electronic device does not need to encapsulate the data generated by the first application (i.e., the data in the first database) in a specific protocol format (i.e., the format of the transmission protocol corresponding to the application type), so that the versatility of the data synchronization method provided in the embodiment of the present application and the efficiency of data synchronization can be guaranteed.
[0362] In some implementations, after the first electronic device obtains the synchronization file and before sending the synchronization file to the second electronic device, the first electronic device may also perform the following steps to update the serial number: the first electronic device updates the first serial number in the first database to the second serial number in the synchronization request; the first electronic device updates the first serial number in the first log to the second serial number in the synchronization request, or updates the first serial number in the updated first log to the second serial number in the synchronization request.
[0363] Optionally, the first log in the above steps may also record the synchronization status of the data operation statement corresponding to the second serial number. Based on this, when the first electronic device updates the first serial number in the first log to the second serial number, the first electronic device also needs to update the synchronization status of the data operation statement corresponding to the second serial number recorded in the first log to a synchronized state.
[0364] For example, in the first log Fig.17 In the case of the first log shown, the first electronic device will Fig.17 In the case where the value of the sequence number field in the header of the first log is updated from the first sequence number to the second sequence number, it is also necessary to change Fig.17 The synchronization status value of the data operation statement corresponding to the second serial number in the index page of the first log shown is updated from "0" to "1", where the synchronization status value "0" indicates that it is not synchronized, and the synchronization status value "1" indicates that it has been synchronized.
[0365] After executing the above steps, after the first electronic device performs the data synchronization according to the synchronization request received from the second electronic device, and receives a new synchronization request sent by the second electronic device again, the first electronic device can accurately determine the data that the first electronic device needs to send to the second electronic device when performing the data synchronization based on the second serial number recorded in the updated first log and the third serial number in the new synchronization request.
[0366] S1640: The second electronic device updates the second database according to the synchronization file to obtain a fourth database, wherein the data in the fourth database is the same as the data in the first database.
[0367] In the embodiment of the present application, the second electronic device executing the above step S1640 is related to the content included in the synchronization file in the above S1640.
[0368] In some implementations, the synchronization file in the above step S1640 is a file obtained by the first electronic device executing the implementation method 1 in the above step S1620. In this implementation, the synchronization file in the above step S1640 includes a first data operation statement. Based on this, the second electronic device executes the above step S1640, that is, the second electronic device updates the second database according to the synchronization file to obtain the fourth database, including: the second electronic device executes the first data operation statement in the synchronization file on the second database, and updates the first serial number in the second database to the second serial number to obtain the fourth database.
[0369] Optionally, the synchronization file in the above steps also includes the first verification code or the second verification code in the above step S1620. Before the second electronic device updates the second database according to the synchronization file to obtain the fourth database, the method executed by the second electronic device also includes: the second electronic device generates a third verification code according to the first data operation statement in the synchronization file; and the second electronic device determines that the first data operation statement in the synchronization file has passed the verification when the third verification code is the same as the verification code in the synchronization file.
[0370] In some other implementations, the synchronization file in the above step S1640 is a file obtained by the first electronic device executing the second implementation in the above step S1620. In this implementation, the synchronization file in the above step S1640 includes the first data. Based on this, the second electronic device executes the above step S1640, that is, the second electronic device updates the second database according to the synchronization file to obtain the fourth database, including: the second electronic device updates the data in the second database according to the first data in the synchronization file, and updates the first serial number in the second database to the second serial number to obtain the fourth database. In the above steps, before the second electronic device updates the second database according to the synchronization file to obtain the fourth database, the method also includes: the second electronic device performs data verification on the first data in the synchronization file to determine that the first data passes the verification.
[0371] In some other implementations, the synchronization file in the above step S1640 is obtained by the first electronic device executing the second implementation in the above step S1620. In this implementation, the synchronization file in the above step S1640 includes the first database. Based on this, the second electronic device executes the above step S1640, that is, the second electronic device updates the second database according to the synchronization file to obtain the fourth database, including: the second electronic device updates the data in the second database according to the first data in the synchronization file, and updates the first serial number in the second database to the second serial number to obtain the fourth database.
[0372] In the above steps, before the second electronic device updates the second database according to the synchronization file to obtain the fourth database, the method also includes: the second electronic device performs an integrity check on the first database in the synchronization file to determine that the first database in the synchronization file passes the integrity check.
[0373] In the embodiment of the present application, the method for the second electronic device to perform integrity check on the first database is not specifically limited. Exemplarily, after the second electronic device receives the synchronization file including the first database, the second electronic device performs integrity check on the first database by performing the following steps: the second electronic device reads the preset bytes in the header of the first database; when the preset bytes indicate that full data synchronization is in progress, the second electronic device can call the native interface of the first database to execute integrity check instructions on the first database.
[0374] In the above technical solution, before the second electronic device updates the second database according to the synchronization file, the data to be synchronized in the synchronization file (for example, the first data, the first data operation statement or the first database) is verified to determine whether the data to be synchronized in the synchronization file is tampered with or lost during the transmission process. Only when the second electronic device determines that the data to be synchronized in the synchronization file passes the verification, will it update the second database according to the data to be synchronized in the synchronization file to obtain the fourth database, which is conducive to ensuring the data consistency of the first database as the main database and the fourth database as the database as much as possible.
[0375] In the above, the interaction steps between the first electronic device and the second electronic device performing S1610 to S1640 are described. In some other implementations, the first electronic device may also include a first application, a first database engine, and a first synchronization service, and the second electronic device may also include a second synchronization service, a second database engine, and a second application. Based on this, when the first electronic device and the second electronic device perform S1610 to S1640, it also involves the interaction steps of various modules in the first electronic device, and the interaction steps of various modules in the second electronic device.
[0376] The interaction steps of the various modules in the first electronic device in the embodiment of the present application can be referred to above. Fig.13 The interaction steps of the various modules in the first electronic device shown in the figure; and the interaction steps of the various modules in the second electronic device in the embodiment of the present application can be referred to above. Fig.13 The interaction steps of the various modules in the second electronic device are shown and will not be described in detail here.
[0377] It should be understood that the above Fig.16 The data synchronization method shown is for illustration only and does not constitute any limitation to the data synchronization method provided in the embodiments of the present application.
[0378] In the embodiment of the present application, a version number and a transaction identifier are added to the database in the first electronic device, a version number, a transaction identifier and a check code are also added to the log of the database in the first electronic device, and a version number is added to the database in the second electronic device. This modification method is simple and easy to implement. When the first electronic device successfully obtains the first data by updating the third database according to the first transaction, and successfully obtains the first log by updating the log of the third database according to the first transaction, the first electronic device can determine whether the first log is modified through the first check code and the second check code. On the one hand, when the first electronic device determines that the first log is modified according to the first check code and the second check code, the first database included in the first electronic device and the modified first log do not match, that is, the data operation statement recorded in the modified first log cannot correctly reflect the data in the first database. Based on this, the first electronic device needs to determine the synchronization file according to the first database. Thereafter, the first electronic device sends the synchronization file determined according to the first database to the second electronic device, so that the second electronic device uses the synchronization file to update the second database to obtain the fourth database, thereby maintaining the data consistency of the first database as the master database and the fourth database as the slave database as much as possible. On the other hand, when the first electronic device determines that the first log has not been modified according to the first verification code and the second verification code, the first database included in the first electronic device matches the first log (that is, the data operation statement recorded in the first log can correctly reflect the data in the first database). Based on this, the first electronic device determines the synchronization file based on the first log. It should be understood that in the case where the first log and the first database match, the determination process of the synchronization file determined by the first electronic device based on the first log is simpler and more efficient than the determination process of the synchronization file determined by the first electronic device based on the first database. Thereafter, the first electronic device sends the synchronization file determined according to the first log to the second electronic device, so that the second electronic device updates the second database using the synchronization file to obtain the fourth database, thereby maintaining the data consistency of the first database as the master database and the fourth database as the slave database as much as possible. In addition, in the above-mentioned data synchronization process, only the first electronic device and the second electronic device are involved, that is, the data synchronization method provided in the embodiment of the present application is applicable to the end-to-end data synchronization scenario of electronic devices, that is, the method can maintain the data consistency of the master database and the slave database in the end-to-end scenario as much as possible.
[0379] Fig.18 It is a schematic diagram of another data synchronization method provided in an embodiment of the present application. It should be understood that Fig.18 The data synchronization method shown above is Fig.16 A specific embodiment of the data synchronization method is shown. Fig.18The data synchronization method provided in the embodiment of the present application is performed by a first electronic device and a second electronic device, wherein the first electronic device includes a first application, a first database engine and a first synchronization service, and the second electronic device includes a second synchronization service, a second database engine and a second application. Exemplary, Fig.18 The first electronic device shown may be Figure 3 The first electronic device 110 is shown. Fig.18 The second electronic device shown may be Figure 3 The second electronic device 120 is shown. It should be understood that Fig.18 The first synchronization service shown is Figure 3 The first database synchronization service 112 is shown. Fig.18 The second synchronization service shown is Figure 3 A second database synchronization service 122 is shown.
[0380] In the embodiment of the present application, in the initial stage, the first electronic device includes a third database and a log of the third database, and the third database and the log of the third database match. Specifically, the header of the third database includes a third version number and a first sequence number, the data page of the third database includes data associated with the second transaction, the header of the log of the third database includes a second version number and a first sequence number, and the data page of the third database includes a data operation statement of the second transaction.
[0381] In the embodiment of the present application, in the initial stage, the second electronic device includes a second database. The header of the second database includes a second version number and a first sequence number, the data page of the second database includes data associated with the second transaction, and the second database is a database obtained by backing up the third database.
[0382] like Fig.18 As shown, the data synchronization method includes S1801 to S1814. S1801 to S1814 are described in detail below.
[0383] S1801: A first application in a first electronic device sends a first transaction to a first database engine of the first electronic device. Correspondingly, the first database engine receives the first transaction from the first application.
[0384] The first transaction in the above step S1801 is the same as the first transaction in the above step S1620. For the contents not described in detail here, please refer to the relevant description in S1620 above.
[0385] S1802, the first electronic device updates the data page of the third database according to the first transaction to obtain the first database, the header of the first database records the third version number and the first serial number, and the data in the data page of the first database is determined based on the first transaction and the data of the data page of the third database.
[0386] S1803, the first electronic device updates the data page of the log of the third database according to the first transaction, and obtains the first log of the first database, the header of the first log records the third version number and the first sequence number, and the data page of the first log records the data operation statements of the first transaction and the data operation statements of the second transaction.
[0387] In the embodiment of the present application, the scenario after the first electronic device executes the above S1802 and the above S1803 is: the first electronic device successfully obtains the first database by updating the third database according to the first transaction, and successfully obtains the first log by updating the log of the third database according to the first transaction. It should be understood that in this scenario, the first database and the first log are matched, that is, the data operation statements recorded in the first log can correctly reflect the data in the first database.
[0388] S1804: The second application of the second electronic device sends a synchronization request to the second synchronization service of the second electronic device. Correspondingly, the second synchronization service receives the synchronization request from the second application.
[0389] S1805: The second synchronization service of the second electronic device sends a synchronization request to the first synchronization service of the first electronic device. Correspondingly, the first synchronization service receives the synchronization request from the second synchronization service.
[0390] S1806: The first synchronization service of the first electronic device sends a synchronization request to the first database engine of the first electronic device. Correspondingly, the first database engine receives the synchronization request from the first synchronization service.
[0391] S1807: The first electronic device updates the first serial number in the first database and the first log to the second serial number.
[0392] Optionally, the first log in the above step S1807 may also record a synchronization status field with a value of "0", and the synchronization status field with a value of "0" is used to indicate that the synchronization status of the data operation statement of the first transaction recorded in the first log is in an unsynchronized state. After the first electronic device executes the above S1807, the first electronic device may also update the synchronization status field with a value of "0" recorded in the first log to a synchronization status field with a value of "1", and the value of the synchronization status field is "1", indicating that the status of the data operation statement of the first transaction associated with the synchronization status field is in a synchronized state.
[0393] S1808, the first electronic device writes the first verification code of the first data operation statement of the first transaction into the second position of the first log according to the first sequence number in the first log and the second sequence number in the synchronization request, the second position in the first log is associated with the first position in the first log, and the first position in the first log stores the data operation statement of the first transaction.
[0394] The method in which the first electronic device writes the first verification code into the second position of the first log in the above step S1808 is the same as the method in which the first electronic device writes the first verification code into the second position of the first log in the above step S1620. For matters not described in detail here, please refer to the relevant description in the above step S1620.
[0395] In the embodiment of the present application, after the first electronic device obtains the first log including the first verification code, if the first log has not been tampered with, then after executing the above S1808, S1809 below is executed; after the first electronic device obtains the first log including the first verification code, if the first log has been tampered with and a modified first log is obtained, then after executing the above S1808, S1810 below is executed. S1809 and S1810 are introduced below.
[0396] S1809, when the first verification code and the second verification code are the same, the first electronic device determines the synchronization file according to the first serial number of the first log and the second serial number in the synchronization request, the synchronization file including the first data operation statement, the second serial number and the first verification code, the first verification code is the verification code of the first data operation statement at the first position recorded in the first log, the second verification code is the verification code generated by the first electronic device according to the second data operation statement obtained from the first position in the first log, and the first data operation statement and the second data operation statement are the same.
[0397] The method for the first electronic device to determine the synchronization file in the above step S1809 is the same as the method for the first electronic device to determine the synchronization file described in implementation method one in step S1620 above. For matters not described in detail here, please refer to the relevant description in implementation method one in step S1620 above.
[0398] S1810, when the first verification code and the second verification code are different, the first electronic device determines a synchronization file based on the first database, the synchronization file includes the first database, the first verification code is a verification code of the first data operation statement at the first position recorded in the modified first log, the second verification code is a verification code generated by the first electronic device based on the second data operation statement obtained from the first position in the modified first log, and the first data operation statement and the second data operation statement are different.
[0399] The method for the first electronic device to determine the synchronization file in the above step S1810 is the same as the method for the first electronic device to determine the synchronization file described in the second implementation method in the above step S1620. For the contents not described in detail here, please refer to the relevant description in the second implementation method in the above step S1620.
[0400] S1811: The first database engine of the first electronic device sends a synchronization file to the first synchronization service of the first electronic device. Correspondingly, the first synchronization service receives the synchronization file from the first database engine.
[0401] The synchronization file in the above S1811 is determined according to the above step S1809 or step S1810.
[0402] In some implementations, after the first electronic device executes S1801 to S1808 above and continues to execute S1809, the synchronization file in the above step S1811 is determined according to step S1809, that is, the synchronization file includes the first data operation statement, the second serial number and the first check code.
[0403] In some other implementations, after the first electronic device executes S1801 to S1808 above and continues to execute S1810, the synchronization file in the above step S1811 is determined according to step S1810, that is, the synchronization file includes the first database.
[0404] S1812: The first synchronization service of the first electronic device sends a synchronization file to the second synchronization service of the second electronic device. Correspondingly, the second synchronization service receives the synchronization file from the first synchronization service.
[0405] S1813: The second synchronization service of the second electronic device sends a synchronization file to the second database engine of the second electronic device. Correspondingly, the second database engine receives the synchronization file from the second synchronization service.
[0406] S1814: The second database engine of the second electronic device updates the second database according to the synchronization file to obtain a fourth database.
[0407] After the second electronic device executes the above step S1814, the data in the fourth database is the same as the data in the first database, and the version number in the fourth database is the same as the version number in the first database.
[0408] In an embodiment of the present application, the synchronization file in the above step S1814 is determined according to the above step S1809 or the above step S1810. In some implementations, after the first electronic device executes the above steps S1801 to S1808 and continues to execute S1809, the synchronization file in the above step S1814 is determined according to the step S1809, that is, the synchronization file includes the first data operation statement, the second sequence number and the first check code. In other implementations, after the first electronic device executes the above steps S1801 to S1808 and continues to execute S1810, the synchronization file in the above step S1814 is determined according to the step S1810, that is, the synchronization file includes the first database.
[0409] The updating method of the first electronic device in the above step S1814 to obtain the fourth database by updating the second data according to the synchronization file is the same as the updating method of the first electronic device in the above step S1640 to obtain the fourth database by updating the second data according to the synchronization file. For the contents not described in detail here, please refer to the relevant description in the above step S1640.
[0410] It should be understood that the above Fig.18 This is only for illustration and does not constitute any limitation on the data synchronization method provided in the embodiment of the present application. Fig.18 The data synchronization method shown is described by taking the example that the database includes the version number and the sequence number, and the database log includes the version number, the sequence number and the check code. Optionally, in other implementations, the database and the database log may also include a transaction identifier.
[0411] Above, combined Figures 6 to 18 The data synchronization method provided by the embodiment of the present application is introduced. Next, the graphical user interface (GUI) displayed by the electronic device when the data synchronization method provided by the embodiment of the present application is applied in the electronic device is introduced in conjunction with the accompanying drawings.
[0412] Fig.19 The desktop 1910 of the second electronic device 1900 is shown. When the second electronic device detects that the user clicks on the icon 1911 of the gallery application (application, APP) on the desktop 1910, the gallery application can be started and the display can be as follows: Fig.19 The gallery interface 1920 is shown. The gallery interface 1920 may include a search box 1921, the device (ie, the second electronic device 1900), other electronic devices, and all photos included in the device.
[0413] In one example, if the user needs to access data in a gallery application of another electronic device, the user can click on another electronic device on the gallery interface 1920. When another electronic device in the gallery interface 1920 is selected, the following information can be displayed: Fig.19 The desktop 1930 is shown, and other electronic devices including the first electronic device are displayed in the desktop 1930. By clicking the first electronic device in the desktop 1930, the user can make the second electronic device 1900 and the first electronic device execute the data synchronization method provided in the embodiment of the present application to back up the data in the gallery application of the first electronic device to the gallery application of the second electronic device 1900, so that the purpose of displaying the data in the gallery application of the first electronic device in the interface of the present device can be achieved.
[0414] Combination of the above Figures 1 to 19 , describes in detail the application scenarios applicable to the embodiments of the present application, the structure of the electronic device, the data synchronization method of the embodiments of the present application, and the user interface displayed when the electronic device executes the data synchronization method provided by the embodiments of the present application. Fig. 20 and Fig.21 , describes in detail the data synchronization device and electronic device embodiments of the present application. It should be understood that the data synchronization device and electronic device in the embodiments of the present application can execute the various data synchronization methods in the aforementioned embodiments of the present application, that is, the specific working processes of the following various products can refer to the corresponding processes in the aforementioned method embodiments.
[0415] Corresponding to the data synchronization method provided in the embodiment of the present application, the present application also provides a data synchronization device.
[0416] Fig. 20 is a schematic diagram of a data synchronization device provided in an embodiment of the present application. Fig. 20 , the data synchronization device includes a processing unit 2010.
[0417] In some implementations, Fig. 20 The data synchronization device shown is applied to a first electronic device including a first application. The first electronic device is the first electronic device in the data synchronization method provided above. In this implementation, the processing unit 2010 is used to execute the steps of the method performed by the first electronic device in the data synchronization method provided in an embodiment of the present application.
[0418] In other implementations, Fig. 20 The data synchronization device shown is applied to a second electronic device, which is the second electronic device in the data synchronization method provided above. In this implementation, the processing unit 2010 is used to execute the steps of the method performed by the second electronic device in the data synchronization method provided in an embodiment of the present application.
[0419] For example, a "unit" may be a software program, a hardware circuit, or a combination of the two that implements the above functions. The hardware circuit may include an application specific integrated circuit (ASIC), an electronic circuit, a processor (e.g., a shared processor, a dedicated processor, or a group processor, etc.) and a memory for executing one or more software or firmware programs, a combined logic circuit, and / or other suitable components that support the described functions.
[0420] Therefore, the units of each example described in the embodiments of the present application can be implemented by electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the present application.
[0421] Corresponding to the data synchronization method provided in the embodiment of the present application, the present application also provides an electronic device.
[0422] Fig.21 It is a schematic diagram of the structure of an electronic device 2100 provided in an embodiment of the present application. Fig.21 A dashed line in a box indicates that the unit or module is optional. Fig.21 The electronic device 2100 shown may be used to execute the steps of the method performed by the first electronic device or the second electronic device in the data synchronization method provided in the embodiment of the present application.
[0423] The electronic device 2100 includes one or more processors 2101, which can support the electronic device 2100 to implement the method steps performed by the first electronic device or the second electronic device in the above data synchronization method embodiment. The processor 2101 can be a general-purpose processor or a special-purpose processor. For example, the processor 2101 can be a central processing unit (CPU), a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, such as discrete gates, transistor logic devices or discrete hardware components.
[0424] The processor 2101 may be used to control the electronic device 2100, execute software programs, and process data of the software programs. The electronic device 2100 may also include a communication unit 2105 to implement input (reception) and output (transmission) of signals.
[0425] For example, the electronic device 2100 may be a chip, the communication unit 2105 may be an input and / or output circuit of the chip, or the communication unit 2105 may be a communication interface of the chip, and the chip may be a component of a terminal device or other electronic devices.
[0426] For another example, the electronic device 2100 may be a terminal device, and the communication unit 2105 may be a transceiver of the terminal device, or the communication unit 2105 may be a transceiver circuit of the terminal device.
[0427] The electronic device 2100 may include one or more memories 2102, on which a program 2104 is stored. The program 2104 can be executed by the processor 2101 to generate instructions 2103, so that the processor 2101 executes the steps of the method executed by the first electronic device or the second electronic device in the data synchronization method provided in the embodiment of the present application according to the instructions 2103.
[0428] Optionally, data may be stored in the memory 2102. Optionally, the processor 2101 may read data stored in the memory 2102, which may be stored at the same storage address as the program 2104, or may be stored at a different storage address than the program 2104.
[0429] The processor 2101 and the memory 2102 may be provided separately or integrated together; for example, integrated on a system on chip (SOC) of a terminal device.
[0430] Exemplarily, the memory 2102 can be used to store the relevant program 2104 of the data synchronization method provided in the embodiment of the present application, and the processor 2101 can be used to call the relevant program 2104 of the data synchronization method stored in the memory 2102 to execute the steps of the method executed by the first electronic device or the second electronic device in the data synchronization method provided in the embodiment of the present application.
[0431] The present application also provides a computer program product, which, when executed by the processor 2101, implements the data synchronization method described in any method embodiment of the present application.
[0432] The computer program product may be stored in the memory 2102 , for example, a program 2104 , which is converted into an executable target file that can be executed by the processor 2101 after preprocessing, compilation, assembly, and linking.
[0433] The present application also provides a computer-readable storage medium on which a computer program is stored, and when the computer program is executed by a computer, the data synchronization method described in any method embodiment of the present application is implemented. The computer program can be a high-level language program or an executable target program.
[0434] The computer-readable storage medium is, for example, a memory 2102. The memory 2102 may be a volatile memory or a nonvolatile memory, or the memory 2102 may include both a volatile memory and a nonvolatile memory. Among them, the nonvolatile memory may be a read-only memory (ROM), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), an electrically erasable programmable read-only memory (EEPROM), or a flash memory. The volatile memory may be a random access memory (RAM), which is used as an external cache. By way of example and not limitation, many forms of RAM are available, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate synchronous dynamic random access memory (DDR SDRAM), enhanced synchronous dynamic random access memory (ESDRAM), synchronous link dynamic random access memory (SLDRAM), and direct rambus RAM (DR RAM).
[0435] In this application, "at least one" means one or more, and "more than one" means two or more. "At least one of the following" or similar expressions refers to any combination of these items, including any combination of single or plural items. For example, at least one of a, b, or c can be represented by: a, b, c, ab, ac, bc, or abc, where a, b, c can be single or multiple.
[0436] It should be understood that in the various embodiments of the present application, the size of the serial numbers of the above-mentioned processes does not mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present application.
[0437] Those of ordinary skill in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.
[0438] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.
[0439] In the several embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are merely schematic; for example, the division of the units is only a logical function division, and there may be other division methods in actual implementation; for example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0440] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0441] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.
[0442] The above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any person skilled in the art who is familiar with the present technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.
Claims
1. A data synchronization method, characterized in that: Applied in a first electronic device, the method includes: receiving a synchronization request from a second electronic device, the synchronization request being used to request that first data in a first database in the first electronic device be backed up to a second database in the second electronic device, the synchronization request including a second version number corresponding to the second database; In the case where the second version number is different from the first version number corresponding to the first database, a synchronization file is determined according to the first database, the synchronization file includes the first data and the first version number, the first data is data associated with a first transaction, the first version number is a version number different from the third version number corresponding to the third database determined when the first electronic device successfully updates a third database in the first electronic device according to the first transaction and fails to update a log of the third database according to the first transaction, the log of the third database matches the third database, the first database is a database obtained after the first transaction is successfully executed on the third database and the third version number is updated to the first version number, the third version number is the same as the second version number, and the data in the third database is the same as the data in the second database; The synchronization file is sent to the second electronic device so that the second electronic device updates the second database using the synchronization file to obtain a fourth database, wherein the data in the fourth database is the same as the data in the first database, and the version number corresponding to the fourth database is the first version number.
2. The method according to claim 1, characterized in that Before receiving the synchronization request from the second electronic device, the method further includes: The third database is updated according to the first transaction to obtain a fifth database; In the case that the update of the log of the third database according to the first transaction fails, deleting all data operation statements in the log of the third database, and updating the third version number in the log of the third database to the first version number, obtaining the log of the first database, and all the data operation statements are used to generate data in the third database; The third version number in the fifth database is updated to the first version number to obtain the first database, wherein the first version number corresponding to the first database is the first version number in the log of the first database or the first version number in the first database.
3. The method according to claim 2, characterized in that The log of the third database and the third database also record a second transaction identifier of the second transaction, the all data operation statements include the data operation statements of the second transaction, and the data in the third database include data determined according to the data operation statements of the second transaction; as well as, The updating of the third database according to the first transaction to obtain a fifth database includes: updating the data in the third database according to the first transaction, and updating the second transaction identifier according to the first transaction identifier of the first transaction, to obtain the fifth database including the first transaction identifier; and In the case where the log update of the third database according to the first transaction fails, deleting all data operation statements in the log of the third database, and updating the third version number in the log of the third database to the first version number, and obtaining the log of the first database, includes: In a process of updating the log of the third database according to the first transaction, when the first electronic device is powered off and then powered on again, determining that the first transaction identifier in the fifth database and the second transaction identifier in the log of the third database are different; When the first transaction identifier in the fifth database and the second transaction identifier in the log of the third database are different, all data operation statements in the log of the third database are deleted, and the third version number in the log of the third database is updated to the first version number to obtain the log of the first database.
4. The method according to claim 1, characterized in that: Before sending the synchronization file to the second electronic device, the method further includes: In the case where the second version number is the same as the first version number corresponding to the first database, the synchronization file is determined according to the first log of the first database, and the first log matches the first database, wherein, in the case where the first electronic device successfully obtains the first database by updating the third database according to the first transaction, and successfully obtains the first log by updating the log of the third database according to the first transaction, the first version number is the same as the third version number corresponding to the third database.
5. The method according to claim 4, characterized in that When the second version number is the same as the first version number corresponding to the first database, determining the synchronization file according to the first log of the first database includes: In a case where the second version number is the same as the first version number corresponding to the first database, and the first verification code and the second verification code are the same, it is determined that the first log has not been modified, the first verification code is a verification code of a first data operation statement at a first position recorded in the first log, the second verification code is a verification code generated by the first electronic device according to a second data operation statement obtained from the first position in the first log, the first data operation statement and the second data operation statement are the same, and the first data operation statement at the first position recorded in the first log is a data operation statement of the first transaction; In the case that the first log has not been modified, the synchronization file is determined based on the first serial number of the first log and the second serial number in the synchronization request, the synchronization file including the first data operation statement, the first serial number being used to indicate the location of data synchronized to the first database by the first electronic device before receiving the synchronization request, and the second serial number being used to indicate the location of data requested to be synchronized to the first database by the synchronization request.
6. The method according to claim 5, characterized in that The synchronization file also includes the first verification code or the second verification code.
7. The method according to claim 4, characterized in that In a case where the second version number is the same as the first version number corresponding to the first database, determining the synchronization file according to the first log of the first database includes: When the second version number is the same as the first version number corresponding to the first database, and the first verification code and the second verification code are different, it is determined that the first log is modified, the first verification code is a verification code of a first data operation statement at a first position recorded in the modified first log, the second verification code is a verification code generated by the first electronic device according to a second data operation statement obtained from the first position in the modified first log, the first data operation statement and the second data operation statement are different, and the first data operation statement at the first position recorded in the modified first log is a data operation statement of the first transaction; In the case where the first log is modified, the synchronization file is determined according to the first database, and the synchronization file includes the first data.
8. The method according to any one of claims 5 to 7, characterized in that: The first verification code is located at a second position in the first log, and the second position is associated with the first position. After receiving the synchronization request from the second electronic device, the method further includes: Acquire the first data operation statement from the first position in the first log according to the first sequence number of the first log and the second sequence number in the synchronization request; The first verification code is generated according to the first data operation statement, and the first verification code is written into the second position of the first log.
9. The method according to any one of claims 4 to 8, characterized in that: a log of the third database and a second transaction identifier of the second transaction recorded in the third database, all data operation statements in the log of the third database include data operation statements of the second transaction, and the data in the third database includes data determined according to the data operation statements of the second transaction; as well as, Before receiving the synchronization request from the second electronic device, the method further includes: updating the data in the third database according to the first transaction, and updating the second transaction identifier in the third database according to the first transaction identifier of the first transaction, to obtain the first database; All the data operation statements in the log of the third database are updated according to the first transaction, and the second transaction identifier in the log of the third database is updated according to the first transaction identifier, so as to obtain the first log.
10. The method according to any one of claims 1 to 9, characterized in that: The synchronization request also includes a second sequence number, where the second sequence number is used to indicate a location of data requested to be synchronized to the first database by the synchronization request, and the synchronization file also includes the second sequence number.
11. The method according to any one of claims 1 to 10, characterized in that: The first database and the third database are associated with a first application in the first electronic device, and the second database and the fourth database are associated with a second application in the second electronic device.
12. The method according to claim 11, characterized in that The first database and the second database are embedded databases.
13. The method according to any one of claims 1 to 12, characterized in that: The first electronic device includes a first database engine and a first synchronization service, The receiving a synchronization request from a second electronic device comprises: The first synchronization service receives the synchronization request from the second electronic device; The method further comprises: The first synchronization service sends the synchronization request to the first database engine; The step of determining the synchronization file according to the first database when the second version number is different from the first version number corresponding to the first database includes: The first database engine determines the synchronization file according to the first database when the second version number is different from the first version number; The method further comprises: The first database engine sends the synchronization file to the first synchronization service; The sending the synchronization file to the second electronic device includes: The first synchronization service sends the synchronization file to the second electronic device.
14. A data synchronization method, characterized in that: Applied in a first electronic device, the method includes: receiving a synchronization request from a second electronic device, the synchronization request being used to request that first data in a first database in the first electronic device be backed up to a second database in the second electronic device, the synchronization request including a second version number corresponding to the second database; In the case where the second version number is the same as the first version number corresponding to the first database, the synchronization file is determined according to the first log of the first database, and the first log matches the first database, wherein, in the case where the first electronic device successfully updates the third database according to the first transaction to obtain the first database, and successfully updates the log of the third database according to the first transaction to obtain the first log, the first version number is the same as the third version number corresponding to the third database, the log of the third database matches the third database, the third version number is the same as the second version number, and the data in the third database is the same as the data in the second database; The synchronization file is sent to the second electronic device, so that the second electronic device updates the second database using the synchronization file to obtain a fourth database, wherein the data in the fourth database is the same as the data in the first database.
15. The method according to claim 14, characterized in that In a case where the second version number is the same as the first version number corresponding to the first database, determining the synchronization file according to the first log of the first database includes: In a case where the second version number is the same as the first version number corresponding to the first database, and the first verification code and the second verification code are the same, it is determined that the first log has not been modified, the first verification code is a verification code of a first data operation statement at a first position recorded in the first log, the second verification code is a verification code generated by the first electronic device according to a second data operation statement obtained from the first position in the first log, the first data operation statement and the second data operation statement are the same, and the first data operation statement at the first position recorded in the first log is a data operation statement of the first transaction; In the case that the first log has not been modified, the synchronization file is determined based on the first serial number of the first log and the second serial number in the synchronization request, the synchronization file including the first data operation statement, the first serial number being used to indicate the location of data synchronized to the first database by the first electronic device before receiving the synchronization request, and the second serial number being used to indicate the location of data requested to be synchronized to the first database by the synchronization request.
16. The method according to claim 14, characterized in that In a case where the second version number is the same as the first version number corresponding to the first database, determining the synchronization file according to the first log of the first database includes: When the second version number is the same as the first version number corresponding to the first database, and the first verification code and the second verification code are different, it is determined that the first log is modified, the first verification code is a verification code of a first data operation statement at a first position recorded in the modified first log, the second verification code is a verification code generated by the first electronic device according to a second data operation statement obtained from the first position in the modified first log, the first data operation statement and the second data operation statement are different, and the first data operation statement at the first position recorded in the modified first log is a data operation statement of the first transaction; In the case where the first log is modified, the synchronization file is determined according to the first database, and the synchronization file includes the first data.
17. A data synchronization method, characterized in that: Applied in a second electronic device, the method includes: Sending a synchronization request to the first electronic device, the synchronization request being used to request to back up first data in a first database in the first electronic device to a second database in the second electronic device, the synchronization request including a second version number corresponding to the second database; receiving a synchronization file from the first electronic device; In the case where the synchronization file includes the first data and a first version number corresponding to the first database, the data in the second database is updated according to the first data, and the second version number in the second database is updated according to the first version number to obtain a fourth database, the data in the fourth database is the same as the data in the first database, and the version number corresponding to the fourth database is the first version number, wherein the synchronization file is determined by the first electronic device according to the first database when the second version number is different from the first version number, the first data is data associated with a first transaction, the first version number is a version number different from the third version number corresponding to the third database determined when the first electronic device successfully updates a third database in the first electronic device according to the first transaction and fails to update the log of the third database according to the first transaction, the log of the third database matches the third database, and the first database is a database obtained after successfully executing the first transaction on the third database and updating the third version number to the first version number.
18. The method according to claim 17, characterized in that The method further comprises: In the case where the synchronization file includes the first data operation statement of the first transaction, the first data operation statement is executed on the second database to obtain the fourth database, and the data in the fourth database is the same as the data in the first database, wherein: The synchronization file is determined by the first electronic device according to the first serial number of the first log and the second serial number in the synchronization request, when the second version number is the same as the first version number corresponding to the first database and the first electronic device determines that the first log of the first database has not been modified according to the first verification code and the second verification code, the first log matches the first database, the first verification code is the verification code of the first data operation statement at the first position recorded in the first log, the second verification code is the verification code generated by the first electronic device according to the second data operation statement obtained from the first position in the first log, the first data operation statement is the same as the second data operation statement, the first serial number is used to indicate the position of the data synchronized to the first database by the first electronic device before receiving the synchronization request, and the second serial number is used to indicate the position of the data requested to be synchronized to the first database by the synchronization request; and, when the first electronic device successfully obtains the first database by updating the third database according to the first transaction, and successfully obtains the first log by updating the log of the third database according to the first transaction, the first version number is the same as the third version number corresponding to the third database.
19. The method according to claim 18, characterized in that The synchronization file further includes the first verification code or the second verification code; and In a case where the synchronization file includes a first data operation statement of the first transaction, before executing the first data operation statement on the second database to obtain the fourth database, the method further includes: Generate a third verification code according to the first data operation statement in the synchronization file; In a case where the third verification code is identical to the verification code included in the synchronization file, it is determined that the first data operation statement in the synchronization file passes the verification.
20. The method according to claim 17, characterized in that In the case where the synchronization file includes the first data, the data in the second database is updated according to the first data to obtain the fourth database, and the data in the fourth database is the same as the data in the first database, wherein: The synchronization file is determined by the first electronic device according to the first database when the second version number is the same as the first version number corresponding to the first database and the first electronic device determines that the first log of the first database is modified according to the first verification code and the second verification code, wherein: The first verification code is a verification code of a first data operation statement at a first position recorded in the modified first log, the second verification code is a verification code generated by the first electronic device from a second data operation statement obtained from the first position in the modified first log, the first data operation statement and the second data operation statement are different, and the first data operation statement at the first position recorded in the modified first log is a data operation statement of the first transaction; and, when the first electronic device successfully updates the third database according to the first transaction to obtain the first database, and successfully obtains the first log according to the log update of the third database according to the first transaction, the first version number is the same as the third version number corresponding to the third database.
21. The method according to claim 17 or 20, characterized in that Before obtaining the fourth database, the method further includes: Perform data verification on the first data in the synchronization file to determine whether the first data passes the verification.
22. The method according to any one of claims 17 to 21, characterized in that The second electronic device includes a second database engine, a second synchronization service and a second application, Before sending the synchronization request to the first electronic device, the method further includes: The second synchronization service receives the synchronization request sent from the second application; The sending a synchronization request to the first electronic device includes: The second synchronization service sends the synchronization request to the first electronic device; The receiving the synchronization file from the first electronic device includes: The second synchronization service receives the synchronization file; The method further comprises: The second synchronization service sends the synchronization file to the second database engine; In a case where the synchronization file includes the first data and a first version number corresponding to the first database, updating the data in the second database according to the first data, and updating the second version number in the second database according to the first version number to obtain a fourth database, comprises: When the synchronization file includes the first data and the first version number corresponding to the first database, the second database engine updates the data in the second database according to the first data, and updates the second version number in the second database according to the first version number, to obtain the fourth database.
23. The method according to any one of claims 17 to 22, characterized in that The first database and the third database are associated with a first application in the first electronic device, and the second database and the fourth database are associated with a second application in the second electronic device.
24. The method according to claim 23, characterized in that The first database and the second database are embedded databases.
25. A data synchronization method, characterized in that: Applied in a system including a first electronic device and a second electronic device, the method includes: The second electronic device sends a synchronization request to the first electronic device, wherein the synchronization request is used to request to back up first data in a first database in the first electronic device to a second database in the second electronic device, and the synchronization request includes a second version number corresponding to the second database; In the case where the second version number is different from the first version number corresponding to the first database, the first electronic device determines a synchronization file according to the first database, the synchronization file including the first data and the first version number, the first data is data associated with a first transaction, the first version number is a version number different from a third version number corresponding to the third database determined when the first electronic device successfully updates a third database in the first electronic device according to the first transaction and fails to update a log of the third database according to the first transaction, the log of the third database matches the third database, the first database is a database obtained after the first transaction is successfully executed on the third database and the third version number is updated to the first version number, the third version number is the same as the second version number, and the data in the third database is the same as the data in the second database; The first electronic device sends the synchronization file to the second electronic device; The second electronic device updates the data in the second database according to the first data, and updates the second version number in the second database according to the first version number to obtain a fourth database, the data in the fourth database is the same as the data in the first database, and the version number corresponding to the fourth database is the first version number.
26. A data synchronization device, characterized in that: Applied in a first electronic device, the data synchronization device comprises a processing unit, and the processing unit is used to: receiving a synchronization request from a second electronic device, the synchronization request being used to request that first data in a first database in the first electronic device be backed up to a second database in the second electronic device, the synchronization request including a second version number corresponding to the second database; In the case where the second version number is different from the first version number corresponding to the first database, a synchronization file is determined according to the first database, the synchronization file includes the first data and the first version number, the first data is data associated with a first transaction, the first version number is a version number different from the third version number corresponding to the third database determined when the first electronic device successfully updates a third database in the first electronic device according to the first transaction and fails to update a log of the third database according to the first transaction, the log of the third database matches the third database, the first database is a database obtained after the first transaction is successfully executed on the third database and the third version number is updated to the first version number, the third version number is the same as the second version number, and the data in the third database is the same as the data in the second database; The synchronization file is sent to the second electronic device so that the second electronic device updates the second database using the synchronization file to obtain a fourth database, wherein the data in the fourth database is the same as the data in the first database, and the version number corresponding to the fourth database is the first version number.
27. A data synchronization device, characterized in that: Applied in a first electronic device, the data synchronization device comprises a processing unit, and the processing unit is used to: receiving a synchronization request from a second electronic device, the synchronization request being used to request that first data in a first database in the first electronic device be backed up to a second database in the second electronic device, the synchronization request including a second version number corresponding to the second database; In the case where the second version number is the same as the first version number corresponding to the first database, the synchronization file is determined according to the first log of the first database, and the first log matches the first database, wherein, in the case where the first electronic device successfully updates the third database according to the first transaction to obtain the first database, and successfully updates the log of the third database according to the first transaction to obtain the first log, the first version number is the same as the third version number corresponding to the third database, the log of the third database matches the third database, the third version number is the same as the second version number, and the data in the third database is the same as the data in the second database; The synchronization file is sent to the second electronic device, so that the second electronic device updates the second database using the synchronization file to obtain a fourth database, wherein the data in the fourth database is the same as the data in the first database.
28. A data synchronization device, characterized in that: Applied in a second electronic device, the data synchronization device comprises a processing unit, and the processing unit is used to: Sending a synchronization request to the first electronic device, the synchronization request being used to request to back up first data in a first database in the first electronic device to a second database in the second electronic device, the synchronization request including a second version number corresponding to the second database; receiving a synchronization file from the first electronic device; In the case where the synchronization file includes the first data and a first version number corresponding to the first database, the data in the second database is updated according to the first data, and the second version number in the second database is updated according to the first version number to obtain a fourth database, the data in the fourth database is the same as the data in the first database, and the version number corresponding to the fourth database is the first version number, wherein the synchronization file is determined by the first electronic device according to the first database when the second version number is different from the first version number, the first data is data associated with a first transaction, the first version number is a version number different from the third version number corresponding to the third database determined when the first electronic device successfully updates a third database in the first electronic device according to the first transaction and fails to update the log of the third database according to the first transaction, the log of the third database matches the third database, and the first database is a database obtained after successfully executing the first transaction on the third database and updating the third version number to the first version number.
29. A data synchronization system, characterized in that: The data synchronization system comprises a first electronic device and a second electronic device. The second electronic device is used to: send a synchronization request to the first electronic device, wherein the synchronization request is used to request to back up the first data in the first database in the first electronic device to the second database in the second electronic device, and the synchronization request includes a second version number corresponding to the second database; The first electronic device is used to: determine a synchronization file according to the first database when the second version number is different from a first version number corresponding to the first database, the synchronization file including the first data and the first version number, the first data being data associated with a first transaction, the first version number being a version number different from a third version number corresponding to the third database determined when the first electronic device successfully updates a third database in the first electronic device according to the first transaction and fails to update a log of the third database according to the first transaction, the log of the third database matches the third database, the first database is a database obtained after the first transaction is successfully executed on the third database and the third version number is updated to the first version number, the third version number is the same as the second version number, and the data in the third database is the same as the data in the second database; The first electronic device is further used to: send the synchronization file to the second electronic device; The second electronic device is also used to: update the data in the second database according to the first data, and update the second version number in the second database according to the first version number to obtain a fourth database, the data in the fourth database is the same as the data in the first database, and the version number corresponding to the fourth database is the first version number.
30. An electronic device, characterized in that: The electronic device includes a processor and a memory, the memory is used to store a computer program, and the processor is used to call and run the computer program from the memory, so that the processor executes the data synchronization method described in any one of claims 1 to 13, 14 to 16, or claims 17 to 24.