Skip to content

Commit

Permalink
Merge pull request #235 from inaka/cabol.221.sumo_architecture_enhanc…
Browse files Browse the repository at this point in the history
…ements

sumo architecture enhancements
  • Loading branch information
Brujo Benavides committed Mar 10, 2016
2 parents 00850e0 + ff3e463 commit 002d25f
Show file tree
Hide file tree
Showing 54 changed files with 1,539 additions and 5,219 deletions.
32 changes: 8 additions & 24 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,19 @@ PROJECT = sumo_db

CONFIG ?= test/test.config

DEPS = lager uuid emysql emongo tirerl epgsql worker_pool riakc iso8601
DEPS = lager uuid worker_pool
SHELL_DEPS = sync

dep_sync = git https://github.com/rustyio/sync.git 9c78e7b
dep_lager = git https://github.com/basho/lager.git 3.0.1
dep_emysql = git https://github.com/inaka/Emysql.git 0.4.2
dep_emongo = git https://github.com/inaka/emongo.git v0.2.1
dep_tirerl = git https://github.com/inaka/tirerl 0278e0856c
dep_epgsql = git https://github.com/epgsql/epgsql 2.0.0
dep_sync = git https://github.com/rustyio/sync.git 9c78e7b
dep_lager = git https://github.com/basho/lager.git 3.0.1
dep_worker_pool = git https://github.com/inaka/worker_pool.git 1.0.4
dep_riakc = git https://github.com/inaka/riak-erlang-client.git 2.1.1-R18
dep_uuid = git https://github.com/okeuday/uuid.git 31f408f4ef
dep_iso8601 = git https://github.com/zerotao/erlang_iso8601.git 0d14540
dep_uuid = git https://github.com/okeuday/uuid.git 31f408f4ef

TEST_DEPS = katana mixer
dep_katana = git https://github.com/inaka/erlang-katana.git 0.2.22
dep_mixer = git https://github.com/inaka/mixer.git 0.1.5
TEST_DEPS = katana_test mixer
dep_katana_test = git https://github.com/inaka/katana-test.git 5491fba
dep_mixer = git git://github.com/inaka/mixer.git 0.1.5

CT_SUITES ?= conditional_logic sumo_basic sumo_config sumo_find sumo_events sumo_meta
CT_SUITES ?= conditional_logic sumo_basic sumo_events sumo_meta

include erlang.mk

Expand Down Expand Up @@ -67,13 +61,3 @@ plt-all: PLT_APPS := $(ALL_TEST_DEPS_DIRS)
plt-all: test-deps test-build-plt plt

dialyze-all: app test-build-plt dialyze

# Riak tests
CT_SUITES_RIAK = nested_docs
CT_OPTS_RIAK = -vvv -erl_args -config test/riak/riak_test.config

