Skip to content

Commit

Permalink
bump to 0.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitc committed Dec 16, 2016
1 parent d1b8ce4 commit 919d1a5
Show file tree
Hide file tree
Showing 67 changed files with 3,505 additions and 2,606 deletions.
2 changes: 1 addition & 1 deletion lib/barrel/src/barrel.app.src
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{application, barrel,
[{description, "Barrel-db core application"},
{vsn, "0.4.1"},
{vsn, "0.6.0"},
{registered, []},
{mod, {barrel_app, []}},
{applications,
Expand Down
200 changes: 103 additions & 97 deletions lib/barrel/src/barrel.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,17 @@
revsdiff/3
]).

-export([
find_by_key/5
]).

%% Database API

-export([
create_database/2,
connect_database/2,
close_database/1,
delete_database/1,
database_names/1,
database_infos/1
open_store/2,
close_store/1,
delete_store/1,
store_infos/1
]).


Expand All @@ -64,10 +66,16 @@

-type dbname() :: binary().
-type store() :: atom().
-type conn() :: map().

%% TODO: to define
-type db_infos() :: list().
-type store_infos() :: #{
name := store(),
id := binary(),
doc_count := non_neg_integer(),
last_update_seq := non_neg_integer(),
system_doc_count := non_neg_integer(),
last_index_seq => non_neg_integer()
}.

-type doc() :: map().
-type rev() :: binary().
Expand Down Expand Up @@ -134,173 +142,171 @@
]).


%% @doc create a database in the store. Return true if it has been created or false if it's already existed.
-spec create_database(Store, DbName) -> Res when
Store :: store(),
DbName :: dbname(),
Res :: {Created:: boolean(), Conn::conn()} | {error, any()}.
create_database(Store, Name) ->
barrel_db:start(Name, Store, [{create_if_missing, true}]).

%% @doc connect to a database in a store.
-spec connect_database(Store, DbName) -> Res when
Store :: store(),
DbName :: dbname(),
Res :: {_, conn()} | {error, not_found} | {error, any()}.
connect_database(Store, Name) ->
case barrel_db:start(Name, Store, []) of
{error, _Reason} = Error -> Error;
{OK, Conn} when is_boolean(OK) -> {ok, Conn}
end.

%% @doc close a database
-spec close_database(Conn::conn()) -> ok.
close_database(Conn) ->
barrel_db:stop(Conn).
open_store(Name, Options) ->
Module = maps:get(adapter, Options, barrel_rocksdb),
Module:open_store(Name, Options).

%% @doc delete a database
-spec delete_database(Name :: dbname()) -> ok.
delete_database(Conn) ->
barrel_db:clean(Conn).
close_store(Name) ->
case ets:lookup(barrel_stores, Name) of
[] ->
ok;
[{Name, Mod}] ->
Mod:stop_store(Name)
end.

-spec database_infos(Conn::conn()) ->
{ok, DbInfos::db_infos()} | {error, term()}.
database_infos(Conn) ->
barrel_db:infos(Conn).

delete_store(Name) ->
case ets:lookup(barrel_stores, Name) of
[] ->
ok;
[{Name, Mod}] ->
Mod:delete_store(Name)
end.

%% @doc Returns a list of database names for a store.
-spec database_names(Store::store()) -> [Conn::conn()].
database_names(Store) ->
barrel_store:all_dbs(Store).

-spec store_infos(Store::store()) ->
{ok, DbInfos::store_infos()} | {error, term()}.
store_infos(Store) ->
barrel_store:infos(Store).

%% Database API.

%% @doc retrieve a document by its key
-spec get(Conn, DocId, Options) -> Res when
Conn::conn(),
-spec get(Store, DocId, Options) -> Res when
Store::store(),
DocId :: docid(),
Options :: read_options(),
Doc :: doc,
Doc :: doc(),
Res :: {ok, Doc} | {error, not_found} | {error, any()}.
get(Conn, DocId, Options) ->
barrel_db:get(Conn, DocId, Options).
get(Store, DocId, Options) ->
barrel_store:get(Store, DocId, Options).


