Skip to content

Instantly share code, notes, and snippets.

@loucash
Last active October 27, 2015 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loucash/8ffe463833052ca779b1 to your computer and use it in GitHub Desktop.
Save loucash/8ffe463833052ca779b1 to your computer and use it in GitHub Desktop.
-module(test).
-export([test/0]).
-define(BUCKET, <<"foo">>).
test() ->
Key = rand_binary(10),
Idx = rand_int(10),
test(Key, Idx).
test(Key, Idx) ->
io:format("Test key: ~p, 2nd idx: ~p~n", [Key, Idx]),
{ok, Pid} = riakc_pb_socket:start_link("localhost", 8087),
ok = riakc_pb_socket:set_bucket(Pid, ?BUCKET, [{allow_mult, true}]),
case riakc_pb_socket:counter_val(Pid, ?BUCKET, Key) of
{ok, _} ->
ok = riakc_pb_socket:counter_incr(Pid, ?BUCKET, Key, 1);
{error, notfound} ->
ok = riakc_pb_socket:counter_incr(Pid, ?BUCKET, Key, 1),
{ok, Obj} = riakc_pb_socket:get(Pid, ?BUCKET, Key),
Indices = [{{integer_index, "ts"}, [Idx]}],
Meta1 = riakc_obj:get_update_metadata(Obj),
Meta2 = riakc_obj:set_secondary_index(Meta1, Indices),
NewObj = riakc_obj:update_metadata(Obj, Meta2),
ok = riakc_pb_socket:put(Pid, NewObj)
end,
{ok, Results} = riakc_pb_socket:get_index(Pid, ?BUCKET, {integer_index, "ts"}, Idx),
io:format("Results: ~p and value: ~p~n", [Results, riakc_pb_socket:counter_val(Pid, ?BUCKET, Key)]),
ok.
rand_binary(N) ->
base64:encode(crypto:strong_rand_bytes(N)).
rand_int(N) ->
random:uniform(N).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment