[erlang-questions] update_counter on steroids

Evgeny M donpedrothird@REDACTED
Wed Aug 7 14:58:35 CEST 2013


I miss an ability to increment more than one counter for a given key. Would 
be very useful for statistical aggregate data.
Second thing I miss is a non-dirty update_counter. It doesn't look very 
useful, but would allow to use non-dirty update_counter in, for example, 
mnesia_frag context.

среда, 7 августа 2013 г., 15:39:11 UTC+4 пользователь Ulf Wiger написал:
>
>
> There was some discussion recently on teaching update_counter some new 
> tricks. 
>
> Here is a suggestion: 
>
> Right now, the function takes (Tab, Key, UpOp | [UpOp]), where 
>   UpOp ::== {Pos, Incr} | {Pos, Incr, Threshold, WrapTo} 
>
> I'd like to save the result of an operation in a temporary variable. 
>
> An atomic reset, for example. (let's say counter c1 has the value 17): 
>
> ets:update_counter(counters, c1, [{2, 0, '$1'}, {2, {'-', '$1'}}]) -> 
>   [17, 0] 
>
> (Increment by 0 means we read the existing value, just like today). 
>
> Why use this instead of simply ets:insert/2? Well, for one thing, we get 
> the old value back, so it's an atomic read-reset. 
>
> Sum counter: 
>
> ets:update_counter(counters, c2, [{2,0,'$1'}, {3,0,'$2'}, {4, {'+', '$1', 
> '$2'}}]) 
>
> Wrap with parameterized threshold and reset value: 
>
> ets:update_counter(counters, c3, [{3,0,'$1'}, {4,0,'$2'}, {2, Incr, '$1', 
> '$2'}]) 
>
> This assumes the following changes: 
>
> - A number of temp variables, like in match specs. 
> - A 3-tuple  {Pos, Incr, SaveTo}, where SaveTo is a variable name (e.g. 
> '$1') 
> - A 5-tuple  {Pos, Incr, Threshold, WrapTo, SaveTo} 
> - The possibility to use, wherever an integer() is expected 
>   VariableName | {UnaryOp, VariableName} | {BinaryOp, Var1, Var2} 
>
> We could also use this to e.g. store a value together with an increment 
> operation. A reset with timestamp: 
>
> TS = timestamp(),  % in milliseconds 
> ets:update_counter(counters, c4, [{2, 0, '$1'}, {2, {'-', '$1'}, {3, 0, 
> '$2'}, {3, {'-', TS, '$2'}}]) 
>
> …a bit bizarre perhaps, but still. 
>
> Comments? 
>
> BR, 
> Ulf W 
>
>
> Ulf Wiger, Co-founder & Developer Advocate, Feuerlabs Inc. 
> http://feuerlabs.com 
>
>
>
> _______________________________________________ 
> erlang-questions mailing list 
> erlang-q...@REDACTED <javascript:> 
> http://erlang.org/mailman/listinfo/erlang-questions 
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130807/6b045b08/attachment.htm>


More information about the erlang-questions mailing list