Last active
June 21, 2017 13:06
-
-
Save goncalotomas/1151ec1efd20557ecfb2dedbd829ea9f to your computer and use it in GitHub Desktop.
Interaction with a Riak node in which maps with nested fields are used.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% Start connection to riak node | |
{ok, Pid} = riakc_pb_socket:start_link("127.0.0.1",8087). | |
%% Create new local riak map object. | |
Map = riakc_map:new(). | |
%% Perform a local update taking the previous map and inserting a nested set inside. | |
Map1 = riakc_map:update({<<"set">>, set}, | |
%% update a nested key called <<"set">> of type set | |
%% pass it an update function that adds X and Y to it. | |
fun(S) -> riakc_set:add_element(<<"X">>,riakc_set:add_element(<<"Y">>, S)) end, | |
Map). | |
%% Save map to Riak | |
ok = riakc_pb_socket:update_type( | |
%% structure of update_type | |
%% (Pid, {bucket_type, bucket_name}, key, object) | |
Pid, {<<"maps">>, <<"bucket">>}, <<"key">>, riakc_map:to_op(Map1) | |
). | |
%% Fetch map from riak. | |
{ok, M0} = riakc_pb_socket:fetch_type(Pid, {<<"maps">>, <<"bucket">>}, <<"key">>). | |
%% Fetch nested set. | |
L0 = riakc_map:fetch({<<"set">>, set}, M0). | |
%% check that what we got from riak contains exactly what we added before. | |
true = lists:member(<<"X">>, L0). | |
true = lists:member(<<"Y">>, L0). | |
2 = length(L0). | |
%% further mutate the map (locally) by deleting X and adding Z to the set. | |
M1 = riakc_map:update({<<"set">>, set}, | |
fun(S) -> riakc_set:del_element(<<"X">>, riakc_set:add_element(<<"Z">>, S)) end, | |
M0). | |
%% send the current version to Riak. | |
ok = riakc_pb_socket:update_type( | |
Pid, {<<"maps">>, <<"bucket">>}, <<"key">>, riakc_map:to_op(M1) | |
). | |
%% fetch map from Riak again to check for changes | |
{ok, M2} = riakc_pb_socket:fetch_type(Pid, {<<"maps">>, <<"bucket">>}, <<"key">>). | |
%% fetch nested set from map | |
L1 = riakc_map:fetch({<<"set">>, set}, M2). | |
%% check that what we got from riak contains exactly what we added before. | |
true = lists:member(<<"Y">>, L1). | |
true = lists:member(<<"Z">>, L1). | |
2= length(L1). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment