forked from benoitc/couchbeam
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor uuids and change a little the way we start couchbeam.
- create couchbeam_uuids module - register couchbeam_sup module - now app are started from the list given in applications key .
- Loading branch information
Showing
7 changed files
with
165 additions
and
137 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,18 @@ | ||
%%% -*- erlang -*- | ||
%%% | ||
%%% This file is part of couchbeam released under the MIT license. | ||
%%% This file is part of couchbeam released under the MIT license. | ||
%%% See the NOTICE for more information. | ||
|
||
{application, couchbeam, | ||
[{description, "Erlang CouchDB kit"}, | ||
{vsn, "0.7.3"}, | ||
{modules, []}, | ||
{registered, []}, | ||
{applications, [kernel, stdlib, crypto, sasl, ibrowse]}, | ||
{registered, [ | ||
couchbeam_sup | ||
]}, | ||
{applications, [kernel, stdlib, crypto, public_key, ssl, sasl, | ||
ibrowse]}, | ||
{env, []}, | ||
{mod, { couchbeam_app, []}}]}. | ||
{mod, { couchbeam_app, []}} | ||
] | ||
}. |
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
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,110 @@ | ||
%%% -*- erlang -*- | ||
%%% | ||
%%% This file is part of couchbeam released under the MIT license. | ||
%%% See the NOTICE for more information. | ||
|
||
-module(couchbeam_uuids). | ||
-behaviour(gen_server). | ||
|
||
-export([start_link/0, get_uuids/2]). | ||
|
||
%% gen_server callbacks | ||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, | ||
terminate/2, code_change/3]). | ||
|
||
|
||
-include("couchbeam.hrl"). | ||
|
||
-record(state, {}). | ||
|
||
%% @doc Get a list of uuids from the server | ||
%% @spec get_uuids(server(), integer()) -> lists() | ||
get_uuids(Server, Count) -> | ||
gen_server:call(?MODULE, {get_uuids, Server, Count}). | ||
|
||
|
||
%%-------------------------------------------------------------------- | ||
%% Function: start_link/0 | ||
%% Description: Starts the server | ||
%%-------------------------------------------------------------------- | ||
%% @doc Starts the couchbeam process linked to the calling process. Usually | ||
%% invoked by the supervisor couchbeam_sup | ||
%% @spec start_link() -> {ok, pid()} | ||
start_link() -> | ||
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). | ||
|
||
%%--------------------------------------------------------------------------- | ||
%% gen_server callbacks | ||
%%--------------------------------------------------------------------------- | ||
%% @private | ||
|
||
init(_) -> | ||
process_flag(trap_exit, true), | ||
ets:new(couchbeam_uuids, [named_table, public, {keypos, 2}]), | ||
{ok, #state{}}. | ||
|
||
handle_call({get_uuids, #server{host=Host, port=Port}=Server, Count}, | ||
_From, State) -> | ||
{ok, Uuids} = do_get_uuids(Server, Count, [], | ||
ets:lookup(couchbeam_uuids, {Host, Port})), | ||
{reply, Uuids, State}. | ||
|
||
|
||
handle_cast(_Msg, State) -> | ||
{noreply, State}. | ||
|
||
|
||
handle_info(_Info, State) -> | ||
{noreply, State}. | ||
|
||
terminate(_Reason, _State) -> | ||
ok. | ||
|
||
code_change(_OldVsn, State, _Extra) -> | ||
{ok, State}. | ||
|
||
%%-------------------------------------------------------------------- | ||
%%% Internal functions | ||
%%-------------------------------------------------------------------- | ||
do_get_uuids(_Server, Count, Acc, _) when length(Acc) >= Count -> | ||
{ok, Acc}; | ||
do_get_uuids(Server, Count, Acc, []) -> | ||
{ok, ServerUuids} = get_new_uuids(Server), | ||
do_get_uuids(Server, Count, Acc, [ServerUuids]); | ||
do_get_uuids(Server, Count, Acc, [#server_uuids{uuids=Uuids}]) -> | ||
case Uuids of | ||
[] -> | ||
{ok, ServerUuids} = get_new_uuids(Server), | ||
do_get_uuids(Server, Count, Acc, [ServerUuids]); | ||
_ -> | ||
{Acc1, Uuids1} = do_get_uuids1(Acc, Uuids, Count), | ||
#server{host=Host, port=Port} = Server, | ||
ServerUuids = #server_uuids{host_port={Host,Port}, | ||
uuids=Uuids1}, | ||
ets:insert(couchbeam_uuids, ServerUuids), | ||
do_get_uuids(Server, Count, Acc1, [ServerUuids]) | ||
end. | ||
|
||
|
||
|
||
do_get_uuids1(Acc, Uuids, 0) -> | ||
{Acc, Uuids}; | ||
do_get_uuids1(Acc, [Uuid|Rest], Count) -> | ||
do_get_uuids1([Uuid|Acc], Rest, Count-1). | ||
|
||
|
||
get_new_uuids(Server=#server{host=Host, port=Port, options=IbrowseOptions}) -> | ||
Url = couchbeam:make_url(Server, "_uuids", [{"count", "1000"}]), | ||
case couchbeam_httpc:request(get, Url, ["200"], IbrowseOptions) of | ||
{ok, _Status, _Headers, Body} -> | ||
{[{<<"uuids">>, Uuids}]} = ejson:decode(Body), | ||
ServerUuids = #server_uuids{host_port={Host, | ||
Port}, uuids=Uuids}, | ||
ets:insert(couchbeam_uuids, ServerUuids), | ||
{ok, ServerUuids}; | ||
Error -> | ||
Error | ||
end. | ||
|
||
|
||
|
Oops, something went wrong.