riak_tests: test-build
mkdir -p logs/ ; \
$(gen_verbose) $(CT_RUN) -suite $(addsuffix _SUITE,$(CT_SUITES_RIAK)) $(CT_OPTS_RIAK)
rm -rf test/*beam

70 changes: 13 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[![Stories in Ready](https://badge.waffle.io/inaka/sumo_db.png?label=ready&title=Ready)](https://waffle.io/inaka/sumo_db)
# sumo_db


## About

This is a work in progress. There's also [an article][sumo-article] about
Expand All @@ -11,6 +12,7 @@ provide some basic information on how to get started.
simple persistance layer capable of interacting with different db's, while
offering a consistent api to your code.


## Contact Us

For **questions** or **general comments** regarding the use of this library,
Expand All @@ -22,6 +24,7 @@ If you find any **bugs** or have a **problem** while using this library, please
And you can check all of our open-source projects at
[inaka.github.io](http://inaka.github.io)


## Overview

* sumo_db gives you a standard way to define your db schema, regardless of the
Expand Down Expand Up @@ -51,6 +54,7 @@ And you can check all of our open-source projects at
`[{age, desc}, {name, asc}]]` will sort descendently by `age` and ascendently
by `name`.


## Backends, Stores and Repositories modules

These three concepts have a specific meaning in the context of sumo_db.
Expand All @@ -65,13 +69,21 @@ These three concepts have a specific meaning in the context of sumo_db.
repository for each entity that's defined in it. The repository is the module
that bridges the model and the store.


## Implementing an Adapter

TO implement an adapter, you must implement two behaviors: `sumo_backend` and
`sumo_store`.


## Example

See: [**examples/blog**][example-blog] for a full example. To run it, while
being in the top level directory:

make all blog


## Running Tests

To run tests successfully, you need to follow these steps first:
Expand All @@ -90,69 +102,13 @@ To run tests successfully, you need to follow these steps first:
to access that DB. For more information visit [MongoDB Tutorial](http://docs.mongodb.org/manual/tutorial).
> - For **Riak** please follow instructions below ([<i class="icon-refresh"></i> Riak](#riak)).
## Riak

### Install Riak

To install/upgrade **Riak** please follow the instructions in this link:
[Installing and Upgrading Riak](http://docs.basho.com/riak/latest/ops/building/installing).

### Initial Configurations

Due to the fact that **Riak** comes with default configuration, we need to
change some parameters required by `sumo_db`.

**Riak** has a main configuration file `riak.conf`, which you can find into
your installation path `$YOUR_INSTALL_PATH/etc/riak.conf`.

> **Note:** For more information check this link [Configuration Files](http://docs.basho.com/riak/latest/ops/advanced/configs/configuration-files).
First parameter to change is the default **Riak** backend from **Bitcask** to
**LevelDB**. This change also enables the use of [Riak Secondary Indexes](http://docs.basho.com/riak/latest/ops/advanced/configs/secondary-index/).

storage_backend = leveldb

Then proceed to enable search capabilities:

search = on

> **Note:** For more information check this link [Riak Search Settings](http://docs.basho.com/riak/latest/ops/advanced/configs/search/).
### Configuring Riak Data Types and Search

First, let's create and activate a bucket type simply called maps that is set up
to store Riak maps:

$ riak-admin bucket-type create maps '{"props":{"datatype":"map"}}'
$ riak-admin bucket-type activate maps

Now, let's create a search index called `sumo_test_index` using the default
schema:

$ curl -XPUT $RIAK_HOST/search/index/sumo_test_index \
-H 'Content-Type: application/json' \
-d '{"schema":"_yz_default"}'

With our index created, we can associate our new `sumo_test_index` index with
our `maps` bucket type:

$ riak-admin bucket-type update maps '{"props":{"search_index":"sumo_test_index"}}'

Now we can start working with **Riak** from `sumo_db`.

> **Note:** For more information check this link [Riak Data Types and Search](http://docs.basho.com/riak/latest/dev/search/search-data-types/#Maps-Example).
## SQL Extras

Aaron Spiegel has written some extra utilities for the SQL backend, which may be found at:

[https://github.com/spiegela/sumo_db-sql-extras](https://github.com/spiegela/sumo_db-sql-extras)

## Change Log

All notable changes to this project will be documented in the
[CHANGELOG.md](CHANGELOG.md).


## Contributors

We want to thank all of [our contributors](CONTRIBUTORS.md) for their hard work
Expand Down
11 changes: 1 addition & 10 deletions elvis.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
elvis,
[
{config,
[#{dirs => ["src", "test"],
[#{dirs => ["src/*", "test"],
filter => "*.erl",
rules => [{elvis_style, line_length, #{limit => 80}},
{elvis_style, no_tabs},
Expand All @@ -15,15 +15,6 @@
{elvis_style, nesting_level, #{level => 3}},
{elvis_style, god_modules, #{limit => 25}},
{elvis_style, no_if_expression},
{elvis_style,
invalid_dynamic_call,
#{ignore =>
[conditional_logic_SUITE,
sumo_find_SUITE,
sumo_basic_SUITE,
sumo,
sumo_internal]}
},
{elvis_style, used_ignored_variable},
{elvis_style, no_behavior_info},
{
Expand Down
14 changes: 4 additions & 10 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,10 @@
warn_untyped_record, debug_info
]}.
{deps, [
{lager, ".*", {git, "https://github.com/basho/lager.git", "2.1.1"}},
{emysql, ".*", {git, "https://github.com/inaka/Emysql.git", "0.4.2"}},
{emongo, ".*", {git, "https://github.com/inaka/emongo.git", "v0.2.1"}},
{tirerl, ".*", {git, "https://github.com/inaka/tirerl", "7ac7d57a24"}},
{epgsql, ".*", {git, "https://github.com/epgsql/epgsql", "2.0.0"}},
{worker_pool, ".*", {git, "https://github.com/inaka/worker_pool.git", "1.0.3"}},
{riakc, ".*", {git, "https://github.com/inaka/riak-erlang-client", "2.1.1-R18"}},
{uuid, ".*", {git, "https://github.com/okeuday/uuid.git", "v1.5.0"}},
{mixer, ".*", {git, "https://github.com/inaka/mixer", "0.1.2"}},
{iso8601, ".*", {git, "https://github.com/kivra/erlang_iso8601.git", "1.1.2"}}
{lager, ".*", {git, "https://github.com/basho/lager.git", "3.0.1"}},
{worker_pool, ".*", {git, "https://github.com/inaka/worker_pool.git", "1.0.4"}},
{uuid, ".*", {git, "https://github.com/okeuday/uuid.git", "v1.5.0"}},
{mixer, ".*", {git, "https://github.com/inaka/mixer", "0.1.5"}}
]}.
{xref_warnings, true}.
{xref_checks, [undefined_function_calls, undefined_functions, locals_not_used, deprecated_function_calls, deprecated_functions]}.
Expand Down
164 changes: 164 additions & 0 deletions src/adapter_test_helpers/sumo_basic_test_helper.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
-module(sumo_basic_test_helper).

%% Test Cases - Helpers
-export([
find/1,
find_all/1,
find_by/1,
delete_all/1,
delete/1,
check_proper_dates/1,
init_store/1
]).

-type config() :: [{atom(), term()}].

%%%=============================================================================
%%% Test Cases - Helpers
%%%=============================================================================

-spec find(config()) -> ok.
find(Config) ->
{_, Module} = lists:keyfind(module, 1, Config),

[First, Second | _] = sumo:find_all(Module),
First = sumo:find(Module, Module:id(First)),
Second = sumo:find(Module, Module:id(Second)),
notfound = sumo:find(Module, 0),
ok.

-spec find_all(config()) -> ok.
find_all(Config) ->
{_, Module} = lists:keyfind(module, 1, Config),

[_, _, _, _, _, _, _, _] = sumo:find_all(Module),
ok.

-spec find_by(config()) -> ok.
find_by(Config) ->
{_, Module} = lists:keyfind(module, 1, Config),

Results = sumo:find_by(Module, [{last_name, <<"D">>}]),
[_, _] = Results,
SortFun = fun(A, B) -> Module:name(A) < Module:name(B) end,
[First, Second | _] = lists:sort(SortFun, Results),

{Today, _} = calendar:universal_time(),

<<"B">> = Module:name(First),
<<"D">> = Module:name(Second),
3 = Module:age(First),
4 = Module:age(Second),
<<"D">> = Module:last_name(First),
undefined = Module:address(First),
Today = Module:birthdate(First),
undefined = Module:height(First),
undefined = Module:description(First),
{Today, _} = Module:created_at(First),
% Check that it returns what we have inserted
[LastPerson | _NothingElse] =
sumo:find_by(Module, [{last_name, <<"LastName">>}]),
<<"Name">> = Module:name(LastPerson),
<<"LastName">> = Module:last_name(LastPerson),
3 = Module:age(LastPerson),
undefined = Module:address(LastPerson),
{Date, _} = calendar:universal_time(),
Date = Module:birthdate(LastPerson),
1.75 = Module:height(LastPerson),
<<"description">> = Module:description(LastPerson),
<<"profile_image">> = Module:profile_image(LastPerson),
<<"weird_field">> = Module:weird_field(LastPerson),
{Today, _} = Module:created_at(LastPerson),

%% Check find_by ID
FirstId = Module:id(First),
[First1] = sumo:find_by(Module, [{id, FirstId}]),
[First1] = sumo:find_by(
Module, [{last_name, <<"D">>}, {id, FirstId}]),
[] = sumo:find_by(Module, [{name, <<"NotB">>}, {id, FirstId}]),
First1 = First,
%% Check pagination
Results1 = sumo:find_by(Module, [], 3, 1),
[_, _, _] = Results1,

%% This test is #177 github issue related
[_, _, _, _, _, _, _, _] = sumo:find_by(Module, []),
Robot = sumo:find_by(Module, [{name, <<"Model T-2000">>}]),
[_] = Robot,
ok.

-spec delete_all(config()) -> ok.
delete_all(Config) ->
{_, Module} = lists:keyfind(module, 1, Config),

sumo:delete_all(Module),
[] = sumo:find_all(Module),
ok.

-spec delete(config()) -> ok.
delete(Config) ->
{_, Module} = lists:keyfind(module, 1, Config),

%% delete_by
2 = sumo:delete_by(Module, [{last_name, <<"D">>}]),
Results = sumo:find_by(Module, [{last_name, <<"D">>}]),
[] = Results,

%% delete
[First | _ ] = All = sumo:find_all(Module),
Id = Module:id(First),
sumo:delete(Module, Id),
NewAll = sumo:find_all(Module),
[_] = All -- NewAll,
ok.

-spec check_proper_dates(config()) -> ok.
check_proper_dates(Config) ->
{_, Module} = lists:keyfind(module, 1, Config),

[P0] = sumo:find_by(Module, [{name, <<"A">>}]),
P1 = sumo:find(Module, Module:id(P0)),
[P2 | _] = sumo:find_all(Module),

{Date, _} = calendar:universal_time(),

Date = Module:birthdate(P0),
{Date, {_, _, _}} = Module:created_at(P0),
Date = Module:birthdate(P1),
{Date, {_, _, _}} = Module:created_at(P1),
Date = Module:birthdate(P2),
{Date, {_, _, _}} = Module:created_at(P2),

Person = sumo:persist(Module, Module:new(<<"X">>, <<"Z">>, 6)),
Date = Module:birthdate(Person),
ok.

-spec init_store(module()) -> ok.
init_store(Module) ->
sumo:create_schema(Module),
sumo:delete_all(Module),

sumo:persist(Module, Module:new(<<"A">>, <<"E">>, 6)),
sumo:persist(Module, Module:new(<<"B">>, <<"D">>, 3)),
sumo:persist(Module, Module:new(<<"C">>, <<"C">>, 5)),
sumo:persist(Module, Module:new(<<"D">>, <<"D">>, 4)),
sumo:persist(Module, Module:new(<<"E">>, <<"A">>, 2)),
sumo:persist(Module, Module:new(<<"F">>, <<"E">>, 1)),
sumo:persist(Module, Module:new(<<"Model T-2000">>, <<"undefined">>, 7)),

{Date, _} = calendar:universal_time(),
sumo:persist(
Module,
Module:new(
<<"Name">>,
<<"LastName">>,
3,
undefined,
Date,
1.75,
<<"description">>,
<<"profile_image">>,
<<"weird_field">>
)
),
ok.
Loading

0 comments on commit 002d25f

Please sign in to comment.