%%% Copyright (c) 2014-2015, 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) -> ok = call(?MODULE, {add_nosync, LeafHash}), ok = index:sync(newentries_path()), ok. %%%%%%%%%%%%%%%%%%%% %% 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_nosync, LeafHash}, _From, State) -> ok = index:addlast_nosync(newentries_path(), LeafHash), {reply, ok, State}.