Skip to content

Commit a22b4cd

Browse files
authored
Pass FSTMutations using a vector (#2357)
1 parent 0074c79 commit a22b4cd

37 files changed

+264
-205
lines changed

Firestore/Example/Tests/Integration/FSTDatastoreTests.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ - (void)tearDown {
207207
- (void)testCommit {
208208
XCTestExpectation *expectation = [self expectationWithDescription:@"commitWithCompletion"];
209209

210-
_datastore->CommitMutations(@[], ^(NSError *_Nullable error) {
210+
_datastore->CommitMutations({}, ^(NSError *_Nullable error) {
211211
XCTAssertNil(error, @"Failed to commit");
212212
[expectation fulfill];
213213
});
@@ -224,7 +224,7 @@ - (void)testStreamingWrite {
224224
FSTSetMutation *mutation = [self setMutation];
225225
FSTMutationBatch *batch = [[FSTMutationBatch alloc] initWithBatchID:23
226226
localWriteTime:[FIRTimestamp timestamp]
227-
mutations:@[ mutation ]];
227+
mutations:{mutation}];
228228
_testWorkerQueue->Enqueue([=] {
229229
[_remoteStore addBatchToWritePipeline:batch];
230230
// The added batch won't be written immediately because write stream wasn't yet open --

Firestore/Example/Tests/Local/FSTLRUGarbageCollectorTests.mm

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
#include <unordered_map>
2222
#include <unordered_set>
23+
#include <utility>
24+
#include <vector>
2325

2426
#import "FIRTimestamp.h"
2527
#import "Firestore/Example/Tests/Util/FSTHelpers.h"
@@ -412,7 +414,7 @@ - (void)testRemoveOrphanedDocuments {
412414
// as any documents with pending mutations.
413415
std::unordered_set<DocumentKey, DocumentKeyHash> expectedRetained;
414416
// we add two mutations later, for now track them in an array.
415-
NSMutableArray *mutations = [NSMutableArray arrayWithCapacity:2];
417+
std::vector<FSTMutation *> mutations;
416418

417419
// Add a target and add two documents to it. The documents are expected to be
418420
// retained, since their membership in the target keeps them alive.
@@ -426,7 +428,7 @@ - (void)testRemoveOrphanedDocuments {
426428
FSTDocument *doc2 = [self cacheADocumentInTransaction];
427429
[self addDocument:doc2.key toTarget:queryData.targetID];
428430
expectedRetained.insert(doc2.key);
429-
[mutations addObject:[self mutationForDocument:doc2.key]];
431+
mutations.push_back([self mutationForDocument:doc2.key]);
430432
});
431433

432434
// Add a second query and register a third document on it
@@ -440,15 +442,15 @@ - (void)testRemoveOrphanedDocuments {
440442
// cache another document and prepare a mutation on it.
441443
_persistence.run("queue a mutation", [&]() {
442444
FSTDocument *doc4 = [self cacheADocumentInTransaction];
443-
[mutations addObject:[self mutationForDocument:doc4.key]];
445+
mutations.push_back([self mutationForDocument:doc4.key]);
444446
expectedRetained.insert(doc4.key);
445447
});
446448

447449
// Insert the mutations. These operations don't have a sequence number, they just
448450
// serve to keep the mutated documents from being GC'd while the mutations are outstanding.
449451
_persistence.run("actually register the mutations", [&]() {
450452
FIRTimestamp *writeTime = [FIRTimestamp timestamp];
451-
_mutationQueue->AddMutationBatch(writeTime, mutations);
453+
_mutationQueue->AddMutationBatch(writeTime, std::move(mutations));
452454
});
453455

454456
// Mark 5 documents eligible for GC. This simulates documents that were mutated then ack'd.

Firestore/Example/Tests/Local/FSTLocalSerializerTests.mm

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
#import <FirebaseFirestore/FIRTimestamp.h>
2020
#import <XCTest/XCTest.h>
2121

22+
#include <utility>
23+
#include <vector>
24+
2225
#import "Firestore/Protos/objc/firestore/local/MaybeDocument.pbobjc.h"
2326
#import "Firestore/Protos/objc/firestore/local/Mutation.pbobjc.h"
2427
#import "Firestore/Protos/objc/firestore/local/Target.pbobjc.h"
@@ -89,7 +92,7 @@ - (void)testEncodesMutationBatch {
8992
FIRTimestamp *writeTime = [FIRTimestamp timestamp];
9093
FSTMutationBatch *model = [[FSTMutationBatch alloc] initWithBatchID:42
9194
localWriteTime:writeTime
92-
mutations:@[ set, patch, del ]];
95+
mutations:{set, patch, del}];
9396

9497
GCFSWrite *setProto = [GCFSWrite message];
9598
setProto.update.name = @"projects/p/databases/d/documents/foo/bar";
@@ -123,7 +126,7 @@ - (void)testEncodesMutationBatch {
123126
FSTMutationBatch *decoded = [self.serializer decodedMutationBatch:batchProto];
124127
XCTAssertEqual(decoded.batchID, model.batchID);
125128
XCTAssertEqualObjects(decoded.localWriteTime, model.localWriteTime);
126-
XCTAssertEqualObjects(decoded.mutations, model.mutations);
129+
FSTAssertEqualVectors(decoded.mutations, model.mutations);
127130
XCTAssertEqual([decoded keys], [model keys]);
128131
}
129132

Firestore/Example/Tests/Local/FSTLocalStoreTests.mm

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#import <FirebaseFirestore/FIRTimestamp.h>
2020
#import <XCTest/XCTest.h>
2121

22+
#include <utility>
2223
#include <vector>
2324

2425
#import "Firestore/Source/Core/FSTQuery.h"
@@ -124,15 +125,16 @@ - (BOOL)isTestBaseClass {
124125
}
125126

126127
- (void)writeMutation:(FSTMutation *)mutation {
127-
[self writeMutations:@[ mutation ]];
128+
[self writeMutations:{mutation}];
128129
}
129130

130-
- (void)writeMutations:(NSArray<FSTMutation *> *)mutations {
131-
FSTLocalWriteResult *result = [self.localStore locallyWriteMutations:mutations];
131+
- (void)writeMutations:(std::vector<FSTMutation *> &&)mutations {
132+
auto mutationsCopy = mutations;
133+
FSTLocalWriteResult *result = [self.localStore locallyWriteMutations:std::move(mutationsCopy)];
132134
XCTAssertNotNil(result);
133135
[self.batches addObject:[[FSTMutationBatch alloc] initWithBatchID:result.batchID
134136
localWriteTime:[FIRTimestamp timestamp]
135-
mutations:mutations]];
137+
mutations:std::move(mutations)]];
136138
_lastChanges = result.changes;
137139
}
138140

@@ -147,7 +149,7 @@ - (void)notifyLocalViewChanges:(FSTLocalViewChanges *)changes {
147149
- (void)acknowledgeMutationWithVersion:(FSTTestSnapshotVersion)documentVersion {
148150
FSTMutationBatch *batch = [self.batches firstObject];
149151
[self.batches removeObjectAtIndex:0];
150-
XCTAssertEqual(batch.mutations.count, 1, @"Acknowledging more than one mutation not supported.");
152+
XCTAssertEqual(batch.mutations.size(), 1, @"Acknowledging more than one mutation not supported.");
151153
SnapshotVersion version = testutil::Version(documentVersion);
152154
FSTMutationResult *mutationResult = [[FSTMutationResult alloc] initWithVersion:version
153155
transformResults:nil];
@@ -227,7 +229,7 @@ - (void)testMutationBatchKeys {
227229
FSTMutation *set2 = FSTTestSetMutation(@"bar/baz", @{@"bar" : @"baz"});
228230
FSTMutationBatch *batch = [[FSTMutationBatch alloc] initWithBatchID:1
229231
localWriteTime:[FIRTimestamp timestamp]
230-
mutations:@[ set1, set2 ]];
232+
mutations:{set1, set2}];
231233
DocumentKeySet keys = [batch keys];
232234
XCTAssertEqual(keys.size(), 2u);
233235
}
@@ -619,10 +621,10 @@ - (void)testHandlesSetMutationThenPatchMutationThenDocumentThenAckThenAck {
619621
- (void)testHandlesSetMutationAndPatchMutationTogether {
620622
if ([self isTestBaseClass]) return;
621623

622-
[self writeMutations:@[
624+
[self writeMutations:{
623625
FSTTestSetMutation(@"foo/bar", @{@"foo" : @"old"}),
624-
FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})
625-
]];
626+
FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})
627+
}];
626628

627629
FSTAssertChanged(
628630
@[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations) ]);
@@ -649,11 +651,11 @@ - (void)testHandlesSetMutationThenPatchMutationThenReject {
649651
- (void)testHandlesSetMutationsAndPatchMutationOfJustOneTogether {
650652
if ([self isTestBaseClass]) return;
651653

652-
[self writeMutations:@[
654+
[self writeMutations:{
653655
FSTTestSetMutation(@"foo/bar", @{@"foo" : @"old"}),
654-
FSTTestSetMutation(@"bar/baz", @{@"bar" : @"baz"}),
655-
FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})
656-
]];
656+
FSTTestSetMutation(@"bar/baz", @{@"bar" : @"baz"}),
657+
FSTTestPatchMutation("foo/bar", @{@"foo" : @"bar"}, {})
658+
}];
657659

658660
FSTAssertChanged((@[
659661
FSTTestDoc("bar/baz", 0, @{@"bar" : @"baz"}, FSTDocumentStateLocalMutations),
@@ -843,11 +845,11 @@ - (void)testThrowsAwayDocumentsWithUnknownTargetIDsImmediately {
843845
- (void)testCanExecuteDocumentQueries {
844846
if ([self isTestBaseClass]) return;
845847

846-
[self.localStore locallyWriteMutations:@[
848+
[self.localStore locallyWriteMutations:{
847849
FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"}),
848-
FSTTestSetMutation(@"foo/baz", @{@"foo" : @"baz"}),
849-
FSTTestSetMutation(@"foo/bar/Foo/Bar", @{@"Foo" : @"Bar"})
850-
]];
850+
FSTTestSetMutation(@"foo/baz", @{@"foo" : @"baz"}),
851+
FSTTestSetMutation(@"foo/bar/Foo/Bar", @{@"Foo" : @"Bar"})
852+
}];
851853
FSTQuery *query = FSTTestQuery("foo/bar");
852854
DocumentMap docs = [self.localStore executeQuery:query];
853855
XCTAssertEqualObjects(docMapToArray(docs), @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"},
@@ -857,13 +859,13 @@ - (void)testCanExecuteDocumentQueries {
857859
- (void)testCanExecuteCollectionQueries {
858860
if ([self isTestBaseClass]) return;
859861

860-
[self.localStore locallyWriteMutations:@[
862+
[self.localStore locallyWriteMutations:{
861863
FSTTestSetMutation(@"fo/bar", @{@"fo" : @"bar"}),
862-
FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"}),
863-
FSTTestSetMutation(@"foo/baz", @{@"foo" : @"baz"}),
864-
FSTTestSetMutation(@"foo/bar/Foo/Bar", @{@"Foo" : @"Bar"}),
865-
FSTTestSetMutation(@"fooo/blah", @{@"fooo" : @"blah"})
866-
]];
864+
FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"}),
865+
FSTTestSetMutation(@"foo/baz", @{@"foo" : @"baz"}),
866+
FSTTestSetMutation(@"foo/bar/Foo/Bar", @{@"Foo" : @"Bar"}),
867+
FSTTestSetMutation(@"fooo/blah", @{@"fooo" : @"blah"})
868+
}];
867869
FSTQuery *query = FSTTestQuery("foo");
868870
DocumentMap docs = [self.localStore executeQuery:query];
869871
XCTAssertEqualObjects(
@@ -887,7 +889,7 @@ - (void)testCanExecuteMixedCollectionQueries {
887889
FSTTestDoc("foo/bar", 20, @{@"a" : @"b"}, FSTDocumentStateSynced), {2},
888890
{})];
889891

890-
[self.localStore locallyWriteMutations:@[ FSTTestSetMutation(@"foo/bonk", @{@"a" : @"b"}) ]];
892+
[self.localStore locallyWriteMutations:{ FSTTestSetMutation(@"foo/bonk", @{@"a" : @"b"}) }];
891893

892894
DocumentMap docs = [self.localStore executeQuery:query];
893895
XCTAssertEqualObjects(docMapToArray(docs), (@[
@@ -942,7 +944,7 @@ - (void)testRemoteDocumentKeysForTarget {
942944
applyRemoteEvent:FSTTestAddedRemoteEvent(
943945
FSTTestDoc("foo/bar", 20, @{@"a" : @"b"}, FSTDocumentStateSynced), {2})];
944946

945-
[self.localStore locallyWriteMutations:@[ FSTTestSetMutation(@"foo/bonk", @{@"a" : @"b"}) ]];
947+
[self.localStore locallyWriteMutations:{ FSTTestSetMutation(@"foo/bonk", @{@"a" : @"b"}) }];
946948

947949
DocumentKeySet keys = [self.localStore remoteDocumentKeysForTarget:2];
948950
DocumentKeySet expected{testutil::Key("foo/bar"), testutil::Key("foo/baz")};

Firestore/Example/Tests/Local/FSTMutationQueueTests.mm

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#import <FirebaseFirestore/FIRTimestamp.h>
2020

2121
#include <set>
22+
#include <utility>
2223
#include <vector>
2324

2425
#import "Firestore/Source/Core/FSTQuery.h"
@@ -50,14 +51,6 @@ - (void)tearDown {
5051
[super tearDown];
5152
}
5253

53-
- (void)assertVector:(const std::vector<FSTMutationBatch *> &)actual
54-
matchesExpected:(const std::vector<FSTMutationBatch *> &)expected {
55-
XCTAssertEqual(actual.size(), expected.size(), @"Vector length mismatch");
56-
for (int i = 0; i < expected.size(); i++) {
57-
XCTAssertEqualObjects(actual[i], expected[i]);
58-
}
59-
}
60-
6154
/**
6255
* Xcode will run tests from any class that extends XCTestCase, but this doesn't work for
6356
* FSTMutationQueueTests since it is incomplete without the implementations supplied by its
@@ -208,15 +201,15 @@ - (void)testAllMutationBatchesAffectingDocumentKey {
208201
NSMutableArray<FSTMutationBatch *> *batches = [NSMutableArray array];
209202
for (FSTMutation *mutation in mutations) {
210203
FSTMutationBatch *batch =
211-
self.mutationQueue->AddMutationBatch([FIRTimestamp timestamp], @[ mutation ]);
204+
self.mutationQueue->AddMutationBatch([FIRTimestamp timestamp], { mutation });
212205
[batches addObject:batch];
213206
}
214207

215208
std::vector<FSTMutationBatch *> expected{batches[1], batches[2]};
216209
std::vector<FSTMutationBatch *> matches =
217210
self.mutationQueue->AllMutationBatchesAffectingDocumentKey(testutil::Key("foo/bar"));
218211

219-
[self assertVector:matches matchesExpected:expected];
212+
FSTAssertEqualVectors(matches, expected);
220213
});
221214
}
222215

@@ -235,7 +228,7 @@ - (void)testAllMutationBatchesAffectingDocumentKeys {
235228
NSMutableArray<FSTMutationBatch *> *batches = [NSMutableArray array];
236229
for (FSTMutation *mutation in mutations) {
237230
FSTMutationBatch *batch =
238-
self.mutationQueue->AddMutationBatch([FIRTimestamp timestamp], @[ mutation ]);
231+
self.mutationQueue->AddMutationBatch([FIRTimestamp timestamp], { mutation });
239232
[batches addObject:batch];
240233
}
241234

@@ -248,29 +241,29 @@ - (void)testAllMutationBatchesAffectingDocumentKeys {
248241
std::vector<FSTMutationBatch *> matches =
249242
self.mutationQueue->AllMutationBatchesAffectingDocumentKeys(keys);
250243

251-
[self assertVector:matches matchesExpected:expected];
244+
FSTAssertEqualVectors(matches, expected);
252245
});
253246
}
254247

255248
- (void)testAllMutationBatchesAffectingDocumentKeys_handlesOverlap {
256249
if ([self isTestBaseClass]) return;
257250

258251
self.persistence.run("testAllMutationBatchesAffectingDocumentKeys_handlesOverlap", [&]() {
259-
NSArray<FSTMutation *> *group1 = @[
260-
FSTTestSetMutation(@"foo/bar", @{@"a" : @1}),
261-
FSTTestSetMutation(@"foo/baz", @{@"a" : @1}),
262-
];
252+
std::vector<FSTMutation *> group1 = {
253+
FSTTestSetMutation(@"foo/bar", @{@"a" : @1}),
254+
FSTTestSetMutation(@"foo/baz", @{@"a" : @1}),
255+
};
263256
FSTMutationBatch *batch1 =
264-
self.mutationQueue->AddMutationBatch([FIRTimestamp timestamp], group1);
257+
self.mutationQueue->AddMutationBatch([FIRTimestamp timestamp], std::move(group1));
265258

266-
NSArray<FSTMutation *> *group2 = @[ FSTTestSetMutation(@"food/bar", @{@"a" : @1}) ];
267-
self.mutationQueue->AddMutationBatch([FIRTimestamp timestamp], group2);
259+
std::vector<FSTMutation *> group2 = {FSTTestSetMutation(@"food/bar", @{@"a" : @1})};
260+
self.mutationQueue->AddMutationBatch([FIRTimestamp timestamp], std::move(group2));
268261

269-
NSArray<FSTMutation *> *group3 = @[
270-
FSTTestSetMutation(@"foo/bar", @{@"b" : @1}),
271-
];
262+
std::vector<FSTMutation *> group3 = {
263+
FSTTestSetMutation(@"foo/bar", @{@"b" : @1}),
264+
};
272265
FSTMutationBatch *batch3 =
273-
self.mutationQueue->AddMutationBatch([FIRTimestamp timestamp], group3);
266+
self.mutationQueue->AddMutationBatch([FIRTimestamp timestamp], std::move(group3));
274267

275268
DocumentKeySet keys{
276269
Key("foo/bar"),
@@ -281,7 +274,7 @@ - (void)testAllMutationBatchesAffectingDocumentKeys_handlesOverlap {
281274
std::vector<FSTMutationBatch *> matches =
282275
self.mutationQueue->AllMutationBatchesAffectingDocumentKeys(keys);
283276

284-
[self assertVector:matches matchesExpected:expected];
277+
FSTAssertEqualVectors(matches, expected);
285278
});
286279
}
287280

@@ -300,7 +293,7 @@ - (void)testAllMutationBatchesAffectingQuery {
300293
NSMutableArray<FSTMutationBatch *> *batches = [NSMutableArray array];
301294
for (FSTMutation *mutation in mutations) {
302295
FSTMutationBatch *batch =
303-
self.mutationQueue->AddMutationBatch([FIRTimestamp timestamp], @[ mutation ]);
296+
self.mutationQueue->AddMutationBatch([FIRTimestamp timestamp], { mutation });
304297
[batches addObject:batch];
305298
}
306299

@@ -309,7 +302,7 @@ - (void)testAllMutationBatchesAffectingQuery {
309302
std::vector<FSTMutationBatch *> matches =
310303
self.mutationQueue->AllMutationBatchesAffectingQuery(query);
311304

312-
[self assertVector:matches matchesExpected:expected];
305+
FSTAssertEqualVectors(matches, expected);
313306
});
314307
}
315308

@@ -327,7 +320,7 @@ - (void)testRemoveMutationBatches {
327320
std::vector<FSTMutationBatch *> found;
328321

329322
found = self.mutationQueue->AllMutationBatches();
330-
[self assertVector:found matchesExpected:batches];
323+
FSTAssertEqualVectors(found, batches);
331324
XCTAssertEqual(found.size(), 9);
332325

333326
self.mutationQueue->RemoveMutationBatch(batches[0]);
@@ -337,15 +330,15 @@ - (void)testRemoveMutationBatches {
337330
XCTAssertEqual([self batchCount], 6);
338331

339332
found = self.mutationQueue->AllMutationBatches();
340-
[self assertVector:found matchesExpected:batches];
333+
FSTAssertEqualVectors(found, batches);
341334
XCTAssertEqual(found.size(), 6);
342335

343336
self.mutationQueue->RemoveMutationBatch(batches[0]);
344337
batches.erase(batches.begin());
345338
XCTAssertEqual([self batchCount], 5);
346339

347340
found = self.mutationQueue->AllMutationBatches();
348-
[self assertVector:found matchesExpected:batches];
341+
FSTAssertEqualVectors(found, batches);
349342
XCTAssertEqual(found.size(), 5);
350343

351344
self.mutationQueue->RemoveMutationBatch(batches[0]);
@@ -357,7 +350,7 @@ - (void)testRemoveMutationBatches {
357350
XCTAssertEqual([self batchCount], 3);
358351

359352
found = self.mutationQueue->AllMutationBatches();
360-
[self assertVector:found matchesExpected:batches];
353+
FSTAssertEqualVectors(found, batches);
361354
XCTAssertEqual(found.size(), 3);
362355
XCTAssertFalse(self.mutationQueue->IsEmpty());
363356

@@ -404,7 +397,7 @@ - (FSTMutationBatch *)addMutationBatchWithKey:(NSString *)key {
404397
FSTSetMutation *mutation = FSTTestSetMutation(key, @{@"a" : @1});
405398

406399
FSTMutationBatch *batch =
407-
self.mutationQueue->AddMutationBatch([FIRTimestamp timestamp], @[ mutation ]);
400+
self.mutationQueue->AddMutationBatch([FIRTimestamp timestamp], { mutation });
408401
return batch;
409402
}
410403

Firestore/Example/Tests/SpecTests/FSTMockDatastore.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class MockDatastore : public Datastore {
7878
/**
7979
* Returns the next write that was "sent to the backend", failing if there are no queued sent
8080
*/
81-
NSArray<FSTMutation*>* NextSentWrite();
81+
std::vector<FSTMutation*> NextSentWrite();
8282
/** Returns the number of writes that have been sent to the backend but not waited on yet. */
8383
int WritesSent() const;
8484

0 commit comments

Comments
 (0)