%% @doc create or update a document. Return the new created revision
%% with the docid or a conflict.
-spec put(Conn, DocId, Body, Options) -> Res when
Conn::conn(),
-spec put(Store, DocId, Body, Options) -> Res when
Store::store(),
DocId :: docid(),
Body :: doc(),
Options :: write_options(),
Res :: {ok, docid(), rev()} | {error, conflict()} | {error, any()}.
put(Conn, DocId, Body, Options) ->
barrel_db:put(Conn, DocId, Body, Options).
put(Store, DocId, Body, Options) ->
barrel_store:put(Store, DocId, Body, Options).

%% @doc insert a specific revision to a a document. Useful for the replication.
%% It takes the document id, the doc to edit and the revision history (list of ancestors).
-spec put_rev(Conn, DocId, Body, History, Options) -> Res when
Conn::conn(),
-spec put_rev(Store, DocId, Body, History, Options) -> Res when
Store::store(),
DocId :: docid(),
Body :: doc(),
History :: [rev()],
Options :: write_options(),
Res :: ok | {error, conflict()} | {error, any()}.
put_rev(Conn, DocId, Body, History, Options) ->
barrel_db:put_rev(Conn, DocId, Body, History, Options).
Res :: {ok, docid(), rev()} | {error, conflict()} | {error, any()}.
put_rev(Store, DocId, Body, History, Options) ->
barrel_store:put_rev(Store, DocId, Body, History, Options).

%% @doc delete a document
-spec delete(Conn, DocId, RevId, Options) -> Res when
Conn::conn(),
-spec delete(Store, DocId, RevId, Options) -> Res when
Store::store(),
DocId :: docid(),
RevId :: rev(),
Options :: write_options(),
Res :: {ok, docid(), rev()} | {error, conflict()} | {error, any()}.
delete(Conn, DocId, RevId, Options) ->
barrel_db:delete(Conn, DocId, RevId, Options).
delete(Store, DocId, RevId, Options) ->
barrel_store:delete(Store, DocId, RevId, Options).

%% @doc create a document . Like put but only create a document without updating the old one.
%% A doc shouldn't have revision. Optionally the document ID can be set in the doc.
-spec post(Conn, Doc, Options) -> Res when
Conn::conn(),
-spec post(Store, Doc, Options) -> Res when
Store::store(),
Doc :: doc(),
Options :: write_options(),
Res :: {ok, docid(), rev()} | {error, conflict()} | {error, any()}.
post(Conn, Doc, Options) ->
barrel_db:post(Conn, Doc, Options).
post(Store, Doc, Options) ->
barrel_store:post(Store, Doc, Options).

%% @doc fold all docs by Id
-spec fold_by_id(Conn, Fun, AccIn, Options) -> AccOut | Error when
Conn::conn(),
-spec fold_by_id(Store, Fun, AccIn, Options) -> AccOut | Error when
Store::store(),
FunRes :: {ok, Acc2::any()} | stop | {stop, Acc2::any()},
Fun :: fun((DocId :: docid(), DocInfo :: docinfo(), Doc :: doc(), Acc1 :: any()) -> FunRes),
Options :: fold_options(),
AccIn :: any(),
AccOut :: any(),
Error :: {error, term()}.
fold_by_id(Conn, Fun, Acc, Options) ->
barrel_db:fold_by_id(Conn, Fun, Acc, Options).
fold_by_id(Store, Fun, Acc, Options) ->
barrel_store:fold_by_id(Store, Fun, Acc, Options).

%% @doc fold all changes since last sequence
-spec changes_since(Conn, Since, Fun, AccIn) -> AccOut when
Conn::conn(),
-spec changes_since(Store, Since, Fun, AccIn) -> AccOut when
Store::store(),
Since :: non_neg_integer(),
FunRes :: {ok, Acc2::any()} | stop | {stop, Acc2::any()},
Fun :: fun((Seq :: non_neg_integer(), Change :: change(), Acc :: any()) -> FunRes),
AccIn :: any(),
AccOut :: any().
changes_since(Conn, Since, Fun, Acc) ->
barrel_db:changes_since(Conn, Since, Fun, Acc, []).
changes_since(Store, Since, Fun, Acc) ->
barrel_store:changes_since(Store, Since, Fun, Acc, []).

