为此,我使用差异执行。
(顺便说一句,“奴隶”这个词对某些人来说是有道理的。)
对于每个远程站点,主站点都有一个顺序文件,表示远程站点上存在的内容。
主站点有一个遍历主集合的过程,当它遍历时,它会读取相应的文件,检测远程站点上当前存在的内容与应该存在的内容之间的差异。这些差异会产生增量,并将其传输到远程站点。同时,该过程写入一个新文件,表示处理增量后远程站点将存在的内容。
这样做的好处是它不依赖于检测主集合中的更改事件,因为这些更改事件通常是不可靠的,或者可以自行取消或与其他更改无关,因此您可以减少到远程站点的不必要传输.
在集合是简单的事物列表的情况下,这归结为拥有远程集合的本地副本并运行diff
算法来获取增量。这里有几个这样的算法:
如果可以对集合进行排序(例如您的 A、B、C 示例),只需运行合并循环:
while(ix<nx && iy<ny){
if (X[ix] < Y[iy]){
// X[ix] was inserted in X
ix++;
} else if (Y[iy] < X[ix]){
// Y[iy] was deleted from X
iy++;
} else {
// the two elements are equal. skip them both;
ix++; iy++;
}
}
while(ix<nx){
// X[ix] was inserted in X
ix++;
}
while(iy<ny>){
// Y[iy] was deleted from X
iy++;
}
如果无法对集合进行排序(注意与Levenshtein distance的关系),
Until we have read through both collections X and Y,
See if the current items are equal
else see if a single item was inserted in X
else see if a single item was deleted from X
else see if 2 items were inserted in X
else see if a single item was replaced in X
else see if 2 items were deleted from X
else see if 3 items were inserted in X
else see if 2 items in X replaced 1 items in Y
else see if 1 items in X replaced 2 items in Y
else see if 3 items were deleted from X
etc. etc. up to some limit
性能通常不是问题,因为该过程不必以高频率运行。
有一个演示此概念的粗略视频,以及用于动态更改用户界面的源代码。