From b9d4a7298de8b5aaf551f8cee9405b97207ecdf6 Mon Sep 17 00:00:00 2001 From: Bikram Chatterjee Date: Mon, 10 Feb 2020 15:48:02 +0100 Subject: [PATCH] Skvh snap filter fix (#318) * #308 '*' is not interpreted as wildcard becuse only symbols in list are considered wildcards. added clause hasWild to interpret '*' as wildcard too. * #308 Added unit test for testing behaviour of filter compilation for ['*'] case * #308 - added specific case for skvh '*' handling in filter2ms * #308 Updated tests according to comments * cosmetics and refactoring Co-authored-by: piosok --- src/imem_snap.erl | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/imem_snap.erl b/src/imem_snap.erl index bdd3aad..0cf3921 100755 --- a/src/imem_snap.erl +++ b/src/imem_snap.erl @@ -1158,13 +1158,16 @@ process_chunk(SkvhMap, FoldFun, #{skvh := true} = Ctx) -> process_chunk(Rec, FoldFun, Ctx) -> apply_fold_fun(Rec, FoldFun, Ctx). -filters2ms(true, Filters) -> - [case hasWild(Filter) of - false -> {#{ckey => Filter}, [], ['$_']}; - true -> {#{ckey => '$1'}, f2mc(Filter, '$1'), ['$_']} - end || Filter <- Filters]; -filters2ms(false, Filters) -> - [{'$1', f2mc(Filter, {element, 2, '$1'}), ['$_']} || Filter <- Filters]. +filters2ms(_, []) -> []; +filters2ms(true, [Filter|Filters]) -> + [filter2ms(Filter) | filters2ms(true, Filters)]; +filters2ms(false, [Filter | Filters]) -> + [{'$1', f2mc(Filter, {element, 2, '$1'}), ['$_']} | filters2ms(false, Filters)]. + +filter2ms(Filter) -> filter2ms(Filter, hasWild(Filter)). +filter2ms('*', _) -> {'$1', [], ['$_']}; +filter2ms(Filter, false) -> {#{ckey => Filter}, [], ['$_']}; +filter2ms(Filter, true) -> {#{ckey => '$1'}, f2mc(Filter, '$1'), ['$_']}. apply_fold_fun(Term, FoldFun, Ctx) -> case FoldFun(Term, Ctx) of @@ -1388,7 +1391,7 @@ f2mc_test_() -> { inparallel, [{ - lists:flatten(io_lib:format("~s : ~p", [Title, Filter])), + Title, fun() -> case catch f2mc(Filter, '$1') of {'EXIT', Exception} -> @@ -1451,6 +1454,21 @@ f2mc_test_() -> ] }. +filters2ms_test_() -> + { + inparallel, + [ + { + Title, + ?_assertEqual(MatchSpec, filters2ms(IsSkvh, Filter)) + } || {Title, IsSkvh, Filter, MatchSpec} <- [ + {"all", false, ['*'], [{'$1',[],['$_']}]}, + {"list all", true, [['*']], [{#{ckey => '$1'},[{is_list,'$1'}],['$_']}] }, + {"all tuple", true, [{'*'}], [{#{ckey => '$1'},[{is_tuple,'$1'}],['$_']}]} + ] + ] + }. + msrun_test_() -> Rows = [ ["a"],