Skip to content

Commit 0ccfd6a

Browse files
authored
port C++ DocumentKey to Local/* (#963)
* port C++ DocumentKey to Local's * address changes
1 parent 6a61d83 commit 0ccfd6a

30 files changed

+242
-179
lines changed

Firestore/Example/Tests/Local/FSTEagerGarbageCollectorTests.mm

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@
1616

1717
#import "Firestore/Source/Local/FSTEagerGarbageCollector.h"
1818

19+
#include <set>
20+
1921
#import <XCTest/XCTest.h>
2022

2123
#import "Firestore/Source/Local/FSTReferenceSet.h"
2224
#import "Firestore/Source/Model/FSTDocumentKey.h"
2325

24-
#import "Firestore/Example/Tests/Util/FSTHelpers.h"
26+
#include "Firestore/core/src/firebase/firestore/model/document_key.h"
27+
#include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
28+
29+
namespace testutil = firebase::firestore::testutil;
30+
using firebase::firestore::model::DocumentKey;
2531

2632
NS_ASSUME_NONNULL_BEGIN
2733

@@ -35,13 +41,13 @@ - (void)testAddOrRemoveReferences {
3541
FSTReferenceSet *referenceSet = [[FSTReferenceSet alloc] init];
3642
[gc addGarbageSource:referenceSet];
3743

38-
FSTDocumentKey *key = FSTTestDocKey(@"foo/bar");
44+
DocumentKey key = testutil::Key("foo/bar");
3945
[referenceSet addReferenceToKey:key forID:1];
40-
FSTAssertEqualSets([gc collectGarbage], @[]);
46+
XCTAssertEqual([gc collectGarbage], std::set<DocumentKey>({}));
4147
XCTAssertFalse([referenceSet isEmpty]);
4248

4349
[referenceSet removeReferenceToKey:key forID:1];
44-
FSTAssertEqualSets([gc collectGarbage], @[ key ]);
50+
XCTAssertEqual([gc collectGarbage], std::set<DocumentKey>({key}));
4551
XCTAssertTrue([referenceSet isEmpty]);
4652
}
4753

@@ -50,20 +56,20 @@ - (void)testRemoveAllReferencesForID {
5056
FSTReferenceSet *referenceSet = [[FSTReferenceSet alloc] init];
5157
[gc addGarbageSource:referenceSet];
5258

53-
FSTDocumentKey *key1 = FSTTestDocKey(@"foo/bar");
54-
FSTDocumentKey *key2 = FSTTestDocKey(@"foo/baz");
55-
FSTDocumentKey *key3 = FSTTestDocKey(@"foo/blah");
59+
DocumentKey key1 = testutil::Key("foo/bar");
60+
DocumentKey key2 = testutil::Key("foo/baz");
61+
DocumentKey key3 = testutil::Key("foo/blah");
5662
[referenceSet addReferenceToKey:key1 forID:1];
5763
[referenceSet addReferenceToKey:key2 forID:1];
5864
[referenceSet addReferenceToKey:key3 forID:2];
5965
XCTAssertFalse([referenceSet isEmpty]);
6066

6167
[referenceSet removeReferencesForID:1];
62-
FSTAssertEqualSets([gc collectGarbage], (@[ key1, key2 ]));
68+
XCTAssertEqual([gc collectGarbage], std::set<DocumentKey>({key1, key2}));
6369
XCTAssertFalse([referenceSet isEmpty]);
6470

6571
[referenceSet removeReferencesForID:2];
66-
FSTAssertEqualSets([gc collectGarbage], @[ key3 ]);
72+
XCTAssertEqual([gc collectGarbage], std::set<DocumentKey>({key3}));
6773
XCTAssertTrue([referenceSet isEmpty]);
6874
}
6975

@@ -77,29 +83,29 @@ - (void)testTwoReferenceSetsAtTheSameTime {
7783
[gc addGarbageSource:localViews];
7884
[gc addGarbageSource:mutations];
7985

80-
FSTDocumentKey *key1 = FSTTestDocKey(@"foo/bar");
86+
DocumentKey key1 = testutil::Key("foo/bar");
8187
[remoteTargets addReferenceToKey:key1 forID:1];
8288
[localViews addReferenceToKey:key1 forID:1];
8389
[mutations addReferenceToKey:key1 forID:10];
8490

85-
FSTDocumentKey *key2 = FSTTestDocKey(@"foo/baz");
91+
DocumentKey key2 = testutil::Key("foo/baz");
8692
[mutations addReferenceToKey:key2 forID:10];
8793

8894
XCTAssertFalse([remoteTargets isEmpty]);
8995
XCTAssertFalse([localViews isEmpty]);
9096
XCTAssertFalse([mutations isEmpty]);
9197

9298
[localViews removeReferencesForID:1];
93-
FSTAssertEqualSets([gc collectGarbage], @[]);
99+
XCTAssertEqual([gc collectGarbage], std::set<DocumentKey>({}));
94100

95101
[remoteTargets removeReferencesForID:1];
96-
FSTAssertEqualSets([gc collectGarbage], @[]);
102+
XCTAssertEqual([gc collectGarbage], std::set<DocumentKey>({}));
97103

98104
[mutations removeReferenceToKey:key1 forID:10];
99-
FSTAssertEqualSets([gc collectGarbage], @[ key1 ]);
105+
XCTAssertEqual([gc collectGarbage], std::set<DocumentKey>({key1}));
100106

101107
[mutations removeReferenceToKey:key2 forID:10];
102-
FSTAssertEqualSets([gc collectGarbage], @[ key2 ]);
108+
XCTAssertEqual([gc collectGarbage], std::set<DocumentKey>({key2}));
103109

104110
XCTAssertTrue([remoteTargets isEmpty]);
105111
XCTAssertTrue([localViews isEmpty]);

Firestore/Example/Tests/Local/FSTMutationQueueTests.mm

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@
2828
#import "Firestore/Example/Tests/Util/FSTHelpers.h"
2929

3030
#include "Firestore/core/src/firebase/firestore/auth/user.h"
31+
#include "Firestore/core/src/firebase/firestore/model/document_key.h"
32+
#include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
3133

34+
namespace testutil = firebase::firestore::testutil;
3235
using firebase::firestore::auth::User;
36+
using firebase::firestore::model::DocumentKey;
3337

3438
NS_ASSUME_NONNULL_BEGIN
3539

@@ -283,7 +287,7 @@ - (void)testAllMutationBatchesAffectingDocumentKey {
283287

284288
NSArray<FSTMutationBatch *> *expected = @[ batches[1], batches[2] ];
285289
NSArray<FSTMutationBatch *> *matches =
286-
[self.mutationQueue allMutationBatchesAffectingDocumentKey:FSTTestDocKey(@"foo/bar")];
290+
[self.mutationQueue allMutationBatchesAffectingDocumentKey:testutil::Key("foo/bar")];
287291

288292
XCTAssertEqualObjects(matches, expected);
289293
}
@@ -395,28 +399,29 @@ - (void)testRemoveMutationBatchesEmitsGarbageEvents {
395399
]];
396400

397401
[self removeMutationBatches:@[ batches[0] ]];
398-
NSSet<FSTDocumentKey *> *garbage = [garbageCollector collectGarbage];
399-
FSTAssertEqualSets(garbage, @[]);
402+
std::set<DocumentKey> garbage = [garbageCollector collectGarbage];
403+
XCTAssertEqual(garbage, std::set<DocumentKey>({}));
400404

401405
[self removeMutationBatches:@[ batches[1] ]];
402406
garbage = [garbageCollector collectGarbage];
403-
FSTAssertEqualSets(garbage, @[ FSTTestDocKey(@"foo/ba") ]);
407+
XCTAssertEqual(garbage, std::set<DocumentKey>({testutil::Key("foo/ba")}));
404408

405409
[self removeMutationBatches:@[ batches[5] ]];
406410
garbage = [garbageCollector collectGarbage];
407-
FSTAssertEqualSets(garbage, @[ FSTTestDocKey(@"bar/baz") ]);
411+
XCTAssertEqual(garbage, std::set<DocumentKey>({testutil::Key("bar/baz")}));
408412

409413
[self removeMutationBatches:@[ batches[2], batches[3] ]];
410414
garbage = [garbageCollector collectGarbage];
411-
FSTAssertEqualSets(garbage, (@[ FSTTestDocKey(@"foo/bar"), FSTTestDocKey(@"foo/bar2") ]));
415+
XCTAssertEqual(garbage,
416+
std::set<DocumentKey>({testutil::Key("foo/bar"), testutil::Key("foo/bar2")}));
412417

413418
[batches addObject:[self addMutationBatchWithKey:@"foo/bar/suffix/baz"]];
414419
garbage = [garbageCollector collectGarbage];
415-
FSTAssertEqualSets(garbage, @[]);
420+
XCTAssertEqual(garbage, std::set<DocumentKey>({}));
416421

417422
[self removeMutationBatches:@[ batches[4], batches[6] ]];
418423
garbage = [garbageCollector collectGarbage];
419-
FSTAssertEqualSets(garbage, @[ FSTTestDocKey(@"foo/bar/suffix/baz") ]);
424+
XCTAssertEqual(garbage, std::set<DocumentKey>({testutil::Key("foo/bar/suffix/baz")}));
420425
}
421426

422427
- (void)testStreamToken {

Firestore/Example/Tests/Local/FSTQueryCacheTests.mm

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,16 @@
2222
#import "Firestore/Source/Local/FSTPersistence.h"
2323
#import "Firestore/Source/Local/FSTQueryData.h"
2424
#import "Firestore/Source/Local/FSTWriteGroup.h"
25-
#import "Firestore/Source/Model/FSTDocumentKey.h"
2625

2726
#import "Firestore/Example/Tests/Util/FSTHelpers.h"
2827
#import "Firestore/third_party/Immutable/Tests/FSTImmutableSortedSet+Testing.h"
2928

29+
#include "Firestore/core/src/firebase/firestore/model/document_key.h"
30+
#include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
31+
32+
namespace testutil = firebase::firestore::testutil;
33+
using firebase::firestore::model::DocumentKey;
34+
3035
NS_ASSUME_NONNULL_BEGIN
3136

3237
@implementation FSTQueryCacheTests {
@@ -164,8 +169,8 @@ - (void)testRemoveQueryRemovesMatchingKeysToo {
164169
FSTQueryData *rooms = [self queryDataWithQuery:_queryRooms];
165170
[self.queryCache addQueryData:rooms group:group];
166171

167-
FSTDocumentKey *key1 = FSTTestDocKey(@"rooms/foo");
168-
FSTDocumentKey *key2 = FSTTestDocKey(@"rooms/bar");
172+
DocumentKey key1 = testutil::Key("rooms/foo");
173+
DocumentKey key2 = testutil::Key("rooms/bar");
169174
[self addMatchingKey:key1 forTargetID:rooms.targetID group:group];
170175
[self addMatchingKey:key2 forTargetID:rooms.targetID group:group];
171176

@@ -182,7 +187,7 @@ - (void)testAddOrRemoveMatchingKeys {
182187
if ([self isTestBaseClass]) return;
183188

184189
FSTWriteGroup *group = [self.persistence startGroupWithAction:@"AddOrRemoveMatchingKeys"];
185-
FSTDocumentKey *key = FSTTestDocKey(@"foo/bar");
190+
DocumentKey key = testutil::Key("foo/bar");
186191

187192
XCTAssertFalse([self.queryCache containsKey:key]);
188193

@@ -204,9 +209,9 @@ - (void)testRemoveMatchingKeysForTargetID {
204209
if ([self isTestBaseClass]) return;
205210

206211
FSTWriteGroup *group = [self.persistence startGroupWithAction:@"RemoveMatchingKeysForTargetID"];
207-
FSTDocumentKey *key1 = FSTTestDocKey(@"foo/bar");
208-
FSTDocumentKey *key2 = FSTTestDocKey(@"foo/baz");
209-
FSTDocumentKey *key3 = FSTTestDocKey(@"foo/blah");
212+
DocumentKey key1 = testutil::Key("foo/bar");
213+
DocumentKey key2 = testutil::Key("foo/baz");
214+
DocumentKey key3 = testutil::Key("foo/blah");
210215

211216
[self addMatchingKey:key1 forTargetID:1 group:group];
212217
[self addMatchingKey:key2 forTargetID:1 group:group];
@@ -233,42 +238,42 @@ - (void)testRemoveEmitsGarbageEvents {
233238
FSTWriteGroup *group = [self.persistence startGroupWithAction:@"RemoveEmitsGarbageEvents"];
234239
FSTEagerGarbageCollector *garbageCollector = [[FSTEagerGarbageCollector alloc] init];
235240
[garbageCollector addGarbageSource:self.queryCache];
236-
FSTAssertEqualSets([garbageCollector collectGarbage], @[]);
241+
XCTAssertEqual([garbageCollector collectGarbage], std::set<DocumentKey>({}));
237242

238243
FSTQueryData *rooms = [self queryDataWithQuery:FSTTestQuery("rooms")];
239-
FSTDocumentKey *room1 = FSTTestDocKey(@"rooms/bar");
240-
FSTDocumentKey *room2 = FSTTestDocKey(@"rooms/foo");
244+
DocumentKey room1 = testutil::Key("rooms/bar");
245+
DocumentKey room2 = testutil::Key("rooms/foo");
241246
[self.queryCache addQueryData:rooms group:group];
242247
[self addMatchingKey:room1 forTargetID:rooms.targetID group:group];
243248
[self addMatchingKey:room2 forTargetID:rooms.targetID group:group];
244249

245250
FSTQueryData *halls = [self queryDataWithQuery:FSTTestQuery("halls")];
246-
FSTDocumentKey *hall1 = FSTTestDocKey(@"halls/bar");
247-
FSTDocumentKey *hall2 = FSTTestDocKey(@"halls/foo");
251+
DocumentKey hall1 = testutil::Key("halls/bar");
252+
DocumentKey hall2 = testutil::Key("halls/foo");
248253
[self.queryCache addQueryData:halls group:group];
249254
[self addMatchingKey:hall1 forTargetID:halls.targetID group:group];
250255
[self addMatchingKey:hall2 forTargetID:halls.targetID group:group];
251256

252-
FSTAssertEqualSets([garbageCollector collectGarbage], @[]);
257+
XCTAssertEqual([garbageCollector collectGarbage], std::set<DocumentKey>({}));
253258

254259
[self removeMatchingKey:room1 forTargetID:rooms.targetID group:group];
255-
FSTAssertEqualSets([garbageCollector collectGarbage], @[ room1 ]);
260+
XCTAssertEqual([garbageCollector collectGarbage], std::set<DocumentKey>({room1}));
256261

257262
[self.queryCache removeQueryData:rooms group:group];
258-
FSTAssertEqualSets([garbageCollector collectGarbage], @[ room2 ]);
263+
XCTAssertEqual([garbageCollector collectGarbage], std::set<DocumentKey>({room2}));
259264

260265
[self.queryCache removeMatchingKeysForTargetID:halls.targetID group:group];
261-
FSTAssertEqualSets([garbageCollector collectGarbage], (@[ hall1, hall2 ]));
266+
XCTAssertEqual([garbageCollector collectGarbage], std::set<DocumentKey>({hall1, hall2}));
262267
[self.persistence commitGroup:group];
263268
}
264269

265270
- (void)testMatchingKeysForTargetID {
266271
if ([self isTestBaseClass]) return;
267272

268273
FSTWriteGroup *group = [self.persistence startGroupWithAction:@"MatchingKeysForTargetID"];
269-
FSTDocumentKey *key1 = FSTTestDocKey(@"foo/bar");
270-
FSTDocumentKey *key2 = FSTTestDocKey(@"foo/baz");
271-
FSTDocumentKey *key3 = FSTTestDocKey(@"foo/blah");
274+
DocumentKey key1 = testutil::Key("foo/bar");
275+
DocumentKey key2 = testutil::Key("foo/baz");
276+
DocumentKey key3 = testutil::Key("foo/blah");
272277

273278
[self addMatchingKey:key1 forTargetID:1 group:group];
274279
[self addMatchingKey:key2 forTargetID:1 group:group];
@@ -335,8 +340,8 @@ - (void)testHighestTargetID {
335340
targetID:1
336341
listenSequenceNumber:10
337342
purpose:FSTQueryPurposeListen];
338-
FSTDocumentKey *key1 = FSTTestDocKey(@"rooms/bar");
339-
FSTDocumentKey *key2 = FSTTestDocKey(@"rooms/foo");
343+
DocumentKey key1 = testutil::Key("rooms/bar");
344+
DocumentKey key2 = testutil::Key("rooms/foo");
340345
[self.queryCache addQueryData:query1 group:group];
341346
[self addMatchingKey:key1 forTargetID:1 group:group];
342347
[self addMatchingKey:key2 forTargetID:1 group:group];
@@ -345,7 +350,7 @@ - (void)testHighestTargetID {
345350
targetID:2
346351
listenSequenceNumber:20
347352
purpose:FSTQueryPurposeListen];
348-
FSTDocumentKey *key3 = FSTTestDocKey(@"halls/foo");
353+
DocumentKey key3 = testutil::Key("halls/foo");
349354
[self.queryCache addQueryData:query2 group:group];
350355
[self addMatchingKey:key3 forTargetID:2 group:group];
351356
XCTAssertEqual([self.queryCache highestTargetID], 2);
@@ -419,15 +424,15 @@ - (FSTQueryData *)queryDataWithQuery:(FSTQuery *)query
419424
resumeToken:resumeToken];
420425
}
421426

422-
- (void)addMatchingKey:(FSTDocumentKey *)key
427+
- (void)addMatchingKey:(cons DocumentKey &)key
423428
forTargetID:(FSTTargetID)targetID
424429
group:(FSTWriteGroup *)group {
425430
FSTDocumentKeySet *keys = [FSTDocumentKeySet keySet];
426431
keys = [keys setByAddingObject:key];
427432
[self.queryCache addMatchingKeys:keys forTargetID:targetID group:group];
428433
}
429434

430-
- (void)removeMatchingKey:(FSTDocumentKey *)key
435+
- (void)removeMatchingKey:(const DocumentKey &)key
431436
forTargetID:(FSTTargetID)targetID
432437
group:(FSTWriteGroup *)group {
433438
FSTDocumentKeySet *keys = [FSTDocumentKeySet keySet];

Firestore/Source/Core/FSTSyncEngine.mm

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@
4343

4444
#include "Firestore/core/src/firebase/firestore/auth/user.h"
4545
#include "Firestore/core/src/firebase/firestore/core/target_id_generator.h"
46+
#include "Firestore/core/src/firebase/firestore/model/document_key.h"
4647

4748
using firebase::firestore::auth::HashUser;
4849
using firebase::firestore::auth::User;
4950
using firebase::firestore::core::TargetIdGenerator;
51+
using firebase::firestore::model::DocumentKey;
5052

5153
NS_ASSUME_NONNULL_BEGIN
5254

@@ -509,9 +511,9 @@ - (void)trackLimboChange:(FSTLimboDocumentChange *)limboChange {
509511

510512
/** Garbage collect the limbo documents that we no longer need to track. */
511513
- (void)garbageCollectLimboDocuments {
512-
NSSet<FSTDocumentKey *> *garbage = [self.limboCollector collectGarbage];
513-
for (FSTDocumentKey *key in garbage) {
514-
FSTBoxedTargetID *limboTarget = self.limboTargetsByKey[key];
514+
const std::set<DocumentKey> garbage = [self.limboCollector collectGarbage];
515+
for (const DocumentKey &key : garbage) {
516+
FSTBoxedTargetID *limboTarget = self.limboTargetsByKey[static_cast<FSTDocumentKey *>(key)];
515517
if (!limboTarget) {
516518
// This target already got removed, because the query failed.
517519
return;

Firestore/Source/Local/FSTDocumentReference.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
#import <Foundation/Foundation.h>
1818

19-
@class FSTDocumentKey;
19+
#include "Firestore/core/src/firebase/firestore/model/document_key.h"
2020

2121
NS_ASSUME_NONNULL_BEGIN
2222

@@ -32,12 +32,13 @@ NS_ASSUME_NONNULL_BEGIN
3232
@interface FSTDocumentReference : NSObject <NSCopying>
3333

3434
/** Initializes the document reference with the given key and ID. */
35-
- (instancetype)initWithKey:(FSTDocumentKey *)key ID:(int32_t)ID NS_DESIGNATED_INITIALIZER;
35+
- (instancetype)initWithKey:(firebase::firestore::model::DocumentKey)key
36+
ID:(int32_t)ID NS_DESIGNATED_INITIALIZER;
3637

3738
- (instancetype)init NS_UNAVAILABLE;
3839

3940
/** The document key that's the target of this reference. */
40-
@property(nonatomic, strong, readonly) FSTDocumentKey *key;
41+
- (const firebase::firestore::model::DocumentKey &)key;
4142

4243
/**
4344
* The targetID of a referring target or the batchID of a referring mutation batch. (Which this

0 commit comments

Comments
 (0)