# Patent

## Title
Database synchronization

## Abstract

Synchronization of data items between parties A and B. A maintains copies of data items and an A change count and a B change count for each data item. The A change count indicates the number of changes made by A, and the B change count indicates the number of changes made by B and known to A. After a synchronization trigger, A sends to B each data item that A has changed, plus the A change count and the B. change count for each sent data item. A receives from B each data item that B has changed, plus the A change count and the B change count for each received data item. A detects a conflict if the received A change count differs from the maintained A change count.

## Background
CROSS-REFERENCE TO RELATED APPLICATIONS
This application relies for priority upon Finnish Application No. 20045472, filed Dec. 10, 2004, as well as U.S. Provisional Application No. 60/651,080, filed Feb. 9, 2005, the contents of both of which are incorporated herein by reference in their entireties.


BACKGROUND OF THE INVENTION
The invention relates to methods and equipment for synchronizing a database between two parties. In a very common but non-restricting application, one party is a desktop computer, while the other one is a portable computer, a pocket computer or a “smart” telephone. In most applications each party contains its own data processor, but the invention is also usable in applications in which a single data processor synchronizes data items between two data storages, and the parties may be construed as processes executing in a single processor. The database contains data items, such as documents or calendar events, that can be changed at either party. As commonly used in the context of databases, synchronization means reducing or eliminating differences between the database copies maintained at the parties, which are supposedly similar but at least one database contains changed data items which are not reflected in the other database.
It is well known to provide data items with timestamps that indicate the time of the latest change. Graphical user interfaces use a ‘drag and drop’ metaphor with which a user may drag a set of files from one location to another such that the more recent versions of the files replace the older ones. Synchronization between two folders can be achieved by performing the ‘drag and drop’ operation in both directions. But the timestamp-based synchronization cannot be used if the data items do not have timestamps. A further problem is that if the parties' clocks are offset in relation to each other, an older version of a data item may accidentally overwrite a more recent one.
BRIEF DESCRIPTION OF THE INVENTION
An object of the present invention is to provide a method and an apparatus for implementing the method so as to solve the above problems. The objects of the invention are achieved by a method and an arrangement which are characterized by what is stated in the independent claims. The preferred embodiments of the invention are disclosed in the dependent claims.
An aspect of the invention is a method for synchronizing a database between an A party and a B party. Another aspect of the invention is an apparatus adapted to act as one of the parties in the method. Yet another aspect of the invention is a computer program product that causes a data processor to carry out the method when the program product is executed.
The invention is based on the idea that each party maintains two change counts per data item. A first change count keeps track of changes to the data item by the party itself, while the second change count keeps track of changes to the data item by its peer, ie, the other party. At each synchronization event, either party sends its peer the data items it has changed after the previous synchronization event plus the two change counts. The sent change counts tell the other party how many changes the party has made and how many peer changes the party is aware of. Either party detects a conflict if the count of changes by the party, as maintained by its peer, differs from the corresponding change count as maintained by the party itself. Such a difference in change counts serves as an indication that the data item received from the peer did not reflect all changes made by the party that detects the conflict.
In a variant of the above inventive idea, each party may send its peer an identifier of each data item it has changed, plus the two change counts. By comparing the received change counts with the corresponding change counts maintained locally, the parties can determine which data items have been changed after the latest synchronization, and request the transmission of the changed data items separately. The two techniques can be summarized by saying that each party sends its peer an indication of a data item it has changed, plus the two change counts. The indication may be an identifier of the data item or the data item itself.
In addition to the two change counts per party, it is beneficial to maintain some record-keeping of which data items have been changed after the latest synchronization, whereby only the changed data items need to be synchronized. For example, such a record-keeping can be based on time stamps of data items that are compared with the time of the latest synchronization. Alternatively, the record-keeping can be based on a one-bit flag, which is marked “changed” when the data item is changed and “unchanged” when the data items are synchronized.
As an alternative to the record-keeping of data items changed after the latest synchronization, the parties may send changed data items or indications thereof immediately, if this is possible. In some implementations, one application (such as a calendar application) may be responsible for changing the data items (calendar events), while a separate application (a communications application or protocol layer) is responsible for actually sending the changed data items.
An advantage of the method and arrangement of the invention is that synchronization of data items is possible without providing each data item with a data stamp. A further advantage is that synchronization is possible even if the parties' clocks are offset in relation to each other.
The invention is particularly suitable in mobile applications because a party receiving a changed data item does not have to acknowledge the reception of received data item. The party sending the changed data item can determine from the next pair of change counts if its peer actually received the changed data item.



