douzhi3667 2015-06-01 21:45
浏览 44

couchbase golang json原子增量

Is there a golang API similar to counter available in other couchbase SDK's that will help us to atomically increment certain fields inside a json document?

For example, I have a below struct with two fields which will be associated with a document D1

type Counter struct {
        c1 string `json:"c1"`
        c2 string `json:"c2"`
}

For every http request that comes in, I would like to do an atomic increment of c1 and c2. since it's within a json document, am not able to use GET and not sure how to use counter method using golang.

  • 写回答

1条回答 默认 最新

  • douqiao2008 2015-06-02 09:28
    关注

    It's not possible to have an atomic counter within a document, you have two options to work around this:

    1) Have your document reference separate atomic counters, rather than holding the counter value they'll just hold the key that points to the counter. Something like the json below, if you retrieved the document then to work with the counter you'd use the value in 'counter_key'.

    {
      "id": "customer::1343"
      "name": "John Smith",
      "counter_key": "counter::customer::1343"
    }
    

    2) Your second option is to keep the field within the Json document that you want to increment, to be able to update this atomically you'll need to look at CAS which is a form of optimistic locking which will allow you to update the value within the actual document rather than having a separate counter, this method does introduce additional coding overhead and won't be as performant as a separate counter. You can read about more about CAS here: http://docs.couchbase.com/developer/dev-guide-3.0/retrieve-by-cas.html

    There is also a nice succinct description here of Couchbase CAS here on Stack Overflow: What is CAS in nosql and how to use it?

    评论

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3