You can use custom match specs guards for searching instead of the ets:fun2ms/1
, for example:
-module(match_spec_guards).
-export([new/0, populate/0, search/1]).
-record(row, {
key :: {shell, Time :: integer(), Name :: string(), ID :: term()},
value :: term()
}).
new() ->
ets:new(?MODULE, [set, named_table, {keypos, #row.key}]).
populate() ->
[ets:insert(?MODULE, #row{key = {shell, I, integer_to_list(I), I * 1000}, value = I * 1000})
|| I <- lists:seq(1,1000)].
search(Filters) ->
Guards = [filter_to_guard(Filter) || Filter <- Filters],
MatchHead = {row, {shell, '$1', '$2', '$3'}, '_'},
Result = ['$_'],
ets:select(?MODULE, [{MatchHead, Guards, Result}]).
filter_to_guard({time_higher_than, X}) -> {'<', X, '$1'};
filter_to_guard({time_lower_than, X}) -> {'>', X, '$1'};
filter_to_guard({name_is, X}) -> {'==', '$2', X};
filter_to_guard({id_is, X}) -> {'==', '$3', X}.
You can use it like:
Erlang/OTP 22 [erts-10.7.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1]
Eshell V10.7.1 (abort with ^G)
1> c(match_spec_guards).
{ok,match_spec_guards}
2> match_spec_guards:new().
match_spec_guards
3> match_spec_guards:populate().
[true,true,true,true,true,true,true,true,true,true,true,
true,true,true,true,true,true,true,true,true,true,true,true,
true,true,true,true,true,true|...]
4> match_spec_guards:search([{time_higher_than, 10}, {time_lower_than, 20}]).
[{row,{shell,15,"15",15000},15000},
{row,{shell,16,"16",16000},16000},
{row,{shell,17,"17",17000},17000},
{row,{shell,12,"12",12000},12000},
{row,{shell,13,"13",13000},13000},
{row,{shell,11,"11",11000},11000},
{row,{shell,19,"19",19000},19000},
{row,{shell,14,"14",14000},14000},
{row,{shell,18,"18",18000},18000}]
5> match_spec_guards:search([{id_is, 15000}]).
[{row,{shell,15,"15",15000},15000}]
6> match_spec_guards:search([{name_is, "15000"}]).
[]
7> match_spec_guards:search([{name_is, "15"}]).
[{row,{shell,15,"15",15000},15000}]
You have more information about the match specs syntaxis in ets:select/2
I'd recommend using
MatchHead = #row{key = {shell, '$1', '$2', '$3'}, value = '_'},
even if you need to tweak the record definition to allow the '$x'
and '_'
atoms.
Also, please consider compiling the match spec and/or using continuations if you are using big tables.
EDIT
By abusing the complete order between erlang terms and the useful '_'
defaults in the search record, you're able to obtain what you need:
-module(match_spec_guards).
-export([new/0, populate/0, search/1]).
-record(row, {
key :: {shell, Time :: integer(), Name :: string(), ID :: term()},
value :: term()
}).
-record(s1shell_query, {
tsched_low = 0,
tsched_high = undefined,
id = '_',
name = '_'
}).
new() ->
ets:new(?MODULE, [set, named_table, {keypos, #row.key}]).
populate() ->
[ets:insert(?MODULE, #row{key = {shell, I, integer_to_list(I), I * 1000}, value = I * 1000})
|| I <- lists:seq(1,1000)].
search(#s1shell_query{tsched_low = Low, tsched_high = High} = Query) ->
MatchHead = #row{key = {shell, '$1', Query#s1shell_query.name, Query#s1shell_query.id}, value = '_'},
Guards = [{'>', High, '$1'}, {'=<', Low, '$1'}],
ets:select(?MODULE, [{MatchHead, Guards, ['$_']}]).
The id
and name
will be taken from the record if it's defined (and use '_'
if it's not), and the guards will perform the filtering naturally if they are defined, and using the complete order for the higher limit if they are not (atoms are always higher than numbers, regardless of the atom and the number).
An example of the usage follows:
Erlang/OTP 22 [erts-10.7.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1]
Eshell V10.7.1 (abort with ^G)
1> c(match_spec_guards).
{ok,match_spec_guards}
2> match_spec_guards:new().
match_spec_guards
3> match_spec_guards:populate().
[true,true,true,true,true,true,true,true,true,true,true,
true,true,true,true,true,true,true,true,true,true,true,true,
true,true,true,true,true,true|...]
4> rr(match_spec_guards).
[row,s1shell_query]
7> match_spec_guards:search(#s1shell_query{id = 14000}).
[#row{key = {shell,14,"14",14000},value = 14000}]
8> match_spec_guards:search(#s1shell_query{name = "14"}).
[#row{key = {shell,14,"14",14000},value = 14000}]
9> match_spec_guards:search(#s1shell_query{tsched_high = 20}).
[#row{key = {shell,1,"1",1000},value = 1000},
#row{key = {shell,15,"15",15000},value = 15000},
#row{key = {shell,6,"6",6000},value = 6000},
#row{key = {shell,16,"16",16000},value = 16000},
#row{key = {shell,8,"8",8000},value = 8000},
#row{key = {shell,2,"2",2000},value = 2000},
#row{key = {shell,9,"9",9000},value = 9000},
#row{key = {shell,17,"17",17000},value = 17000},
#row{key = {shell,12,"12",12000},value = 12000},
#row{key = {shell,7,"7",7000},value = 7000},
#row{key = {shell,13,"13",13000},value = 13000},
#row{key = {shell,10,"10",10000},value = 10000},
#row{key = {shell,3,"3",3000},value = 3000},
#row{key = {shell,11,"11",11000},value = 11000},
#row{key = {shell,19,"19",19000},value = 19000},
#row{key = {shell,14,"14",14000},value = 14000},
#row{key = {shell,5,"5",5000},value = 5000},
#row{key = {shell,4,"4",4000},value = 4000},
#row{key = {shell,18,"18",18000},value = 18000}]
10> match_spec_guards:search(#s1shell_query{tsched_low = 998}).
[#row{key = {shell,998,"998",998000},value = 998000},
#row{key = {shell,999,"999",999000},value = 999000},
#row{key = {shell,1000,"1000",1000000},value = 1000000}]