如何解决连续复制中的冲突

编程入门 行业动态 更新时间:2024-10-28 18:27:40
本文介绍了如何解决连续复制中的冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我对CouchDB和PouchDB还是陌生的,并且正在使用它来创建联系人管理系统,该系统可以在移动设备和台式机设备之间进行同步,并且可以脱机使用。我发现使用PouchDB比编写PHP / MySQL后端要容易得多。

I'm new to both CouchDB and PouchDB and am using it to create a contact management system that syncs across mobile and desktop devices, and can be used offline. I am seeing that using PouchDB is infinitely easier than having to write a PHP/MySQL backend.

我一直在成功使用它,当我在脱机设备上进行有冲突的更改时,CouchDB使用一种算法来任意选择一个获胜者,然后将其正确推送到所有设备。

I have been using it successfully, and when I make conflicting changes on offline devices, CouchDB uses an algorithm to arbitrarily pick a winner and then correctly pushes it to all the devices.

我想做的是实现一个自定义算法来合并有冲突的记录。这是我要使用的算法:

What I would like to do is implement a custom algorithm to merge conflicting records. Here is the algorithm I would like to use:

  • 如果一条记录在一个客户端上被删除而仅在另一个客户端上被更新,则除非两个客户都同意删除,否则更新版本将获胜。
  • 具有最新修改时间戳的记录将成为主记录,而较旧的记录将成为辅助记录。
  • 仅存在于次要字段中的任何字段(或在主文件中为空的字段)都移至主文件。
  • If a record is deleted on one client and merely updated on another, the updated version wins, unless both clients agree on the delete.
  • The record with the most recent "modified" timestamp becomes the master, and the older record becomes the secondary.
  • Any fields that exist only in the secondary (or are empty in the master) are moved over to the master.
  • The master revision is saved and the secondary is deleted.
  • CouchDB的指南具有很好的解释,但是我不知道如何在连续复制期间使用PouchDB API来实现它。根据 PouchDB API ,复制选项中有一个 onChange侦听器,但我不了解如何使用它来拦截冲突。

    CouchDB's guide has a good explanation, but I don't have a clue how to implement it with the PouchDB API during a continuous replication. According to the PouchDB API, there is an "onChange" listener in the replicate options, but I don't understand how to use it to intercept conflicts.

    如果有人可以编写一个简短的教程(包括一些示例代码),我本人和我肯定还有很多PouchDB用户会

    If someone could write a brief tutorial including some sample code, myself and I'm sure many other PouchDB users would appreciate it!

    推荐答案

    写一篇带有确切如何管理冲突解决方法示例的文章是一个好主意,这可能会造成混淆,但缺少一个

    Writing an article with examples of exactly how to manage conflict resolution is a really good idea, it can be confusing, but with the lack of one

    这个想法与CouchDB完全相同,要解决冲突,您可以删除未获胜的修订(并在需要时写出新的获胜者)

    The idea is the exact same as CouchDB, to resolve conflicts you delete the revisions that didnt win (and write a new winner if needed)

    #1仍然是解决CouchDB冲突的方法,因此您不必担心,删除的叶子不会发生冲突

    #1 is how CouchDB conflict resolution works anyway, so you dont need to worry about that, deleted leafs dont conflict

    function onChange(doc) { if (!doc._conflicts) return; collectConflicts(doc._conflicts, function(docs) { var master = docs.sort(byTime).unshift(); for (var doc in docs) { for (var prop in doc) { if (!(prop in master)) { master[prop] = doc[prop]; } } } db.put(master, function(err) { if (!err) { for (var doc in docs) { db.remove(doc); } } }); }); } } db.changes({conflicts: true, onChange: onChange});

    这将需要错误处理等,并且可能会写得更好,只是快速绘制一下内容代码看起来像

    This will need error handling etc and could be written much nicer, was just a quick napkin drawing of what the code could look like

    更多推荐

    如何解决连续复制中的冲突

    本文发布于:2023-11-16 06:32:26,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1601851.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:如何解决   冲突

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!