aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar zxu <zxu@google.com>2018-01-25 18:39:46 -0500
committerGravatar GitHub <noreply@github.com>2018-01-25 18:39:46 -0500
commit7226b4adf38e4732dfb9a840d25f86d3e5533bdb (patch)
tree01c7d79608eae5221deca452f8e461f2758b1ef0
parent53064743963d7e5cc12f7a42bb036814f5a6df17 (diff)
port TargetIdGenerator to iOS (#709)
* port TargetIdGenerator to iOS * fix style * move pointer property to instance variable * TriggerTravis
-rw-r--r--Firestore/Example/Firestore.xcodeproj/project.pbxproj4
-rw-r--r--Firestore/Example/Tests/Core/FSTTargetIDGeneratorTests.m94
-rw-r--r--Firestore/Source/Core/FSTSyncEngine.mm (renamed from Firestore/Source/Core/FSTSyncEngine.m)16
-rw-r--r--Firestore/Source/Core/FSTTargetIDGenerator.h55
-rw-r--r--Firestore/Source/Core/FSTTargetIDGenerator.m105
-rw-r--r--Firestore/Source/Local/FSTLocalStore.mm (renamed from Firestore/Source/Local/FSTLocalStore.m)19
-rw-r--r--Firestore/core/src/firebase/firestore/core/target_id_generator.h3
7 files changed, 24 insertions, 272 deletions
diff --git a/Firestore/Example/Firestore.xcodeproj/project.pbxproj b/Firestore/Example/Firestore.xcodeproj/project.pbxproj
index 7bc57fc..7c6508a 100644
--- a/Firestore/Example/Firestore.xcodeproj/project.pbxproj
+++ b/Firestore/Example/Firestore.xcodeproj/project.pbxproj
@@ -111,7 +111,6 @@
DE51B1CE1F0D48CD0013853F /* FSTEventManagerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DE51B1AA1F0D48AC0013853F /* FSTEventManagerTests.m */; };
DE51B1CF1F0D48CD0013853F /* FSTQueryListenerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DE51B1AB1F0D48AC0013853F /* FSTQueryListenerTests.m */; };
DE51B1D01F0D48CD0013853F /* FSTQueryTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DE51B1AC1F0D48AC0013853F /* FSTQueryTests.m */; };
- DE51B1D11F0D48CD0013853F /* FSTTargetIDGeneratorTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DE51B1AE1F0D48AC0013853F /* FSTTargetIDGeneratorTests.m */; };
DE51B1D21F0D48CD0013853F /* FSTTimestampTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DE51B1AF1F0D48AC0013853F /* FSTTimestampTests.m */; };
DE51B1D31F0D48CD0013853F /* FSTViewSnapshotTest.m in Sources */ = {isa = PBXBuildFile; fileRef = DE51B1B01F0D48AC0013853F /* FSTViewSnapshotTest.m */; };
DE51B1D41F0D48CD0013853F /* FSTViewTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DE51B1B11F0D48AC0013853F /* FSTViewTests.m */; };
@@ -331,7 +330,6 @@
DE51B1AB1F0D48AC0013853F /* FSTQueryListenerTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FSTQueryListenerTests.m; sourceTree = "<group>"; };
DE51B1AC1F0D48AC0013853F /* FSTQueryTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FSTQueryTests.m; sourceTree = "<group>"; };
DE51B1AD1F0D48AC0013853F /* FSTSyncEngine+Testing.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "FSTSyncEngine+Testing.h"; sourceTree = "<group>"; };
- DE51B1AE1F0D48AC0013853F /* FSTTargetIDGeneratorTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FSTTargetIDGeneratorTests.m; sourceTree = "<group>"; };
DE51B1AF1F0D48AC0013853F /* FSTTimestampTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FSTTimestampTests.m; sourceTree = "<group>"; };
DE51B1B01F0D48AC0013853F /* FSTViewSnapshotTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FSTViewSnapshotTest.m; sourceTree = "<group>"; };
DE51B1B11F0D48AC0013853F /* FSTViewTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FSTViewTests.m; sourceTree = "<group>"; };
@@ -718,7 +716,6 @@
DE51B1AA1F0D48AC0013853F /* FSTEventManagerTests.m */,
DE51B1AB1F0D48AC0013853F /* FSTQueryListenerTests.m */,
DE51B1AC1F0D48AC0013853F /* FSTQueryTests.m */,
- DE51B1AE1F0D48AC0013853F /* FSTTargetIDGeneratorTests.m */,
DE51B1AF1F0D48AC0013853F /* FSTTimestampTests.m */,
DE51B1B01F0D48AC0013853F /* FSTViewSnapshotTest.m */,
DE51B1B11F0D48AC0013853F /* FSTViewTests.m */,
@@ -1253,7 +1250,6 @@
DE51B1F41F0D491B0013853F /* FSTRemoteEventTests.m in Sources */,
548DB927200D590300E00ABC /* assert_test.cc in Sources */,
54E928241F33953300C1953E /* FSTEventAccumulator.m in Sources */,
- DE51B1D11F0D48CD0013853F /* FSTTargetIDGeneratorTests.m in Sources */,
5436F32420008FAD006E51E3 /* string_printf_test.cc in Sources */,
DE51B1EF1F0D49140013853F /* FSTDocumentTests.m in Sources */,
DE51B1DC1F0D490D0013853F /* FSTLocalSerializerTests.m in Sources */,
diff --git a/Firestore/Example/Tests/Core/FSTTargetIDGeneratorTests.m b/Firestore/Example/Tests/Core/FSTTargetIDGeneratorTests.m
deleted file mode 100644
index 6f54fd1..0000000
--- a/Firestore/Example/Tests/Core/FSTTargetIDGeneratorTests.m
+++ /dev/null
@@ -1,94 +0,0 @@
-/*
- * Copyright 2017 Google
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#import "Firestore/Source/Core/FSTTargetIDGenerator.h"
-
-#import <XCTest/XCTest.h>
-
-NS_ASSUME_NONNULL_BEGIN
-
-@interface FSTTargetIDGenerator ()
-- (instancetype)initWithGeneratorID:(NSInteger)generatorID startingAfterID:(FSTTargetID)after;
-@end
-
-@interface FSTTargetIDGeneratorTests : XCTestCase
-@end
-
-@implementation FSTTargetIDGeneratorTests
-
-- (void)testConstructor {
- XCTAssertEqual([[[FSTTargetIDGenerator alloc] initWithGeneratorID:0 startingAfterID:0] nextID],
- 2);
- XCTAssertEqual([[[FSTTargetIDGenerator alloc] initWithGeneratorID:1 startingAfterID:0] nextID],
- 1);
-
- XCTAssertEqual([[FSTTargetIDGenerator generatorForLocalStoreStartingAfterID:0] nextID], 2);
- XCTAssertEqual([[FSTTargetIDGenerator generatorForSyncEngineStartingAfterID:0] nextID], 1);
-}
-
-- (void)testSkipPast {
- FSTTargetIDGenerator *gen =
- [[FSTTargetIDGenerator alloc] initWithGeneratorID:1 startingAfterID:-1];
- XCTAssertEqual([gen nextID], 1);
-
- gen = [[FSTTargetIDGenerator alloc] initWithGeneratorID:1 startingAfterID:2];
- XCTAssertEqual([gen nextID], 3);
-
- gen = [[FSTTargetIDGenerator alloc] initWithGeneratorID:1 startingAfterID:4];
- XCTAssertEqual([gen nextID], 5);
-
- for (int i = 4; i < 12; ++i) {
- FSTTargetIDGenerator *gen0 =
- [[FSTTargetIDGenerator alloc] initWithGeneratorID:0 startingAfterID:i];
- FSTTargetIDGenerator *gen1 =
- [[FSTTargetIDGenerator alloc] initWithGeneratorID:1 startingAfterID:i];
- XCTAssertEqual([gen0 nextID], i + 2 & ~1, @"Skip failed for index %d", i);
- XCTAssertEqual([gen1 nextID], i + 1 | 1, @"Skip failed for index %d", i);
- }
-
- gen = [[FSTTargetIDGenerator alloc] initWithGeneratorID:1 startingAfterID:12];
- XCTAssertEqual([gen nextID], 13);
-
- gen = [[FSTTargetIDGenerator alloc] initWithGeneratorID:0 startingAfterID:22];
- XCTAssertEqual([gen nextID], 24);
-}
-
-- (void)testIncrement {
- FSTTargetIDGenerator *gen =
- [[FSTTargetIDGenerator alloc] initWithGeneratorID:0 startingAfterID:0];
- XCTAssertEqual([gen nextID], 2);
- XCTAssertEqual([gen nextID], 4);
- XCTAssertEqual([gen nextID], 6);
- gen = [[FSTTargetIDGenerator alloc] initWithGeneratorID:0 startingAfterID:46];
- XCTAssertEqual([gen nextID], 48);
- XCTAssertEqual([gen nextID], 50);
- XCTAssertEqual([gen nextID], 52);
- XCTAssertEqual([gen nextID], 54);
-
- gen = [[FSTTargetIDGenerator alloc] initWithGeneratorID:1 startingAfterID:0];
- XCTAssertEqual([gen nextID], 1);
- XCTAssertEqual([gen nextID], 3);
- XCTAssertEqual([gen nextID], 5);
- gen = [[FSTTargetIDGenerator alloc] initWithGeneratorID:1 startingAfterID:46];
- XCTAssertEqual([gen nextID], 47);
- XCTAssertEqual([gen nextID], 49);
- XCTAssertEqual([gen nextID], 51);
- XCTAssertEqual([gen nextID], 53);
-}
-
-@end
-
-NS_ASSUME_NONNULL_END
diff --git a/Firestore/Source/Core/FSTSyncEngine.m b/Firestore/Source/Core/FSTSyncEngine.mm
index f90c5dd..d82cc99 100644
--- a/Firestore/Source/Core/FSTSyncEngine.m
+++ b/Firestore/Source/Core/FSTSyncEngine.mm
@@ -22,7 +22,6 @@
#import "Firestore/Source/Auth/FSTUser.h"
#import "Firestore/Source/Core/FSTQuery.h"
#import "Firestore/Source/Core/FSTSnapshotVersion.h"
-#import "Firestore/Source/Core/FSTTargetIDGenerator.h"
#import "Firestore/Source/Core/FSTTransaction.h"
#import "Firestore/Source/Core/FSTView.h"
#import "Firestore/Source/Core/FSTViewSnapshot.h"
@@ -41,6 +40,8 @@
#import "Firestore/Source/Util/FSTDispatchQueue.h"
#import "Firestore/Source/Util/FSTLogger.h"
+#include "Firestore/core/src/firebase/firestore/core/target_id_generator.h"
+
NS_ASSUME_NONNULL_BEGIN
// Limbo documents don't use persistence, and are eagerly GC'd. So, listens for them don't need
@@ -141,14 +142,14 @@ static const FSTListenSequenceNumber kIrrelevantSequenceNumber = -1;
NSMutableDictionary<FSTUser *, NSMutableDictionary<NSNumber *, FSTVoidErrorBlock> *>
*mutationCompletionBlocks;
-/** Used for creating the FSTTargetIDs for the listens used to resolve limbo documents. */
-@property(nonatomic, strong, readonly) FSTTargetIDGenerator *targetIdGenerator;
-
@property(nonatomic, strong) FSTUser *currentUser;
@end
-@implementation FSTSyncEngine
+@implementation FSTSyncEngine {
+ /** Used for creating the FSTTargetIDs for the listens used to resolve limbo documents. */
+ firebase::firestore::core::TargetIdGenerator _targetIdGenerator;
+}
- (instancetype)initWithLocalStore:(FSTLocalStore *)localStore
remoteStore:(FSTRemoteStore *)remoteStore
@@ -167,7 +168,8 @@ static const FSTListenSequenceNumber kIrrelevantSequenceNumber = -1;
[_limboCollector addGarbageSource:_limboDocumentRefs];
_mutationCompletionBlocks = [NSMutableDictionary dictionary];
- _targetIdGenerator = [FSTTargetIDGenerator generatorForSyncEngineStartingAfterID:0];
+ _targetIdGenerator =
+ firebase::firestore::core::TargetIdGenerator::SyncEngineTargetIdGenerator(0);
_currentUser = initialUser;
}
return self;
@@ -490,7 +492,7 @@ static const FSTListenSequenceNumber kIrrelevantSequenceNumber = -1;
if (!self.limboTargetsByKey[key]) {
FSTLog(@"New document in limbo: %@", key);
- FSTTargetID limboTargetID = [self.targetIdGenerator nextID];
+ FSTTargetID limboTargetID = _targetIdGenerator.NextId();
FSTQuery *query = [FSTQuery queryWithPath:key.path];
FSTQueryData *queryData = [[FSTQueryData alloc] initWithQuery:query
targetID:limboTargetID
diff --git a/Firestore/Source/Core/FSTTargetIDGenerator.h b/Firestore/Source/Core/FSTTargetIDGenerator.h
deleted file mode 100644
index 0b230ae..0000000
--- a/Firestore/Source/Core/FSTTargetIDGenerator.h
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * Copyright 2017 Google
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#import <Foundation/Foundation.h>
-
-#import "Firestore/Source/Core/FSTTypes.h"
-
-NS_ASSUME_NONNULL_BEGIN
-
-/**
- * FSTTargetIDGenerator generates monotonically increasing integer IDs. There are separate
- * generators for different scopes. While these generators will operate independently of each
- * other, they are scoped, such that no two generators will ever produce the same ID. This is
- * useful, because sometimes the backend may group IDs from separate parts of the client into the
- * same ID space.
- */
-@interface FSTTargetIDGenerator : NSObject
-
-/**
- * Creates and returns the FSTTargetIDGenerator for the local store.
- *
- * @param after An ID to start at. Every call to nextID will return an ID > @a after.
- * @return A shared instance of FSTTargetIDGenerator.
- */
-+ (instancetype)generatorForLocalStoreStartingAfterID:(FSTTargetID)after;
-
-/**
- * Creates and returns the FSTTargetIDGenerator for the sync engine.
- *
- * @param after An ID to start at. Every call to nextID will return an ID > @a after.
- * @return A shared instance of FSTTargetIDGenerator.
- */
-+ (instancetype)generatorForSyncEngineStartingAfterID:(FSTTargetID)after;
-
-- (id)init __attribute__((unavailable("Use a static constructor method.")));
-
-/** Returns the next ID in the sequence. */
-- (FSTTargetID)nextID;
-
-@end
-
-NS_ASSUME_NONNULL_END
diff --git a/Firestore/Source/Core/FSTTargetIDGenerator.m b/Firestore/Source/Core/FSTTargetIDGenerator.m
deleted file mode 100644
index 58092ec..0000000
--- a/Firestore/Source/Core/FSTTargetIDGenerator.m
+++ /dev/null
@@ -1,105 +0,0 @@
-/*
- * Copyright 2017 Google
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#import "Firestore/Source/Core/FSTTargetIDGenerator.h"
-
-#import <libkern/OSAtomic.h>
-
-NS_ASSUME_NONNULL_BEGIN
-
-#pragma mark - FSTTargetIDGenerator
-
-static const int kReservedBits = 1;
-
-/** FSTTargetIDGeneratorID is the set of all valid generators. */
-typedef NS_ENUM(NSInteger, FSTTargetIDGeneratorID) {
- FSTTargetIDGeneratorIDLocalStore = 0,
- FSTTargetIDGeneratorIDSyncEngine = 1
-};
-
-@interface FSTTargetIDGenerator () {
- // This is volatile so it can be used with OSAtomicAdd32.
- volatile FSTTargetID _previousID;
-}
-
-/**
- * Initializes the generator.
- *
- * @param generatorID A unique ID indicating which generator this is.
- * @param after Every call to nextID will return a number > @a after.
- */
-- (instancetype)initWithGeneratorID:(FSTTargetIDGeneratorID)generatorID
- startingAfterID:(FSTTargetID)after NS_DESIGNATED_INITIALIZER;
-
-// This is typed as FSTTargetID because we need to do bitwise operations with them together.
-@property(nonatomic, assign) FSTTargetID generatorID;
-@end
-
-@implementation FSTTargetIDGenerator
-
-#pragma mark - Constructors
-
-- (instancetype)initWithGeneratorID:(FSTTargetIDGeneratorID)generatorID
- startingAfterID:(FSTTargetID)after {
- self = [super init];
- if (self) {
- _generatorID = generatorID;
-
- // Replace the generator part of |after| with this generator's ID.
- FSTTargetID afterWithoutGenerator = (after >> kReservedBits) << kReservedBits;
- FSTTargetID afterGenerator = after - afterWithoutGenerator;
- if (afterGenerator >= _generatorID) {
- // For example, if:
- // self.generatorID = 0b0000
- // after = 0b1011
- // afterGenerator = 0b0001
- // Then:
- // previous = 0b1010
- // next = 0b1100
- _previousID = afterWithoutGenerator | self.generatorID;
- } else {
- // For example, if:
- // self.generatorID = 0b0001
- // after = 0b1010
- // afterGenerator = 0b0000
- // Then:
- // previous = 0b1001
- // next = 0b1011
- _previousID = (afterWithoutGenerator | self.generatorID) - (1 << kReservedBits);
- }
- }
- return self;
-}
-
-+ (instancetype)generatorForLocalStoreStartingAfterID:(FSTTargetID)after {
- return [[FSTTargetIDGenerator alloc] initWithGeneratorID:FSTTargetIDGeneratorIDLocalStore
- startingAfterID:after];
-}
-
-+ (instancetype)generatorForSyncEngineStartingAfterID:(FSTTargetID)after {
- return [[FSTTargetIDGenerator alloc] initWithGeneratorID:FSTTargetIDGeneratorIDSyncEngine
- startingAfterID:after];
-}
-
-#pragma mark - Public methods
-
-- (FSTTargetID)nextID {
- return OSAtomicAdd32(1 << kReservedBits, &_previousID);
-}
-
-@end
-
-NS_ASSUME_NONNULL_END
diff --git a/Firestore/Source/Local/FSTLocalStore.m b/Firestore/Source/Local/FSTLocalStore.mm
index 3a5b0b4..fa77e37 100644
--- a/Firestore/Source/Local/FSTLocalStore.m
+++ b/Firestore/Source/Local/FSTLocalStore.mm
@@ -20,7 +20,6 @@
#import "Firestore/Source/Core/FSTListenSequence.h"
#import "Firestore/Source/Core/FSTQuery.h"
#import "Firestore/Source/Core/FSTSnapshotVersion.h"
-#import "Firestore/Source/Core/FSTTargetIDGenerator.h"
#import "Firestore/Source/Core/FSTTimestamp.h"
#import "Firestore/Source/Local/FSTGarbageCollector.h"
#import "Firestore/Source/Local/FSTLocalDocumentsView.h"
@@ -42,6 +41,8 @@
#import "Firestore/Source/Util/FSTAssert.h"
#import "Firestore/Source/Util/FSTLogger.h"
+#include "Firestore/core/src/firebase/firestore/core/target_id_generator.h"
+
NS_ASSUME_NONNULL_BEGIN
@interface FSTLocalStore ()
@@ -74,9 +75,6 @@ NS_ASSUME_NONNULL_BEGIN
/** Maps a targetID to data about its query. */
@property(nonatomic, strong) NSMutableDictionary<NSNumber *, FSTQueryData *> *targetIDs;
-/** Used to generate targetIDs for queries tracked locally. */
-@property(nonatomic, strong) FSTTargetIDGenerator *targetIDGenerator;
-
@property(nonatomic, strong) FSTListenSequence *listenSequence;
/**
@@ -92,7 +90,10 @@ NS_ASSUME_NONNULL_BEGIN
@end
-@implementation FSTLocalStore
+@implementation FSTLocalStore {
+ /** Used to generate targetIDs for queries tracked locally. */
+ firebase::firestore::core::TargetIdGenerator _targetIDGenerator;
+}
- (instancetype)initWithPersistence:(id<FSTPersistence>)persistence
garbageCollector:(id<FSTGarbageCollector>)garbageCollector
@@ -113,6 +114,9 @@ NS_ASSUME_NONNULL_BEGIN
_targetIDs = [NSMutableDictionary dictionary];
_heldBatchResults = [NSMutableArray array];
+
+ _targetIDGenerator =
+ firebase::firestore::core::TargetIdGenerator::LocalStoreTargetIdGenerator(0);
}
return self;
}
@@ -150,7 +154,8 @@ NS_ASSUME_NONNULL_BEGIN
[self.queryCache start];
FSTTargetID targetID = [self.queryCache highestTargetID];
- self.targetIDGenerator = [FSTTargetIDGenerator generatorForLocalStoreStartingAfterID:targetID];
+ _targetIDGenerator =
+ firebase::firestore::core::TargetIdGenerator::LocalStoreTargetIdGenerator(targetID);
FSTListenSequenceNumber sequenceNumber = [self.queryCache highestListenSequenceNumber];
self.listenSequence = [[FSTListenSequence alloc] initStartingAfter:sequenceNumber];
}
@@ -393,7 +398,7 @@ NS_ASSUME_NONNULL_BEGIN
} else {
FSTWriteGroup *group = [self.persistence startGroupWithAction:@"Allocate query"];
- targetID = [self.targetIDGenerator nextID];
+ targetID = _targetIDGenerator.NextId();
cached = [[FSTQueryData alloc] initWithQuery:query
targetID:targetID
listenSequenceNumber:sequenceNumber
diff --git a/Firestore/core/src/firebase/firestore/core/target_id_generator.h b/Firestore/core/src/firebase/firestore/core/target_id_generator.h
index 345f141..7d30cf9 100644
--- a/Firestore/core/src/firebase/firestore/core/target_id_generator.h
+++ b/Firestore/core/src/firebase/firestore/core/target_id_generator.h
@@ -37,6 +37,9 @@ enum class TargetIdGeneratorId { LocalStore = 0, SyncEngine = 1 };
*/
class TargetIdGenerator {
public:
+ // Makes Objective-C++ code happy to provide a default ctor.
+ TargetIdGenerator() = default;
+
TargetIdGenerator(const TargetIdGenerator& value);
/**