summaryrefslogtreecommitdiff
path: root/src/storagedb.erl
blob: 9cdf4c10e7cf9b2931c4566aa8e4b32dc52f5e1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
%%% 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) ->
    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}.