在后期挂钩中处理riak数据类型 [英] dealing with riak datatypes in postcommit hooks

查看:166
本文介绍了在后期挂钩中处理riak数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要实现一个用于riak的postcommit钩子,它将在另一个bucket中的地图中递减一个计数器。但是我在处理riak数据类型时遇到麻烦。

I am wanting to implement a postcommit hook for riak that decrements a counter in a map in another bucket. However I am having a bit of trouble dealing with the riak datatypes.

这是我从 riak console

(riak@127.0.0.1)9>{ok, C} = riak:local_client().
{ok,{riak_client,['riak@127.0.0.1',undefined]}}
(riak@127.0.0.1)10>{ok, Obj} = C:get({<<"product">>, <<"default">>}, <<"1">>).
{ok,{r_object,{<<"product">>,<<"default">>},
          <<"1">>,
          [{r_content,{dict,5,16,16,8,80,48,
                            {[],[],[],[],[],[],[],[],[],[],[],[],...},
                            {{[],[],[],[],
                              [[<<"dot">>|{<<"#\tþù"...>>,{...}}]],
                              [],[],[],[],[],...}}},
                      <<69,2,0,0,0,11,114,105,97,107,95,100,116,95,109,97,112,
                        77,1,...>>}],
          [{<<35,9,254,249,108,41,151,242>>,{1,63593788980}}],
          {dict,1,16,16,8,80,48,
                {[],[],[],[],[],[],[],[],[],[],[],[],[],...},
                {{[],[],[],[],[],[],[],[],[],[],[],...}}},
          undefined}}
(riak@127.0.0.1)11> Mp = riak_object:get_value(O3).
<<69,2,0,0,0,11,114,105,97,107,95,100,116,95,109,97,112,
  77,1,131,80,0,0,0,206,120,1,203,96,...>>
(riak@127.0.0.1)12> MpP = riak_dt_map:from_binary(Mp).
{error,invalid_binary}

产品 bucket数据类型设置为 map 。存储的每个对象都应该有一个称为数量的计数器,我想减少。

product bucket datatype is set to map. Each object stored should have a counter called quantity which I'd like to decrement.

但是我找不到任何在前提或后期提交上下文中处理数据类型的文档或示例代码。 (实际上是很少的例子)。我一直在阅读 riak_client riak_dt_map ,但我是新来的erlang,所以我进展缓慢,感谢一些帮助。

However I cannot find any documentation or sample code dealing with datatypes in a pre or post commit context. (actually examples of any kind are few). I've been reading the source of riak_client and riak_dt_map but I am new to erlang so I'm making slow progress and would appreciate some help.

推荐答案

您拥有的r_object不会直接持有riak_dt_map,而是包含一个包含riak_dt_map反过来包含您的计数器。

The r_object that you have there does not directly hold a riak_dt_map, rather it holds a riak_kv_crdt that contains a riak_dt_map that in turn contains your counter.

要更新计数器,您需要先从地图获取上下文:

To update the counter, you would need to first get the context from the map:

{{Context,_},_}=riak_kv_crdt:value(Obj,riak_dt_map).

然后构建操作,在地图中增加名为&,name的计数器包含在CRDT中:

Then build the operation to increment the counter named <<"name">> in the map contained in the CRDT:

Op = {crdt_op,riak_dt_map,{update,[{update,{<<"name">>,riak_dt_emcntr},increment}]},Context}.

然后将该操作应用于CRDT,提供要用于更新vclock的actor ID / version vector:

And then apply that operation to the CRDT, providing the actor ID you want to use to update the vclock/version vector:

NewObj = riak_kv_crdt:update(Obj,Actor,Op).

结果应该是另一个准备发回存储的r_object。

The result should be another r_object that is ready to be sent back for storage.

这篇关于在后期挂钩中处理riak数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