简体   繁体   English

如何对ArangoDB中的文档字段执行原子操作?

[英]How can I do an atomic operation to a document field in ArangoDB?

I would like to know which is best way to do this with ArangoDB, I started using transactions, or updating it records using revision check, but it's not very good with many concurrent updates. 我想知道哪种最好的方式是使用ArangoDB做到这一点,我开始使用事务,或者使用修订检查来更新记录,但是对于许多并发更新来说,效果不是很好。

With the new release 2.1 I see that that I can register functions in the server to execute every X amount of time, as I'm keeping raw data for a long period of time, I can count it async using one server task but It gives more latency to the counter. 在新版本2.1中,我看到我可以在服务器中注册函数以每X的时间执行一次,因为我要长时间保留原始数据,因此我可以使用一个服务器任务将其异步计数,但是它可以计数器的等待时间更长。

Is there any other way to do it? 还有其他方法吗?

Thanks, 谢谢,

Diego Guraieb 迭戈·格莱布

At the moment there would be at least three ways to increase a counter: 目前,至少有三种方法可以增加计数器:

  1. let the client fetch the fetch counter value, increase it and send the updated document back to the server. 让客户端获取获取计数器值,增加该值并将更新后的文档发送回服务器。 on update, check with the document's etag / revision id whether there was a concurrent update and repeat if yes 更新时,检查文档的etag /修订版ID是否存在并发更新,如果是,则重复
  2. create a server-side transaction that performs the update. 创建执行更新的服务器端事务。 an example (not using a counter but a list modification can be found here: https://groups.google.com/forum/#!topic/arangodb/2t9RV4eYUi4 ) 一个示例(不使用计数器,但可以在此处找到列表修改: https : //groups.google.com/forum/#!topic/arangodb/2t9RV4eYUi4
  3. save each counter modification operation (eg {"op":"inc","value":1}) separately, and calculate the current counter periodically using a server-side task. 分别保存每个计数器修改操作(例如{“ op”:“ inc”,“ value”:1}),并使用服务器端任务定期计算当前计数器。 This alternative 3 can also be implemented in an incremental fashion if desired. 如果需要,该替代方案3也可以以增量方式实施。

Alternative 1 will require at least 2 HTTP calls (fetch + update), and you will need error handling for the case of a concurrent update. 备选方案1将至少需要2个HTTP调用(提取+更新),并且对于并发更新,您将需要错误处理。 So it's not very useful. 所以它不是很有用。

Alternative 2 will be the easiest-to-use from the client perspective I think. 从客户的角度来看,备选方案2将是最容易使用的。 It allows exposing the "increase counter" operation via the REST API, and the fetch-and-update can be implemented in an atomic transaction. 它允许通过REST API公开“增加计数器”操作,并且获取和更新可以在原子事务中实现。

Alternative 3 will work, too, if you can afford your counters to be slightly outdated. 如果您可以负担得起一些过时的计数器,则备选方案3也将起作用。 When implemented, it should be implemented incrementally, eg by keeping a total for the counter and adding only the new values on each update task execution. 在实施时,应以增量方式实施,例如,通过保留计数器的总数并在每次更新任务执行时仅添加新值。 Otherwise your "counter operations" collection will grow infinitely and updates will become slower and slower. 否则,您的“计数器操作”集合将无限增长,并且更新速度将越来越慢。 Alternative #3 also has the advantage that you can keep a (small) log of recent operations, eg for auditing. 备选方案3的优势还在于,您可以保留(少量)近期操作的日志,例如用于审核。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM