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

全卓 zhuo.quan@REDACTED
Sat Nov 7 12:44:15 CET 2015


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
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20151107/dedd7aa6/attachment.htm>


More information about the erlang-questions mailing list