[erlang-questions] mnesia:ets/2 vs direct ets calls
Oleksii Semilietov
spylik@REDACTED
Wed Nov 2 12:10:38 CET 2016
Question about mnesia:ets/3 and direct ets calls for mnesia tables.
In case of lookup (sequence calls from same node as ram replica node), what
is the benefit to use mnesia:ets instead of direct ets:lookup call?
I have single writer to DB (I have one node ram type, other replicas - disk
copies just for save data to disk somewhere) and would like do something
like this on ram node (sequence pseudo-code):
......
X = receive
NewData#somerecord{id = TransID} ->
case ets:lookup(Table, TransID) of
[#somerecord{date = undefined}] = OldData ->
NewDate = erlang:system_time(milli_seconds),
mnesia:dirty_write(OldData#somerecord{date = NewDate}),
{TransID, NewDate};
[#somerecord{date = Date}] ->
{TransID, Date}
[] ->
NewDate = erlang:system_time(milli_seconds),
mnesia:dirty_write(NewData),
{TransID, Date}
end
OtherReceive -> ...blabala....
end,
some_operation_with_x(X).
Or I should wrap this stuff to something like this?:
.....
receive
NewData#somerecord{id = TransID} ->
mnesia:ets(fun may_update/3, [NewData, Table, TransID]);
OtherReceive -> ...blabala....
end,
some_operation_with_x(X).
may_update(NewData, Table, TransID) ->
case mnesia:read(Table, TransID) of
[#somerecord{date = undefined}] = OldData ->
NewDate = erlang:system_time(milli_seconds),
mnesia:write(OldData#somerecord{date = NewDate}),
{TransID, NewDate};
[#somerecord{date = Date}] ->
{TransID, Date}
[] ->
NewDate = erlang:system_time(milli_seconds),
mnesia:write(NewData),
{TransID, Date}
end
).
What is the benefit to wrap it to mnesia:ets?
Best regards.
--
Oleksii D. Semilietov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20161102/1e038f05/attachment.htm>
More information about the erlang-questions
mailing list