Bug in mnesia:dirty_update_counter

Anders Nygren <>
Wed Mar 9 16:21:40 CET 2005


On Wed, 9 Mar 2005 06:36:35 -0800 (PST), Thomas Lindgren
<> wrote:
> 
> --- Anders Nygren <> wrote:
> > There seems to be a bug in
> > mnesia:dirty_update_counter.
> > The first time dirty_update_counter(Tab,Key,Incr) is
> > called with a specific Key
> > the counter is set to 0.
> 
> I'd say that is a feature, actually ... If only
> ets:update_counter had the same behaviour (or _nearly_
> the same: behaving _as_if_ the counter had value 0
> before this operation).
> 
> I could then replace
> 
>   case catch ets:update_counter(Tab, Key, Incr) of
>      {'EXIT',_} -> ets:insert(Tab, {Key, Incr}), Incr;
>      N -> N
>   end
> 
> with (the silently initializing version):
> 
>   ets:update_counter(Tab, Key, Incr)
> 
> Pretty please, someone? :-)
> 

While I agee that the ets behaviour is annoying, I really dont like the
current mnesia behaviour.
I currently have to count things where the set of keys is not known
in advance so I have to do

case mnesia:dirty_update_counter(Tab,Key,Incr) of
   0 ->
      mnesia:dirty_update_counter(Tab,Key,Incr);
   N ->
      N
end

Currently Incr is always >0 so I dont have to worry about the case of
the counter decreasing to =< 0.

Which I guess would turn the above into something like

case Incr>0 of
   true ->
      case mnesia:dirty_update_counter(Tab,Key,Incr) of
         0 ->
            mnesia:dirty_update_counter(Tab,Key,Incr);
         N ->
            N
      end;
   false ->
         mnesia:dirty_update_counter(Tab,Key,Incr)
end

/Anders



More information about the erlang-questions mailing list