Skip to content

Commit

Permalink
[#74] Fixed elastic-blog example.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfacorro committed Oct 15, 2014
1 parent 5bd11c6 commit c543667
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 84 deletions.
141 changes: 76 additions & 65 deletions examples/elastic-blog/run
Original file line number Diff line number Diff line change
Expand Up @@ -4,79 +4,90 @@

%% Main entry point.
main(_) ->
setup_codepath(),
start_apps(),
setup_codepath(),
start_apps(),

%% Install our event manager and register our handlers (not mandatory).
{ok, _EventManagerPid} = gen_event:start({local, my_event_bus}),
gen_event:add_handler(my_event_bus, blog_event_handler, []),

%% Let's first create our db schema. Database should be already created. All
%% other schemas are auto generated.
ok = sumo:create_schema(),

%% Cleanup authors and posts, so we can have a fresh runup.
_NumRows1 = blog:del_author(),
_NumRows2 = blog:del_post(),
_NumRows3 = blog:del_reader(),
_NumRows3 = blog:del_vote(),

%% Let's create a new blog author and a new blog entry. The result is the
%% same author and post with their new id's filled in.
Author = blog:new_author(<<"Pepe Gorostiga">>, <<"some binary jpg">>),
Post = blog:new_post(<<"How awesome sumo_db is">>,
<<"It just make things easy">>,
Author),

%% Let's now delete them.
true = blog:del_post(Post),
true = blog:del_author(Author),

%% Let's create them again
Author2 = blog:new_author(<<"Pepe Gorostiga">>, <<"some binary jpg">>),
_Author3 = blog:new_author(<<"Some other guy">>, <<"some other photo">>),
Post2 = blog:new_post(<<"How awesome sumo_db is">>,
<<"It just make things easy">>,
Author2),

timer:sleep(1000),

%% Update author and post.
blog:save_author(blog_author:update_photo(<<"new photo">>, Author2)),
blog:save_post(blog_post:update_title("My new shiny title", Post2)),

io:format("Authors by name: ~p~n",
[blog:find_authors_by_name(<<"Pepe Gorostiga">>, 0, 0)]),
io:format("Authors: ~p~n", [blog:find_all_authors(10, 0)]),
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,
%% 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.
Reader = blog:new_reader("Marcelo Gornstein", "[email protected]"),
Reader2 = blog:new_reader("Pepe Gorostiga", "[email protected]"),

timer:sleep(1000),

io:format("A Reader: ~p~n", [blog:find_reader(blog_reader:id(Reader))]),
blog:save_reader(blog_reader:update_email("[email protected]", Reader)),
blog:save_reader(blog_reader:update_email("[email protected]", Reader2)),
io:format("Another Reader: ~p~n",
[blog:find_reader(blog_reader:id(Reader))]),

%% Create a vote
_Vote = blog:new_vote(blog_reader:id(Reader2), blog_post:id(Post2)),

io:format("Deleted ~p authors~n",
[blog:del_author_by_name("Pepe Gorostiga")]).

% Install our event manager and register our handlers (not mandatory).
{ok, _EventManagerPid} = gen_event:start({local, my_event_bus}),
gen_event:add_handler(my_event_bus, blog_event_handler, []),

% Let's first create our db schema. Database should be already created. All
% other schemas are auto generated.
ok = sumo:create_schema(),

% Cleanup authors and posts, so we can have a fresh runup.
_NumRows1 = blog:del_author(),
_NumRows2 = blog:del_post(),
_NumRows3 = blog:del_reader(),

% Let's create a new blog author and a new blog entry. The result is the
% same author and post with their new id's filled in.
Author = blog:new_author("Pepe Gorostiga", <<"some binary jpg">>),
Post = blog:new_post("How awesome sumo_db is", "It just make things easy", Author),

% Let's now delete them.
true = blog:del_post(Post),
true = blog:del_author(Author),

% Let's create them again
Author2 = blog:new_author("Pepe Gorostiga", <<"some binary jpg">>),
Author3 = blog:new_author("Some other guy", <<"some other photo">>),
Post2 = blog:new_post("How awesome sumo_db is", "It just make things easy", Author2),

% Update author and post.
blog:save_author(blog_author:update_photo(<<"new photo">>, Author2)),
blog:save_post(blog_post:update_title("My new shiny title", Post2)),

io:format("Authors by name: ~p~n", [blog:find_authors_by_name("Pepe Gorostiga", 10, 0)]),
io:format("Authors: ~p~n", [blog:find_all_authors(10, 0)]),
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,
% 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.
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))]),

