Skip to content

Commit bb10ecb

Browse files
Rename numericAdd to increment
1 parent 261c7c8 commit bb10ecb

File tree

9 files changed

+85
-80
lines changed

9 files changed

+85
-80
lines changed

firebase-firestore/src/androidTest/java/com/google/firebase/firestore/NumericTransformsTest.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,55 +81,55 @@ private void expectLocalAndRemoteValue(long expectedSum) {
8181

8282
@Test
8383
public void createDocumentWithIncrement() {
84-
waitFor(docRef.set(map("sum", FieldValue.numericAdd(1337))));
84+
waitFor(docRef.set(map("sum", FieldValue.increment(1337))));
8585
expectLocalAndRemoteValue(1337L);
8686
}
8787

8888
@Test
8989
public void mergeOnNonExistingDocumentWithIncrement() {
90-
waitFor(docRef.set(map("sum", FieldValue.numericAdd(1337)), SetOptions.merge()));
90+
waitFor(docRef.set(map("sum", FieldValue.increment(1337)), SetOptions.merge()));
9191
expectLocalAndRemoteValue(1337L);
9292
}
9393

9494
@Test
9595
public void integerIncrementWithExistingInteger() {
9696
writeInitialData(map("sum", 1337L));
97-
waitFor(docRef.update("sum", FieldValue.numericAdd(1)));
97+
waitFor(docRef.update("sum", FieldValue.increment(1)));
9898
expectLocalAndRemoteValue(1338L);
9999
}
100100

101101
@Test
102102
public void doubleIncrementWithExistingDouble() {
103103
writeInitialData(map("sum", 13.37D));
104-
waitFor(docRef.update("sum", FieldValue.numericAdd(0.1)));
104+
waitFor(docRef.update("sum", FieldValue.increment(0.1)));
105105
expectLocalAndRemoteValue(13.47D);
106106
}
107107

108108
@Test
109109
public void integerIncrementWithExistingDouble() {
110110
writeInitialData(map("sum", 13.37D));
111-
waitFor(docRef.update("sum", FieldValue.numericAdd(1)));
111+
waitFor(docRef.update("sum", FieldValue.increment(1)));
112112
expectLocalAndRemoteValue(14.37D);
113113
}
114114

115115
@Test
116116
public void doubleIncrementWithExistingInteger() {
117117
writeInitialData(map("sum", 1337L));
118-
waitFor(docRef.update("sum", FieldValue.numericAdd(0.1)));
118+
waitFor(docRef.update("sum", FieldValue.increment(0.1)));
119119
expectLocalAndRemoteValue(1337.1D);
120120
}
121121

122122
@Test
123123
public void integerIncrementWithExistingString() {
124124
writeInitialData(map("sum", "overwrite"));
125-
waitFor(docRef.update("sum", FieldValue.numericAdd(1337)));
125+
waitFor(docRef.update("sum", FieldValue.increment(1337)));
126126
expectLocalAndRemoteValue(1337L);
127127
}
128128

129129
@Test
130130
public void doubleIncrementWithExistingString() {
131131
writeInitialData(map("sum", "overwrite"));
132-
waitFor(docRef.update("sum", FieldValue.numericAdd(13.37)));
132+
waitFor(docRef.update("sum", FieldValue.increment(13.37)));
133133
expectLocalAndRemoteValue(13.37D);
134134
}
135135

@@ -139,9 +139,9 @@ public void multipleDoubleIncrements() throws ExecutionException, InterruptedExc
139139

140140
Tasks.await(docRef.getFirestore().disableNetwork());
141141

142-
docRef.update("sum", FieldValue.numericAdd(0.1D));
143-
docRef.update("sum", FieldValue.numericAdd(0.01D));
144-
docRef.update("sum", FieldValue.numericAdd(0.001D));
142+
docRef.update("sum", FieldValue.increment(0.1D));
143+
docRef.update("sum", FieldValue.increment(0.01D));
144+
docRef.update("sum", FieldValue.increment(0.001D));
145145

146146
DocumentSnapshot snap = accumulator.awaitLocalEvent();
147147
assertEquals(0.1D, snap.getDouble("sum"), DOUBLE_EPSILON);

firebase-firestore/src/main/java/com/google/firebase/firestore/FieldValue.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,17 @@ List<Object> getElements() {
8383
}
8484
}
8585

