Skip to content
This repository has been archived by the owner on Sep 19, 2019. It is now read-only.

Commit

Permalink
Optimize next_replication by using queue:out
Browse files Browse the repository at this point in the history
  • Loading branch information
Bob Dionne authored and kocolosk committed Apr 27, 2012
1 parent 183696d commit cf73606
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/mem3_sync.erl
Expand Up @@ -20,7 +20,7 @@
-export([start_link/0, get_active/0, get_queue/0, push/1, push/2,
remove_node/1, initial_sync/1]).

-import(queue, [in/2, to_list/1, from_list/1, is_empty/1]).
-import(queue, [in/2, out/1, to_list/1, join/2, from_list/1, is_empty/1]).

-include("mem3.hrl").
-include_lib("couch/include/couch_db.hrl").
Expand Down Expand Up @@ -164,7 +164,7 @@ handle_replication_exit(State, Pid) ->
_ ->
Count = length(Active1),
NewState = if Count < Limit ->
case next_replication(Active1, Waiting) of
case next_replication(Active1, Waiting, queue:new()) of
nil -> % all waiting replications are also active
State#state{active = Active1, count = Count};
{#job{name=DbName, node=Node} = Job, StillWaiting} ->
Expand Down Expand Up @@ -253,14 +253,19 @@ start_update_notifier() ->

%% @doc Finds the next {DbName,Node} pair in the list of waiting replications
%% which does not correspond to an already running replication
-spec next_replication([#job{}], [#job{}]) -> {#job{}, [#job{}]} | nil.
next_replication(Active, Waiting) ->
Fun = fun(#job{name=S, node=N}) -> is_running(S,N,Active) end,
case lists:splitwith(Fun, to_list(Waiting)) of
{_, []} ->
-spec next_replication([#job{}], [#job{}], [#job{}]) -> {#job{}, [#job{}]} | nil.
next_replication(Active, Waiting, WaitingAndRunning) ->
case is_empty(Waiting) of
true ->
nil;
{Running, [Job|Rest]} ->
{Job, from_list(Running ++ Rest)}
false ->
{{value, #job{name=S, node=N} = Job}, RemQ} = out(Waiting),
case is_running(S,N,Active) of
true ->
next_replication(Active, RemQ, in(Job, WaitingAndRunning));
false ->
{Job, join(Waiting, WaitingAndRunning)}
end
end.

is_running(DbName, Node, ActiveList) ->
Expand Down

0 comments on commit cf73606

Please sign in to comment.