-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
David Weldon
committed
Apr 15, 2011
1 parent
e6e085d
commit 4cf67a1
Showing
3 changed files
with
95 additions
and
6 deletions.
There are no files selected for viewing
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
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
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
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. |