[erlang-questions] mnesia:dirty_update_counter and replicated tables

Vance Shipley vances@REDACTED
Thu Sep 21 21:00:38 CEST 2006


On Thu, Sep 21, 2006 at 02:10:22AM -0500, Scott Lystig Fritchie wrote:
}  There hasn't been any discussion of mnesia:dirty_update_counter here
}  in quite a while.

Well since you've brought it up ...

In ETS update_counter/3 is implemented as:

   update_counter(Tab, Key, {Pos,Incr,Threshold,SetValue}) -> Result
   update_counter(Tab, Key, {Pos,Incr}) -> Result
   update_counter(Tab, Key, Incr) -> Result

You can atomically update a wrap around counter.  E.g.:

	ets:update_counter(tid, {2,1,16#ffffffff,0})

This maintains a 16 bit counter.

In mnesia the wrap around functionality is missing:

   dirty_update_counter(Tab, Key, Incr) -> NewVal | exit({aborted, Reason}) 

So whereas I'd like to do:

   mnesia:dirty_update_counter(counters,transactionID,{2,1,16#ffffffff,0})

Instead I have to do:

   Fun = fun() ->
            [C] = mnesia:read(counters, transactionID, write),
            LocalTID = (C#counters.val + 1) rem 16#ffffffff,
            mnesia:write(counters, C#counters{val = LocalTID}, write),
            LocalTID
   end,
   {atomic, LocalTID} = mnesia:transaction(Fun),

... which may be safer but for a simple counter unecessary.

	-Vance



More information about the erlang-questions mailing list