python couchdb 版本冲突

发布于 2024-12-10 12:41:33 字数 516 浏览 1 评论 0原文

我有一个 python 程序,可以访问 couchDB 数据库、创建新文档并更新现有文档。 对于给定的数据库、db 和文档 Doc,我尝试在每次修改之前小心并重新加载文档:

somedoc = Doc.load(db,id)

在通过更新之前

doc.store(db)

据我所知,这不是必需的,因为每次都应该更新 doc.rev商店被称为。但是......我收到冲突消息:

couchdb.http.ResourceConflict: (u'conflict', u'Document update conflict.')

有什么方法可以强制不更新并显示警告消息,而不是在这些冲突上出现致命错误。或者,更好的是,是否有某种方法可以快速检查文档修订号 - 数据库正在由两个都更新的脚本访问,但两者都小心地加载每个文档,快速进行修改并在尽可能短的时间内更新数据库以尽量减少冲突的可能性......

干杯

I have a python program that accesses a couchDB database, creates new documents and updates existing ones.
For a given database, db, and document, Doc, I try to take care and reload the document before each modification:

somedoc = Doc.load(db,id)

before updating via

doc.store(db)

As far as I am aware this shouldn't be necessary, as doc.rev should be updated each time store is called. BUT ... I am getting conflict messages:

couchdb.http.ResourceConflict: (u'conflict', u'Document update conflict.')

Is there any way of just forcing no update and a warning message rather than having a fatal error upon these conflicts. Or, better, is there some way of quickly checking the document revision number - the db is being accessed by two scripts that both update, but both are careful to load each doc, make modifications quickly and update the db in as little time as possible to minimise the chance of a conflict....

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雪化雨蝶 2024-12-17 12:41:33

我仍然没有弄清楚资源冲突从何而来,考虑到在存储文档之前我检查修订是否正确:

latestRev = CouchDoc.load(db,doc.id)
if latestRev.rev != doc.rev:
    print 'revision mismatch ' + doc.rev + '\t' + revs.rev
else:
    doc.store(db)

但是,有一种简单的方法可以解决导致致命错误的冲突(对于 python 来说是新的,所以之前没有想过这样做):

try:
    doc.store(db)
except couchdb.http.ResourceConflict:
    latestDoc = ConflictDoc.load(db,doc.id)

这确实要求在 couchDB 抛出 ResourceConflict 时,在再次存储之前重做 try 命令之前对 doc 的任何更新。

I still haven't worked out where the resource conflict is coming from, given that just before storing a doc I check that the revision is correct:

latestRev = CouchDoc.load(db,doc.id)
if latestRev.rev != doc.rev:
    print 'revision mismatch ' + doc.rev + '\t' + revs.rev
else:
    doc.store(db)

However, there is an easy way round the conflict causing a fatal error (new to python, so didn't think of doing this before):

try:
    doc.store(db)
except couchdb.http.ResourceConflict:
    latestDoc = ConflictDoc.load(db,doc.id)

This does require that any updates to doc before the try command are redone before storing again when couchDB throws a ResourceConflict.

黒涩兲箜 2024-12-17 12:41:33

您是否检查过以确保在存储时更新了doc的修订版本? 我使用的库不会修改doc,而是在成功存储后返回新版本。

编辑:我猜他们会编辑< code>doc:

>>> db = s.get_or_create_db('test')
>>> doc = { '_id': 'abc', 'content': 'words' }
>>> db.save_doc(doc)
{'rev': '1-a5856d62f2444efb55cbcb5d1db7e02c', 'ok': True, 'id': 'abc'}
>>> doc
{'content': 'words', '_rev': '1-a5856d62f2444efb55cbcb5d1db7e02c', '_id': 'abc'}
>>> 

此示例是使用 couchdbkit 生成的。

Have you checked to make sure that doc's revision is updated when you store? Libraries I've used won't modify doc but instead return the new revision upon a successful store.

Edit: I guess they will edit doc:

>>> db = s.get_or_create_db('test')
>>> doc = { '_id': 'abc', 'content': 'words' }
>>> db.save_doc(doc)
{'rev': '1-a5856d62f2444efb55cbcb5d1db7e02c', 'ok': True, 'id': 'abc'}
>>> doc
{'content': 'words', '_rev': '1-a5856d62f2444efb55cbcb5d1db7e02c', '_id': 'abc'}
>>> 

This sample was generated using couchdbkit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文