From 5fc8e7b2b996bc86d3a914aa2aaea0ee3e9e24c8 Mon Sep 17 00:00:00 2001 From: Pedram Nimreezi Date: Mon, 16 Nov 2015 15:15:40 -0500 Subject: [PATCH] Add support for not equal --- README.org | 7 +++++++ src/glc.erl | 20 ++++++++++++++++++-- src/glc_code.erl | 3 ++- src/glc_lib.erl | 7 +++++++ src/glc_ops.erl | 11 ++++++++++- 5 files changed, 44 insertions(+), 4 deletions(-) diff --git a/README.org b/README.org index 73ec4c8..4fac9b8 100644 --- a/README.org +++ b/README.org @@ -51,6 +51,11 @@ Select all events where 'a' exists and is equal to 0. glc:eq(a, 0). #+END_EXAMPLE +Select all events where 'a' exists and is not equal to 0. +#+BEGIN_EXAMPLE + glc:neq(a, 0). +#+END_EXAMPLE + Select all events where 'a' exists and is less than 0. #+BEGIN_EXAMPLE glc:lt(a, 0). @@ -220,6 +225,8 @@ or #+END_EXAMPLE * CHANGELOG +0.1.8 +- Add support for not equal 0.1.7 - Support multiple functions specified using `with/2` diff --git a/src/glc.erl b/src/glc.erl index 969c9ca..b3ae326 100644 --- a/src/glc.erl +++ b/src/glc.erl @@ -28,6 +28,8 @@ %% glc:gt(a, 0). %% %% Select all events where 'a' exists and is equal to 0. %% glc:eq(a, 0). +%% %% Select all events where 'a' exists and is not equal to 0. +%% glc:neq(a, 0). %% %% Select all events where 'a' exists and is less than 0. %% glc:lt(a, 0). %% %% Select all events where 'a' exists and is anything. @@ -74,7 +76,7 @@ -export([ lt/2, lte/2, - eq/2, + eq/2, neq/2, gt/2, gte/2, wc/1, nf/1 @@ -113,6 +115,10 @@ lte(Key, Term) -> eq(Key, Term) -> glc_ops:eq(Key, Term). +-spec neq(atom(), term()) -> glc_ops:op(). +neq(Key, Term) -> + glc_ops:neq(Key, Term). + -spec gt(atom(), term()) -> glc_ops:op(). gt(Key, Term) -> glc_ops:gt(Key, Term). @@ -434,13 +440,23 @@ events_test_() -> fun() -> %% If a selection condition but no body is specified the event %% counts as input to the query, but not as filtered out. - {compiled, Mod} = setup_query(testmod7, glc:eq('$n', 'noexists@nohost')), + {compiled, Mod} = setup_query(testmod7a, glc:eq('$n', 'noexists@nohost')), glc:handle(Mod, gre:make([{'$n', 'noexists@nohost'}], [list])), ?assertEqual(1, Mod:info(input)), ?assertEqual(0, Mod:info(filter)), ?assertEqual(1, Mod:info(output)) end }, + {"opfilter not equal test", + fun() -> + {compiled, Mod} = setup_query(testmod7b, glc:neq('$n', 'noexists@nohost')), + glc:handle(Mod, gre:make([{'$n', 'noexists@nohost'}], [list])), + glc:handle(Mod, gre:make([{'$n', 'notexists@nohost'}], [list])), + ?assertEqual(2, Mod:info(input)), + ?assertEqual(1, Mod:info(filter)), + ?assertEqual(1, Mod:info(output)) + end + }, {"opfilter wildcard test", fun() -> {compiled, Mod} = setup_query(testmod8, glc:wc(a)), diff --git a/src/glc_code.erl b/src/glc_code.erl index 5325652..5a5c0fe 100644 --- a/src/glc_code.erl +++ b/src/glc_code.erl @@ -253,10 +253,11 @@ abstract_filter_({Key, '!'}, OnMatch, OnNomatch, State) -> _OnMatch=fun(#state{}=State2) -> OnMatch(State2) end, State); abstract_filter_({Key, Op, Value}, OnMatch, OnNomatch, State) - when Op =:= '>'; Op =:= '='; Op =:= '<'; + when Op =:= '>'; Op =:= '='; Op =:= '!='; Op =:= '<'; Op =:= '>='; Op =:= '=<'; Op =:= '<=' -> Op2 = case Op of '=' -> '=:='; + '!=' -> '=/='; '<=' -> '=<'; Op -> Op end, diff --git a/src/glc_lib.erl b/src/glc_lib.erl index c0ac3e5..787cb3c 100644 --- a/src/glc_lib.erl +++ b/src/glc_lib.erl @@ -65,6 +65,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; @@ -253,6 +258,8 @@ is_valid({Field, '=<', _Term}) when is_atom(Field) -> true; is_valid({Field, '=', _Term}) when is_atom(Field) -> true; +is_valid({Field, '!=', _Term}) when is_atom(Field) -> + true; is_valid({Field, '>=', _Term}) when is_atom(Field) -> true; is_valid({Field, '>', _Term}) when is_atom(Field) -> diff --git a/src/glc_ops.erl b/src/glc_ops.erl index 8b093ce..837fce4 100644 --- a/src/glc_ops.erl +++ b/src/glc_ops.erl @@ -3,7 +3,7 @@ -export([ lt/2, lte/2, - eq/2, + eq/2, neq/2, gt/2, gte/2, wc/1, nf/1 @@ -23,6 +23,7 @@ -type op() :: {atom(), '=<', term()} | {atom(), '=', term()} | + {atom(), '!=', term()} | {atom(), '>', term()} | {atom(), '>=', term()} | {atom(), '*'} | @@ -55,6 +56,14 @@ eq(Key, Term) when is_atom(Key) -> eq(Key, Term) -> erlang:error(badarg, [Key, Term]). +%% @doc Test that a field value is not equal to a term. +-spec neq(atom(), term()) -> op(). +neq(Key, Term) when is_atom(Key) -> + {Key, '!=', Term}; +neq(Key, Term) -> + erlang:error(badarg, [Key, Term]). + + %% @doc Test that a field value is greater than a term. -spec gt(atom(), term()) -> op(). gt(Key, Term) when is_atom(Key) ->