summaryrefslogtreecommitdiff
path: root/src/db.erl
blob: a0855b91bbf0485b00b5688566bb303455250840 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
%%% Copyright (c) 2014, NORDUnet A/S.
%%% See LICENSE for licensing information.

-module(db).
-behaviour(gen_server).

%% API.
-export([start_link/0, stop/0]).
-export([add/4, size/0]).
-export([get_by_index/1, get_by_indices/3, get_by_leaf_hash/1, get_by_entry_hash/1]).
%% gen_server callbacks.
-export([init/1, handle_call/3, terminate/2, handle_cast/2, handle_info/2,
         code_change/3]).

-include_lib("stdlib/include/qlc.hrl").
-include("db.hrl").

size() ->
    binary_to_integer(atomic:readfile(treesize_path())).

init(_Args) ->
    {ok, []}.

start_link() ->
    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

stop() ->
    gen_server:call(?MODULE, stop).

%%%%%%%%%%%%%%%%%%%%
%% Public API.

-spec add(binary(), binary(), binary(), non_neg_integer()) -> ok.
add(LeafHash, EntryHash, Data, Index) ->
    gen_server:call(?MODULE, {add, {LeafHash, EntryHash, Data, Index}}).

-spec get_by_indices(integer(), integer(), {sorted, true|false}) ->
                            [{non_neg_integer(), binary(), binary()}].
get_by_indices(Start, End, {sorted, Sorted}) ->
    gen_server:call(?MODULE, {get_by_indices, {Start, End, Sorted}}).

-spec get_by_index(binary()) -> notfound |
                                {non_neg_integer(), binary(), binary()}.
get_by_index(Index) ->
    gen_server:call(?MODULE, {get_by_index, Index}).

-spec get_by_leaf_hash(binary()) -> notfound |
                                    {non_neg_integer(), binary(), binary()}.
get_by_leaf_hash(LeafHash) ->
    gen_server:call(?MODULE, {get_by_leaf_hash, LeafHash}).

-spec get_by_entry_hash(binary()) -> notfound |
                                     {non_neg_integer(), binary(), binary()}.
get_by_entry_hash(EntryHash) ->
    gen_server:call(?MODULE, {get_by_entry_hash, EntryHash}).

%%%%%%%%%%%%%%%%%%%%
%% 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.

%%%%%%%%%%%%%%%%%%%%
%% The meat.

% Table for Leaf hash -> Entry
entry_root_path() ->
    {ok, Value} = application:get_env(plop, entry_root_path),
    Value.

% Table for Leaf hash -> Entry
indexforhash_root_path() ->
    {ok, Value} = application:get_env(plop, indexforhash_root_path),
    Value.

% Table for Index -> Leaf hash
index_path() ->
    {ok, Value} = application:get_env(plop, index_path),
    Value.

% Table for Entry hash -> Leaf hash
entryhash_root_path() ->
    {ok, Value} = application:get_env(plop, entryhash_root_path),
    Value.

% File that stores tree size
treesize_path() ->
    {ok, Value} = application:get_env(plop, treesize_path),
    Value.


entry_for_leafhash(LeafHash) ->
    perm:readfile(entry_root_path(), LeafHash).

index_for_leafhash(LeafHash) ->
    binary_to_integer(perm:readfile(indexforhash_root_path(), LeafHash)).

leafhash_for_index(Index) ->
    index:get(index_path(), Index).

leafhash_for_indices(Start, End) ->
    index:getrange(index_path(), Start, End).

leafhash_for_entryhash(EntryHash) ->
    perm:readfile(entryhash_root_path(), EntryHash).

get_by_indices_helper(Start, _End) when Start < 0 ->
    [];
get_by_indices_helper(Start, End) ->
    EndBound = min(End, size() - 1),
    case Start =< EndBound of
        true ->
            lists:map(fun ({LeafHash, Index}) ->
                              {Index, LeafHash, notfetched}
                      end, lists:zip(leafhash_for_indices(Start, EndBound),
                                     lists:seq(Start, EndBound)));
        false ->
            []
    end.

handle_call(stop, _From, State) ->
    {stop, normal, stopped, State};

handle_call({add, {LeafHash, EntryHash, Data, Index}}, _From, State) ->
    ok = perm:ensurefile(entry_root_path(), LeafHash, Data),
    ok = perm:ensurefile(entryhash_root_path(), EntryHash, LeafHash),
    ok = perm:ensurefile(indexforhash_root_path(),
                         LeafHash, integer_to_binary(Index)),
    ok = index:add(index_path(), Index, LeafHash),
    ok = atomic:replacefile(treesize_path(), integer_to_list(Index+1)),
    {reply, ok, State};

handle_call({get_by_indices, {Start, End, _Sorted}}, _From, State) ->
    {reply, get_by_indices_helper(Start, End), State};

handle_call({get_by_index, Index}, _From, State) ->
    LeafHash = leafhash_for_index(Index),
    Entry = entry_for_leafhash(LeafHash),
    R = {Index, LeafHash, Entry},
    {reply, R, State};

handle_call({get_by_leaf_hash, LeafHash}, _From, State) ->
    Entry = entry_for_leafhash(LeafHash),
    Index = index_for_leafhash(LeafHash),
    R = {Index, LeafHash, Entry},
    {reply, R, State};

handle_call({get_by_entry_hash, EntryHash}, _From, State) ->
    R = case leafhash_for_entryhash(EntryHash) of
            noentry ->
                notfound;
            LeafHash ->
                Entry = entry_for_leafhash(LeafHash),
                Index = index_for_leafhash(LeafHash),
                {Index, LeafHash, Entry}
        end,
    {reply, R, State}.