[erlang-questions] mnesia question

Linan Wang <>
Sat Nov 22 01:28:03 CET 2008


hi,
I'm coding a simple queue application on top of mnesia. I found one strange
thing that the last inserted row would lost if not referenced after restart.
Is this a feature, bug, or my mistake? It has been troubling me for two
days. I'm running R12 B5 on an iMac. Any suggestion is appreciated.

=================================
My codes:
=================================

-module(taskq).
-export([start/0, db_put/1, db_get/0]).

-record(oid, {name, id}).
-record(task, {id, info}).

start()->
    %% start db or create;
    mnesia:start(),
    try
        mnesia:table_info(task, type)
    catch
        exit: _ ->
            utils:log("No db exists, create one."),
            mnesia:stop(),
            mnesia:create_schema([node()]),

            mnesia:start(),
            mnesia:create_table(oid,  [{disc_copies, [node()]}, {type, set},
{attributes, record_info(fields, oid)}]),
            mnesia:create_table(task, [{disc_copies, [node()]}, {type,
ordered_set}, {attributes, record_info(fields, task)}]),

            mnesia:transaction(fun()->
                    mnesia:s_write(#oid{name = task, id = 0})
                end),

            utils:log("Taskdb created.")
    end.

db_put(Info)->
    mnesia:transaction(fun()->
        Id = mnesia:dirty_update_counter({oid, task}, 1),
        mnesia:s_write(#task{id = Id, info =  Info})
        end).

db_get()->
    {atomic, R} = mnesia:transaction(fun()->
        Id = mnesia:first(task),
        case Id of
            '$end_of_table' ->
                none;
            _ ->
                [Ret] = mnesia:wread({task, Id}),
                mnesia:s_delete({task, Id}),
                Ret#task.info
        end
        end),
    R.

==================================
Execution session:
==================================

Linan-iMac:~$ erl
Erlang (BEAM) emulator version 5.6.5 [source] [async-threads:0]
[kernel-poll:false]

Eshell V5.6.5  (abort with ^G)
1> c(taskq).
{ok,taskq}
2> c(utils).
{ok,utils}
3> taskq:start().
2008/11/22 0:23:17 No db exists, create one.

=INFO REPORT==== 22-Nov-2008::00:23:17 ===
    application: mnesia
    exited: stopped
    type: temporary
2008/11/22 0:23:18 Taskdb created.
ok
4> taskq:put(abcde).
{atomic,ok}
5>
BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
       (v)ersion (k)ill (D)b-tables (d)istribution
q

Linan-iMac:~$ erl
Erlang (BEAM) emulator version 5.6.5 [source] [async-threads:0]
[kernel-poll:false]

Eshell V5.6.5  (abort with ^G)
1> taskq:start().
ordered_set
2> taskq:get().
none
3>

Linan Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20081122/09d0149d/attachment.html>


More information about the erlang-questions mailing list