Last active
October 27, 2015 13:04
-
-
Save loucash/8ffe463833052ca779b1 to your computer and use it in GitHub Desktop.
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
-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