Skip to content

Commit

Permalink
Merge pull request #105 from inaka/jfacorro.104.remove.repo.mentions
Browse files Browse the repository at this point in the history
[Closes #104] Removed all references to repositories.
  • Loading branch information
Brujo Benavides committed Dec 16, 2014
2 parents 114f664 + d5c5837 commit 2548d0e
Show file tree
Hide file tree
Showing 18 changed files with 86 additions and 82 deletions.
2 changes: 1 addition & 1 deletion examples/blog/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# About
This is an escript that uses sumo_db (and the code in ./src) to implement a blog.

It uses different repositories (mysql, mongo) just as an example and
It uses different stores (mysql, mongo) just as an example and
to make a point about how easy it is to switch or mix different databases.

# Tour
Expand Down
2 changes: 1 addition & 1 deletion examples/blog/blog.config
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
{poolsize, 5}
]}
]},
{repositories, [
{stores, [
{mysql, sumo_store_mysql, [
{storage_backend, mysql_backend},
{workers, 5}
Expand Down
4 changes: 2 additions & 2 deletions examples/blog/run
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ main(_) ->
io:format("A blog: ~p~n", [blog:find_post(blog_post:id(Post2))]),
io:format("An author: ~p~n", [blog:find_author(blog_author:id(Author2))]),

% Call a specific method of a repo (not handled by the generic repo,
% Call a specific method of a store (not handled by the generic store,
% i.e: a "complex" work).
% io:format("Total number of posts: ~p~n", [blog:total_posts()]),

% Create a blog reader, in a nother repo and backend.
% Create a blog reader, in a nother store and backend.
Reader = blog:new_reader("Marcelo Gornstein", "[email protected]"),
Reader2 = blog:new_reader("Pepe Gorostiga", "[email protected]"),
io:format("A Reader: ~p~n", [blog:find_reader(blog_reader:id(Reader))]),
Expand Down
4 changes: 2 additions & 2 deletions examples/elastic-blog/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# About
This is an escript that uses sumo_db (and the code in ./src) to implement a blog.

It uses different repositories (mysql, mongo) just as an example and
to make a point about how easy it is to switch or mix different databases.
It only uses an elasticsearch store, for an example on using multiple stores check
the `blog` example.

# Tour
* Start by taking a rough look at **./run**, and then **src/blog.erl**.
Expand Down
12 changes: 6 additions & 6 deletions examples/elastic-blog/config/blog.config
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@
{poolsize, 10}
]}
]},
{repositories,
{stores,
[
{elasticsearch_repo, sumo_store_elasticsearch,
{elasticsearch_store, sumo_store_elasticsearch,
[
{storage_backend, elasticsearch_backend},
{workers, 10}
]}
]},
{docs,
[
{blog_post, elasticsearch_repo},
{blog_author, elasticsearch_repo},
{blog_reader, elasticsearch_repo},
{blog_vote, elasticsearch_repo}
{blog_post, elasticsearch_store},
{blog_author, elasticsearch_store},
{blog_reader, elasticsearch_store},
{blog_vote, elasticsearch_store}
]},
{events, []}
]},
Expand Down
4 changes: 2 additions & 2 deletions examples/elastic-blog/run
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ main(_) ->
io:format("A blog: ~p~n", [blog:find_post(blog_post:id(Post2))]),
io:format("An author: ~p~n", [blog:find_author(blog_author:id(Author2))]),

%% Call a specific method of a repo (not handled by the generic repo,
%% Call a specific method of a store (not handled by the generic store,
%% i.e: a "complex" work).
%% io:format("Total number of posts: ~p~n", [blog:total_posts()]),

%% Create a blog reader, in a nother repo and backend.
%% Create a blog reader, in a nother store and backend.
Reader = blog:new_reader("Marcelo Gornstein", "[email protected]"),
Reader2 = blog:new_reader("Pepe Gorostiga", "[email protected]"),

Expand Down
72 changes: 38 additions & 34 deletions src/sumo.erl
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ get_docs() ->
-spec create_schema() -> ok.
create_schema() ->
lists:foreach(
fun({DocName, Repo}) ->
create_schema(DocName, Repo)
fun({DocName, Store}) ->
create_schema(DocName, Store)
end,
get_docs()
),
Expand All @@ -100,56 +100,60 @@ find(DocName, Id) ->
IdFieldName = sumo_internal:id_field_name(DocName),
find_one(DocName, [{IdFieldName, Id}]).

%% @doc Returns all docs from the given repo.
%% @doc Returns all docs from the given store.
-spec find_all(schema_name()) -> [user_doc()].
find_all(DocName) ->
case sumo_store:find_all(sumo_internal:get_repo(DocName), DocName) of
case sumo_store:find_all(sumo_internal:get_store(DocName), DocName) of
{ok, Docs} -> docs_wakeup(DocName, Docs);
Error -> throw(Error)
end.

%% @doc Returns Limit docs from the given repo, starting at offset.
%% @doc Returns Limit docs from the given store, starting at offset.
-spec find_all(schema_name(), sort(), non_neg_integer(), non_neg_integer()) ->
[user_doc()].
find_all(DocName, SortFields0, Limit, Offset) ->
SortFields = normalize_sort_fields(SortFields0),
Repo = sumo_internal:get_repo(DocName),
case sumo_store:find_all(Repo, DocName, SortFields, Limit, Offset) of
Store = sumo_internal:get_store(DocName),
case sumo_store:find_all(Store, DocName, SortFields, Limit, Offset) of
{ok, Docs} -> docs_wakeup(DocName, Docs);
Error -> throw(Error)
end.

%% @doc Returns *all* docs that match Conditions.
-spec find_by(schema_name(), conditions()) -> [user_doc()].
find_by(DocName, Conditions) ->
Repo = sumo_internal:get_repo(DocName),
case sumo_store:find_by(Repo, DocName, Conditions) of
Store = sumo_internal:get_store(DocName),
case sumo_store:find_by(Store, DocName, Conditions) of
{ok, Docs} -> docs_wakeup(DocName, Docs);
Error -> throw(Error)
end.

%% @doc Returns Limit number of docs that match Conditions, starting at
%% offset Offset.
-spec find_by(
schema_name(), conditions(), non_neg_integer(), non_neg_integer()
) -> [user_doc()].
schema_name(), conditions(), non_neg_integer(), non_neg_integer()
) -> [user_doc()].
find_by(DocName, Conditions, Limit, Offset) ->
Repo = sumo_internal:get_repo(DocName),
case sumo_store:find_by(Repo, DocName, Conditions, Limit, Offset) of
Store = sumo_internal:get_store(DocName),
case sumo_store:find_by(Store, DocName, Conditions, Limit, Offset) of
{ok, Docs} -> docs_wakeup(DocName, Docs);
Error -> throw(Error)
end.

%% @doc Returns Limit number of docs that match Conditions, starting at
%% offset Offset.
-spec find_by(
schema_name(), conditions(), sort(), non_neg_integer(), non_neg_integer()
) -> [user_doc()].
schema_name(),
conditions(),
sort(),
non_neg_integer(),
non_neg_integer()
) -> [user_doc()].
find_by(DocName, Conditions, SortFields0, Limit, Offset) ->
SortFields = normalize_sort_fields(SortFields0),
Repo = sumo_internal:get_repo(DocName),
Store = sumo_internal:get_store(DocName),
case sumo_store:find_by(
Repo, DocName, Conditions, SortFields, Limit, Offset
Store, DocName, Conditions, SortFields, Limit, Offset
) of
{ok, Docs} -> docs_wakeup(DocName, Docs);
Error -> throw(Error)
Expand All @@ -161,11 +165,11 @@ persist(DocName, State) ->
IdField = sumo_internal:id_field_name(DocName),
DocMap = DocName:sumo_sleep(State),
EventName = case maps:get(IdField, DocMap) of
undefined -> created;
_ -> updated
end,
Repo = sumo_internal:get_repo(DocName),
case sumo_store:persist(Repo, sumo_internal:new_doc(DocName, DocMap)) of
undefined -> created;
_ -> updated
end,
Store = sumo_internal:get_store(DocName),
case sumo_store:persist(Store, sumo_internal:new_doc(DocName, DocMap)) of
{ok, NewDoc} ->
Ret = sumo_internal:wakeup(DocName, NewDoc),
sumo_event:dispatch(DocName, EventName, [Ret]),
Expand All @@ -176,8 +180,8 @@ persist(DocName, State) ->
%% @doc Deletes all docs of type DocName.
-spec delete_all(schema_name()) -> non_neg_integer().
delete_all(DocName) ->
Repo = sumo_internal:get_repo(DocName),
case sumo_store:delete_all(Repo, DocName) of
Store = sumo_internal:get_store(DocName),
case sumo_store:delete_all(Store, DocName) of
{ok, NumRows} ->
case NumRows > 0 of
true -> sumo_event:dispatch(DocName, deleted_all);
Expand All @@ -199,8 +203,8 @@ delete(DocName, Id) ->
%% @doc Deletes the doc identified by Conditions.
-spec delete_by(schema_name(), conditions()) -> non_neg_integer().
delete_by(DocName, Conditions) ->
Repo = sumo_internal:get_repo(DocName),
case sumo_store:delete_by(Repo, DocName, Conditions) of
Store = sumo_internal:get_store(DocName),
case sumo_store:delete_by(Store, DocName, Conditions) of
{ok, 0} ->
0;
{ok, NumRows} ->
Expand All @@ -213,29 +217,29 @@ delete_by(DocName, Conditions) ->
%% @doc Creates the schema for the docs of type DocName.
-spec create_schema(schema_name()) -> ok.
create_schema(DocName) ->
create_schema(DocName, sumo_internal:get_repo(DocName)).
create_schema(DocName, sumo_internal:get_store(DocName)).

%% @doc Creates the schema for the docs of type DocName using the given
%% repository.
%% store.
-spec create_schema(schema_name(), atom()) -> ok.
create_schema(DocName, Repo) ->
case sumo_store:create_schema(Repo, sumo_internal:get_schema(DocName)) of
create_schema(DocName, Store) ->
case sumo_store:create_schema(Store, sumo_internal:get_schema(DocName)) of
ok ->
sumo_event:dispatch(DocName, schema_created),
ok;
Error -> throw(Error)
end.

%% @doc Calls the given custom function of a repo.
%% @doc Calls the given custom function of a store.
-spec call(schema_name(), atom()) -> term().
call(DocName, Function) ->
call(DocName, Function, []).

%% @doc Calls the given custom function of a repo with the given args.
%% @doc Calls the given custom function of a store with the given args.
-spec call(schema_name(), atom(), [term()]) -> term().
call(DocName, Function, Args) ->
Repo = sumo_internal:get_repo(DocName),
case sumo_store:call(Repo, DocName, Function, Args) of
Store = sumo_internal:get_store(DocName),
case sumo_store:call(Store, DocName, Function, Args) of
{ok, {docs, Docs}} -> docs_wakeup(DocName, Docs);
{ok, {raw, Value}} -> Value
end.
Expand Down
2 changes: 1 addition & 1 deletion src/sumo_backend_sup.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
%%% @hidden
%%% @doc Repository supervisor.
%%% @doc Backend supervisor.
%%%
%%% Copyright 2012 Inaka <[email protected]>
%%%
Expand Down
2 changes: 1 addition & 1 deletion src/sumo_doc.erl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%%% @doc Implement this behavior on your entities so the repositories can
%%% @doc Implement this behavior on your entities so the stores can
%%% properly (un)marshall them.
%%%
%%% Copyright 2012 Inaka <[email protected]>
Expand Down
12 changes: 6 additions & 6 deletions src/sumo_internal.erl
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
-export([get_field/2, set_field/3, id_field_name/1, get_schema/1, field_is/2]).
-export([field_name/1, field_type/1, field_attrs/1]).

%%% API for repo handling.
-export([get_repo/1]).
%%% API for store handling.
-export([get_store/1]).

%%% API for opaqueness
-export([wakeup/1, wakeup/2,
Expand Down Expand Up @@ -122,12 +122,12 @@ get_docs() ->

%% @doc Returns the process name that handles persistence for the given
%% Doc or DocName.
-spec get_repo(sumo:schema_name() | doc()) -> atom().
get_repo(DocName) when is_atom(DocName) ->
-spec get_store(sumo:schema_name() | doc()) -> atom().
get_store(DocName) when is_atom(DocName) ->
proplists:get_value(DocName, get_docs());

get_repo(_Doc = #{name := Name}) ->
get_repo(Name).
get_store(_Doc = #{name := Name}) ->
get_store(Name).

%% @doc Returns the schema for a given DocName.
-spec get_schema(sumo:schema_name()) -> schema().
Expand Down
24 changes: 12 additions & 12 deletions src/sumo_store.erl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%%% @doc Main interface for repositories.
%%% @doc Main interface for stores.
%%%
%%% Copyright 2012 Inaka <[email protected]>
%%%
Expand Down Expand Up @@ -83,7 +83,7 @@
%% External API.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% @doc Starts and links a new process for the given repo implementation.
%% @doc Starts and links a new process for the given store implementation.
-spec start_link(atom(), module(), [term()]) -> {ok, pid()}.
start_link(Name, Module, Options) ->
Poolsize = proplists:get_value(workers, Options, 100),
Expand All @@ -95,19 +95,19 @@ start_link(Name, Module, Options) ->
],
wpool:start_pool(Name, WPoolConfigOpts ++ WPoolOptions).

%% @doc Creates the schema of the given docs in the given repository name.
%% @doc Creates the schema of the given docs in the given store name.
-spec create_schema(atom(), sumo:schema()) -> ok | {error, term()}.
create_schema(Name, Schema) ->
wpool:call(Name, {create_schema, Schema}).

%% @doc Persist the given doc with the given repository name.
%% @doc Persist the given doc with the given store name.
-spec persist(
atom(), sumo_internal:doc()
) -> {ok, sumo_internal:doc()} | {error, term()}.
persist(Name, Doc) ->
wpool:call(Name, {persist, Doc}).

%% @doc Deletes the doc identified by id in the given repository name.
%% @doc Deletes the doc identified by id in the given store name.
-spec delete(
atom(), sumo:schema_name(), sumo:field_value()
) -> ok | {error, term()}.
Expand All @@ -121,21 +121,21 @@ delete(Name, DocName, Id) ->
delete_by(Name, DocName, Conditions) ->
wpool:call(Name, {delete_by, DocName, Conditions}).

%% @doc Deletes all docs in the given repository name.
%% @doc Deletes all docs in the given store name.
-spec delete_all(
atom(), sumo:schema_name()
) -> {ok, non_neg_integer()} | {error, term()}.
delete_all(Name, DocName) ->
wpool:call(Name, {delete_all, DocName}).

%% @doc Returns all docs from the given repositoru name.
%% @doc Returns all docs from the given store name.
-spec find_all(
atom(), sumo:schema_name()
) -> {ok, [sumo_internal:doc()]} | {error, term()}.
find_all(Name, DocName) ->
wpool:call(Name, {find_all, DocName}).

%% @doc Returns Limit docs starting at Offset from the given repository name,
%% @doc Returns Limit docs starting at Offset from the given store name,
%% ordered by OrderField. OrderField may be 'undefined'.
-spec find_all(
atom(), sumo:schema_name(), sumo:field_name(),
Expand All @@ -145,15 +145,15 @@ find_all(Name, DocName, SortFields, Limit, Offset) ->
wpool:call(Name, {find_all, DocName, SortFields, Limit, Offset}).

%% @doc Finds documents that match the given conditions in the given
%% repository name.
%% store name.
-spec find_by(
atom(), sumo:schema_name(), sumo:conditions()
) -> {ok, [sumo_internal:doc()]} | {error, term()}.
find_by(Name, DocName, Conditions) ->
wpool:call(Name, {find_by, DocName, Conditions}).

%% @doc Finds documents that match the given conditions in the given
%% repository name.
%% store name.
-spec find_by(
atom(), sumo:schema_name(), sumo:conditions(),
non_neg_integer(), non_neg_integer()
Expand All @@ -162,7 +162,7 @@ find_by(Name, DocName, Conditions, Limit, Offset) ->
wpool:call(Name, {find_by, DocName, Conditions, Limit, Offset}).

%% @doc Finds documents that match the given conditions in the given
%% repository name.
%% store name.
-spec find_by(
atom(), sumo:schema_name(), sumo:conditions(),
sumo:sort(), non_neg_integer(), non_neg_integer()
Expand All @@ -171,7 +171,7 @@ find_by(Name, DocName, Conditions, SortFields, Limit, Offset) ->
wpool:call(Name, {find_by, DocName, Conditions, SortFields, Limit, Offset}).


%% @doc Calls a custom function in the given repository name.
%% @doc Calls a custom function in the given store name.
-spec call(
atom(), sumo:schema_name(), atom(), [term()]
) -> ok | {ok, term()} | {error, term()}.
Expand Down
2 changes: 1 addition & 1 deletion src/sumo_store_elasticsearch.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
%%% @hidden
%%% @doc ElasticSearch repository implementation.
%%% @doc ElasticSearch store implementation.
%%%
%%% Copyright 2012 Inaka <[email protected]>
%%%
Expand Down
Loading

0 comments on commit 2548d0e

Please sign in to comment.