From 339656a35441a5d7d02282e6a5c6cdfeaf0571d6 Mon Sep 17 00:00:00 2001 From: Linus Nordberg Date: Thu, 1 May 2014 18:02:31 +0200 Subject: Move some records out of public header file. Improve db initialisation some. Wait for mnesia tables in init() and make the init-functions do some more. --- src/db.erl | 16 ++++++++++------ src/db.hrl | 8 ++++++++ src/plop.hrl | 1 + 3 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 src/db.hrl create mode 120000 src/plop.hrl (limited to 'src') diff --git a/src/db.erl b/src/db.erl index b534757..857615c 100644 --- a/src/db.erl +++ b/src/db.erl @@ -12,27 +12,30 @@ code_change/3]). -include_lib("stdlib/include/qlc.hrl"). +-include("db.hrl"). -include("plop.hrl"). %% @doc Set up a database schema on all nodes that are to be included %% in the "database cluster". Has to be run _before_ mnesia has been %% started. init_db() -> - init_db([]). + init_db([node()]). init_db(Nodes) -> - mnesia:create_schema([node()] ++ Nodes), - init_tables(Nodes). + ok = mnesia:create_schema(Nodes), + rpc:multicall(Nodes, application, start, [mnesia]), + init_tables(Nodes), + rpc:multicall(Nodes, application, stop, [mnesia]). %% @doc Run once, or rather every time you start on a new database. %% If run more than once, we'll get {aborted, {already_exists, TABLE}}. init_tables() -> - init_tables([]). + init_tables([node()]). init_tables(Nodes) -> %% We've once upon a time invoked mnesia:create_schema/1 with the %% nodes that will be part of the database. RamCopies = [], DiscCopies = [], - DiscOnlyCopies = [node()] ++ Nodes, + DiscOnlyCopies = Nodes, mnesia:start(), mnesia:create_table(plop, [{type, set}, {ram_copies, RamCopies}, @@ -48,7 +51,8 @@ dump_to_file(Filename) -> mnesia:dump_to_textfile(Filename). init(_Args) -> - {ok, []}. % TODO: return state + {mnesia:wait_for_tables([plop], 5000), + []}. start_link() -> gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). diff --git a/src/db.hrl b/src/db.hrl new file mode 100644 index 0000000..16b9103 --- /dev/null +++ b/src/db.hrl @@ -0,0 +1,8 @@ +%% @doc What's stored in the database. +%% 'index' is the primary key, 'hash' is also indexed. +-record(plop, { + index :: non_neg_integer(), % Primary key. + hash :: binary(), % Hash over mtl. + mtl :: mtl(), % Merkle Tree Leaf, an #mtl{}. + spt_text :: binary() % Signed Plop Timestamp, an #spt_on_wire{}. + }). diff --git a/src/plop.hrl b/src/plop.hrl new file mode 120000 index 0000000..38dfcbf --- /dev/null +++ b/src/plop.hrl @@ -0,0 +1 @@ +/home/linus/usr/src/plop/include/plop.hrl \ No newline at end of file -- cgit v1.1