<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On 11 May 2014 09:34, Anthony Ramine <span dir="ltr"><<a href="mailto:n.oxyde@gmail.com" target="_blank">n.oxyde@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Indeed.<br>
<br>
And now we can have 4-ary BIFs. :p<br>
<br>
I plan to implement ets:take_all_objects(Table) which would empty out a table and return all its rows. Would you have a use for such a thing too?<br></blockquote><div><br>I do. On one system I've a table holding many different counters, that are periodically flushed to an external system. Without the atomic take_all_objects/1, I'm retrieving all and then iterating and subtracting to all keys the values that I've just read..<br>
</div><div>btw, ets:update_counter/4 with default value will simplify it a lot too, thanks!<br></div><div><br><br><br><br><br><br><br><br><br> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
Regards,<br>
<span class=""><font color="#888888"><br>
--<br>
Anthony Ramine<br>
<br>
Le 11 mai 2014 à 13:52, Ulf Wiger <<a href="mailto:ulf@feuerlabs.com">ulf@feuerlabs.com</a>> a écrit :<br>
</font></span><div class=""><div class="h5"><br>
><br>
> Hi Anthony,<br>
><br>
> That’s certainly a function that many erlangers have had to implement over and over. :)<br>
><br>
> And of course, having to write it in Erlang by combining several operation into a non-atomic function introduces some problems of its own.<br>
><br>
> BR,<br>
> Ulf W<br>
><br>
> On 11 May 2014, at 13:12, Anthony Ramine <<a href="mailto:n.oxyde@gmail.com">n.oxyde@gmail.com</a>> wrote:<br>
><br>
>> Hello Ulf,<br>
>><br>
>> Not what you asked for, but I implemented ets:update_counter/4 which takes a default value yesterday:<br>
>><br>
>> ets:new(counters, [set,named_table]),<br>
>> 1 = ets:update_counter(counters, c1, 1, {c1,1}),<br>
>> 3 = ets:update_counter(counters, c1, 2, {c1,1}),<br>
>> [{c1,3}] = ets:tab2list(counters).<br>
>><br>
>> Regards,<br>
>><br>
>> <a href="https://github.com/erlang/otp/pull/362" target="_blank">https://github.com/erlang/otp/pull/362</a><br>
>><br>
>> --<br>
>> Anthony Ramine<br>
>><br>
>> Le 7 août 2013 à 13:39, Ulf Wiger <<a href="mailto:ulf@feuerlabs.com">ulf@feuerlabs.com</a>> a écrit :<br>
>><br>
>>><br>
>>> There was some discussion recently on teaching update_counter some new tricks.<br>
>>><br>
>>> Here is a suggestion:<br>
>>><br>
>>> Right now, the function takes (Tab, Key, UpOp | [UpOp]), where<br>
>>> UpOp ::== {Pos, Incr} | {Pos, Incr, Threshold, WrapTo}<br>
>>><br>
>>> I'd like to save the result of an operation in a temporary variable.<br>
>>><br>
>>> An atomic reset, for example. (let's say counter c1 has the value 17):<br>
>>><br>
>>> ets:update_counter(counters, c1, [{2, 0, '$1'}, {2, {'-', '$1'}}]) -><br>
>>> [17, 0]<br>
>>><br>
>>> (Increment by 0 means we read the existing value, just like today).<br>
>>><br>
>>> 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.<br>
>>><br>
>>> Sum counter:<br>
>>><br>
>>> ets:update_counter(counters, c2, [{2,0,'$1'}, {3,0,'$2'}, {4, {'+', '$1', '$2'}}])<br>
>>><br>
>>> Wrap with parameterized threshold and reset value:<br>
>>><br>
>>> ets:update_counter(counters, c3, [{3,0,'$1'}, {4,0,'$2'}, {2, Incr, '$1', '$2'}])<br>
>>><br>
>>> This assumes the following changes:<br>
>>><br>
>>> - A number of temp variables, like in match specs.<br>
>>> - A 3-tuple {Pos, Incr, SaveTo}, where SaveTo is a variable name (e.g. '$1')<br>
>>> - A 5-tuple {Pos, Incr, Threshold, WrapTo, SaveTo}<br>
>>> - The possibility to use, wherever an integer() is expected<br>
>>> VariableName | {UnaryOp, VariableName} | {BinaryOp, Var1, Var2}<br>
>>><br>
>>> We could also use this to e.g. store a value together with an increment operation. A reset with timestamp:<br>
>>><br>
>>> TS = timestamp(), % in milliseconds<br>
>>> ets:update_counter(counters, c4, [{2, 0, '$1'}, {2, {'-', '$1'}, {3, 0, '$2'}, {3, {'-', TS, '$2'}}])<br>
>>><br>
>>> …a bit bizarre perhaps, but still.<br>
>>><br>
>>> Comments?<br>
>>><br>
>>> BR,<br>
>>> Ulf W<br>
>>><br>
>>><br>
>>> Ulf Wiger, Co-founder & Developer Advocate, Feuerlabs Inc.<br>
>>> <a href="http://feuerlabs.com" target="_blank">http://feuerlabs.com</a><br>
>>><br>
>>><br>
>>><br>
>>> _______________________________________________<br>
>>> erlang-questions mailing list<br>
>>> <a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
>>> <a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
>><br>
><br>
> Ulf Wiger, Co-founder & Developer Advocate, Feuerlabs Inc.<br>
> <a href="http://feuerlabs.com" target="_blank">http://feuerlabs.com</a><br>
><br>
><br>
><br>
<br>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>Pablo Polvorin<br>ProcessOne
</div></div>