[erlang-questions] Mnesia wildcard query

karol skocik karol.skocik@REDACTED
Tue May 14 20:11:22 CEST 2013


On Tue, May 14, 2013 at 6:01 PM, Janos Hary <janos.n.hary@REDACTED> wrote:

> Thanks for the answer. As far as I understand it works great if the prefix
> is known at compile time. I wasn’t clear on this, but in my case the prefix
> is given in runtime.****
>
> **
>

Yes, literal fun as arg to ets:fun2ms is a compile time entity. It's good
for getting the first sketch of match spec in shell.


> So far I come up with this solution. It understands a ‘*’ character at a
> pattern’s end. I’d be happy to get suggestions to improve it.****
>
> ** **
>
> %% finds '*' in pattern and return a minimum and maximum pattern****
>
> %% e.g. <<"hello*">> -> {<<"hello">>, <<"hellp">>}****
>
> %% if binary string X starts with 'hello' then <<"hello">> <= X <
> <<"hellp">>****
>
> wildcard(Pattern) *when* is_binary(Pattern) ->****
>
>        *case* binary:match(Pattern, <<"*">>) *of*****
>
>               nomatch -> ****
>
>                      {Pattern, Pattern};****
>
>               {0, _}  -> ****
>
>                      {error, wrong_pattern};****
>
>               {S, _}  ->****
>
>                      PatPref = binary:part(Pattern, 0, S-1),****
>
>                      io:format("*~s*", [PatPref]),****
>
>                      <<PatLast:8>> = binary:part(Pattern, S-1, 1),****
>
>                      {<<PatPref/binary, PatLast:8>>, <<PatPref/binary, (
> PatLast+1):8>>}****
>
>        *end*.****
>
> ** **
>
> find(Pat) ->****
>
>        {PatMin, PatMax} = wildcard(Pat),****
>
>        F = *fun*() ->****
>
>                      qlc:eval(qlc:q(****
>
>                            [Patient || #patient{search_name = N}=Patient<- mnesia:table(pat),
> ****
>
>                                          N >= PatMin, N < PatMax]****
>
>                      ))****
>
>               *end*,****
>
>        mnesia:activity(transaction, F, [], mnesia_frag).****
>
> **
>

This is a full scan of the table, and since match spec's first arg can't be
a binary with prefix only the full scan can't be avoided.
Function you have is pretty much the only way to do what you want.

Cheers
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130514/e6c4c63b/attachment.htm>


More information about the erlang-questions mailing list