BRIEF DESCRIPTION OF THE DRAWINGS
In the following the invention will be described in greater detail by means of preferred embodiments with reference to the drawings, in which
FIG. 1 shows a hypothetical set of events in a synchronization process according to the invention;
FIG. 2 shows a user interface screen for user-assisted conflict resolution; and
FIG. 3 shows a combination of data items in a conflict situation.



DETAILED DESCRIPTION OF PREFERRED EMBODIMENTS
FIG. 1 shows a hypothetical set of events in a synchronization process according to the invention. The left-hand side of FIG. 1 relates to the A party, while the right-hand side relates to the B party. Reference numerals 100A and 100B denote copies (versions) of a data item at the A and B party, respectively.
In step 1-0 a new data item 100A, such as an electronic document or calendar event, is created at the A party. At this time the data item does not yet exist at the B party. In step 1-2 the A party detects a synchronization trigger and sends the data item 100A to the B party. The synchronization trigger may be the simultaneous existence of a changed (in this case: newly-created) data item and a data connection between the parties. Both parties maintain two change counts that relate to the data item. The change counts are denoted by the following reference signs:

    
    
        NAA, number of changes made by A, as seen by A;
        NBA, number of changes made by B, as seen by A;
        NAB, number of changes made by A, as seen by B; and
        NBB, number of changes made by B, as seen by B.
    
    