86-
/* FieldValue class for numericAdd() transforms. */
87-
static class NumericAddFieldValue extends FieldValue {
86+
/* FieldValue class for increment() transforms. */
87+
static class NumericIncrementFieldValue extends FieldValue {
8888
private final Number operand;
8989

90-
NumericAddFieldValue(Number operand) {
90+
NumericIncrementFieldValue(Number operand) {
9191
this.operand = operand;
9292
}
9393

9494
@Override
9595
String getMethodName() {
96-
return "FieldValue.numericAdd";
96+
return "FieldValue.increment";
9797
}
9898

9999
Number getOperand() {
@@ -154,8 +154,8 @@ public static FieldValue arrayRemove(@NonNull Object... elements) {
154154
}
155155

156156
/**
157-
* Returns a special value that can be used with set() or update() that tells the server to add
158-
* the given value to the field's current value.
157+
* Returns a special value that can be used with set() or update() that tells the server to
158+
* increment the field's current value by the given value.
159159
*
160160
* <p>If the current field value is an integer, possible integer overflows are resolved to
161161
* Long.MAX_VALUE or Long.MIN_VALUE. If the current field value is a double, both values will be
@@ -168,13 +168,13 @@ public static FieldValue arrayRemove(@NonNull Object... elements) {
168168
*/
169169
@NonNull
170170
@PublicApi
171-
public static FieldValue numericAdd(long l) {
172-
return new NumericAddFieldValue(l);
171+
public static FieldValue increment(long l) {
172+
return new NumericIncrementFieldValue(l);
173173
}
174174

175175
/**
176-
* Returns a special value that can be used with set() or update() that tells the server to add
177-
* the given value to the field's current value.
176+
* Returns a special value that can be used with set() or update() that tells the server to
177+
* increment the field's current value by the given value.
178178
*
179179
* <p>If the current value is an integer or a double, both the current and the given value will be
180180
* interpreted as doubles and all arithmetic will follow IEEE 754 semantics. Otherwise, the
@@ -184,7 +184,7 @@ public static FieldValue numericAdd(long l) {
184184
*/
185185
@NonNull
186186
@PublicApi
187-
public static FieldValue numericAdd(double l) {
188-
return new NumericAddFieldValue(l);
187+
public static FieldValue increment(double l) {
188+
return new NumericIncrementFieldValue(l);
189189
}
190190
}

firebase-firestore/src/main/java/com/google/firebase/firestore/UserDataConverter.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import com.google.firebase.firestore.model.FieldPath;
3232
import com.google.firebase.firestore.model.mutation.ArrayTransformOperation;
3333
import com.google.firebase.firestore.model.mutation.FieldMask;
34-
import com.google.firebase.firestore.model.mutation.NumericAddTransformOperation;
34+
import com.google.firebase.firestore.model.mutation.NumericIncrementTransformOperation;
3535
import com.google.firebase.firestore.model.mutation.ServerTimestampOperation;
3636
import com.google.firebase.firestore.model.value.ArrayValue;
3737
import com.google.firebase.firestore.model.value.BlobValue;
@@ -351,11 +351,14 @@ private void parseSentinelFieldValue(
351351
ArrayTransformOperation arrayRemove = new ArrayTransformOperation.Remove(parsedElements);
352352
context.addToFieldTransforms(context.getPath(), arrayRemove);
353353

354-
} else if (value instanceof com.google.firebase.firestore.FieldValue.NumericAddFieldValue) {
355-
com.google.firebase.firestore.FieldValue.NumericAddFieldValue numericAddFieldValue =
356-
(com.google.firebase.firestore.FieldValue.NumericAddFieldValue) value;
357-
NumberValue operand = (NumberValue) parseQueryValue(numericAddFieldValue.getOperand());
358-
NumericAddTransformOperation numericAdd = new NumericAddTransformOperation(operand);
354+
} else if (value
355+
instanceof com.google.firebase.firestore.FieldValue.NumericIncrementFieldValue) {
356+
com.google.firebase.firestore.FieldValue.NumericIncrementFieldValue
357+
numericIncrementFieldValue =
358+
(com.google.firebase.firestore.FieldValue.NumericIncrementFieldValue) value;
359+
NumberValue operand = (NumberValue) parseQueryValue(numericIncrementFieldValue.getOperand());
360+
NumericIncrementTransformOperation numericAdd =
361+
new NumericIncrementTransformOperation(operand);
359362
context.addToFieldTransforms(context.getPath(), numericAdd);
360363

361364
} else {

firebase-firestore/src/main/java/com/google/firebase/firestore/local/LocalStore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public LocalWriteResult writeLocally(List<Mutation> mutations) {
203203
ImmutableSortedMap<DocumentKey, MaybeDocument> existingDocuments =
204204
localDocuments.getDocuments(keys);
205205

206-
// For non-idempotent mutations (such as `FieldValue.numericAdd()`), we record the base
206+
// For non-idempotent mutations (such as `FieldValue.increment()`), we record the base
207207
// state in a separate patch mutation. This is later used to guarantee consistent values
208208
// and prevents flicker even if the backend sends us an update that already includes our
209209
// transform.
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,22 @@
2323
import com.google.firebase.firestore.model.value.NumberValue;
2424

2525
/**
26-
* Implements the backend semantics for locally computed NUMERIC_ADD transforms. Converts all field
27-
* values to longs or doubles and resolves overflows to Long.MAX_VALUE/Long.MIN_VALUE.
26+
* Implements the backend semantics for locally computed NUMERIC_ADD (increment) transforms.
27+
* Converts all field values to longs or doubles and resolves overflows to
28+
* Long.MAX_VALUE/Long.MIN_VALUE.
2829
*/
29-
public class NumericAddTransformOperation implements TransformOperation {
30+
public class NumericIncrementTransformOperation implements TransformOperation {
3031
private NumberValue operand;
3132

32-
public NumericAddTransformOperation(NumberValue operand) {
33+
public NumericIncrementTransformOperation(NumberValue operand) {
3334
this.operand = operand;
3435
}
3536

3637
@Override
3738
public FieldValue applyToLocalView(FieldValue previousValue, Timestamp localWriteTime) {
3839
// Return an integer value only if the previous value and the operand is an integer.
3940
if (previousValue instanceof IntegerValue && operand instanceof IntegerValue) {
40-
long sum = safeAdd(((IntegerValue) previousValue).getInternalValue(), operandAsLong());
41+
long sum = safeIncrement(((IntegerValue) previousValue).getInternalValue(), operandAsLong());
4142
return IntegerValue.valueOf(sum);
4243
} else if (previousValue instanceof IntegerValue) {
4344
double sum = ((IntegerValue) previousValue).getInternalValue() + operandAsDouble();
@@ -55,7 +56,7 @@ public FieldValue applyToLocalView(FieldValue previousValue, Timestamp localWrit
5556
* Implementation of Java 8's `addExact()` that resolves positive and negative numeric overflows
5657
* to Long.MAX_VALUE or Long.MIN_VALUE respectively (instead of throwing an ArithmeticException).
5758
*/
58-
private long safeAdd(long x, long y) {
59+
private long safeIncrement(long x, long y) {
5960
long r = x + y;
6061

6162
// See "Hacker's Delight" 2-12: Overflow if both arguments have the opposite sign of the result

firebase-firestore/src/main/java/com/google/firebase/firestore/remote/RemoteSerializer.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
import com.google.firebase.firestore.model.mutation.FieldTransform;
4646
import com.google.firebase.firestore.model.mutation.Mutation;
4747
import com.google.firebase.firestore.model.mutation.MutationResult;
48-
import com.google.firebase.firestore.model.mutation.NumericAddTransformOperation;
48+
import com.google.firebase.firestore.model.mutation.NumericIncrementTransformOperation;
4949
import com.google.firebase.firestore.model.mutation.PatchMutation;
5050
import com.google.firebase.firestore.model.mutation.Precondition;
5151
import com.google.firebase.firestore.model.mutation.ServerTimestampOperation;
@@ -560,8 +560,9 @@ private DocumentTransform.FieldTransform encodeFieldTransform(FieldTransform fie
560560
.setFieldPath(fieldTransform.getFieldPath().canonicalString())
561561
.setRemoveAllFromArray(encodeArrayTransformElements(remove.getElements()))
562562
.build();
563-
} else if (transform instanceof NumericAddTransformOperation) {
564-
NumericAddTransformOperation numericAdd = (NumericAddTransformOperation) transform;
563+
} else if (transform instanceof NumericIncrementTransformOperation) {
564+
NumericIncrementTransformOperation numericAdd =
565+
(NumericIncrementTransformOperation) transform;
565566
return DocumentTransform.FieldTransform.newBuilder()
566567
.setFieldPath(fieldTransform.getFieldPath().canonicalString())
567568
.setNumericAdd(encodeValue(numericAdd.getOperand()))
@@ -611,7 +612,7 @@ private FieldTransform decodeFieldTransform(DocumentTransform.FieldTransform fie
611612
operand.getClass().getCanonicalName());
612613
return new FieldTransform(
613614
FieldPath.fromServerFormat(fieldTransform.getFieldPath()),
614-
new NumericAddTransformOperation(
615+
new NumericIncrementTransformOperation(
615616
(NumberValue) decodeValue(fieldTransform.getNumericAdd())));
616617
}
617618
default:

firebase-firestore/src/proto/google/firebase/firestore/proto/mutation.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ message WriteBatch {
6060
// write batch was initially created. When computing the local view batch,
6161
// these base_writes are applied prior to the real writes in order to
6262
// override certain document fields from the remote document cache. This is
63-
// necessary in the case of non-idempotent writes (e.g. numericAdd
63+
// necessary in the case of non-idempotent writes (e.g. increment
6464
// transforms) to make sure that the local view of the modified documents
6565
// doesn't flicker if the remote document cache receives the result of the
6666
// non-idempotent write before the write is removed from the queue.

firebase-firestore/src/test/java/com/google/firebase/firestore/local/LocalStoreTestCase.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ public void testRemoteDocumentKeysForTarget() {
950950
assertSetEquals(asList(key("foo/bar"), key("foo/baz")), keys);
951951
}
952952

953-
// TODO(mrschmidt): The FieldValue.numericAdd() field transform tests below would probably be
953+
// TODO(mrschmidt): The FieldValue.increment() field transform tests below would probably be
954954
// better implemented as spec tests but currently they don't support transforms.
955955

956956
@Test
@@ -959,11 +959,11 @@ public void testHandlesSetMutationThenTransformMutationThenTransformMutation() {
959959
assertContains(doc("foo/bar", 0, map("sum", 0), Document.DocumentState.LOCAL_MUTATIONS));
960960
assertChanged(doc("foo/bar", 0, map("sum", 0), Document.DocumentState.LOCAL_MUTATIONS));
961961

962-
writeMutation(transformMutation("foo/bar", map("sum", FieldValue.numericAdd(1))));
962+
writeMutation(transformMutation("foo/bar", map("sum", FieldValue.increment(1))));
963963
assertContains(doc("foo/bar", 0, map("sum", 1), Document.DocumentState.LOCAL_MUTATIONS));
964964
assertChanged(doc("foo/bar", 0, map("sum", 1), Document.DocumentState.LOCAL_MUTATIONS));
965965

966-
writeMutation(transformMutation("foo/bar", map("sum", FieldValue.numericAdd(2))));
966+
writeMutation(transformMutation("foo/bar", map("sum", FieldValue.increment(2))));
967967
assertContains(doc("foo/bar", 0, map("sum", 3), Document.DocumentState.LOCAL_MUTATIONS));
968968
assertChanged(doc("foo/bar", 0, map("sum", 3), Document.DocumentState.LOCAL_MUTATIONS));
969969
}
@@ -985,15 +985,15 @@ public void testHandlesSetMutationThenAckThenTransformMutationThenAckThenTransfo
985985
assertChanged(doc("foo/bar", 1, map("sum", 0), Document.DocumentState.COMMITTED_MUTATIONS));
986986
assertContains(doc("foo/bar", 1, map("sum", 0), Document.DocumentState.COMMITTED_MUTATIONS));
987987

988-
writeMutation(transformMutation("foo/bar", map("sum", FieldValue.numericAdd(1))));
988+
writeMutation(transformMutation("foo/bar", map("sum", FieldValue.increment(1))));
989989
assertContains(doc("foo/bar", 1, map("sum", 1), Document.DocumentState.LOCAL_MUTATIONS));
990990
assertChanged(doc("foo/bar", 1, map("sum", 1), Document.DocumentState.LOCAL_MUTATIONS));
991991

992992
acknowledgeMutation(2, 1);
993993
assertChanged(doc("foo/bar", 2, map("sum", 1), Document.DocumentState.COMMITTED_MUTATIONS));
994994
assertContains(doc("foo/bar", 2, map("sum", 1), Document.DocumentState.COMMITTED_MUTATIONS));
995995

996-
writeMutation(transformMutation("foo/bar", map("sum", FieldValue.numericAdd(2))));
996+
writeMutation(transformMutation("foo/bar", map("sum", FieldValue.increment(2))));
997997
assertContains(doc("foo/bar", 2, map("sum", 3), Document.DocumentState.LOCAL_MUTATIONS));
998998
assertChanged(doc("foo/bar", 2, map("sum", 3), Document.DocumentState.LOCAL_MUTATIONS));
999999
}
@@ -1013,7 +1013,7 @@ public void testHandlesSetMutationThenTransformMutationThenRemoteEventThenTransf
10131013
assertChanged(doc("foo/bar", 1, map("sum", 0), Document.DocumentState.SYNCED));
10141014
assertContains(doc("foo/bar", 1, map("sum", 0), Document.DocumentState.SYNCED));
10151015

1016-
writeMutation(transformMutation("foo/bar", map("sum", FieldValue.numericAdd(1))));
1016+
writeMutation(transformMutation("foo/bar", map("sum", FieldValue.increment(1))));
10171017
assertChanged(doc("foo/bar", 1, map("sum", 1), Document.DocumentState.LOCAL_MUTATIONS));
10181018
assertContains(doc("foo/bar", 1, map("sum", 1), Document.DocumentState.LOCAL_MUTATIONS));
10191019

@@ -1023,7 +1023,7 @@ public void testHandlesSetMutationThenTransformMutationThenRemoteEventThenTransf
10231023
assertContains(doc("foo/bar", 2, map("sum", 1), Document.DocumentState.LOCAL_MUTATIONS));
10241024

10251025
// Add another increment. Note that we still compute the increment based on the local value.
1026-
writeMutation(transformMutation("foo/bar", map("sum", FieldValue.numericAdd(2))));
1026+
writeMutation(transformMutation("foo/bar", map("sum", FieldValue.increment(2))));
10271027
assertChanged(doc("foo/bar", 2, map("sum", 3), Document.DocumentState.LOCAL_MUTATIONS));
10281028
assertContains(doc("foo/bar", 2, map("sum", 3), Document.DocumentState.LOCAL_MUTATIONS));
10291029

@@ -1072,7 +1072,7 @@ public void testHoldsBackOnlyNonIdempotentTransforms() {
10721072

10731073
writeMutations(
10741074
Arrays.asList(
1075-
transformMutation("foo/bar", map("sum", FieldValue.numericAdd(1))),
1075+
transformMutation("foo/bar", map("sum", FieldValue.increment(1))),
10761076
transformMutation("foo/bar", map("array_union", FieldValue.arrayUnion("foo")))));
10771077
assertChanged(
10781078
doc(
@@ -1105,7 +1105,7 @@ public void testHandlesMergeMutationWithTransformThenRemoteEvent() {
11051105
writeMutations(
11061106
asList(
11071107
patchMutation("foo/bar", map(), Collections.emptyList()),
1108-
transformMutation("foo/bar", map("sum", FieldValue.numericAdd(1)))));
1108+
transformMutation("foo/bar", map("sum", FieldValue.increment(1)))));
11091109
assertChanged(doc("foo/bar", 0, map("sum", 1), Document.DocumentState.LOCAL_MUTATIONS));
11101110
assertContains(doc("foo/bar", 0, map("sum", 1), Document.DocumentState.LOCAL_MUTATIONS));
11111111

@@ -1123,7 +1123,7 @@ public void testHandlesPatchMutationWithTransformThenRemoteEvent() {
11231123
writeMutations(
11241124
asList(
11251125
patchMutation("foo/bar", map()),
1126-
transformMutation("foo/bar", map("sum", FieldValue.numericAdd(1)))));
1126+
transformMutation("foo/bar", map("sum", FieldValue.increment(1)))));
11271127
assertChanged(deletedDoc("foo/bar", 0));
11281128
assertNotContains("foo/bar");
11291129

0 commit comments

Comments
 (0)