diff options
author | Magnus Ahltorp <map@kth.se> | 2015-03-25 14:14:42 +0100 |
---|---|---|
committer | Magnus Ahltorp <map@kth.se> | 2015-03-25 14:23:21 +0100 |
commit | 44d18b6db6d9747fdf402393832e719975b508a6 (patch) | |
tree | 2ad8a26def7a407255e86ab94a6fe9ece3bf47fc /src | |
parent | 4d586a032257ee600e2191e7e701243cd8f7e92c (diff) |
Read db size from sth file and cachedbsize
Diffstat (limited to 'src')
-rw-r--r-- | src/db.erl | 24 | ||||
-rw-r--r-- | src/frontend.erl | 1 | ||||
-rw-r--r-- | src/plop.erl | 61 | ||||
-rw-r--r-- | src/plop_app.erl | 1 |
4 files changed, 43 insertions, 44 deletions
@@ -6,6 +6,7 @@ %% API. -export([start_link/0, stop/0]). +-export([create_size_table/0]). -export([add/2, add_entryhash/2, add_index_nosync/2, set_treesize/1, size/0]). -export([get_by_index/1, get_by_indices/3, get_by_leaf_hash/1]). -export([get_by_entry_hash/1, entry_for_leafhash/1, leafhash_for_index/1]). @@ -19,8 +20,11 @@ -include_lib("stdlib/include/qlc.hrl"). -include("db.hrl"). +-define(DB_SIZE_TABLE, db_size). + size() -> - binary_to_integer(atomic:readfile(treesize_path())). + [{_, Size}] = ets:lookup(?DB_SIZE_TABLE, db_size), + Size. indexsize() -> index:indexsize(index_path()). @@ -28,6 +32,15 @@ indexsize() -> init(_Args) -> {ok, []}. +create_size_table() -> + case ets:info(?DB_SIZE_TABLE) of + undefined -> + ok; + _ -> + ets:delete(?DB_SIZE_TABLE) + end, + ets:new(?DB_SIZE_TABLE, [set, public, named_table]). + start_link() -> gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). @@ -55,7 +68,8 @@ add_index_nosync(LeafHash, Index) -> -spec set_treesize(non_neg_integer()) -> ok. set_treesize(Size) -> - ok = atomic:replacefile(treesize_path(), integer_to_binary(Size)). + true = ets:insert(?DB_SIZE_TABLE, {db_size, Size}), + ok. -spec get_by_indices(integer(), integer(), {sorted, true|false}) -> [{non_neg_integer(), binary(), binary()}]. @@ -138,12 +152,6 @@ 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). diff --git a/src/frontend.erl b/src/frontend.erl index 68039c2..e528419 100644 --- a/src/frontend.erl +++ b/src/frontend.erl @@ -80,7 +80,6 @@ request(post, "ct/frontend/sendsth", Input) -> OwnRootHash = ht:root(Treesize - 1), case {plop:verify_sth(Treesize, Timestamp, RootHash, Signature), OwnRootHash} of {true, RootHash} -> - ok = db:set_treesize(Treesize), ok = plop:save_sth({struct, PropList}), success({[{result, <<"ok">>}]}); {false, RootHash} -> diff --git a/src/plop.erl b/src/plop.erl index 61af616..cfb62fc 100644 --- a/src/plop.erl +++ b/src/plop.erl @@ -21,18 +21,14 @@ %%% - get rid of CT-specific stuff that has creeped in -module(plop). --behaviour(gen_server). %% API. --export([start_link/0, stop/0]). +-export([initsize/0]). -export([get_logid/0, serialise/1]). -export([add/3, sth/0, get/1, get/2, spt/1, consistency/2, inclusion/2, inclusion_and_entry/2]). -export([generate_timestamp/0, save_sth/1, verify_sth/4]). %% API for tests. -export([testing_get_pubkey/0]). -%% gen_server callbacks. --export([init/1, handle_call/3, terminate/2, - handle_cast/2, handle_info/2, code_change/3]). -import(stacktrace, [call/2]). -include("plop.hrl"). @@ -57,20 +53,6 @@ }). -type sth_signed() :: #sth_signed{}. -start_link() -> - gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). - -stop() -> - call(?MODULE, stop). - -%%%%%%%%%%%%%%%%%%%% -init([]) -> - _Tree = ht:reset_tree([db:size() - 1]), - {ok, []}. - -handle_cast(_Request, State) -> - {noreply, State}. - handle_http_reply(TreeLeafHash, RepliesUntilQuorum, StatusCode, Body) -> lager:debug("leafhash ~s: http_reply: ~p", @@ -92,16 +74,6 @@ handle_http_reply(TreeLeafHash, RepliesUntilQuorum, end end. -handle_info(_Info, State) -> - {noreply, State}. - -code_change(_OldVsn, State, _Extra) -> - {ok, State}. - -terminate(_Reason, _State) -> - io:format("~p terminating~n", [?MODULE]), - ok. - %%%%%%%%%%%%%%%%%%%% -spec add(binary(), binary(), binary()) -> ok. add(LogEntry, TreeLeafHash, EntryHash) -> @@ -117,12 +89,34 @@ add(LogEntry, TreeLeafHash, EntryHash) -> save_sth(STH) -> {ok, STHFile} = application:get_env(plop, sth_path), + {struct, PropList} = STH, + Treesize = proplists:get_value(<<"tree_size">>, PropList), lager:debug("writing new sth to ~p: ~p", [STHFile, STH]), - atomic:replacefile(STHFile, mochijson2:encode(STH)). + ok = atomic:replacefile(STHFile, mochijson2:encode(STH)), + ok = db:set_treesize(Treesize). + +initsize() -> + db:create_size_table(), + Treesize = case sth() of + noentry -> + 0; + {struct, PropList} -> + proplists:get_value(<<"tree_size">>, PropList) + end, + db:set_treesize(Treesize). sth() -> - {ok, STHFile} = application:get_env(plop, sth_path), - mochijson2:decode(atomic:readfile(STHFile)). + case application:get_env(plop, sth_path) of + {ok, STHFile} -> + case atomic:readfile(STHFile) of + noentry -> + noentry; + Contents -> + mochijson2:decode(Contents) + end; + undefined -> + noentry + end. -spec get(non_neg_integer(), non_neg_integer()) -> [{non_neg_integer(), binary(), binary()}]. @@ -296,9 +290,6 @@ fill_in_entry({_Index, LeafHash, notfetched}) -> db:get_by_leaf_hash(LeafHash). %%%%%%%%%%%%%%%%%%%% -handle_call(stop, _From, Plop) -> - {stop, normal, stopped, Plop}. - verify_sth(Treesize, Timestamp, Roothash, PackedSignature) -> STH = serialise(#sth_signed{ diff --git a/src/plop_app.erl b/src/plop_app.erl index 5aae9d9..04f1c1e 100644 --- a/src/plop_app.erl +++ b/src/plop_app.erl @@ -8,6 +8,7 @@ start(normal, Args) -> hackney:start(), http_auth:init_key_table(), + plop:initsize(), plop_sup:start_link(Args). stop(_State) -> |