In the embodiment shown in FIG. 1, the creation of the data item is considered a change, which is why after steps 1-0 and 1-2 the change counts NAA and NAB have a value of ‘1’. However, the invention operates equally well if the initial creation of a data item is not considered a change and the change counts begin at zero.
In step 1-4 the A party changes the data item 100A. Synchronization does not take place immediately, possibly because a data connection between the parties does not exist. The NAA change count is ‘2’ because A knows it has changed the data item twice. The NAB change count remains at ‘1’ because B is not yet aware of the latest change.
In step 1-6 the data item is again synchronized. In other words, A sends to B the data item 100A and the NAA and NBA change counts. B compares the received data counts with the respective data counts NAB and NBB maintained at the B party. Since NAA=‘2’ and NAB=‘1’, B detects that the data item has been changed by A after the previous synchronization. B can accept the changed data item because the NBA change count equals the NBB change count (both are zeros).
In step 1-8 the B party changes its copy 100B of the data item. In the interest of clarity, the A and B parties respectively add letters ‘A’ and ‘B’, in upper or lower case, to the data item. Since this is B's first change to the data item, B also changes the NBB change count to ‘1’. The NBA change count remains at ‘0’ because A is not yet aware of the change made by B.
In step 1-8 the data item is again synchronized. B sends to A the data item 100B and the NAB and NBB change counts. A compares the received data counts with the respective data counts NAA and NBA maintained at the A party. Since NBB=‘1’ and NBA=‘0’, the A party detects that the data item has been changed by B after the previous synchronization in step 1-6. A can accept the changed data item because the NAB change count equals the NAA change count (both have a value of ‘2’).
In step 1-12 both parties A and B change their respective copies 100A and 100B of the data item and the change counts. The A party increments the NAA change count to ‘3’, while B increments the NBB change count to ‘2’. But neither party is yet aware of the changes made by its peer.
Reference numeral 1-14A describes a situation in which B attempts to send the data item 100B to A. B also sends the NAB and NBB change counts. A compares the received data counts with the respective data counts NAA and NBA maintained at the A party. Since NBB=‘2’ and NBA=‘1’, A detects that the data item has been changed by B after the previous synchronization. But A cannot accept the changed data item because the received NAB change count (with a value of ‘2’) is lower than the locally maintained NAA change count (with a value of ‘3’). Thus A detects a conflict if the NAB change count (count of changes made by A and known to B) is lower than the NAA change count, ie, the corresponding change count maintained by A itself. In such a situation, A detects that the data item sent by B did not include all the changes made by A, and if A accepted the data item, some of the changes made by A would be lost. The big ‘X’ over the arrow in step 1-14A signifies the fact that A does not accept the changed data item, at least not without some conflict resolution. For the purposes of FIG. 1, it is assumed that A rejects the data item sent by B.
Reference numeral 1-14B describes a situation in which A attempts to send the data item 100A to B. The situation is symmetrical to the previously described situation 1-14A. In this case B detects a conflict because the change count NBA (with a value of 1) received from A is lower than the corresponding change count NBB (with a value of 2) maintained by B itself. Thus B detects that the data item sent by A did not include all the changes made by B.
In the scenario shown in FIG. 1, it was assumed that in the synchronization events 1-6 and 1-10, the changed data items 100A and 100B were sent with the change counts. It is also possible to send only an identifier of a changed data item, whereby the party receiving the change counts will compare the received change counts with their locally maintained counterparts and request the actual data item only if it has been changed.
FIG. 2 shows a user interface screen for user-assisted conflict resolution. In step 1-12 of FIG. 1, the copies 100A and 100B of the data item was changed by both parties A and B when synchronization was not possible. The synchronization attempts in step 1-14A or 1-14B caused a conflict detection as either party noted that it its peer had not been aware of all changes made to the data item.
The synchronization technique preferably comprises a set of predetermined rules for conflict resolution. The applicable rules may be displayed for user selection, or they may be selected by some predetermined criteria that may, for example, be based on the type of the data item.
FIG. 2 shows a user interface screen 20. Reference numeral 22 denotes a menu of the applicable rules. For example, the user may select to select the more recent copy of the data items 100A and 100B, assuming that each data item has a time stamp. Alternatively, the user may select the bigger version, or one version (A's or B's) explicitly. Or, the user may opt to display both versions simultaneously and made the selection after seeing both versions. Finally, the user may opt to combine the versions of the data items.
FIG. 3 shows a combined data item 30. Reference numerals 31 and 32 respectively denote data obtained from the A party (version 100A) and the B party (version 100B).
Instead of presenting the applicable rules for user selection, the party that detects the conflict may select one of the rules automatically, preferably on the basis of the type of the data item. For example, if the data items are calendar events, it is relatively safe to assume that the newer one is to replace the older one. If the data items are word processing documents, the documents may be combined for later editing by the user.
It will be apparent to a person skilled in the art that, as the technology advances, the inventive concept can be implemented in various ways. The invention and its embodiments are not limited to the examples described above but may vary within the scope of the claims.

## Claims

1. A method for synchronizing a first data storage and a second data storage, the method comprising:
maintaining by a processor a first change count and a second change count for a data item a first data storage, wherein the first change count maintained the processor indicates a number of changes made to the data item at the first data storage, wherein the second change count maintained by the processor indicates a number of changes made to a corresponding data item at a second data storage; and
responsive to a synchronization trigger that is generated when the data item is changed and a data connection is available:
receiving an identifier for the changed data item along with a first change count and a second change count maintained at the second data storage;
detecting a conflict in the data item between the first and second data storages based on the maintained first and second change counts and the received first and second change counts; and
resolving the conflict according to a set of predetermined rules,

wherein applicable rules of the set of predetermined rules are displayed as a menu on a user interface for selection by a user; and
wherein one application is responsible for receiving the identifier for the changed data item and another application is responsible for the changes made to the data item at the first data storage.

