我对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:
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
更多推荐
如何解决连续复制中的冲突
发布评论