0

是否有可能进入putbulkDocs进入 couchdb/pouchdb 并获得与复制中相同的行为,即赢得修订_conflicts而不是409响应?

基本上我想避免conflict以下代码中的情况:

  const docs = Object
    .keys(pendingSet)
    .map(id => toDoc(deepClone(pendingSet[id]), { id, rev: this.revCache.get(id) }))

  const results = await this.db.bulkDocs(docs)
  const conflicts = []

  for (let n = 0; n < results.length; ++n) {
    const result = results[n]
    if (result.error === 'conflict') {
      // TODO: This needs review...
      const doc = await this.db.get(docs[n]._id)
      const rev = `${doc._rev.split('-')[0]}-${this.serverName}`
      conflicts.push({
        ...docs[n],
        _rev: rev
      })
      this.revCache.set(doc._id, rev)
    } else if (result.error) {
      callback(result.error)
    } else {
      this.revCache.set(result.id, result.rev)
    }
  }

  await this.db.bulkDocs(conflicts, { new_edits: false })

我从pouchdb得到了一些提示,但我仍然不确定如何应用它。

EDIT1:使用最新代码更新。

4

2 回答 2

1

CouchDB tries to protect itself from conflicts, so if you try and modify a revision of a document that CouchDB 'knows' that has already been superseded, you will get a 409 response.

The way replication "gets away with it" is because documents are bulk written to the target machine with the flag "new_edits=false". This instructs CouchDB not to police the revision tokens, but to accept the incoming one (writes coming from a replication source already have their own revision trees).

You can do this yourself with calls like this:

ccurl -X POST -d '{"docs":[{"_id":"x","_rev":"1-myrevtoken","y":3}],"new_edits":false}' '/a/_bulk_docs'

In this case I have forced a second "revision 1" into a document that already had a "revision 2":

id = x
├─ 1
│  ├─ 1-myrevtoken
│  └─ 1-a6664c0114e6002415a47b18d4c9d32f
└─ 2-bca8f049e40b76dbfca560e132aa5c31 *

The winner is still "revision 2" but the conflict on revision 1 remains unresolved, until you decide to resolve it.

于 2016-10-14T16:25:38.637 回答
0

使用 CouchDB,您可以all_or_nothing: true在 _bulk_docs 请求中进行设置。无论冲突如何,这都会创建新的修订。如果您没有获得对new_edits: false复制有意义的新修订版,但如果您实际上提交了文档的更新,则可能不会。PouchDB 在其 bulkDocs 上没有 all_or_nothing 选项。

于 2016-12-22T20:52:57.217 回答