[erlang-questions] How to delete records in a mnesia table when traverse it

Erik Søe Sørensen eriksoe@REDACTED
Sat Nov 7 15:24:44 CET 2015


How about calculating dirty_next() *before* deleting the item?
Den 07/11/2015 12.44 skrev "全卓" <zhuo.quan@REDACTED>:

> Hi, list:
>
>   I have a mnesia table about 15K records in my application. As the time
> goes, I find that some records(10K) in the table is useless. So I want to
> delete these useless items with a piece of code like this:
>
> Here is the code:
> %%%%%%%%%%%%%%%%%%%%%
> 1.   delete() ->
> 2.        traversal_tab(tab).
>
> 3.   traversal_tab(TabName) ->
> 4.       traversal_tab(TabName, mnesia:dirty_first(TabName)) .
>
> 5.  traversal_tab(TabName, '$end_of_table') ->
> 6.       io:format("finished~n");
> 7.  traversal_tab(TabName, Key) ->
> 8.       delete_entry(TabName, Key),
> 9.       traversal_tab(TabName, mnesia:dirty_next(TabName, Key)).
>
> 10.  delete_entry(TabName, Key) ->
> 11.      if ...
>              %% delete the record if it fulfils the condition.
> 12.        mnesia:dirty_delete(TabName, Key)
>               ...
> 13.       end.
> %%%%%%%%%%%%%%%%%%%%%%%%
>
> The problem is if I delete the certain record in line 12,  the function
> mnesia:dirty_next(TabName, Key) in line 9 will fail.
> How to delete records when i traverse a mnesia table? Anyone Help?
>
> Thank you !
>
> Regards
> Quan Zhuo
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20151107/085fc260/attachment.htm>


More information about the erlang-questions mailing list