这是一个功能同步算法吗? [英] Is this a functional syncing algorithm?
问题描述
我正在为用户的笔记工作一个基本的同步算法。我有很多的想法,但在我开始编程之前,我想运行它在这里,看看是否有意义。通常我最终没有意识到一个巨大的重要事情,别人很容易看到,我不能。这是它的工作原理:
我在我的数据库中有一个表插入对象 SyncOperation
。 SyncOperation
是一种关于每个设备需要执行以使其为最新的性质的元数据。假设用户有2个注册设备, firstDevice
和 secondDevice
。 firstDevice
创建一个新笔记并将其推送到服务器。现在,使用注释的Id,操作类型和 processedDeviceList
创建 SyncOperation
。我使用类型 NewNot
e创建一个 SyncOperation
,并将发送设备ID添加到 SyncOperation
的 processedDeviceList
。所以现在 secondDevice
检入服务器,看看是否需要进行任何更新。它进行查询以获取所有 SyncOperations
,其中secondDeviceId不在 processedDeviceList
中。它发现它的类型是 NewNote
,所以它获得新的注释,并将自己添加到 processedDeviceList
。
当我删除记事时,我发现已在表格中创建了 SyncOperation
类型为NewNote。我将类型更改为删除,删除 processedDevicesList
中的所有设备,但删除注释的设备除外。所以现在当新设备调用以查看他们需要更新时,由于他们的deviceId不在 processedList
中,他们必须处理 SyncOperation
,它告诉他们的设备删除相应的笔记。
这通常是如何工作的。我的解决方案太复杂了吗?可以简化吗?任何人都可以想到一种情况,这将不工作?
听起来很复杂 - 中央数据库不应负责确定哪些设备已接收哪些更新。
- 数据库保存一个
SyncOperations表
为每个更改。每个SyncOperation
都有以升序编号的change_id
(即change_id INTEGER PRIMARY KEY AUTOINCREMENT
。) - 每个设备都会保留一个
current_change_id
>
- 当设备要更新时,它会执行
SELECT * FROM SyncOperations WHERE change_id> current_change_id
。这得到它需要是最新的所有更改的列表。 - The database keeps a table of
SyncOperations
for each change. EachSyncOperation
is has achange_id
numbered in ascending order (that is,change_id INTEGER PRIMARY KEY AUTOINCREMENT
.) - Each device keeps a
current_change_id
number representing what change it last saw. - When a device wants to update, it does
SELECT * FROM SyncOperations WHERE change_id > current_change_id
. This gets it the list of all changes it needs to be up-to-date. Apply each of them in chronological order.
这有一个迷人的功能,如果你想,你可以初始化一个新的设备只是通过使用 current_change_id = 0
创建一个新客户端。
请注意,如果两个用户可以进行并发编辑, (编辑胜利?)。您可以自动尝试合并修改,也可以向用户发出通知。如果您想要一些灵感,请查看 git
版本控制系统(或Mercurial或CVS ...)对冲突编辑的操作。
I'm working on a basic syncing algorithm for a user's notes. I've got most of it figured out, but before I start programming it, I want to run it by here to see if it makes sense. Usually I end up not realizing one huge important thing that someone else easily saw that I couldn't. Here's how it works:
I have a table in my database where I insert objects called SyncOperation
. A SyncOperation
is a sort of metadata on the nature of what every device needs to perform to be up to date. Say a user has 2 registered devices, firstDevice
and secondDevice
. firstDevice
creates a new note and pushes it to the server. Now, a SyncOperation
is created with the note's Id, operation type, and processedDeviceList
. I create a SyncOperation
with type "NewNot
e", and I add the originating device ID to that SyncOperation
's processedDeviceList
. So now secondDevice
checks in to the server to see if it needs to make any updates. It makes a query to get all SyncOperations
where secondDeviceId is not in the processedDeviceList
. It finds out its type is NewNote
, so it gets the new note and adds itself to the processedDeviceList
. Now this device is in sync.
When I delete a note, I find the already created SyncOperation
in the table with type "NewNote". I change the type to Delete, remove all devices from processedDevicesList
except for the device that deleted the note. So now when new devices call in to see what they need to update, since their deviceId is not in the processedList
, they'll have to process that SyncOperation
, which tells their device to delete that respective note.
And that's generally how it'd work. Is my solution too complicated? Can it be simplified? Can anyone think of a situation where this wouldn't work? Will this be inefficient on a large scale?
Sounds very complicated - the central database shouldn't be responsible for determining which devices have recieved which updates. Here's how I'd do it:
This has the charming feature that, if you wanted to, you could initialise a new device simply by creating a new client with current_change_id = 0
. Then it would pull in all updates.
Note that this won't really work if two users can be doing concurrent edits (which edit "wins"?). You can try and merge edits automatically, or you can raise a notification to the user. If you want some inspiration, look at the operation of the git
version control system (or Mercurial, or CVS...) for conflicting edits.
这篇关于这是一个功能同步算法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!