Skip to content
Permalink
Browse files

Added timestamps to pending reads and writes.

  • Loading branch information...
lethain committed Dec 2, 2009
1 parent 369754a commit 53b070411b501d6b1a95a7108cf211053605df24
Showing with 8 additions and 8 deletions.
  1. +8 −8 kvs.erl
16 kvs.erl
@@ -74,14 +74,14 @@ store(Store = #kvs_store{data=Data, pending_reads=Reads, pending_writes=Writes})
end, pg2:get_members(kvs)),
% ?KVS_READS is required # of nodes to read from
% [] is used to collect read values
Reads2 = [{{Sender, Key}, {?KVS_READS, []}} | Reads],
store(Store#kvs_store{pending_reads=Reads2});
Reads2 = [{{Sender, Key}, {?KVS_READS, [], ts()}} | Reads],
store(Store#kvs_store{pending_reads=Reads2});
{Sender, retrieve, Client, Key} ->
Sender ! {self(), retrieved, Client, Key, proplists:get_value(Key, Data)},
store(Store);
{_Sender, retrieved, Client, Key, Value} ->
case proplists:get_value({Client, Key}, Reads) of
{1, Values} ->
{1, Values, _Timestamp} ->
Freq = lists:foldr(fun(X, Acc) ->
case proplists:get_value(X, Acc) of
undefined -> [{X, 1} | Acc];
@@ -92,25 +92,25 @@ store(Store = #kvs_store{data=Data, pending_reads=Reads, pending_writes=Writes})
Client ! {self(), got, Popular},
store(Store#kvs_store{
pending_reads=proplists:delete({Key, Value}, Reads)});
{Count, Values} ->
{Count, Values, Timestamp} ->
store(Store#kvs_store{
pending_reads=[{{Client, Key}, {Count-1, [Value | Values]}},
pending_reads=[{{Client, Key}, {Count-1, [Value | Values], Timestamp}},
proplists:delete({Client, Key}, Reads)]})
end;
{Sender, set, Key, Value} ->
% client interface for updating values
lists:foreach(fun(Pid) ->
Pid ! {self(), update, Sender, Key, Value}
end, pg2:get_members(kvs)),
Writes2 = [{{Sender, Key}, ?KVS_WRITES} | Writes],
Writes2 = [{{Sender, Key}, {?KVS_WRITES, ts()}} | Writes],
store(Store#kvs_store{pending_writes=Writes2});
{Sender, update, Client, Key, Value} ->
% sent to all nodes by first receiving node
Sender ! {self(), updated, Client, Key, Value},
store(Store#kvs_store{data=[{Key, Value} |
proplists:delete(Key, Data)]});
{_Sender, updated, Client, Key, Value} ->
Count = proplists:get_value({Client, Key}, Writes),
{Count, Timestamp} = proplists:get_value({Client, Key}, Writes),
case Count of
undefined ->
store(Store);
@@ -120,7 +120,7 @@ store(Store = #kvs_store{data=Data, pending_reads=Reads, pending_writes=Writes})
pending_writes=proplists:delete({Key, Value}, Writes)});
_ ->
store(Store#kvs_store{
pending_writes=[{{Client, Key}, Count-1},
pending_writes=[{{Client, Key}, {Count-1, Timestamp}},
proplists:delete({Client, Key}, Writes)]})
end;
stop ->

0 comments on commit 53b0704

Please sign in to comment.
You can’t perform that action at this time.