From 0be218452a815f266e0acd35d8066e3df6466a1d Mon Sep 17 00:00:00 2001 From: Joseph Wayne Norton Date: Mon, 24 Sep 2012 23:25:42 +0900 Subject: [PATCH] Add open/3, open_link/1-3, and fix some dialyzer warnings --- src/hanoidb.erl | 43 +++++++++++++++++++++++++++++++++++------ src/hanoidb_nursery.erl | 2 +- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/hanoidb.erl b/src/hanoidb.erl index 3798834..53c09db 100644 --- a/src/hanoidb.erl +++ b/src/hanoidb.erl @@ -31,7 +31,8 @@ -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). --export([open/1, open/2, transact/2, close/1, get/2, lookup/2, delete/2, put/3, put/4, +-export([open/1, open/2, open/3, open_link/1, open_link/2, open_link/3, + transact/2, close/1, get/2, lookup/2, delete/2, put/3, put/4, fold/3, fold_range/4, destroy/1]). -export([get_opt/2, get_opt/3]). @@ -74,16 +75,46 @@ %% Create or open a hanoidb store. Argument `Dir' names a %% directory in which to keep the data files. By convention, we %% name hanoidb data directories with extension ".hanoidb". -- spec open(Dir::string()) -> hanoidb(). +- spec open(Dir::string()) -> {ok, hanoidb()} | ignore | {error, term()}. open(Dir) -> open(Dir, []). %% @doc Create or open a hanoidb store. -- spec open(Dir::string(), Opts::[config_option()]) -> hanoidb(). +- spec open(Dir::string(), Opts::[config_option()]) -> {ok, hanoidb()} | ignore | {error, term()}. open(Dir, Opts) -> ok = start_app(), gen_server:start(?MODULE, [Dir, Opts], []). +%% @doc Create or open a hanoidb store with a registered name. +- spec open(Name::{local, Name::atom()} | {global, GlobalName::term()} | {via, ViaName::term()}, + Dir::string(), Opts::[config_option()]) -> {ok, hanoidb()} | ignore | {error, term()}. +open(Name, Dir, Opts) -> + ok = start_app(), + gen_server:start(Name, ?MODULE, [Dir, Opts], []). + +%% @doc +%% Create or open a hanoidb store as part of a supervision tree. +%% Argument `Dir' names a directory in which to keep the data files. +%% By convention, we name hanoidb data directories with extension +%% ".hanoidb". +- spec open_link(Dir::string()) -> {ok, hanoidb()} | ignore | {error, term()}. +open_link(Dir) -> + open_link(Dir, []). + +%% @doc Create or open a hanoidb store as part of a supervision tree. +- spec open_link(Dir::string(), Opts::[config_option()]) -> {ok, hanoidb()} | ignore | {error, term()}. +open_link(Dir, Opts) -> + ok = start_app(), + gen_server:start_link(?MODULE, [Dir, Opts], []). + +%% @doc Create or open a hanoidb store as part of a supervision tree +%% with a registered name. +- spec open_link(Name::{local, Name::atom()} | {global, GlobalName::term()} | {via, ViaName::term()}, + Dir::string(), Opts::[config_option()]) -> {ok, hanoidb()} | ignore | {error, term()}. +open_link(Name, Dir, Opts) -> + ok = start_app(), + gen_server:start_link(Name, ?MODULE, [Dir, Opts], []). + %% @doc %% Close a Hanoi data store. - spec close(Ref::pid()) -> ok. @@ -277,7 +308,7 @@ open_levels(Dir, Options) -> %% remove old nursery data file NurseryFileName = filename:join(Dir, "nursery.data"), - file:delete(NurseryFileName), + _ = file:delete(NurseryFileName), %% Do enough incremental merge to be sure we won't deadlock in insert {TopLevel, MaxMerge} = @@ -314,7 +345,7 @@ handle_info({bottom_level, N}, #state{ nursery=Nursery, top=TopLevel }=State) State2 = State#state{ max_level = N, nursery= hanoidb_nursery:set_max_level(Nursery, N) }, - hanoidb_level:set_max_level(TopLevel, N), + _ = hanoidb_level:set_max_level(TopLevel, N), {noreply, State2}; @@ -367,7 +398,7 @@ handle_call({get, Key}, From, State=#state{ top=Top, nursery=Nursery } ) when is {value, Value} when is_binary(Value) -> {reply, {ok, Value}, State}; none -> - hanoidb_level:lookup(Top, Key, fun(Reply) -> gen_server:reply(From, Reply) end), + _ = hanoidb_level:lookup(Top, Key, fun(Reply) -> gen_server:reply(From, Reply) end), {noreply, State} end; diff --git a/src/hanoidb_nursery.erl b/src/hanoidb_nursery.erl index 40b4f82..db80f9a 100644 --- a/src/hanoidb_nursery.erl +++ b/src/hanoidb_nursery.erl @@ -99,7 +99,7 @@ read_nursery_from_log(Directory, MaxLevel, Config) -> %% @doc Add a Key/Value to the nursery %% @end --spec do_add(#nursery{}, binary(), binary()|?TOMBSTONE, pos_integer() | infinity, pid()) -> {ok, #nursery{}}. +-spec do_add(#nursery{}, binary(), binary()|?TOMBSTONE, non_neg_integer() | infinity, pid()) -> {ok, #nursery{}}. do_add(Nursery, Key, Value, infinity, Top) -> do_add(Nursery, Key, Value, 0, Top); do_add(Nursery=#nursery{log_file=File, cache=Cache, total_size=TotalSize, count=Count, config=Config}, Key, Value, KeyExpiryTime, Top) ->