blog:save_reader(blog_reader:update_email("[email protected]", Reader)),
blog:save_reader(blog_reader:update_email("[email protected]", Reader2)),
io:format("Another Reader: ~p~n", [blog:find_reader(blog_reader:id(Reader))]),

% Create a vote
_Vote = blog:new_vote(blog_reader:id(Reader2), blog_post:id(Post2)),
setup_codepath() ->
Dir = filename:dirname(escript:script_name()),

io:format("Deleted ~p authors~n", [blog:del_author_by_name("Pepe Gorostiga")]).
DepsDirs = filelib:wildcard(Dir ++ "/../../deps/*"),

setup_codepath() ->
Dir = filename:dirname(escript:script_name()),
Deps = [
"lager", "goldrush", "worker_pool", "emysql", "emongo",
"sqlite3"
],
DepsDir = filename:absname(Dir ++ "/../../deps/"),
code:add_patha(filename:absname(Dir ++ "/../../ebin")),
[ code:add_patha(DepsDir ++ "/" ++ X ++ "/ebin") || X <- Deps].
code:add_patha(filename:absname(Dir ++ "/../../ebin")),
code:add_patha(filename:absname(Dir ++ "/ebin")),
[code:add_patha(X ++ "/ebin") || X <- DepsDirs].

start_apps() ->
application:ensure_all_started(tirerl),

application:ensure_all_started(goldrush),
Apps = [
sasl, compiler, syntax_tools, lager, crypto,
emysql, emongo, sqlite3, worker_pool,
sumo_db
],
[ ok = application:start(X) || X <- Apps].
[ application:start(X) || X <- Apps ].
10 changes: 8 additions & 2 deletions examples/elastic-blog/src/blog.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@

%%% Posts API.
-export([
total_posts/0, new_post/3, save_post/1, del_post/0, del_post/1, find_post/1
total_posts/0, new_post/3, save_post/1, del_post/0, del_post/1, del_vote/0,
find_post/1
]).

%%% Author API.
Expand Down Expand Up @@ -60,7 +61,7 @@ find_all_authors(Limit, Offset) ->

-spec find_authors_by_name(string(), non_neg_integer(), non_neg_integer()) -> [blog_author:author()].
find_authors_by_name(Name, Limit, Offset) ->
sumo:find_by(blog_author, [{name, list_to_atom(Name)}], Limit, Offset).
sumo:find_by(blog_author, [{name, Name}], Limit, Offset).

%% @doc Finds a post given the id.
-spec find_post(blog_post:id()) -> blog_post:post()|notfound.
Expand Down Expand Up @@ -97,6 +98,11 @@ del_post() ->
del_reader() ->
sumo:delete_all(blog_reader).

%% @doc Deletes all readers.
-spec del_vote() -> non_neg_integer().
del_vote() ->
sumo:delete_all(blog_vote).

%% @doc Deletes the given author.
-spec del_author_by_name(string()) -> non_neg_integer().
del_author_by_name(Name) ->
Expand Down
2 changes: 1 addition & 1 deletion examples/elastic-blog/src/blog_author.erl
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ sumo_sleep(Author) ->
-spec sumo_schema() -> sumo:schema().
sumo_schema() ->
sumo:new_schema(?MODULE, [
sumo:new_field(id, string),
sumo:new_field(id, string, [id]),
sumo:new_field(name, string),
sumo:new_field(photo, string)
]).
21 changes: 13 additions & 8 deletions examples/elastic-blog/src/blog_post.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
%% Exports.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-export([sumo_schema/0, sumo_sleep/1, sumo_wakeup/1]).
-export([new/4]).
-export([new/3]).
-export([id/1, author/1, title/1, content/1, update_content/2, update_title/2]).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand All @@ -41,12 +41,17 @@
%% Public API.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% @doc Returns a new post.
-spec new(binary(), binary(), binary(), blog_author:id()) -> post().
new(Id, Title, Content, AuthorId)
when is_binary(Id),
is_binary(Title),
-spec new(binary(), binary(), blog_author:id()) -> post().
new(Title, Content, AuthorId)
when is_binary(Title),
is_binary(Content),
is_integer(AuthorId) ->
is_binary(AuthorId) ->
create(undefined, Title, Content, AuthorId).

