summaryrefslogtreecommitdiff
path: root/src/storagedb.erl
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/storagedb.erl
parentf8902f7899b4d76cabbf65763866d1a28fbcf743 (diff)
Seralize writes to new entries index
Diffstat (limited to 'src/storagedb.erl')
-rw-r--r--src/storagedb.erl68
1 files changed, 68 insertions, 0 deletions
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}.