From 5fc1ec8ac6d76f5798373658b06021696f5e1e02 Mon Sep 17 00:00:00 2001 From: Linus Nordberg Date: Thu, 29 May 2014 12:38:41 +0200 Subject: Add db:size/0 and some error checking. --- src/db.erl | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'src/db.erl') diff --git a/src/db.erl b/src/db.erl index 50117e4..08960f7 100644 --- a/src/db.erl +++ b/src/db.erl @@ -4,7 +4,7 @@ %% API. -export([start_link/0, stop/0]). -export([init_db/0, init_db/1, init_tables/0, init_tables/1]). --export([add/1, find/1, get_by_index/2]). +-export([add/1, find/1, get_by_index/2, size/0]). %% API for testing. -export([dump/1, destroy_tables/0, info_tables/0, dump_to_file/1]). %% gen_server callbacks. @@ -37,18 +37,24 @@ init_tables(Nodes) -> DiscCopies = [], DiscOnlyCopies = Nodes, mnesia:start(), - mnesia:create_table(plop, [{type, set}, - {ram_copies, RamCopies}, - {disc_copies, DiscCopies}, - {disc_only_copies, DiscOnlyCopies}, - {attributes, record_info(fields, plop)}]), - mnesia:add_table_index(plop, hash). + {atomic, ok} = + mnesia:create_table(plop, + [{type, set}, + {ram_copies, RamCopies}, + {disc_copies, DiscCopies}, + {disc_only_copies, DiscOnlyCopies}, + {attributes, record_info(fields, plop)}, + {majority, true}]), + {atomic, ok} = mnesia:add_table_index(plop, hash). + destroy_tables() -> mnesia:delete_table(plop). info_tables() -> mnesia:table_info(plop, all). dump_to_file(Filename) -> mnesia:dump_to_textfile(Filename). +size() -> + mnesia:table_info(plop, size). init(_Args) -> {mnesia:wait_for_tables([plop], 5000), []}. @@ -59,6 +65,7 @@ start_link() -> stop() -> gen_server:call(?MODULE, stop). +%% API. add(Entry) -> gen_server:call(?MODULE, {add, Entry}). @@ -119,5 +126,5 @@ handle_call({get_by_index, {Start, End}}, _From, State) -> Result = ['$2'], mnesia:select(plop, [{MatchHead, Guard, Result}]) end, - Res = mnesia:transaction(F), + {atomic, Res} = mnesia:transaction(F), {reply, Res, State}. -- cgit v1.1