[erlang-questions] Strategies for updating Mnesia tables

Ulf Wiger (TN/EAB) ulf.wiger@REDACTED
Fri May 9 17:42:06 CEST 2008


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.

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