summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMagnus Ahltorp <map@kth.se>2014-11-14 23:21:04 +0100
committerMagnus Ahltorp <map@kth.se>2014-11-19 05:03:19 +0100
commitc224989af3216b92c668c2f979b83551d49760cc (patch)
tree50905eee20f0861c2fe2bf7d80dbf2255d4d0f44 /src
parentf8902f7899b4d76cabbf65763866d1a28fbcf743 (diff)
Seralize writes to new entries index
Diffstat (limited to 'src')
-rw-r--r--src/plop_sup.erl5
-rw-r--r--src/storage.erl13
-rw-r--r--src/storagedb.erl68
3 files changed, 76 insertions, 10 deletions
diff --git a/src/plop_sup.erl b/src/plop_sup.erl
index bcb9756..663b3bc 100644
--- a/src/plop_sup.erl
+++ b/src/plop_sup.erl
@@ -23,6 +23,11 @@ init(Args) ->
permanent,
10000,
worker, [db]},
+ {the_storagedb,
+ {storagedb, start_link, []},
+ permanent,
+ 10000,
+ worker, [storagedb]},
{fsync,
{fsyncport, start_link, []},
permanent,
diff --git a/src/storage.erl b/src/storage.erl
index 8136308..e632a8e 100644
--- a/src/storage.erl
+++ b/src/storage.erl
@@ -20,7 +20,7 @@ request(post, "ct/storage/sendentry", Input) ->
TreeLeafHash = base64:decode(proplists:get_value(<<"treeleafhash">>, PropList)),
ok = db:add(TreeLeafHash, LogEntry),
- ok = index:addlast(newentries_path(), TreeLeafHash),
+ ok = storagedb:add(TreeLeafHash),
success({[{result, <<"ok">>}]})
end;
request(post, "ct/storage/entrycommitted", Input) ->
@@ -34,7 +34,8 @@ request(post, "ct/storage/entrycommitted", Input) ->
success({[{result, <<"ok">>}]})
end;
request(get, "ct/storage/fetchnewentries", _Input) ->
- NewHashes = fetchnewhashes(0),
+ NewHashes = storagedb:fetchnewhashes(0),
+ % XXX send only hashes, implement getentry
Entries = lists:map(fun(LeafHash) ->
{[{hash, base64:encode(LeafHash)},
{entry, base64:encode(db:entry_for_leafhash(LeafHash))}]}
@@ -42,14 +43,6 @@ request(get, "ct/storage/fetchnewentries", _Input) ->
success({[{result, <<"ok">>},
{entries, Entries}]}).
-fetchnewhashes(Index) ->
- case index:indexsize(newentries_path()) of
- 0 ->
- [];
- Size ->
- index:getrange(newentries_path(), Index, Size - 1)
- end.
-
%% Private functions.
html(Text, Input) ->
{400, [{"Content-Type", "text/html"}],
diff --git a/src/storagedb.erl b/src/storagedb.erl
new file mode 100644
index 0000000..444abc1
--- /dev/null
+++ b/src/storagedb.erl
@@ -0,0 +1,68 @@
+%%% Copyright (c) 2014, NORDUnet A/S.
+%%% See LICENSE for licensing information.
+
+%%% @doc Storage DB
+
+-module(storagedb).
+-behaviour(gen_server).
+%% API
+-export([start_link/0, stop/0]).
+-export([add/1, fetchnewhashes/1]).
+%% gen_server callbacks.
+-export([init/1, handle_call/3, terminate/2, handle_cast/2, handle_info/2,
+ code_change/3]).
+
+-import(stacktrace, [call/2]).
+
+init(_Args) ->
+ {ok, []}.
+
+start_link() ->
+ gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+
+stop() ->
+ call(?MODULE, stop).
+
+%%%%%%%%%%%%%%%%%%%%
+%% Public API.
+
+fetchnewhashes(Index) ->
+ case index:indexsize(newentries_path()) of
+ 0 ->
+ [];
+ Size ->
+ index:getrange(newentries_path(), Index, Size - 1)
+ end.
+
+-spec add(binary()) -> ok.
+add(LeafHash) ->
+ call(?MODULE, {add, LeafHash}).
+
+%%%%%%%%%%%%%%%%%%%%
+%% gen_server callbacks.
+
+handle_cast(_Request, State) ->
+ {noreply, State}.
+
+handle_info(_Info, State) ->
+ {noreply, State}.
+
+code_change(_OldVsn, State, _Extra) ->
+ {ok, State}.
+
+terminate(_Reason, _State) ->
+ io:format("~p terminating~n", [?MODULE]),
+ ok.
+
+%%%%%%%%%%%%%%%%%%%%
+
+newentries_path() ->
+ {ok, Value} = application:get_env(plop, newentries_path),
+ Value.
+
+handle_call(stop, _From, State) ->
+ {stop, normal, stopped, State};
+
+handle_call({add, LeafHash}, _From, State) ->
+ ok = index:addlast(newentries_path(), LeafHash),
+ {reply, ok, State}.