[erlang-questions] Strategies for updating Mnesia tables

Alexander Lamb <>
Fri May 9 17:58:33 CEST 2008


Le 9 mai 08 à 17:42, Ulf Wiger (TN/EAB) a écrit :

> Alexander Lamb skrev:
>> Is there a way to match against a variable number of
> > columns, giving the first columns patterns.
> > Something like:
>> F = fun() -> mnesia:match_object(
> >                profiles,
> >                {profiles, {'_','_','_'},
> >                System_Name,Production_Type,
> >                '_','_','_', * },read) end,
>> See the "*" at the end of the match_object match part.
>
> If you use records, you can write
>
>  #profiles{key = {'_','_','_'},
>            system_name = System_Name,
>            production_type = Production_Type,
>            _ = '_'}
>
> Remember to load a new version of the module at the
> same time as you convert the table.
>
Not quite certain what you mean. I do use records:

To define the table I used:

-record(profiles, {full_profile, system_name, production_type,  
timestamp, features, security_level}).

So if I understand correctly, I can do:

R = #profiles{full_profile = {'_','_','_'} , system_name =  
System_Name, production_type = Production_Type, _ = '_' },
F = fun() -> mnesia:match_object(profiles,R,read) end,

Or am I totally wrong?

Thanks,

Alex

> If you don't use records, you can spend a few more
> CPU cycles and do something like this:
>
> search_pattern(Tab, KV_list) ->
>    Wild = mnesia:table_info(Tab, wild_pattern),
>    Attrs = mnesia:table_info(Tab, attributes),
>    lists:foldl(
>        fun({K,V}, R) ->
>            setelement(attr_pos(K,Attrs), R, V)
>        end, Wild, KV_list).
>
> attr_pos(Attr, Attrs) ->
>    pos(Attrs, Attr, 2).
>
> pos([A|_], A, P) -> P;
> pos([_|As], A, P) ->
>    pos(As, A, P+1).
>
>
> BR,
> Ulf W
>




More information about the erlang-questions mailing list