Skip to content

Commit

Permalink
added client convenience functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
David Weldon committed Apr 15, 2011
1 parent e6e085d commit 4cf67a1
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 6 deletions.
13 changes: 12 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,20 @@ the complete documentation by running `make doc`.
ok
3> riakpool:execute(fun(C) -> riakc_pb_socket:ping(C) end).
{ok,pong}
4> riakpool:count().
4> riakpool_client:put(<<"groceries">>, <<"mine">>, <<"eggs">>).
ok
5> riakpool_client:get(<<"groceries">>, <<"mine">>).
{ok,<<"eggs">>}
6> riakpool_client:list_keys(<<"groceries">>).
{ok,[<<"mine">>]}
7> riakpool_client:delete(<<"groceries">>, <<"mine">>).
ok
8> riakpool:count().
1

Note that the use of riakpool_client is completely optional - it is simply a
collection of convenience functions which call riakpool:execute/1.

Starting the Pool
-----------------
Prior to any calls to `riakpool:execute/1`, the pool must be started. This can
Expand Down
10 changes: 5 additions & 5 deletions src/riakpool_app.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
-behaviour(application).
-export([start/2, stop/1]).

start(_StartType, _StartArgs) ->
riakpool_sup:start_link().
start(_StartType, _StartArgs) -> riakpool_sup:start_link().

stop(_State) ->
ok.
stop(_State) -> ok.

-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
Expand All @@ -21,6 +19,8 @@ app_test() ->
Fun = fun(C) -> riakc_pb_socket:ping(C) end,
?assertEqual({ok, pong}, riakpool:execute(Fun)),
?assertEqual(1, riakpool:count()),
application:stop(riakpool).
application:stop(riakpool),
application:unset_env(riakpool, riakpool_host),
application:unset_env(riakpool, riakpool_port).

-endif.
78 changes: 78 additions & 0 deletions src/riakpool_client.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
%% @author David Weldon
%% @doc riakpool_client is a collection of convenience functions for using
%% riakpool.

-module(riakpool_client).
-export([delete/2, get/2, list_keys/1, put/3]).

%% @spec delete(binary(), binary()) -> ok
%% @doc Delete `Key' from `Bucket'.
delete(Bucket, Key) ->
riakpool:execute(fun(C) -> riakc_pb_socket:delete(C, Bucket, Key) end), ok.

%% @spec get(binary(), binary()) -> {ok, binary()} | {error, any()}
%% @doc Returns the value associated with `Key' in `Bucket' as `{ok, binary()}'.
%% If an error was encountered or the value was not present, returns
%% `{error, any()}'.
get(Bucket, Key) ->
Fun =
fun(C) ->
case riakc_pb_socket:get(C, Bucket, Key) of
{ok, O} -> riakc_obj:get_value(O);
{error, E} -> {error, E}
end
end,
case riakpool:execute(Fun) of
{ok, Value} when is_binary(Value) -> {ok, Value};
{ok, {error, E}} -> {error, E};
{error, E} -> {error, E}
end.

%% @spec list_keys(binary()) -> {ok, list()} | {error, any()}
%% @doc Returns the list of keys in `Bucket' as `{ok, list()}'. If an error was
%% encountered, returns `{error, any()}'.
list_keys(Bucket) ->
Fun = fun(C) -> riakc_pb_socket:list_keys(C, Bucket) end,
case riakpool:execute(Fun) of
{ok, {ok, Keys}} -> {ok, Keys};
{error, E} -> {error, E}
end.

%% @spec put(binary(), binary(), binary()) -> ok
%% @doc Associates `Key' with `Value' in `Bucket'. If `Key' already exists in
%% `Bucket', an update will be preformed.
put(Bucket, Key, Value) ->
Fun =
fun(C) ->
case riakc_pb_socket:get(C, Bucket, Key) of
{ok, O} ->
O2 = riakc_obj:update_value(O, Value),
riakc_pb_socket:put(C, O2);
{error, _} ->
O = riakc_obj:new(Bucket, Key, Value),
riakc_pb_socket:put(C, O)
end
end,
riakpool:execute(Fun), ok.

-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").

client_test() ->
{B, K, V1, V2} = {<<"groceries">>, <<"mine">>, <<"eggs">>, <<"toast">>},
application:start(riakpool),
?assertMatch({error, _}, list_keys(B)),
?assertMatch({error, _}, get(B, K)),
riakpool:start_pool(),
?assertEqual({ok, []}, list_keys(B)),
?assertMatch({error, _}, get(B, K)),
?assertEqual(ok, put(B, K, V1)),
?assertEqual({ok, V1}, get(B, K)),
?assertEqual(ok, put(B, K, V2)),
?assertEqual({ok, V2}, get(B, K)),
?assertEqual({ok, [K]}, list_keys(B)),
?assertEqual(ok, delete(B, K)),
?assertEqual({ok, []}, list_keys(B)),
application:stop(riakpool).

-endif.

0 comments on commit 4cf67a1

Please sign in to comment.