您的位置:首页 > 其它

erlang mnesia 遍历

2016-05-27 10:31 435 查看

Best way to print out Mnesia table

up vote
5
down vote
favorite
2

I tried this code snippet:

print_next(Current) ->
case mnesia:dirty_next(muppet, Current) of
'$end_of_table' ->
io:format("~n", []),
ok;
Next ->
[Muppet] = mnesia:dirty_read({muppet, Next}),
io:format("~p~n", [Muppet]),
print_next(Next),
ok
end.

print() ->
case mnesia:dirty_first(muppet) of
'$end_of_table' ->
ok;
First ->
[Muppet] = mnesia:dirty_read({muppet, First}),
io:format("~p~n", [Muppet]),
print_next(First),
ok
end.

But it is so long. Also I can use
dirty_all_keys
and then iterate through key list, but I want to know if there is a better way to print out Mnesia table contents.

erlang

mnesia
shareimprove
this question
edited
Jan 11 at 11:31





Hamidreza Soleimani
1,286413

asked
Oct 14 '11 at 6:14




Yola
4,65842848

 
1 
If you are just looking for a way to browse a table, you can also use the

table visualizer. – nox
Oct 15 '11
at 10:27
add a comment

4 Answers

active

oldest
votes

up vote
7
down vote
accepted
Well, if the intent is to see the contents of your table, there is the application called
tv, which can view both ETS and mnesia tables.

If you wish to see all the table contents on your terminal, then 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 the function as follows:

traverse_table_and_show(muppet).

Advantages of this:

If its executed within a transaction , it will have no problems of nested transactions. It is less work because its done within one mnesia transaction through mnesia iterator functionality as compared to your implementation of get_next_key -> do_read_with_key
-> then read the record (these are many operations). With this, mnesia will automatically tell that it has covered all the records in your entire table. Also, if the table is fragmented, your functionality will only display records in the first fragment. This
will iterate through all the fragments the belong to that table.

In this iteration mnesia method, i do nothing with the Accumulator variable which should go along with the
Iterator
fun and thats why 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


shareimprove
this answer
edited
Aug 28 '15 at 23:44





Jesse Weigert
3,03521630

answered
Oct 14 '11 at 8:53




Muzaaya Joshua
5,26532863

 
1 
thanks, voted up – Yola
Oct 14 '11
at 10:48
add a comment

up vote
5
down vote
If you just want a quick and dirty way to print the contents of a Mnesia table in the shell, and if your table is not of type
disc_only_copies
, then you can take advantage of the fact that Mnesia stores its data in ETS tables and run:

ets:tab2list(my_table).

or, if you think the shell truncates the data too much:

io:format("~p~n", [ets:tab2list(my_table)]).

Not recommended for "real" code, of course.

shareimprove
this answer
answered
Oct 14 '11 at 13:04




legoscia
22.5k84272

 
1 
if data is too much, that list may hog the shell process. beware the data size in the ETS table before doing this – Muzaaya
Joshua Oct
14 '11 at 14:04
add a comment

up vote
4
down vote
For a simple and quick look at your table contents you can use select function of
mnesia with catch-all
Match Specification as follows:

CatchAll = [{'_',[],['$_']}].
mnesia:dirty_select(TableName, CatchAll).

and also you can run it inside a transaction context:

CatchAll = [{'_',[],['$_']}].
SelectFun = fun() -> mnesia:select(TableName, CatchAll) end.
mnesia:transaction(SelectFun).

however be careful if you are in a production environment with a big data.

shareimprove
this answer
answered
Oct 5 '14 at 15:05




Hamidreza Soleimani
1,286413

 add a comment

up vote
1
down vote
As Muzaaya told, you can you use tv (table visualizer tool) to view both mnesia and ets tables.Alternatively, you can use the following code to get mnesia table data - Print on terminal or in case you want to store the result in a file :

select_all() ->
mnesia:transaction(
fun() ->
P=qlc:e(qlc:q([E || E <- mnesia:table(tableName)])),      %query to select all data from table named 'tableName'
io:format(" ~p ~n ", [P]), % Prints table data on terminal
to_file("fileName.txt",P) % to_file method writes the data to file
end ).

to_file(File, L) ->
mnesia:transaction(
fun() ->
{ok, S} = file:open(File, write),
lists:foreach(fun(X) -> io:format(S, "~p.~n" ,[X]) end, L),
file:close(S)
end).


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  erlang mnesia