[erlang-questions] (no subject)
Sun Nov 8 14:17:32 CET 2015
From: ?? <zhuo.quan@REDACTED>
To: Erlang???? <erlang-questions@REDACTED>
Subject: [erlang-questions] How to delete records in a mnesia table
when traverse it
Content-Type: text/plain; charset="gb2312"
> 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?
Depending on your criteria, maybe you could use dirty-match or dirty-select to collect the records that need to be deleted in one pass, and then delete those objects by iterating though that collection.
Alternately, you could have a process that (at regular intervals) walks the table marking records for deletion (by setting a field reserved for this purpose) that meet your criteria and then deletes them as described above.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the erlang-questions