2. The method of claim 1, wherein, if the data item is a document, the conflict is resolved by merging conflicting versions for subsequent editing.

3. The method of claim 2, wherein, if the data item having conflicting versions is a calendar event, the conflict is resolved by replacing an older version with a newer version of the conflicting versions.

4. The method of claim 1, wherein resolving the conflict includes displaying conflicting versions of the data item in the user interface and receiving an input from the user interface to resolve the conflict.

5. The method of claim 4, further comprising offering the conflicting versions for user selection via the user interface.

6. The method of claim 1, wherein, the first data storage and the second data storage are coupled via a mobile communication network.

7. The method of claim 1, further comprising:
sending an indication that the data item in the first data storage has changed after the synchronization event, plus the first change count and the second change count maintained in the first data storage.

8. The method of claim 7, further comprising:
sending the first data item, and replacing the corresponding data item in the second data storage with the sent data item if:
the second change count maintained at the second data storage for the data item corresponds to the second change count maintained in the first data storage for the data item; and
the first change count maintained at the second data storage for the data item does not correspond to the first change count maintained in the first data storage for the data item.

9. A system for performing a method for synchronizing a first data storage and a second data storage over a mobile communications network, the system comprising:
means for maintaining in a first data storage a first change count and a second change count for a data item in the first data storage,
wherein the first change count maintained in the first data storage indicates a number of changes made to the data item in the first data storage,
wherein the second change count maintained in the first data storage indicates a number of changes made to a corresponding data item maintained in a second data storage;
means for receiving an indication that the data item in the second data storage has changed after a synchronization event, the indication including an identifier of the changed data item,
means for requesting the changed data item; and
means for resolving a conflict detected from the first and second change counts according to a set of predetermined rules;
wherein applicable rules of the set of predetermined rules are displayed as menu selections on a user interface for selection by a user; and
wherein one application is responsible for receiving an indication that the data item in the second data storage has changed and another application is responsible for the changes made to the data item at the first data storage.

10. The system of claim 9, further comprising means for displaying conflicting versions of the data item in the user interface.

11. The system of claim 10, further comprising means fir offering the set of user selectable options for manipulating the conflicting versions of the data item to resolve the conflict.

12. The system of claim 10, wherein, if the data item having conflicting versions is a calendar event, the conflict is resolved by replacing an older version with a newer version of the conflicting versions; wherein, if the data item is a document, the conflict is resolved by merging the conflicting versions for subsequent editing.

13. The system of claim 9, further comprising means for connecting the first data storage to the second data storage via the mobile communication network.

14. The system of claim 9, further comprising,
means for receiving, after the latest synchronization event, a first change count maintained in the second data storage for the data item,
wherein the first change count maintained in the second data storage indicates a number of changes made to the data item maintained at the first data storage.

15. The system of claim 14, wherein the conflict is detected from the first change count maintained in the second data storage and the first change count maintained in the first data storage.

16. The system of claim 9, further comprising,
means for receiving, after the latest synchronization event, a second change count maintained at the second data storage for each received data item,
wherein the second change count maintained at the second data storage indicates a number of changes made to the data item at the second data storage.

17. An apparatus for synchronizing a first data storage and a second data storage, the apparatus comprising:
a memory configured to:
maintain a first change count and a second change count for each data item in a first data storage, wherein the first change count indicates a number of changes made to the data item in the first data storage,
wherein the second change count indicates a number of changes made to a corresponding data item in a second data storage;
a processor configured to execute instructions to:
detect a conflict in the data item between the first and second data storages based on the first and second change counts, in response to a synchronization trigger; and
resolve the conflict according to a set of predetermined rules,
wherein applicable rules of the set of predetermined rules are displayed as menu selections on a user interface for selection by a user; and
wherein one application is responsible for receiving an indication that the data item in the second data storage has changed and another application is responsible for the changes made to the data item in the first data storage.