diff --git a/README.org b/README.org index a0d943d..2b6b589 100644 --- a/README.org +++ b/README.org @@ -221,13 +221,14 @@ Return the stored value in this query module. * Job processing with composed modules - - You can use query modules to execute jobs, if the job doesn't error, process an event. + - You can use query modules to execute jobs, if the job errors or not, process an event. - `with' is similar to `run', the main difference is additional statistics and execution order + - when a job completes in error, the event data will contain an additional {error, _} item To execute a job through the query module, inputting an event on success. #+BEGIN_EXAMPLE Event = gre:make([{'a', 2}], [list]). - Result = glc:run(Module, fun(Event, State) -> + {ExecutionTime, Result}= glc:run(Module, fun(Event, State) -> %% do not end with {error, _} or throw an exception end, Event). #+END_EXAMPLE @@ -287,6 +288,10 @@ Return the query combining the conditional logic of multiple modules glc_lib:reduce(glc:all([Module1:info('query'), Module2:info('query')]). #+END_EXAMPLE +Return all statistics from this query module. +#+BEGIN_EXAMPLE +glc:info(Module). +#+END_EXAMPLE * Build diff --git a/src/glc_lib.erl b/src/glc_lib.erl index 787cb3c..2e35748 100644 --- a/src/glc_lib.erl +++ b/src/glc_lib.erl @@ -60,6 +60,11 @@ matches({Key, '<', Term}, Event) -> {true, Term2} -> Term2 < Term; false -> false end; +matches({Key, '=<', Term}, Event) -> + case gre:find(Key, Event) of + {true, Term2} -> Term2 =< Term; + false -> false + end; matches({Key, '=', Term}, Event) -> case gre:find(Key, Event) of {true, Term2} -> Term2 =:= Term; @@ -75,6 +80,11 @@ matches({Key, '>', Term}, Event) -> {true, Term2} -> Term2 > Term; false -> false end; +matches({Key, '>=', Term}, Event) -> + case gre:find(Key, Event) of + {true, Term2} -> Term2 >= Term; + false -> false + end; matches({Key, '*'}, Event) -> case gre:find(Key, Event) of {true, _} -> true; @@ -97,10 +107,14 @@ repeat(Query, Fun) -> -spec onoutput(glc_ops:op()) -> output | no_return(). onoutput({_, '<', _}) -> output; +onoutput({_, '=<', _}) -> + output; onoutput({_, '=', _}) -> output; onoutput({_, '>', _}) -> output; +onoutput({_, '>=', _}) -> + output; onoutput({_, '*'}) -> output; onoutput({_, '!'}) -> @@ -382,12 +396,33 @@ delete_from_any_test() -> default_is_output_test_() -> [?_assertEqual(output, glc_lib:onoutput(glc:lt(a, 1))), + ?_assertEqual(output, glc_lib:onoutput(glc:lte(a, 1))), ?_assertEqual(output, glc_lib:onoutput(glc:eq(a, 1))), ?_assertEqual(output, glc_lib:onoutput(glc:gt(a, 1))), + ?_assertEqual(output, glc_lib:onoutput(glc:gte(a, 1))), ?_assertEqual(output, glc_lib:onoutput(glc:wc(a))), ?_assertEqual(output, glc_lib:onoutput(glc:nf(a))) ]. +matches_test_() -> + Event = gre:make([{a, 2}], [list]), + [?_assertEqual(true, glc_lib:matches(glc:lt(a, 3), Event)), + ?_assertEqual(true, glc_lib:matches(glc:lte(a, 2), Event)), + ?_assertEqual(true, glc_lib:matches(glc:eq(a, 2), Event)), + ?_assertEqual(true, glc_lib:matches(glc:gt(a, 1), Event)), + ?_assertEqual(true, glc_lib:matches(glc:gte(a, 2), Event)), + ?_assertEqual(true, glc_lib:matches(glc:wc(a), Event)), + ?_assertEqual(true, glc_lib:matches(glc:nf(b), Event)), + + ?_assertEqual(false, glc_lib:matches(glc:lt(a, 2), Event)), + ?_assertEqual(false, glc_lib:matches(glc:lte(a, 1), Event)), + ?_assertEqual(false, glc_lib:matches(glc:eq(a, 3), Event)), + ?_assertEqual(false, glc_lib:matches(glc:gt(a, 2), Event)), + ?_assertEqual(false, glc_lib:matches(glc:gte(a, 3), Event)), + ?_assertEqual(false, glc_lib:matches(glc:wc(b), Event)), + ?_assertEqual(false, glc_lib:matches(glc:nf(a), Event)) + ]. + -ifdef(PROPER). diff --git a/src/glc_ops.erl b/src/glc_ops.erl index 837fce4..36927e5 100644 --- a/src/glc_ops.erl +++ b/src/glc_ops.erl @@ -21,6 +21,7 @@ ]). -type op() :: + {atom(), '<', term()} | {atom(), '=<', term()} | {atom(), '=', term()} | {atom(), '!=', term()} |