diff options
author | zxu <zxu@google.com> | 2018-01-25 18:39:46 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-25 18:39:46 -0500 |
commit | 7226b4adf38e4732dfb9a840d25f86d3e5533bdb (patch) | |
tree | 01c7d79608eae5221deca452f8e461f2758b1ef0 /Firestore/Source/Core | |
parent | 53064743963d7e5cc12f7a42bb036814f5a6df17 (diff) |
port TargetIdGenerator to iOS (#709)
* port TargetIdGenerator to iOS
* fix style
* move pointer property to instance variable
* TriggerTravis
Diffstat (limited to 'Firestore/Source/Core')
-rw-r--r-- | Firestore/Source/Core/FSTSyncEngine.mm (renamed from Firestore/Source/Core/FSTSyncEngine.m) | 16 | ||||
-rw-r--r-- | Firestore/Source/Core/FSTTargetIDGenerator.h | 55 | ||||
-rw-r--r-- | Firestore/Source/Core/FSTTargetIDGenerator.m | 105 |
3 files changed, 9 insertions, 167 deletions
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 |