%% @doc Returns a new post (internal).
-spec create(undefined|id(), string(), string(), blog_author:id()) -> post().
create(Id, Title, Content, AuthorId)
when is_binary(Title), is_binary(Content), is_binary(AuthorId) ->
[{id, Id}, {title, Title}, {content, Content}, {author_id, AuthorId}].

%% @doc Returns the id of the given post.
Expand Down Expand Up @@ -109,10 +114,10 @@ sumo_sleep(Post) ->
-spec sumo_schema() -> sumo:schema().
sumo_schema() ->
sumo:new_schema(?MODULE, [
sumo:new_field(id, string),
sumo:new_field(id, string, [id]),
sumo:new_field(title, string),
sumo:new_field(content, string),
sumo:new_field(author_id, integer)
sumo:new_field(author_id, string)
]).

%% We don't have the extends module attribute in R16, so this was moved out from
Expand Down
2 changes: 1 addition & 1 deletion examples/elastic-blog/src/blog_reader.erl
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ sumo_sleep(Reader) ->
-spec sumo_schema() -> sumo:schema().
sumo_schema() ->
sumo:new_schema(?MODULE, [
sumo:new_field(id, string),
sumo:new_field(id, string, [id]),
sumo:new_field(name, string),
sumo:new_field(email, string)
]).
2 changes: 1 addition & 1 deletion examples/elastic-blog/src/blog_vote.erl
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ sumo_sleep(Vote) ->
-spec sumo_schema() -> sumo:schema().
sumo_schema() ->
sumo:new_schema(?MODULE, [
sumo:new_field(id, string),
sumo:new_field(id, string, [id]),
sumo:new_field(post_id, string),
sumo:new_field(reader_id, string)
]).
26 changes: 20 additions & 6 deletions src/sumo_repo_elasticsearch.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,16 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Public API.
-export([
init/1, create_schema/2, persist/2, find_by/3, find_by/5, find_all/2,
delete/3, delete_by/3, delete_all/2
init/1,
create_schema/2,
persist/2,
find_by/3,
find_by/5,
find_all/2,
find_all/5,
delete/3,
delete_by/3,
delete_all/2
]).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down Expand Up @@ -82,8 +90,9 @@ persist(Doc, #{index := Index, pool_name := PoolName} = State) ->

sumo_internal:set_field(IdField, GenId, Doc);
Id ->
Update = #{doc => FieldsMap},
{ok, _ } =
tirerl:update_doc(PoolName, Index, Type, Id, FieldsMap),
tirerl:update_doc(PoolName, Index, Type, Id, Update),
Doc
end,

Expand All @@ -98,14 +107,16 @@ delete_by(DocName, Conditions, #{index := Index, pool_name := PoolName} = State)

{ok, _} = tirerl:delete_by_query(PoolName, Index, Type, Query, []),

{ok, not_implemented, State}.
{ok, 1, State}.

delete_all(DocName, #{index := Index, pool_name := PoolName} = State) ->
lager:debug("dropping type: ~p", [DocName]),
lager:debug("deleting all: ~p", [DocName]),
Type = atom_to_binary(DocName, utf8),
MatchAll = #{query => #{match_all => #{}}},

{ok, _} = tirerl:delete_by_query(PoolName, Index, Type, MatchAll, []),
{ok, unknown, State}.

{ok, 1, State}.

find_by(DocName, Conditions, Limit, Offset,
#{index := Index, pool_name := PoolName} = State) ->
Expand All @@ -126,6 +137,9 @@ find_by(DocName, Conditions, State) ->
find_all(DocName, State) ->
find_by(DocName, [], State).

find_all(DocName, _OrderField, Limit, Offset, State) ->
find_by(DocName, [], Limit, Offset, State).

create_schema(Schema, #{index := Index, pool_name := PoolName} = State) ->
SchemaName = sumo_internal:schema_name(Schema),
Type = atom_to_binary(SchemaName, utf8),
Expand Down

0 comments on commit c543667

Please sign in to comment.