summaryrefslogtreecommitdiff
path: root/src/ts.erl
blob: 345ed6620f45f759bf07894e52adbd55505f3b0d (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
% tree storage
-module(ts).
-include_lib("eunit/include/eunit.hrl").
-export_type([tree_store/0]).
-export([new/0, delete/1, size/1, store/3, retrieve/2, retrieve_hash/2]).

%% -record(tree_store, {warm :: ets:tid(),
%%                      frozen :: list()}).  % [ets:tid()]
-record(tree_store, {table :: ets:tid()}).
-type tree_store() :: #tree_store{}.

new() ->
    %% tree_store#{warm = ets:new(nil, [{read_concurrency, true}]),
    %%             frozen = ets:new(nil, [{read_concurrency, true}])}.
    #tree_store{table = ets:new(nil, [{read_concurrency, true}])}.

delete(Store) ->
    ets:delete(Store#tree_store.table).

size(Store) ->
    ets:info(Store#tree_store.table, size).

-spec store(tree_store(), tuple(), binary()) -> tree_store().
store(Store, IR, Hash) ->
    ets:insert(Store#tree_store.table, {IR, Hash}),
    Store.

-spec retrieve(tree_store(), tuple()) -> {tuple(), binary()}.
retrieve(#tree_store{table = Tab}, IR) ->
    case ets:lookup(Tab, IR) of
        [] -> exit(IR);
        [R] -> R
    end.

-spec retrieve_hash(tree_store(), tuple()) -> binary().
retrieve_hash(#tree_store{table = Tab}, IR) ->
    ets:lookup_element(Tab, IR, 2).