Well, if the goal is to view the contents of your table, there is an application called tv that can view ETS and mnesia tables.
If you want to see the entire contents of the table on your terminal, try something like this:
traverse_table_and_show (Table_name) ->
Iterator = fun (Rec, _) ->
io: format ("~ p ~ n", [Rec]),
[]
end
case mnesia: is_transaction () of
true -> mnesia: foldl (Iterator, [], Table_name);
false ->
Exec = fun ({Fun, Tab}) -> mnesia: foldl (Fun, [], Tab) end,
mnesia: activity (transaction, Exec, [{Iterator, Table_name}], mnesia_frag)
end.
Then, if your table is called muppet
, you use this function as follows:
traverse_table_and_show (muppet).
The advantages of this:
If it is executed as part of a transaction, it will not have problems with nested transactions. This works less because it is executed as part of a single mnesia transaction via the mnesia iterator functionality compared to your implementation of get_next_key -> do_read_with_key ->, then reads the record (this is a lot of operations). At the same time, mnesia will automatically report that it has covered all the records in the entire table. Also, if the table is fragmented, your functionality will only display entries in the first fragment. This will repeat all the fragments belonging to this table.
In this mnesia iteration method, I do nothing with the Accumulator variable, which should be combined with Iterator
fun, and so you see the underscore for the second variable.
Details of this iteration can be found here: http://www.erlang.org/doc/man/mnesia.html#foldl-3