问题描述
我想做一个"uppend_multi":一个 append_multi
( http://pythonhosted.org/couchbase/api/couchbase.html#couchbase.bucket.Bucket.append_multi ),其中某些键可能不存在.如果它们尚不存在,则追加操作应为插入操作.我怎样才能做到这一点?API文档中没有任何内容表明支持此功能,但我想这是一个非常常见的操作.
I want to do an "uppend_multi": an append_multi
(http://pythonhosted.org/couchbase/api/couchbase.html#couchbase.bucket.Bucket.append_multi) where some of the keys may not exist. If they do not already exist then the append operation should be an insert. How can I do this? Nothing in the API docs suggests this is supported, but I imagine it is a very common operation.
现在我正在执行此操作,但是需要做的事情似乎很自然,以至于我高度怀疑这是执行此操作的最佳方法:
Right now I am doing this, but it seems so natural of a thing to need to do that I highly doubt this is the best way to do it:
def _uppend_multi(bucket, append_dict):
reinsert_dict = {}
try:
bucket.append_multi(append_dict, format = couchbase.FMT_UTF8)
except CouchbaseError as exc:
for k, res in exc.all_results.items():
if res.success:
pass
else:
reinsert_dict[k] = append_dict[k]
if len(reinsert_dict.keys()) > 0:
bucket.insert_multi(reinsert_dict, format = couchbase.FMT_UTF8)
推荐答案
据我所知,Couchbase Python SDK和较低级别的C API( libcouchbase
).
As far as I know there is no such "uppend" operation in the Couchbase Python SDK, nor in the lower-level C API (libcouchbase
).
您当前正在做的可能是一个非常合理的解决方案.
What you're currently doing is probably a pretty reasonable solution.
这篇关于Couchbase:python SDK"uppend"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!