From 6c3265b86582d4848e1c77efee3a649a137694d6 Mon Sep 17 00:00:00 2001 From: Sverker Eriksson Date: Fri, 5 May 2017 14:51:58 +0200 Subject: stdlib: Add examples for ets:select_replace docs --- lib/stdlib/doc/src/ets.xml | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) (limited to 'lib/stdlib/doc/src/ets.xml') diff --git a/lib/stdlib/doc/src/ets.xml b/lib/stdlib/doc/src/ets.xml index d1ec176f81..62e97218cd 100644 --- a/lib/stdlib/doc/src/ets.xml +++ b/lib/stdlib/doc/src/ets.xml @@ -1495,17 +1495,37 @@ is_integer(X), is_integer(Y), X + Y < 4711]]> Match and replace objects atomically in an ETS table

Matches the objects in the table Tab using a - match specification. If - an object is matched, the existing object is replaced with - the match specification result, which must retain - the original key or the operation will fail with badarg.

+ match specification. For each + matched object, the existing object is replaced with + the match specification result.

+

The match-and-replace operation for each individual object is guaranteed to be + atomic and isolated. The + select_replace table iteration as a whole, like all other select functions, + does not give such guarantees.

+

The match specifiction must be guaranteed to retain the key + of any matched object. If not, select_replace will fail with badarg + without updating any objects.

For the moment, due to performance and semantic constraints, tables of type bag are not yet supported.

The function returns the total number of replaced objects.

- -

The match/replacement operation atomicity scope is limited - to each individual object.

-
+

Example

+

For all 2-tuples with a list in second position, add atom 'marker' first in the list:

+
+1> T = ets:new(x,[]), ets:insert(T, {key, [1, 2, 3]}).
+true
+2> MS = ets:fun2ms(fun({K, L}) when is_list(L) -> {K, [marker | L]} end).
+[{{'$1','$2'},[{is_list,'$2'}],[{{'$1',[marker|'$2']}}]}]
+3> ets:select_replace(T, MS).
+1
+4> ets:tab2list(T).
+[{key,[marker,1,2,3]}]
+	
+

A generic single object compare-and-swap operation:

+
+[Old] = ets:lookup(T, Key),
+New = update_object(Old),
+Success = (1 =:= ets:select_replace(T, [{Old, [], [{const, New}]}])),
+	
-- cgit v1.2.3