Skip to content

Commit

Permalink
[#28] Implement shards:new/3.
Browse files Browse the repository at this point in the history
  • Loading branch information
cabol committed Dec 9, 2016
1 parent 606bc7a commit 1579457
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 10 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,19 +282,19 @@ distributed.
Node `a`:

```
$ erl -sname a@localhost -pa _build/default/lib/*/ebin -s shards
$ rebar3 shell --sname a@localhost
```

Node `b`:

```
$ erl -sname b@localhost -pa _build/default/lib/*/ebin -s shards
$ rebar3 shell --sname b@localhost
```

Node `c`:

```
$ erl -sname c@localhost -pa _build/default/lib/*/ebin -s shards
$ rebar3 shell --sname c@localhost
```

**2.** Create a table with global scope (`{scope, g}`) on each node:
Expand Down
20 changes: 20 additions & 0 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,26 @@

{deps, []}.

%% == Common Test ==

{ct_compile_opts, [
debug_info,
warnings_as_errors,
warn_unused_vars,
ewarn_export_all,
warn_shadow_vars,
warn_unused_import,
warn_unused_function,
warn_bif_clash,
warn_unused_record,
warn_deprecated_function,
warn_obsolete_guard,
strict_validation,
warn_export_vars,
warn_exported_vars,
warn_untyped_record
]}.

%% == EDoc ==

{edoc_opts, []}.
Expand Down
6 changes: 5 additions & 1 deletion src/shards.erl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
match_spec_compile/1,
match_spec_run/2,
member/2,
new/2,
new/2, new/3,
next/2,
prev/2,
safe_fixtable/2,
Expand Down Expand Up @@ -453,6 +453,10 @@ new(Name, Options) ->
_ -> shards_local:new(Name, Options)
end.

%% @equiv shards_dist:new(Name, Options, Nodes)
new(Name, Options, Nodes) ->
shards_dist:new(Name, Options, Nodes).

%% @doc
%% Wrapper to `shards_local:next/3' and `shards_dist:next/3'.
%%
Expand Down
17 changes: 15 additions & 2 deletions src/shards_dist.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
delete/1, delete/3,
delete_all_objects/2,
delete_object/3,
new/2,
new/2, new/3,
insert/3,
insert_new/3,
lookup/3,
Expand Down Expand Up @@ -58,7 +58,8 @@ join(Tab, Nodes) ->
get_nodes(Tab).

%% @private
join_(Tab) -> pg2:join(Tab, whereis(Tab)).
join_(Tab) ->
pg2:join(Tab, whereis(Tab)).

-spec leave(Tab, Nodes) -> LeavedNodes when
Tab :: atom(),
Expand Down Expand Up @@ -239,6 +240,18 @@ member(Tab, Key, State) ->
new(Name, Options) ->
shards_local:new(Name, Options).

-spec new(Name, Options, Nodes) -> Name when
Name :: atom(),
Options :: [shards_local:option()],
Nodes :: [node()].
new(Name, Options, Nodes) ->
AllNodes = lists:usort([node() | Nodes]),
global:trans({?MODULE, Name}, fun() ->
rpc:multicall(AllNodes, shards, new, [Name, Options])
end),
_ = join(Name, AllNodes),
Name.

-spec select(Tab, MatchSpec, State) -> [Match] when
Tab :: atom(),
MatchSpec :: ets:match_spec(),
Expand Down
3 changes: 0 additions & 3 deletions src/shards_owner_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
%% Macro to setup a supervisor worker
-define(worker(Mod, Args, Spec), child(worker, Mod, Args, Spec)).

%% Default number of shards
-define(N_SHARDS, erlang:system_info(schedulers_online)).

%% Macro to check if restart strategy is allowed
-define(is_restart_strategy(S_), S_ == one_for_one; S_ == one_for_all).

Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.hrl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
%%%-------------------------------------------------------------------
%%% @doc
%%% Common definitions.
%%% Commons.
%%% @end
%%%-------------------------------------------------------------------

Expand Down

0 comments on commit 1579457

Please sign in to comment.