%% @doc fold all changes since last sequence
-spec changes_since(Conn, Since, Fun, AccIn, Opts) -> AccOut when
Conn::conn(),
-spec changes_since(Store, Since, Fun, AccIn, Opts) -> AccOut when
Store::store(),
Since :: non_neg_integer(),
FunRes :: {ok, Acc2::any()} | stop | {stop, Acc2::any()},
Fun :: fun((Seq :: non_neg_integer(), Change :: change(), Acc :: any()) -> FunRes),
AccIn :: any(),
AccOut :: any(),
Opts :: list().
changes_since(Conn, Since, Fun, Acc, Opts) ->
barrel_db:changes_since(Conn, Since, Fun, Acc, Opts).
changes_since(Store, Since, Fun, Acc, Opts) ->
barrel_store:changes_since(Store, Since, Fun, Acc, Opts).

%% @doc find in the index a document by its path
-spec find_by_key(Store, Path, Fun, AccIn, Options) -> AccOut | Error when
Store::store(),
Path :: binary(),
FunRes :: {ok, Acc2::any()} | stop | {stop, Acc2::any()},
Fun :: fun((DocId :: docid(), Doc :: doc(), Acc1 :: any()) -> FunRes),
Options :: fold_options(),
AccIn :: any(),
AccOut :: any(),
Error :: {error, term()}.
find_by_key(Store, Path, Fun, AccIn, Opts) ->
barrel_store:find_by_key(Store, Path, Fun, AccIn, Opts).

%% @doc get all revisions ids that differ in a doc from the list given
-spec revsdiff(Conn, DocId, RevIds) -> Res when
Conn::conn(),
-spec revsdiff(Store, DocId, RevIds) -> Res when
Store::store(),
DocId :: docid(),
RevIds :: [revid()],
Res:: {ok, Missing :: [revid()], PossibleAncestors :: [revid()]}.
revsdiff(Conn, DocId, RevIds) ->
barrel_db:revsdiff(Conn, DocId, RevIds).
revsdiff(Store, DocId, RevIds) ->
barrel_store:revsdiff(Store, DocId, RevIds).



attach(Conn, DocId, AttDescription, Options) ->
barrel_attachments:attach(Conn, DocId, AttDescription, Options).
attach(Store, DocId, AttDescription, Options) ->
barrel_attachments:attach(Store, DocId, AttDescription, Options).

attach(Conn, DocId, AttDescription, Binary, Options) ->
barrel_attachments:attach(Conn, DocId, AttDescription, Binary, Options).
attach(Store, DocId, AttDescription, Binary, Options) ->
barrel_attachments:attach(Store, DocId, AttDescription, Binary, Options).

get_attachment(Conn, DocId, AttId, Options) ->
barrel_attachments:get_attachment(Conn, DocId, AttId, Options).
get_attachment(Store, DocId, AttId, Options) ->
barrel_attachments:get_attachment(Store, DocId, AttId, Options).

get_attachment_binary(Conn, DocId, AttId, Options) ->
barrel_attachments:get_attachment_binary(Conn, DocId, AttId, Options).
get_attachment_binary(Store, DocId, AttId, Options) ->
barrel_attachments:get_attachment_binary(Store, DocId, AttId, Options).

replace_attachment(Conn, DocId, AttId, AttDescription, Options) ->
barrel_attachments:replace_attachment(Conn, DocId, AttId, AttDescription, Options).
replace_attachment(Store, DocId, AttId, AttDescription, Options) ->
barrel_attachments:replace_attachment(Store, DocId, AttId, AttDescription, Options).

replace_attachment_binary(Conn, DocId, AttId, Binary, Options) ->
barrel_attachments:replace_attachment_binary(Conn, DocId, AttId, Binary, Options).
replace_attachment_binary(Store, DocId, AttId, Binary, Options) ->
barrel_attachments:replace_attachment_binary(Store, DocId, AttId, Binary, Options).

delete_attachment(Conn, DocId, AttId, Options) ->
barrel_attachments:delete_attachment(Conn, DocId, AttId, Options).
delete_attachment(Store, DocId, AttId, Options) ->
barrel_attachments:delete_attachment(Store, DocId, AttId, Options).

attachments(Conn, DocId, Options) ->
barrel_attachments:attachments(Conn, DocId, Options).
attachments(Store, DocId, Options) ->
barrel_attachments:attachments(Store, DocId, Options).



Expand Down
40 changes: 20 additions & 20 deletions lib/barrel/src/barrel_attachments.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,28 @@

-define(ATTTAG, <<"_attachments">>).

