Skip to content

Commit

Permalink
port C++ DocumentKey to Local/* (#963)
Browse files Browse the repository at this point in the history
* port C++ DocumentKey to Local's

* address changes
  • Loading branch information
zxu123 committed Mar 23, 2018
1 parent 6a61d83 commit 0ccfd6a
Show file tree
Hide file tree
Showing 30 changed files with 242 additions and 179 deletions.
36 changes: 21 additions & 15 deletions Firestore/Example/Tests/Local/FSTEagerGarbageCollectorTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@

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

#include <set>

#import <XCTest/XCTest.h>

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

#import "Firestore/Example/Tests/Util/FSTHelpers.h"
#include "Firestore/core/src/firebase/firestore/model/document_key.h"
#include "Firestore/core/test/firebase/firestore/testutil/testutil.h"

namespace testutil = firebase::firestore::testutil;
using firebase::firestore::model::DocumentKey;

NS_ASSUME_NONNULL_BEGIN

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

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

[referenceSet removeReferenceToKey:key forID:1];
FSTAssertEqualSets([gc collectGarbage], @[ key ]);
XCTAssertEqual([gc collectGarbage], std::set<DocumentKey>({key}));
XCTAssertTrue([referenceSet isEmpty]);
}

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

FSTDocumentKey *key1 = FSTTestDocKey(@"foo/bar");
FSTDocumentKey *key2 = FSTTestDocKey(@"foo/baz");
FSTDocumentKey *key3 = FSTTestDocKey(@"foo/blah");
DocumentKey key1 = testutil::Key("foo/bar");
DocumentKey key2 = testutil::Key("foo/baz");
DocumentKey key3 = testutil::Key("foo/blah");
[referenceSet addReferenceToKey:key1 forID:1];
[referenceSet addReferenceToKey:key2 forID:1];
[referenceSet addReferenceToKey:key3 forID:2];
XCTAssertFalse([referenceSet isEmpty]);

[referenceSet removeReferencesForID:1];
FSTAssertEqualSets([gc collectGarbage], (@[ key1, key2 ]));
XCTAssertEqual([gc collectGarbage], std::set<DocumentKey>({key1, key2}));
XCTAssertFalse([referenceSet isEmpty]);

[referenceSet removeReferencesForID:2];
FSTAssertEqualSets([gc collectGarbage], @[ key3 ]);
XCTAssertEqual([gc collectGarbage], std::set<DocumentKey>({key3}));
XCTAssertTrue([referenceSet isEmpty]);
}

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

FSTDocumentKey *key1 = FSTTestDocKey(@"foo/bar");
DocumentKey key1 = testutil::Key("foo/bar");
[remoteTargets addReferenceToKey:key1 forID:1];
[localViews addReferenceToKey:key1 forID:1];
[mutations addReferenceToKey:key1 forID:10];

FSTDocumentKey *key2 = FSTTestDocKey(@"foo/baz");
DocumentKey key2 = testutil::Key("foo/baz");
[mutations addReferenceToKey:key2 forID:10];

XCTAssertFalse([remoteTargets isEmpty]);
XCTAssertFalse([localViews isEmpty]);
XCTAssertFalse([mutations isEmpty]);

[localViews removeReferencesForID:1];
FSTAssertEqualSets([gc collectGarbage], @[]);
XCTAssertEqual([gc collectGarbage], std::set<DocumentKey>({}));

[remoteTargets removeReferencesForID:1];
FSTAssertEqualSets([gc collectGarbage], @[]);
XCTAssertEqual([gc collectGarbage], std::set<DocumentKey>({}));

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

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

XCTAssertTrue([remoteTargets isEmpty]);
XCTAssertTrue([localViews isEmpty]);
Expand Down
21 changes: 13 additions & 8 deletions Firestore/Example/Tests/Local/FSTMutationQueueTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@
#import "Firestore/Example/Tests/Util/FSTHelpers.h"

#include "Firestore/core/src/firebase/firestore/auth/user.h"
#include "Firestore/core/src/firebase/firestore/model/document_key.h"
#include "Firestore/core/test/firebase/firestore/testutil/testutil.h"

namespace testutil = firebase::firestore::testutil;
using firebase::firestore::auth::User;
using firebase::firestore::model::DocumentKey;

NS_ASSUME_NONNULL_BEGIN

Expand Down Expand Up @@ -283,7 +287,7 @@ - (void)testAllMutationBatchesAffectingDocumentKey {

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

XCTAssertEqualObjects(matches, expected);
}
Expand Down Expand Up @@ -395,28 +399,29 @@ - (void)testRemoveMutationBatchesEmitsGarbageEvents {
]];

[self removeMutationBatches:@[ batches[0] ]];
NSSet<FSTDocumentKey *> *garbage = [garbageCollector collectGarbage];
FSTAssertEqualSets(garbage, @[]);
std::set<DocumentKey> garbage = [garbageCollector collectGarbage];
XCTAssertEqual(garbage, std::set<DocumentKey>({}));

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

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

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

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

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

- (void)testStreamToken {
Expand Down
53 changes: 29 additions & 24 deletions Firestore/Example/Tests/Local/FSTQueryCacheTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@
#import "Firestore/Source/Local/FSTPersistence.h"
#import "Firestore/Source/Local/FSTQueryData.h"
#import "Firestore/Source/Local/FSTWriteGroup.h"
#import "Firestore/Source/Model/FSTDocumentKey.h"

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

#include "Firestore/core/src/firebase/firestore/model/document_key.h"
#include "Firestore/core/test/firebase/firestore/testutil/testutil.h"

namespace testutil = firebase::firestore::testutil;
using firebase::firestore::model::DocumentKey;

NS_ASSUME_NONNULL_BEGIN

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

FSTDocumentKey *key1 = FSTTestDocKey(@"rooms/foo");
FSTDocumentKey *key2 = FSTTestDocKey(@"rooms/bar");
DocumentKey key1 = testutil::Key("rooms/foo");
DocumentKey key2 = testutil::Key("rooms/bar");
[self addMatchingKey:key1 forTargetID:rooms.targetID group:group];
[self addMatchingKey:key2 forTargetID:rooms.targetID group:group];

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

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

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

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

FSTWriteGroup *group = [self.persistence startGroupWithAction:@"RemoveMatchingKeysForTargetID"];
FSTDocumentKey *key1 = FSTTestDocKey(@"foo/bar");
FSTDocumentKey *key2 = FSTTestDocKey(@"foo/baz");
FSTDocumentKey *key3 = FSTTestDocKey(@"foo/blah");
DocumentKey key1 = testutil::Key("foo/bar");
DocumentKey key2 = testutil::Key("foo/baz");
DocumentKey key3 = testutil::Key("foo/blah");

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

FSTQueryData *rooms = [self queryDataWithQuery:FSTTestQuery("rooms")];
FSTDocumentKey *room1 = FSTTestDocKey(@"rooms/bar");
FSTDocumentKey *room2 = FSTTestDocKey(@"rooms/foo");
DocumentKey room1 = testutil::Key("rooms/bar");
DocumentKey room2 = testutil::Key("rooms/foo");
[self.queryCache addQueryData:rooms group:group];
[self addMatchingKey:room1 forTargetID:rooms.targetID group:group];
[self addMatchingKey:room2 forTargetID:rooms.targetID group:group];

FSTQueryData *halls = [self queryDataWithQuery:FSTTestQuery("halls")];
FSTDocumentKey *hall1 = FSTTestDocKey(@"halls/bar");
FSTDocumentKey *hall2 = FSTTestDocKey(@"halls/foo");
DocumentKey hall1 = testutil::Key("halls/bar");
DocumentKey hall2 = testutil::Key("halls/foo");
[self.queryCache addQueryData:halls group:group];
[self addMatchingKey:hall1 forTargetID:halls.targetID group:group];
[self addMatchingKey:hall2 forTargetID:halls.targetID group:group];

FSTAssertEqualSets([garbageCollector collectGarbage], @[]);
XCTAssertEqual([garbageCollector collectGarbage], std::set<DocumentKey>({}));

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

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

[self.queryCache removeMatchingKeysForTargetID:halls.targetID group:group];
FSTAssertEqualSets([garbageCollector collectGarbage], (@[ hall1, hall2 ]));
XCTAssertEqual([garbageCollector collectGarbage], std::set<DocumentKey>({hall1, hall2}));
[self.persistence commitGroup:group];
}

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

FSTWriteGroup *group = [self.persistence startGroupWithAction:@"MatchingKeysForTargetID"];
FSTDocumentKey *key1 = FSTTestDocKey(@"foo/bar");
FSTDocumentKey *key2 = FSTTestDocKey(@"foo/baz");
FSTDocumentKey *key3 = FSTTestDocKey(@"foo/blah");
DocumentKey key1 = testutil::Key("foo/bar");
DocumentKey key2 = testutil::Key("foo/baz");
DocumentKey key3 = testutil::Key("foo/blah");

[self addMatchingKey:key1 forTargetID:1 group:group];
[self addMatchingKey:key2 forTargetID:1 group:group];
Expand Down Expand Up @@ -335,8 +340,8 @@ - (void)testHighestTargetID {
targetID:1
listenSequenceNumber:10
purpose:FSTQueryPurposeListen];
FSTDocumentKey *key1 = FSTTestDocKey(@"rooms/bar");
FSTDocumentKey *key2 = FSTTestDocKey(@"rooms/foo");
DocumentKey key1 = testutil::Key("rooms/bar");
DocumentKey key2 = testutil::Key("rooms/foo");
[self.queryCache addQueryData:query1 group:group];
[self addMatchingKey:key1 forTargetID:1 group:group];
[self addMatchingKey:key2 forTargetID:1 group:group];
Expand All @@ -345,7 +350,7 @@ - (void)testHighestTargetID {
targetID:2
listenSequenceNumber:20
purpose:FSTQueryPurposeListen];
FSTDocumentKey *key3 = FSTTestDocKey(@"halls/foo");
DocumentKey key3 = testutil::Key("halls/foo");
[self.queryCache addQueryData:query2 group:group];
[self addMatchingKey:key3 forTargetID:2 group:group];
XCTAssertEqual([self.queryCache highestTargetID], 2);
Expand Down Expand Up @@ -419,15 +424,15 @@ - (FSTQueryData *)queryDataWithQuery:(FSTQuery *)query
resumeToken:resumeToken];
}

- (void)addMatchingKey:(FSTDocumentKey *)key
- (void)addMatchingKey:(cons DocumentKey &)key
forTargetID:(FSTTargetID)targetID
group:(FSTWriteGroup *)group {
FSTDocumentKeySet *keys = [FSTDocumentKeySet keySet];
keys = [keys setByAddingObject:key];
[self.queryCache addMatchingKeys:keys forTargetID:targetID group:group];
}

- (void)removeMatchingKey:(FSTDocumentKey *)key
- (void)removeMatchingKey:(const DocumentKey &)key
forTargetID:(FSTTargetID)targetID
group:(FSTWriteGroup *)group {
FSTDocumentKeySet *keys = [FSTDocumentKeySet keySet];
Expand Down
8 changes: 5 additions & 3 deletions Firestore/Source/Core/FSTSyncEngine.mm
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@

#include "Firestore/core/src/firebase/firestore/auth/user.h"
#include "Firestore/core/src/firebase/firestore/core/target_id_generator.h"
#include "Firestore/core/src/firebase/firestore/model/document_key.h"

using firebase::firestore::auth::HashUser;
using firebase::firestore::auth::User;
using firebase::firestore::core::TargetIdGenerator;
using firebase::firestore::model::DocumentKey;

NS_ASSUME_NONNULL_BEGIN

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

/** Garbage collect the limbo documents that we no longer need to track. */
- (void)garbageCollectLimboDocuments {
NSSet<FSTDocumentKey *> *garbage = [self.limboCollector collectGarbage];
for (FSTDocumentKey *key in garbage) {
FSTBoxedTargetID *limboTarget = self.limboTargetsByKey[key];
const std::set<DocumentKey> garbage = [self.limboCollector collectGarbage];
for (const DocumentKey &key : garbage) {
FSTBoxedTargetID *limboTarget = self.limboTargetsByKey[static_cast<FSTDocumentKey *>(key)];
if (!limboTarget) {
// This target already got removed, because the query failed.
return;
Expand Down
7 changes: 4 additions & 3 deletions Firestore/Source/Local/FSTDocumentReference.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#import <Foundation/Foundation.h>

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

NS_ASSUME_NONNULL_BEGIN

Expand All @@ -32,12 +32,13 @@ NS_ASSUME_NONNULL_BEGIN
@interface FSTDocumentReference : NSObject <NSCopying>

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

- (instancetype)init NS_UNAVAILABLE;

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

/**
* The targetID of a referring target or the batchID of a referring mutation batch. (Which this
Expand Down
Loading

0 comments on commit 0ccfd6a

Please sign in to comment.