[erlang-questions] mnesia question

Linan Wang tali.wang@REDACTED
Sat Nov 22 18:16:23 CET 2008


Vance,Thank you very much. but it really not.
=====================
= code
=====================
-module(taskq).
-export([start/0, db_put/1, db_get/0]).


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

start()->

    mnesia:start(),
    try
        mnesia:table_info(task, type)
    catch
        exit: _ ->
            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:write(#oid{name = task, id = 0})
                end)
    end.

db_put(Info)->
    mnesia:transaction(fun()->
        Id = mnesia:dirty_update_counter({oid, task}, 1),
        mnesia: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:delete({task, Id}),
                Ret#task.info
        end
        end),
    R.

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

Eshell V5.6.5  (abort with ^G)
1> c(taskdb).
error
2> c(taskq).
{ok,taskq}
3> taskq:start().

=INFO REPORT==== 22-Nov-2008::17:07:20 ===
    application: mnesia
    exited: stopped
    type: temporary
{atomic,ok}
4> taskq:put(abcdefa).
** exception error: undefined function taskq:put/1
5> taskq:db_put(abcdefa).
{atomic,ok}
6>
BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
       (v)ersion (k)ill (D)b-tables (d)istribution
q
Linan-iMac:~ wangii$ 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:db_get().
none
3>

On Sat, Nov 22, 2008 at 4:40 PM, Vance Shipley <vances@REDACTED> wrote:

> Linan,
>
> I tried your sample code and confirmed the behaviour you reported.
> Then I changed mnesia:s_write/1 to mnesia:write/1 and it worked as
> expected.
>
>        -Vance
>
> On Sat, Nov 22, 2008 at 04:32:52PM +0000, Linan Wang wrote:
> }  thanks for the suggestion.I changed all s_write, wread, s_delete to
> }  straightforward write, read, delete. it does not help.
>



-- 
Best regards

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


More information about the erlang-questions mailing list