attach(Conn, DocId, AttDescription, Options) ->
{ok, Doc} = barrel_db:get(Conn, DocId, Options),
attach(Store, DocId, AttDescription, Options) ->
{ok, Doc} = barrel_store:get(Store, DocId, Options),
Attachments = maps:get(?ATTTAG, Doc, []),
AttId = maps:get(<<"id">>, AttDescription),
case find_att_doc(AttId, Attachments) of
{ok, _} -> {error, attachment_conflict};
{error, not_found} ->
Attachments2 = [AttDescription|Attachments],
barrel_db:put(Conn, DocId, Doc#{?ATTTAG => Attachments2}, Options)
barrel_store:put(Store, DocId, Doc#{?ATTTAG => Attachments2}, Options)
end.

attach(Conn, DocId, AttDescription, Binary, Options) ->
attach(Store, DocId, AttDescription, Binary, Options) ->
Data = base64:encode(Binary),
attach(Conn, DocId, AttDescription#{<<"_data">> => Data}, Options).
attach(Store, DocId, AttDescription#{<<"_data">> => Data}, Options).

get_attachment(Conn, DocId, AttId, Options) ->
{ok, Doc} = barrel_db:get(Conn, DocId, Options),
get_attachment(Store, DocId, AttId, Options) ->
{ok, Doc} = barrel_store:get(Store, DocId, Options),
Attachments = maps:get(?ATTTAG, Doc, []),
find_att_doc(AttId, Attachments).

get_attachment_binary(Conn, DocId, AttId, Options) ->
{ok, Doc} = barrel_db:get(Conn, DocId, Options),
get_attachment_binary(Store, DocId, AttId, Options) ->
{ok, Doc} = barrel_store:get(Store, DocId, Options),
Attachments = maps:get(?ATTTAG, Doc, []),
case find_att_doc(AttId, Attachments) of
{error, not_found} -> {error, not_found};
Expand All @@ -57,32 +57,32 @@ get_attachment_binary(Conn, DocId, AttId, Options) ->
{ok, base64:decode(Data)}
end.

replace_attachment(Conn, DocId, AttId, AttDescription, Options) ->
{ok, Doc} = barrel_db:get(Conn, DocId, Options),
replace_attachment(Store, DocId, AttId, AttDescription, Options) ->
{ok, Doc} = barrel_store:get(Store, DocId, Options),
Attachments = maps:get(?ATTTAG, Doc, []),
AttId = maps:get(<<"id">>, AttDescription),
NewAttachments = replace_att_doc(AttId, AttDescription, Attachments),
barrel_db:put(Conn, DocId, Doc#{?ATTTAG => NewAttachments}, Options).
barrel_store:put(Store, DocId, Doc#{?ATTTAG => NewAttachments}, Options).

replace_attachment_binary(Conn, DocId, AttId, Binary, Options) ->
{ok, Doc} = barrel_db:get(Conn, DocId, Options),
replace_attachment_binary(Store, DocId, AttId, Binary, Options) ->
{ok, Doc} = barrel_store:get(Store, DocId, Options),
Attachments = maps:get(?ATTTAG, Doc, []),
case find_att_doc(AttId, Attachments) of
{error, not_found} -> {error, not_found};
{ok, Attachment} ->
NewData = base64:encode(Binary),
NewAttachment = Attachment#{<<"_data">> => NewData},
replace_attachment(Conn, DocId, AttId, NewAttachment, Options)
replace_attachment(Store, DocId, AttId, NewAttachment, Options)
end.

delete_attachment(Conn, DocId, AttId, Options) ->
{ok, Doc} = barrel_db:get(Conn, DocId, Options),
delete_attachment(Store, DocId, AttId, Options) ->
{ok, Doc} = barrel_store:get(Store, DocId, Options),
Attachments = maps:get(?ATTTAG, Doc, []),
NewAttachments = delete_att_doc(AttId, Attachments),
barrel_db:put(Conn, DocId, Doc#{?ATTTAG => NewAttachments}, Options).
barrel_store:put(Store, DocId, Doc#{?ATTTAG => NewAttachments}, Options).

attachments(Conn, DocId, Options) ->
{ok, Doc} = barrel_db:get(Conn, DocId, Options),
attachments(Store, DocId, Options) ->
{ok, Doc} = barrel_store:get(Store, DocId, Options),
maps:get(?ATTTAG, Doc, []).


Expand Down
Loading

0 comments on commit 919d1a5

Please sign in to comment.