Skip to content

Commit

Permalink
replaced comment-based spec and type declarations.
Browse files Browse the repository at this point in the history
  • Loading branch information
dweldon committed Aug 6, 2012
1 parent c2685ff commit f3de637
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
26 changes: 13 additions & 13 deletions src/riakpool.erl
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,20 @@
handle_info/2,
terminate/2,
code_change/3]).
-record(state, {host, port, pids}).

%% @type host() = string() | atom().
-type host() :: string() | atom().

-record(state, {host :: host(), port ::non_neg_integer(), pids}).

%% @spec count() -> integer()
%% @doc Returns the number of connections as seen by the supervisor.
-spec count() -> integer().
count() ->
Props = supervisor:count_children(riakpool_connection_sup),
case proplists:get_value(active, Props) of
N when is_integer(N) -> N;
undefined -> 0
end.

%% @spec execute(Fun) -> {ok, Value::any()} | {error, any()}
%% Fun = function(pid())
%% @doc Finds the next available connection pid from the pool and calls
%% `Fun(Pid)'. Returns `{ok, Value}' if the call was successful, and
%% `{error, any()}' otherwise. If no connection could be found, a new connection
Expand All @@ -53,6 +52,7 @@ count() ->
%% > riakpool:execute(fun(C) -> riakc_pb_socket:ping(C) end).
%% {ok,pong}
%% '''
-spec execute(fun((pid()) -> any())) -> {ok, Value::any()} | {error, any()}.
execute(Fun) ->
case gen_server:call(?MODULE, check_out) of
{ok, Pid} ->
Expand All @@ -62,23 +62,23 @@ execute(Fun) ->
{error, E} -> {error, E}
end.

%% @spec start_link() -> {ok, pid()} | {error, any()}
%% @doc Starts the server.
-spec start_link() -> {ok, pid()} | {error, any()}.
start_link() -> gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

%% @spec start_pool() -> ok | {error, any()}
%% @doc Starts a connection pool to a server listening on {"127.0.0.1", 8087}.
%% Note that a pool can only be started once.
-spec start_pool() -> ok | {error, any()}.
start_pool() -> start_pool("127.0.0.1", 8087).

%% @spec start_pool(host(), integer()) -> ok | {error, any()}
%% @doc Starts a connection pool to a server listening on {`Host', `Port'}.
%% Note that a pool can only be started once.
-spec start_pool(host(), integer()) -> ok | {error, any()}.
start_pool(Host, Port) when is_integer(Port) ->
gen_server:call(?MODULE, {start_pool, Host, Port}).

%% @spec stop() -> ok
%% @doc Stops the server.
-spec stop() -> ok.
stop() -> gen_server:cast(?MODULE, stop).

%% @hidden
Expand Down Expand Up @@ -133,33 +133,33 @@ terminate(_Reason, #state{pids=Pids}) ->
%% @hidden
code_change(_OldVsn, State, _Extra) -> {ok, State}.

%% @spec new_state(host(), integer()) -> state() | undefined
%% @doc Returns a state with a single pid if a connection could be established,
%% otherwise returns undefined.
-spec new_state(host(), integer()) -> #state{} | undefined.
new_state(Host, Port) ->
case new_connection(Host, Port) of
{ok, Pid} ->
#state{host=Host, port=Port, pids=queue:in(Pid, queue:new())};
error -> undefined
end.

%% @spec new_connection(host(), integer()) -> {ok, Pid} | error
%% @doc Returns {ok, Pid} if a new connection was established and added to the
%% supervisor, otherwise returns error.
-spec new_connection(host(), integer()) -> {ok, pid()} | error.
new_connection(Host, Port) ->
case supervisor:start_child(riakpool_connection_sup, [Host, Port]) of
{ok, Pid} when is_pid(Pid) -> {ok, Pid};
{ok, Pid, _} when is_pid(Pid) -> {ok, Pid};
_ -> error
end.

%% @spec next_pid(host(), integer(), queue()) -> {ok, pid(), queue()} |
%% {error, queue()}
%% @doc Recursively dequeues Pids in search of a live connection. Dead
%% connections are removed from the queue as it is searched. If no connection
%% pid could be found, a new one will be established. Returns {ok, Pid, NewPids}
%% where NewPids is the queue after any necessary dequeues. Returns error if no
%% live connection could be found and no new connection could be established.
-spec next_pid(host(), integer(), queue()) -> {ok, pid(), queue()} |
{error, queue()}.
next_pid(Host, Port, Pids) ->
case queue:out(Pids) of
{{value, Pid}, NewPids} ->
Expand Down
8 changes: 4 additions & 4 deletions src/riakpool_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
-module(riakpool_client).
-export([delete/2, get/2, list_keys/1, put/3]).

%% @spec delete(binary(), binary()) -> ok
%% @doc Delete `Key' from `Bucket'.
-spec delete(binary(), binary()) -> ok.
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()}'.
-spec get(binary(), binary()) -> {ok, binary()} | {error, any()}.
get(Bucket, Key) ->
Fun =
fun(C) ->
Expand All @@ -28,19 +28,19 @@ get(Bucket, Key) ->
{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()}'.
-spec list_keys(binary()) -> {ok, list()} | {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.
-spec put(binary(), binary(), binary()) -> ok.
put(Bucket, Key, Value) ->
Fun =
fun(C) ->
Expand Down

0 comments on commit f3de637

Please sign in to comment.