Skip to content

Commit ceb991a

Browse files
Port Mutation Batch API from Android/Web (#1963)
1 parent cf11a13 commit ceb991a

File tree

7 files changed

+187
-118
lines changed

7 files changed

+187
-118
lines changed

Firestore/Example/Tests/Model/FSTMutationTests.mm

+18-28
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ - (void)testAppliesSetsToDocuments {
6060
FSTDocument *baseDoc = FSTTestDoc("collection/key", 0, docData, NO);
6161

6262
FSTMutation *set = FSTTestSetMutation(@"collection/key", @{@"bar" : @"bar-value"});
63-
FSTMaybeDocument *setDoc = [set applyTo:baseDoc baseDocument:baseDoc localWriteTime:_timestamp];
63+
FSTMaybeDocument *setDoc =
64+
[set applyToLocalDocument:baseDoc baseDocument:baseDoc localWriteTime:_timestamp];
6465

6566
NSDictionary *expectedData = @{@"bar" : @"bar-value"};
6667
XCTAssertEqualObjects(setDoc, FSTTestDoc("collection/key", 0, expectedData, YES));
@@ -72,7 +73,7 @@ - (void)testAppliesPatchesToDocuments {
7273

7374
FSTMutation *patch = FSTTestPatchMutation("collection/key", @{@"foo.bar" : @"new-bar-value"}, {});
7475
FSTMaybeDocument *patchedDoc =
75-
[patch applyTo:baseDoc baseDocument:baseDoc localWriteTime:_timestamp];
76+
[patch applyToLocalDocument:baseDoc baseDocument:baseDoc localWriteTime:_timestamp];
7677

7778
NSDictionary *expectedData = @{@"foo" : @{@"bar" : @"new-bar-value"}, @"baz" : @"baz-value"};
7879
XCTAssertEqualObjects(patchedDoc, FSTTestDoc("collection/key", 0, expectedData, YES));
@@ -88,7 +89,7 @@ - (void)testDeletesValuesFromTheFieldMask {
8889
value:[FSTObjectValue objectValue]
8990
precondition:Precondition::None()];
9091
FSTMaybeDocument *patchedDoc =
91-
[patch applyTo:baseDoc baseDocument:baseDoc localWriteTime:_timestamp];
92+
[patch applyToLocalDocument:baseDoc baseDocument:baseDoc localWriteTime:_timestamp];
9293

9394
NSDictionary *expectedData = @{@"foo" : @{@"baz" : @"baz-value"}};
9495
XCTAssertEqualObjects(patchedDoc, FSTTestDoc("collection/key", 0, expectedData, YES));
@@ -100,7 +101,7 @@ - (void)testPatchesPrimitiveValue {
100101

101102
FSTMutation *patch = FSTTestPatchMutation("collection/key", @{@"foo.bar" : @"new-bar-value"}, {});
102103
FSTMaybeDocument *patchedDoc =
103-
[patch applyTo:baseDoc baseDocument:baseDoc localWriteTime:_timestamp];
104+
[patch applyToLocalDocument:baseDoc baseDocument:baseDoc localWriteTime:_timestamp];
104105

105106
NSDictionary *expectedData = @{@"foo" : @{@"bar" : @"new-bar-value"}, @"baz" : @"baz-value"};
106107
XCTAssertEqualObjects(patchedDoc, FSTTestDoc("collection/key", 0, expectedData, YES));
@@ -110,7 +111,7 @@ - (void)testPatchingDeletedDocumentsDoesNothing {
110111
FSTMaybeDocument *baseDoc = FSTTestDeletedDoc("collection/key", 0);
111112
FSTMutation *patch = FSTTestPatchMutation("collection/key", @{@"foo" : @"bar"}, {});
112113
FSTMaybeDocument *patchedDoc =
113-
[patch applyTo:baseDoc baseDocument:baseDoc localWriteTime:_timestamp];
114+
[patch applyToLocalDocument:baseDoc baseDocument:baseDoc localWriteTime:_timestamp];
114115
XCTAssertEqualObjects(patchedDoc, baseDoc);
115116
}
116117

@@ -121,7 +122,7 @@ - (void)testAppliesLocalServerTimestampTransformToDocuments {
121122
FSTMutation *transform = FSTTestTransformMutation(
122123
@"collection/key", @{@"foo.bar" : [FIRFieldValue fieldValueForServerTimestamp]});
123124
FSTMaybeDocument *transformedDoc =
124-
[transform applyTo:baseDoc baseDocument:baseDoc localWriteTime:_timestamp];
125+
[transform applyToLocalDocument:baseDoc baseDocument:baseDoc localWriteTime:_timestamp];
125126

126127
// Server timestamps aren't parsed, so we manually insert it.
127128
FSTObjectValue *expectedData = FSTTestObjectValue(
@@ -297,7 +298,7 @@ - (void)transformBaseDoc:(NSDictionary<NSString *, id> *)baseData
297298
FSTMutation *transform = FSTTestTransformMutation(@"collection/key", transformData);
298299

299300
FSTMaybeDocument *transformedDoc =
300-
[transform applyTo:baseDoc baseDocument:baseDoc localWriteTime:_timestamp];
301+
[transform applyToLocalDocument:baseDoc baseDocument:baseDoc localWriteTime:_timestamp];
301302

302303
FSTDocument *expectedDoc = [FSTDocument documentWithData:FSTTestObjectValue(expectedData)
303304
key:FSTTestDocKey(@"collection/key")
@@ -318,10 +319,8 @@ - (void)testAppliesServerAckedServerTimestampTransformToDocuments {
318319
initWithVersion:testutil::Version(1)
319320
transformResults:@[ [FSTTimestampValue timestampValue:_timestamp] ]];
320321

321-
FSTMaybeDocument *transformedDoc = [transform applyTo:baseDoc
322-
baseDocument:baseDoc
323-
localWriteTime:_timestamp
324-
mutationResult:mutationResult];
322+
FSTMaybeDocument *transformedDoc =
323+
[transform applyToRemoteDocument:baseDoc mutationResult:mutationResult];
325324

326325
NSDictionary *expectedData = @{@"foo" : @{@"bar" : _timestamp.dateValue}, @"baz" : @"baz-value"};
327326
XCTAssertEqualObjects(transformedDoc, FSTTestDoc("collection/key", 0, expectedData, NO));
@@ -341,10 +340,8 @@ - (void)testAppliesServerAckedArrayTransformsToDocuments {
341340
initWithVersion:testutil::Version(1)
342341
transformResults:@[ [FSTNullValue nullValue], [FSTNullValue nullValue] ]];
343342

344-
FSTMaybeDocument *transformedDoc = [transform applyTo:baseDoc
345-
baseDocument:baseDoc
346-
localWriteTime:_timestamp
347-
mutationResult:mutationResult];
343+
FSTMaybeDocument *transformedDoc =
344+
[transform applyToRemoteDocument:baseDoc mutationResult:mutationResult];
348345

349346
NSDictionary *expectedData = @{@"array_1" : @[ @1, @2, @3 ], @"array_2" : @[ @"b" ]};
350347
XCTAssertEqualObjects(transformedDoc, FSTTestDoc("collection/key", 0, expectedData, NO));
@@ -356,7 +353,7 @@ - (void)testDeleteDeletes {
356353

357354
FSTMutation *mutation = FSTTestDeleteMutation(@"collection/key");
358355
FSTMaybeDocument *result =
359-
[mutation applyTo:baseDoc baseDocument:baseDoc localWriteTime:_timestamp];
356+
[mutation applyToLocalDocument:baseDoc baseDocument:baseDoc localWriteTime:_timestamp];
360357
XCTAssertEqualObjects(result, FSTTestDeletedDoc("collection/key", 0));
361358
}
362359

@@ -367,10 +364,7 @@ - (void)testSetWithMutationResult {
367364
FSTMutation *set = FSTTestSetMutation(@"collection/key", @{@"foo" : @"new-bar"});
368365
FSTMutationResult *mutationResult =
369366
[[FSTMutationResult alloc] initWithVersion:testutil::Version(4) transformResults:nil];
370-
FSTMaybeDocument *setDoc = [set applyTo:baseDoc
371-
baseDocument:baseDoc
372-
localWriteTime:_timestamp
373-
mutationResult:mutationResult];
367+
FSTMaybeDocument *setDoc = [set applyToRemoteDocument:baseDoc mutationResult:mutationResult];
374368

375369
NSDictionary *expectedData = @{@"foo" : @"new-bar"};
376370
XCTAssertEqualObjects(setDoc, FSTTestDoc("collection/key", 0, expectedData, NO));
@@ -383,10 +377,8 @@ - (void)testPatchWithMutationResult {
383377
FSTMutation *patch = FSTTestPatchMutation("collection/key", @{@"foo" : @"new-bar"}, {});
384378
FSTMutationResult *mutationResult =
385379
[[FSTMutationResult alloc] initWithVersion:testutil::Version(4) transformResults:nil];
386-
FSTMaybeDocument *patchedDoc = [patch applyTo:baseDoc
387-
baseDocument:baseDoc
388-
localWriteTime:_timestamp
389-
mutationResult:mutationResult];
380+
FSTMaybeDocument *patchedDoc =
381+
[patch applyToRemoteDocument:baseDoc mutationResult:mutationResult];
390382

391383
NSDictionary *expectedData = @{@"foo" : @"new-bar"};
392384
XCTAssertEqualObjects(patchedDoc, FSTTestDoc("collection/key", 0, expectedData, NO));
@@ -396,10 +388,8 @@ - (void)testPatchWithMutationResult {
396388
do { \
397389
FSTMutationResult *mutationResult = \
398390
[[FSTMutationResult alloc] initWithVersion:testutil::Version(0) transformResults:nil]; \
399-
FSTMaybeDocument *actual = [mutation applyTo:base \
400-
baseDocument:base \
401-
localWriteTime:_timestamp \
402-
mutationResult:mutationResult]; \
391+
FSTMaybeDocument *actual = \
392+
[mutation applyToRemoteDocument:base mutationResult:mutationResult]; \
403393
XCTAssertEqualObjects(actual, expected); \
404394
} while (0);
405395

Firestore/Source/Local/FSTLocalDocumentsView.mm

+4-3
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ - (nullable FSTMaybeDocument *)documentForKey:(const DocumentKey &)key
7272
inBatches:(NSArray<FSTMutationBatch *> *)batches {
7373
FSTMaybeDocument *_Nullable document = [self.remoteDocumentCache entryForKey:key];
7474
for (FSTMutationBatch *batch in batches) {
75-
document = [batch applyTo:document documentKey:key];
75+
document = [batch applyToLocalDocument:document documentKey:key];
7676
}
7777

7878
return document;
@@ -129,8 +129,9 @@ - (FSTDocumentDictionary *)documentsMatchingCollectionQuery:(FSTQuery *)query {
129129
FSTDocumentKey *key = static_cast<FSTDocumentKey *>(mutation.key);
130130
// baseDoc may be nil for the documents that weren't yet written to the backend.
131131
FSTMaybeDocument *baseDoc = results[key];
132-
FSTMaybeDocument *mutatedDoc =
133-
[mutation applyTo:baseDoc baseDocument:baseDoc localWriteTime:batch.localWriteTime];
132+
FSTMaybeDocument *mutatedDoc = [mutation applyToLocalDocument:baseDoc
133+
baseDocument:baseDoc
134+
localWriteTime:batch.localWriteTime];
134135

135136
if (!mutatedDoc || [mutatedDoc isKindOfClass:[FSTDeletedDocument class]]) {
136137
results = [results dictionaryByRemovingObjectForKey:key];

Firestore/Source/Local/FSTLocalStore.mm

+1-1
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ - (void)applyBatchResult:(FSTMutationBatchResult *)batchResult {
552552
"docVersions should contain every doc in the write.");
553553
const SnapshotVersion &ackVersion = ackVersionIter->second;
554554
if (!doc || doc.version < ackVersion) {
555-
doc = [batch applyTo:doc documentKey:docKey mutationBatchResult:batchResult];
555+
doc = [batch applyToRemoteDocument:doc documentKey:docKey mutationBatchResult:batchResult];
556556
if (!doc) {
557557
HARD_ASSERT(!remoteDoc, "Mutation batch %s applied to document %s resulted in nil.", batch,
558558
remoteDoc);

Firestore/Source/Model/FSTMutation.h

+16-15
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,11 @@ NS_ASSUME_NONNULL_BEGIN
7979
NS_DESIGNATED_INITIALIZER;
8080

8181
/**
82-
* Applies this mutation to the given FSTDocument, FSTDeletedDocument or nil, if we don't have
83-
* information about this document. Both the input and returned documents can be nil.
82+
* Applies this mutation to the given FSTDocument, FSTDeletedDocument or nil for the purposes of
83+
* computing a new remote document. Both the input and returned documents can be nil.
8484
*
8585
* @param maybeDoc The current state of the document to mutate. The input document should be nil if
8686
* it does not currently exist.
87-
* @param baseDoc The state of the document prior to this mutation batch. The input document should
88-
* be nil if it the document did not exist.
89-
* @param localWriteTime A timestamp indicating the local write time of the batch this mutation is
90-
* a part of.
9187
* @param mutationResult Optional result info from the backend. If omitted, it's assumed that
9288
* this is merely a local (latency-compensated) application, and the resulting document will
9389
* have its hasLocalMutations flag set.
@@ -122,18 +118,23 @@ NS_ASSUME_NONNULL_BEGIN
122118
* apply the transform if the prior mutation resulted in an FSTDocument (always true for an
123119
* FSTSetMutation, but not necessarily for an FSTPatchMutation).
124120
*/
125-
- (nullable FSTMaybeDocument *)applyTo:(nullable FSTMaybeDocument *)maybeDoc
126-
baseDocument:(nullable FSTMaybeDocument *)baseDoc
127-
localWriteTime:(FIRTimestamp *)localWriteTime
128-
mutationResult:(nullable FSTMutationResult *)mutationResult;
121+
- (nullable FSTMaybeDocument *)applyToRemoteDocument:(nullable FSTMaybeDocument *)maybeDoc
122+
mutationResult:(FSTMutationResult *)mutationResult;
129123

130124
/**
131-
* A helper version of applyTo for applying mutations locally (without a mutation result from the
132-
* backend).
125+
* Applies this mutation to the given MaybeDocument for the purposes of computing the new local view
126+
* of a document. Both the input and returned documents can be null.
127+
*
128+
* @param maybeDoc The current state of the document to mutate. The input document should be nil if
129+
* it does not currently exist.
130+
* @param baseDoc The state of the document prior to this mutation batch. The input document should
131+
* be nil if it the document did not exist.
132+
* @param localWriteTime A timestamp indicating the local write time of the batch this mutation is
133+
* a part of.
133134
*/
134-
- (nullable FSTMaybeDocument *)applyTo:(nullable FSTMaybeDocument *)maybeDoc
135-
baseDocument:(nullable FSTMaybeDocument *)baseDoc
136-
localWriteTime:(nullable FIRTimestamp *)localWriteTime;
135+
- (nullable FSTMaybeDocument *)applyToLocalDocument:(nullable FSTMaybeDocument *)maybeDoc
136+
baseDocument:(nullable FSTMaybeDocument *)baseDoc
137+
localWriteTime:(FIRTimestamp *)localWriteTime;
137138

138139
- (const firebase::firestore::model::DocumentKey &)key;
139140

0 commit comments

Comments
 (0)