Skip to content

Commit

Permalink
Add MAYBE macro
Browse files Browse the repository at this point in the history
Add MAYBE macro that conditionally evaluates expression based on another
expression. It can be used to reduce lines of code where you would
usually use a case expression to match against a boolean.

Use the MAYBE macro in event tick to clean up code a bit.
  • Loading branch information
rzezeski committed Feb 18, 2014
1 parent c58e8b2 commit aba0fc4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
10 changes: 10 additions & 0 deletions include/yokozuna.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@

-define(DATA_DIR, application:get_env(riak_core, platform_data_dir)).


-define(MAYBE(Check, Expression, Default),
case Check of
true -> Expression;
false -> Default
end).

-define(MAYBE(Check, Expression),
?MAYBE(Check, Expression, ok)).

%% Core security requires `{BucketType, Bucket}'. Technically, these
%% arent so strict, and so we refer to them as `{Resource,
%% SubResource}' where `Resource' is a resource like index or schema
Expand Down
22 changes: 7 additions & 15 deletions src/yz_events.erl
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,14 @@ handle_info(tick, S) ->
PrevHash = S#state.prev_index_hash,
CurrHash = riak_core_metadata:prefix_hash(?YZ_META_INDEXES),
NumTicks = S#state.num_ticks,
FullCheck = (NumTicks == ?NUM_TICKS_START),

case yz_solr:is_up() of
true ->
case FullCheck orelse (PrevHash /= CurrHash) of
true -> ok = sync_indexes();
false -> ok
end;
false ->
ok
end,
IsFullCheck = (NumTicks == ?NUM_TICKS_START),
DidHashChange = PrevHash /= CurrHash,

case FullCheck of
true -> ok = remove_non_owned_data(yz_cover:get_ring_used());
false -> ok
end,
ok = ?MAYBE(yz_solr:is_up() andalso (IsFullCheck orelse DidHashChange),
sync_indexes()),

ok = ?MAYBE(IsFullCheck,
remove_non_owned_data(yz_cover:get_ring_used())),

ok = set_tick(),

Expand Down

0 comments on commit aba0fc4

Please sign in to comment.