[erlang-questions] Residual indexes with mnesia

Salazard Rémy r.salazard@REDACTED
Thu Mar 15 11:48:40 CET 2007


Hi,

I have a problem with a mnesia table of type 'bag' with indexed fields.

When I'm using mnesia:dirty_delete_object/2, sometimes the indexes are
removed and sometimes not :(

Here is a testcase module:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-module(test).

-export([start/0]).
-export([set/0, unset/0]).

-record(mytbl, {one, two}).

start() ->
    mnesia:create_schema([node()]),
    mnesia:start(),
    mnesia:create_table(mytbl,
                        [{ram_copies, [node()]},
                         {attributes, record_info(fields, mytbl)},
                         {type, bag}]),
    mnesia:add_table_index(mytbl, two).

set() ->
    add({1,1}), add({2,1}), add({2,2}), add({3,1}),
    add({4,1}), add({2,4}), add({3,2}), add({2,0}),
    ok.

unset() ->
    del({1,1}), del({2,1}), del({2,2}), del({3,1}),
    del({4,1}), del({2,4}), del({3,2}), del({2,0}),
    ok.

add({One, Two}) ->
    mnesia:dirty_write(#mytbl{one = One,
                              two = Two}).
del({One, Two}) ->
    mnesia:dirty_delete_object(mytbl, {mytbl, One, Two}).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

As you can see, there are only two fields and the second is indexed.

1> test:start().
%% two tables are created, 'mytbl' and the mnesia protected table
'mnesia_index' (its ets id is 43).

2> test:set().
%% 'mytbl' contains 8 records:
3> ets:i(mytbl).
<1   > {mytbl,4,1}
<2   > {mytbl,3,1}
<3   > {mytbl,3,2}
<4   > {mytbl,2,1}
<5   > {mytbl,2,2}
<6   > {mytbl,2,4}
<7   > {mytbl,2,0}
<8   > {mytbl,1,1}
EOT  (q)uit (p)Digits (k)ill /Regexp -->
%% and 'mnesia_index' contains 8 records too:
4> ets:i(43).
<1   > {0,2}
<2   > {4,2}
<3   > {2,2}
<4   > {2,3}
<5   > {1,1}
<6   > {1,2}
<7   > {1,3}
<8   > {1,4}
EOT  (q)uit (p)Digits (k)ill /Regexp -->

5> test:unset().
6> ets:i(mytbl).
EOT  (q)uit (p)Digits (k)ill /Regexp -->
%% now 'mytbl' is empty, but...
7> ets:i(43).
<1   > {4,2}
<2   > {2,2}
<3   > {1,2}
<4   > {1,3}
EOT  (q)uit (p)Digits (k)ill /Regexp -->

(notice that the residual indexes correspond to mytbl's records which
had no unique first indexe.)


Do I make a mistake or is it a bug?

Thanks

Rémy



More information about the erlang-questions mailing list