打印出Mnesia表的最佳方式 [英] Best way to print out Mnesia table

查看:166
本文介绍了打印出Mnesia表的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过这段代码片段:

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.

但是这么久了另外我也可以使用 dirty_all_keys ,然后重复列表,但是我想知道是否有更好的打印Mnesia表内容的方法。

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.

推荐答案

嗯,如果意图看到你的表的内容,那么应用程序叫做 电视 ,可以查看ETS和mnesia表。

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.

然后,如果您的表被称为 muppet ,则使用以下功能:

Then if your table is called muppet, you use the function as follows:


traverse_table_and_show(muppet).

此优点:

如果在事务中执行,它不会有嵌套交易的问题。它的工作较少,因为它通过mnesia迭代器的功能在一个mnesia事务中完成,与执行get_next_key - > do_read_with_key - >相比,读取记录(这些操作很多)。有了这个,mnesia会自动的告诉你,它覆盖了整个表中的所有记录。另外,如果表分段,您的功能将仅显示第一个片段中的记录。这将遍历属于该表的所有片段。

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.

在这个迭代mnesia方法中,我不用与Accumulator变量一起执行,该变量应该与 Iterator 有趣,这就是为什么你看到第二个变量的下划线。

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.

这个迭代的细节可以在这里找到: http://www.erlang.org/doc/man/mnesia.html#foldl-3

Details of this iteration can be found here: http://www.erlang.org/doc/man/mnesia.html#foldl-3

这篇关于打印出Mnesia表的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