Permalink
Browse files

Update riakc, move to tiny_pq

  • Loading branch information...
1 parent 4767a79 commit 22cbbd2f73a8a5522266e4fb00585c93ce687f73 @evanmiller evanmiller committed Sep 5, 2012
Showing with 6 additions and 42 deletions.
  1. +2 −1 rebar.config
  2. +4 −4 src/boss_news_controller.erl
  3. +0 −37 src/boss_pq.erl
View
@@ -8,5 +8,6 @@
{mongodb, ".*", {git, "git://github.com/mongodb/mongodb-erlang.git", {tag, "e5e20a0cbd"}}},
{mysql, ".*", {git, "git://github.com/dizzyd/erlang-mysql-driver.git", {tag, "16cae84b5e"}}},
{poolboy, ".*", {git, "git://github.com/devinus/poolboy.git", {tag, "855802e0cc"}}},
- {riakc, ".*", {git, "git://github.com/basho/riak-erlang-client", {tag, "01a2d3770a"}}}
+ {riakc, ".*", {git, "git://github.com/basho/riak-erlang-client", {tag, "1.3.0"}}},
+ {tiny_pq, ".*", {git, "git://github.com/evanmiller/tiny_pq", {tag, "HEAD"}}}
]}.
@@ -106,14 +106,14 @@ handle_call({set_watch, WatchId, TopicString, CallBack, UserInfo, TTL}, From, St
user_info = UserInfo,
exp_time = ExpTime,
ttl = TTL}, NewState#state.watch_dict),
- ttl_tree = boss_pq:insert_value(ExpTime, WatchId, NewState#state.ttl_tree)
+ ttl_tree = tiny_pq:insert_value(ExpTime, WatchId, NewState#state.ttl_tree)
}};
Error -> {reply, Error, State}
end;
handle_call({cancel_watch, WatchId}, _From, State) ->
{RetVal, NewState} = case dict:find(WatchId, State#state.watch_dict) of
{ok, #watch{ exp_time = ExpTime }} ->
- NewTree = boss_pq:move_value(ExpTime, 0, WatchId, State#state.ttl_tree),
+ NewTree = tiny_pq:move_value(ExpTime, 0, WatchId, State#state.ttl_tree),
{ok, State#state{ ttl_tree = NewTree }};
_ ->
{{error, not_found}, State}
@@ -124,7 +124,7 @@ handle_call({extend_watch, WatchId}, _From, State0) ->
{RetVal, NewState} = case dict:find(WatchId, State#state.watch_dict) of
{ok, #watch{ exp_time = ExpTime, ttl = TTL } = Watch} ->
NewExpTime = future_time(TTL),
- NewTree = boss_pq:move_value(ExpTime, NewExpTime, WatchId, State#state.ttl_tree),
+ NewTree = tiny_pq:move_value(ExpTime, NewExpTime, WatchId, State#state.ttl_tree),
{ok, State#state{ ttl_tree = NewTree,
watch_dict = dict:store(WatchId, Watch#watch{ exp_time = NewExpTime }, State#state.watch_dict) }};
_ ->
@@ -252,7 +252,7 @@ activate_record(Id, Attrs) ->
prune_expired_entries(#state{ ttl_tree = Tree } = State) ->
Now = future_time(0),
- {NewState, NewTree} = boss_pq:prune(fun(WatchId, StateAcc) ->
+ {NewState, NewTree} = tiny_pq:prune_collect_old(fun(WatchId, StateAcc) ->
#watch{ watch_list = WatchList } = dict:fetch(WatchId, StateAcc#state.watch_dict),
NewState = lists:foldr(fun
({id, TopicString}, Acc) ->
View
@@ -1,37 +0,0 @@
-%% A priority queue based on gb_trees
--module(boss_pq).
-
--export([prune/4, delete_value/3, insert_value/3, move_value/4]).
-
-prune(Function, State, {Size, TreeNode}, Now) ->
- {Acc1, Tree1, NumDeleted} = prune_expired_nodes(Function, State, TreeNode, Now),
- {Acc1, {Size - NumDeleted, Tree1}}.
-
-prune_expired_nodes(Function, Acc, {K, V, S, L}, Now) when K > Now ->
- {Acc1, Tree1, NumDeleted} = prune_expired_nodes(Function, Acc, S, Now),
- {Acc1, {K, V, Tree1, L}, NumDeleted};
-prune_expired_nodes(Function, Acc, {K, V, S, L}, Now) when K =< Now ->
- Acc1 = lists:foldr(Function, Acc, V),
- {Acc2, _, NumDeleted_S} = prune_expired_nodes(Function, Acc1, S, Now),
- {Acc3, Tree3, NumDeleted_L} = prune_expired_nodes(Function, Acc2, L, Now),
- {Acc3, Tree3, NumDeleted_S + NumDeleted_L + 1};
-prune_expired_nodes(_Function, Acc, nil, _Now) ->
- {Acc, nil, 0}.
-
-delete_value(Time, Value, Tree) ->
- case gb_trees:lookup(Time, Tree) of
- {value, [Value]} ->
- gb_trees:delete(Time, Tree);
- {value, Values} ->
- gb_trees:enter(Time, lists:delete(Value, Values), Tree)
- end.
-
-insert_value(FutureTime, WatchId, Tree) ->
- NewVal = case gb_trees:lookup(FutureTime, Tree) of
- none -> [WatchId];
- {value, Watches} -> [WatchId|Watches]
- end,
- gb_trees:enter(FutureTime, NewVal, Tree).
-
-move_value(OldTime, NewTime, Value, Tree) ->
- insert_value(NewTime, Value, delete_value(OldTime, Value, Tree)).

0 comments on commit 22cbbd2

Please sign in to comment.