From b3050e42d842a4aef122efd277481c46bfa88dff Mon Sep 17 00:00:00 2001 From: zxu Date: Sat, 24 Mar 2018 11:06:32 -0400 Subject: port C++ `DocumentKey` to `Remote/*` (#965) * port C++ DocumentKey to Remote's * port C++ DocumentKey to Remote's * address changes * address changes * address changes --- Firestore/Source/Remote/FSTDatastore.h | 6 ++- Firestore/Source/Remote/FSTDatastore.mm | 29 +++++++++---- Firestore/Source/Remote/FSTRemoteEvent.h | 10 +++-- Firestore/Source/Remote/FSTRemoteEvent.mm | 64 ++++++++++++++-------------- Firestore/Source/Remote/FSTRemoteStore.mm | 5 ++- Firestore/Source/Remote/FSTSerializerBeta.h | 9 ++-- Firestore/Source/Remote/FSTSerializerBeta.mm | 32 +++++++------- Firestore/Source/Remote/FSTWatchChange.h | 11 ++--- Firestore/Source/Remote/FSTWatchChange.mm | 21 ++++++--- 9 files changed, 107 insertions(+), 80 deletions(-) (limited to 'Firestore/Source/Remote') diff --git a/Firestore/Source/Remote/FSTDatastore.h b/Firestore/Source/Remote/FSTDatastore.h index 7b8274c..b3ba46c 100644 --- a/Firestore/Source/Remote/FSTDatastore.h +++ b/Firestore/Source/Remote/FSTDatastore.h @@ -16,14 +16,16 @@ #import +#include + #import "Firestore/Source/Core/FSTTypes.h" #include "Firestore/core/src/firebase/firestore/auth/credentials_provider.h" #include "Firestore/core/src/firebase/firestore/core/database_info.h" #include "Firestore/core/src/firebase/firestore/model/database_id.h" +#include "Firestore/core/src/firebase/firestore/model/document_key.h" #include "absl/strings/string_view.h" -@class FSTDocumentKey; @class FSTDispatchQueue; @class FSTMutation; @class FSTMutationResult; @@ -88,7 +90,7 @@ NS_ASSUME_NONNULL_BEGIN token:(const absl::string_view)token; /** Looks up a list of documents in datastore. */ -- (void)lookupDocuments:(NSArray *)keys +- (void)lookupDocuments:(const std::vector &)keys completion:(FSTVoidMaybeDocumentArrayErrorBlock)completion; /** Commits data to datastore. */ diff --git a/Firestore/Source/Remote/FSTDatastore.mm b/Firestore/Source/Remote/FSTDatastore.mm index 63499df..501237f 100644 --- a/Firestore/Source/Remote/FSTDatastore.mm +++ b/Firestore/Source/Remote/FSTDatastore.mm @@ -16,6 +16,9 @@ #import "Firestore/Source/Remote/FSTDatastore.h" +#include +#include + #import #import @@ -24,7 +27,6 @@ #import "Firestore/Source/API/FIRFirestoreVersion.h" #import "Firestore/Source/Local/FSTLocalStore.h" #import "Firestore/Source/Model/FSTDocument.h" -#import "Firestore/Source/Model/FSTDocumentKey.h" #import "Firestore/Source/Model/FSTMutation.h" #import "Firestore/Source/Remote/FSTSerializerBeta.h" #import "Firestore/Source/Remote/FSTStream.h" @@ -38,6 +40,7 @@ #include "Firestore/core/src/firebase/firestore/auth/token.h" #include "Firestore/core/src/firebase/firestore/core/database_info.h" #include "Firestore/core/src/firebase/firestore/model/database_id.h" +#include "Firestore/core/src/firebase/firestore/model/document_key.h" #include "Firestore/core/src/firebase/firestore/util/error_apple.h" #include "Firestore/core/src/firebase/firestore/util/string_apple.h" @@ -46,6 +49,7 @@ using firebase::firestore::auth::CredentialsProvider; using firebase::firestore::auth::Token; using firebase::firestore::core::DatabaseInfo; using firebase::firestore::model::DatabaseId; +using firebase::firestore::model::DocumentKey; NS_ASSUME_NONNULL_BEGIN @@ -245,17 +249,21 @@ typedef GRPCProtoCall * (^RPCFactory)(void); [self invokeRPCWithFactory:rpcFactory errorHandler:completion]; } -- (void)lookupDocuments:(NSArray *)keys +- (void)lookupDocuments:(const std::vector &)keys completion:(FSTVoidMaybeDocumentArrayErrorBlock)completion { GCFSBatchGetDocumentsRequest *request = [GCFSBatchGetDocumentsRequest message]; request.database = [self.serializer encodedDatabaseID]; - for (FSTDocumentKey *key in keys) { + for (const DocumentKey &key : keys) { [request.documentsArray addObject:[self.serializer encodedDocumentKey:key]]; } - __block FSTMaybeDocumentDictionary *results = - [FSTMaybeDocumentDictionary maybeDocumentDictionary]; + struct Closure { + std::vector keys; + FSTMaybeDocumentDictionary *results; + }; + __block std::shared_ptr closure = std::make_shared( + Closure{keys, [FSTMaybeDocumentDictionary maybeDocumentDictionary]}); RPCFactory rpcFactory = ^GRPCProtoCall * { __block GRPCProtoCall *rpc = [self.service RPCToBatchGetDocumentsWithRequest:request @@ -275,16 +283,19 @@ typedef GRPCProtoCall * (^RPCFactory)(void); // Streaming response, accumulate result FSTMaybeDocument *doc = [self.serializer decodedMaybeDocumentFromBatch:response]; - results = [results dictionaryBySettingObject:doc forKey:doc.key]; + closure->results = + [closure->results dictionaryBySettingObject:doc + forKey:doc.key]; } else { // Streaming response is done, call completion FSTLog(@"RPC BatchGetDocuments completed successfully."); [FSTDatastore logHeadersForRPC:rpc RPCName:@"BatchGetDocuments"]; FSTAssert(!response, @"Got response after done."); NSMutableArray *docs = - [NSMutableArray arrayWithCapacity:keys.count]; - for (FSTDocumentKey *key in keys) { - [docs addObject:results[key]]; + [NSMutableArray arrayWithCapacity:closure->keys.size()]; + for (const DocumentKey &key : closure->keys) { + [docs addObject:closure->results[static_cast( + key)]]; } completion(docs, nil); } diff --git a/Firestore/Source/Remote/FSTRemoteEvent.h b/Firestore/Source/Remote/FSTRemoteEvent.h index 72f71a5..dd45117 100644 --- a/Firestore/Source/Remote/FSTRemoteEvent.h +++ b/Firestore/Source/Remote/FSTRemoteEvent.h @@ -16,12 +16,15 @@ #import +#include + #import "Firestore/Source/Core/FSTTypes.h" #import "Firestore/Source/Model/FSTDocumentDictionary.h" #import "Firestore/Source/Model/FSTDocumentKeySet.h" +#include "Firestore/core/src/firebase/firestore/model/document_key.h" + @class FSTDocument; -@class FSTDocumentKey; @class FSTExistenceFilter; @class FSTMaybeDocument; @class FSTSnapshotVersion; @@ -148,7 +151,7 @@ typedef NS_ENUM(NSUInteger, FSTCurrentStatusUpdate) { eventWithSnapshotVersion:(FSTSnapshotVersion *)snapshotVersion targetChanges:(NSMutableDictionary *)targetChanges documentUpdates: - (NSMutableDictionary *)documentUpdates; + (std::map)documentUpdates; /** The snapshot version this event brings us up to. */ @property(nonatomic, strong, readonly) FSTSnapshotVersion *snapshotVersion; @@ -161,8 +164,7 @@ eventWithSnapshotVersion:(FSTSnapshotVersion *)snapshotVersion * A set of which documents have changed or been deleted, along with the doc's new values * (if not deleted). */ -@property(nonatomic, strong, readonly) - NSDictionary *documentUpdates; +- (const std::map &)documentUpdates; /** Adds a document update to this remote event */ - (void)addDocumentUpdate:(FSTMaybeDocument *)document; diff --git a/Firestore/Source/Remote/FSTRemoteEvent.mm b/Firestore/Source/Remote/FSTRemoteEvent.mm index cd073b3..a3c85df 100644 --- a/Firestore/Source/Remote/FSTRemoteEvent.mm +++ b/Firestore/Source/Remote/FSTRemoteEvent.mm @@ -16,9 +16,11 @@ #import "Firestore/Source/Remote/FSTRemoteEvent.h" +#include +#include + #import "Firestore/Source/Core/FSTSnapshotVersion.h" #import "Firestore/Source/Model/FSTDocument.h" -#import "Firestore/Source/Model/FSTDocumentKey.h" #import "Firestore/Source/Remote/FSTWatchChange.h" #import "Firestore/Source/Util/FSTAssert.h" #import "Firestore/Source/Util/FSTClasses.h" @@ -26,6 +28,8 @@ #include "Firestore/core/src/firebase/firestore/model/document_key.h" +using firebase::firestore::model::DocumentKey; + NS_ASSUME_NONNULL_BEGIN #pragma mark - FSTTargetMapping @@ -33,20 +37,20 @@ NS_ASSUME_NONNULL_BEGIN @interface FSTTargetMapping () /** Private mutator method to add a document key to the mapping */ -- (void)addDocumentKey:(FSTDocumentKey *)documentKey; +- (void)addDocumentKey:(const DocumentKey &)documentKey; /** Private mutator method to remove a document key from the mapping */ -- (void)removeDocumentKey:(FSTDocumentKey *)documentKey; +- (void)removeDocumentKey:(const DocumentKey &)documentKey; @end @implementation FSTTargetMapping -- (void)addDocumentKey:(FSTDocumentKey *)documentKey { +- (void)addDocumentKey:(const DocumentKey &)documentKey { @throw FSTAbstractMethodException(); // NOLINT } -- (void)removeDocumentKey:(FSTDocumentKey *)documentKey { +- (void)removeDocumentKey:(const DocumentKey &)documentKey { @throw FSTAbstractMethodException(); // NOLINT } @@ -92,11 +96,11 @@ NS_ASSUME_NONNULL_BEGIN return self.documents.hash; } -- (void)addDocumentKey:(FSTDocumentKey *)documentKey { +- (void)addDocumentKey:(const DocumentKey &)documentKey { self.documents = [self.documents setByAddingObject:documentKey]; } -- (void)removeDocumentKey:(FSTDocumentKey *)documentKey { +- (void)removeDocumentKey:(const DocumentKey &)documentKey { self.documents = [self.documents setByRemovingObject:documentKey]; } @@ -160,12 +164,12 @@ NS_ASSUME_NONNULL_BEGIN return result; } -- (void)addDocumentKey:(FSTDocumentKey *)documentKey { +- (void)addDocumentKey:(const DocumentKey &)documentKey { self.addedDocuments = [self.addedDocuments setByAddingObject:documentKey]; self.removedDocuments = [self.removedDocuments setByRemovingObject:documentKey]; } -- (void)removeDocumentKey:(FSTDocumentKey *)documentKey { +- (void)removeDocumentKey:(const DocumentKey &)documentKey { self.addedDocuments = [self.addedDocuments setByRemovingObject:documentKey]; self.removedDocuments = [self.removedDocuments setByAddingObject:documentKey]; } @@ -240,42 +244,39 @@ NS_ASSUME_NONNULL_BEGIN #pragma mark - FSTRemoteEvent @interface FSTRemoteEvent () { - NSMutableDictionary *_documentUpdates; NSMutableDictionary *_targetChanges; } - (instancetype) initWithSnapshotVersion:(FSTSnapshotVersion *)snapshotVersion targetChanges:(NSMutableDictionary *)targetChanges - documentUpdates: - (NSMutableDictionary *)documentUpdates; + documentUpdates:(std::map)documentUpdates; @property(nonatomic, strong) FSTSnapshotVersion *snapshotVersion; @end -@implementation FSTRemoteEvent - +@implementation FSTRemoteEvent { + std::map _documentUpdates; +} + (instancetype) eventWithSnapshotVersion:(FSTSnapshotVersion *)snapshotVersion targetChanges:(NSMutableDictionary *)targetChanges - documentUpdates: - (NSMutableDictionary *)documentUpdates { + documentUpdates:(std::map)documentUpdates { return [[FSTRemoteEvent alloc] initWithSnapshotVersion:snapshotVersion targetChanges:targetChanges - documentUpdates:documentUpdates]; + documentUpdates:std::move(documentUpdates)]; } -- (instancetype) -initWithSnapshotVersion:(FSTSnapshotVersion *)snapshotVersion - targetChanges:(NSMutableDictionary *)targetChanges - documentUpdates: - (NSMutableDictionary *)documentUpdates { +- (instancetype)initWithSnapshotVersion:(FSTSnapshotVersion *)snapshotVersion + targetChanges: + (NSMutableDictionary *)targetChanges + documentUpdates:(std::map)documentUpdates { self = [super init]; if (self) { _snapshotVersion = snapshotVersion; _targetChanges = targetChanges; - _documentUpdates = documentUpdates; + _documentUpdates = std::move(documentUpdates); } return self; } @@ -284,13 +285,13 @@ initWithSnapshotVersion:(FSTSnapshotVersion *)snapshotVersion return static_cast *>(_targetChanges); } -- (NSDictionary *)documentUpdates { - return static_cast *>(_documentUpdates); +- (const std::map &)documentUpdates { + return _documentUpdates; } /** Adds a document update to this remote event */ - (void)addDocumentUpdate:(FSTMaybeDocument *)document { - _documentUpdates[(FSTDocumentKey *)document.key] = document; + _documentUpdates[document.key] = document; } /** Handles an existence filter mismatch */ @@ -326,10 +327,6 @@ initWithSnapshotVersion:(FSTSnapshotVersion *)snapshotVersion @property(nonatomic, strong, readonly) NSMutableDictionary *targetChanges; -/** Keeps track of document to update */ -@property(nonatomic, strong, readonly) - NSMutableDictionary *documentUpdates; - /** The set of open listens on the client */ @property(nonatomic, strong, readonly) NSDictionary *listenTargets; @@ -341,6 +338,8 @@ initWithSnapshotVersion:(FSTSnapshotVersion *)snapshotVersion @implementation FSTWatchChangeAggregator { NSMutableDictionary *_existenceFilters; + /** Keeps track of document to update */ + std::map _documentUpdates; } - (instancetype) @@ -357,7 +356,6 @@ initWithSnapshotVersion:(FSTSnapshotVersion *)snapshotVersion _pendingTargetResponses = [NSMutableDictionary dictionaryWithDictionary:pendingTargetResponses]; _existenceFilters = [NSMutableDictionary dictionary]; - _documentUpdates = [NSMutableDictionary dictionary]; } return self; } @@ -418,7 +416,7 @@ initWithSnapshotVersion:(FSTSnapshotVersion *)snapshotVersion // Only update the document if there is a new document to replace, this might be just a target // update instead. if (docChange.document && relevant) { - self.documentUpdates[docChange.documentKey] = docChange.document; + _documentUpdates[docChange.documentKey] = docChange.document; } } @@ -522,7 +520,7 @@ initWithSnapshotVersion:(FSTSnapshotVersion *)snapshotVersion self.frozen = YES; return [FSTRemoteEvent eventWithSnapshotVersion:self.snapshotVersion targetChanges:targetChanges - documentUpdates:self.documentUpdates]; + documentUpdates:_documentUpdates]; } @end diff --git a/Firestore/Source/Remote/FSTRemoteStore.mm b/Firestore/Source/Remote/FSTRemoteStore.mm index f484278..28ee594 100644 --- a/Firestore/Source/Remote/FSTRemoteStore.mm +++ b/Firestore/Source/Remote/FSTRemoteStore.mm @@ -24,7 +24,6 @@ #import "Firestore/Source/Local/FSTLocalStore.h" #import "Firestore/Source/Local/FSTQueryData.h" #import "Firestore/Source/Model/FSTDocument.h" -#import "Firestore/Source/Model/FSTDocumentKey.h" #import "Firestore/Source/Model/FSTMutation.h" #import "Firestore/Source/Model/FSTMutationBatch.h" #import "Firestore/Source/Remote/FSTDatastore.h" @@ -37,10 +36,12 @@ #import "Firestore/Source/Util/FSTLogger.h" #include "Firestore/core/src/firebase/firestore/auth/user.h" +#include "Firestore/core/src/firebase/firestore/model/document_key.h" #include "Firestore/core/src/firebase/firestore/util/string_apple.h" namespace util = firebase::firestore::util; using firebase::firestore::auth::User; +using firebase::firestore::model::DocumentKey; NS_ASSUME_NONNULL_BEGIN @@ -382,7 +383,7 @@ static const int kMaxPendingWrites = 10; // updates. Without applying a deleted document there might be another query that will // raise this document as part of a snapshot until it is resolved, essentially exposing // inconsistency between queries - FSTDocumentKey *key = [FSTDocumentKey keyWithPath:query.path]; + const DocumentKey key{query.path}; FSTDeletedDocument *deletedDoc = [FSTDeletedDocument documentWithKey:key version:snapshotVersion]; [remoteEvent addDocumentUpdate:deletedDoc]; diff --git a/Firestore/Source/Remote/FSTSerializerBeta.h b/Firestore/Source/Remote/FSTSerializerBeta.h index 0f1c3ae..d96dbeb 100644 --- a/Firestore/Source/Remote/FSTSerializerBeta.h +++ b/Firestore/Source/Remote/FSTSerializerBeta.h @@ -17,8 +17,8 @@ #import #include "Firestore/core/src/firebase/firestore/model/database_id.h" +#include "Firestore/core/src/firebase/firestore/model/document_key.h" -@class FSTDocumentKey; @class FSTFieldValue; @class FSTMaybeDocument; @class FSTMutation; @@ -70,8 +70,8 @@ NS_ASSUME_NONNULL_BEGIN /** Returns the database ID, such as `projects/{project id}/databases/{database_id}`. */ - (NSString *)encodedDatabaseID; -- (NSString *)encodedDocumentKey:(FSTDocumentKey *)key; -- (FSTDocumentKey *)decodedDocumentKey:(NSString *)key; +- (NSString *)encodedDocumentKey:(const firebase::firestore::model::DocumentKey &)key; +- (firebase::firestore::model::DocumentKey)decodedDocumentKey:(NSString *)key; - (GCFSValue *)encodedFieldValue:(FSTFieldValue *)fieldValue; - (FSTFieldValue *)decodedFieldValue:(GCFSValue *)valueProto; @@ -95,7 +95,8 @@ NS_ASSUME_NONNULL_BEGIN - (FSTWatchChange *)decodedWatchChange:(GCFSListenResponse *)watchChange; - (FSTSnapshotVersion *)versionFromListenResponse:(GCFSListenResponse *)watchChange; -- (GCFSDocument *)encodedDocumentWithFields:(FSTObjectValue *)objectValue key:(FSTDocumentKey *)key; +- (GCFSDocument *)encodedDocumentWithFields:(FSTObjectValue *)objectValue + key:(const firebase::firestore::model::DocumentKey &)key; /** * Encodes an FSTObjectValue into a dictionary. diff --git a/Firestore/Source/Remote/FSTSerializerBeta.mm b/Firestore/Source/Remote/FSTSerializerBeta.mm index 77d2ec7..9531ddc 100644 --- a/Firestore/Source/Remote/FSTSerializerBeta.mm +++ b/Firestore/Source/Remote/FSTSerializerBeta.mm @@ -35,7 +35,6 @@ #import "Firestore/Source/Core/FSTSnapshotVersion.h" #import "Firestore/Source/Local/FSTQueryData.h" #import "Firestore/Source/Model/FSTDocument.h" -#import "Firestore/Source/Model/FSTDocumentKey.h" #import "Firestore/Source/Model/FSTFieldValue.h" #import "Firestore/Source/Model/FSTMutation.h" #import "Firestore/Source/Model/FSTMutationBatch.h" @@ -44,12 +43,14 @@ #import "Firestore/Source/Util/FSTAssert.h" #include "Firestore/core/src/firebase/firestore/model/database_id.h" +#include "Firestore/core/src/firebase/firestore/model/document_key.h" #include "Firestore/core/src/firebase/firestore/model/field_path.h" #include "Firestore/core/src/firebase/firestore/model/resource_path.h" #include "Firestore/core/src/firebase/firestore/util/string_apple.h" namespace util = firebase::firestore::util; using firebase::firestore::model::DatabaseId; +using firebase::firestore::model::DocumentKey; using firebase::firestore::model::FieldPath; using firebase::firestore::model::ResourcePath; @@ -104,19 +105,19 @@ NS_ASSUME_NONNULL_BEGIN return [[FIRGeoPoint alloc] initWithLatitude:latLng.latitude longitude:latLng.longitude]; } -#pragma mark - FSTDocumentKey <=> Key proto +#pragma mark - DocumentKey <=> Key proto -- (NSString *)encodedDocumentKey:(FSTDocumentKey *)key { - return [self encodedResourcePathForDatabaseID:self.databaseID path:key.path]; +- (NSString *)encodedDocumentKey:(const DocumentKey &)key { + return [self encodedResourcePathForDatabaseID:self.databaseID path:key.path()]; } -- (FSTDocumentKey *)decodedDocumentKey:(NSString *)name { +- (DocumentKey)decodedDocumentKey:(NSString *)name { const ResourcePath path = [self decodedResourcePathWithDatabaseID:name]; FSTAssert(path[1] == self.databaseID->project_id(), @"Tried to deserialize key from different project."); FSTAssert(path[3] == self.databaseID->database_id(), @"Tried to deserialize key from different datbase."); - return [FSTDocumentKey keyWithPath:[self localResourcePathForQualifiedResourcePath:path]]; + return DocumentKey{[self localResourcePathForQualifiedResourcePath:path]}; } - (NSString *)encodedResourcePathForDatabaseID:(const DatabaseId *)databaseID @@ -306,12 +307,12 @@ NS_ASSUME_NONNULL_BEGIN } - (GCFSValue *)encodedReferenceValueForDatabaseID:(const DatabaseId *)databaseID - key:(FSTDocumentKey *)key { + key:(const DocumentKey &)key { FSTAssert(*databaseID == *self.databaseID, @"Database %s:%s cannot encode reference from %s:%s", self.databaseID->project_id().c_str(), self.databaseID->database_id().c_str(), databaseID->project_id().c_str(), databaseID->database_id().c_str()); GCFSValue *result = [GCFSValue message]; - result.referenceValue = [self encodedResourcePathForDatabaseID:databaseID path:key.path]; + result.referenceValue = [self encodedResourcePathForDatabaseID:databaseID path:key.path()]; return result; } @@ -319,8 +320,7 @@ NS_ASSUME_NONNULL_BEGIN const ResourcePath path = [self decodedResourcePathWithDatabaseID:resourceName]; const std::string &project = path[1]; const std::string &database = path[3]; - FSTDocumentKey *key = - [FSTDocumentKey keyWithPath:[self localResourcePathForQualifiedResourcePath:path]]; + const DocumentKey key{[self localResourcePathForQualifiedResourcePath:path]}; const DatabaseId database_id(project, database); FSTAssert(database_id == *self.databaseID, @"Database %s:%s cannot encode reference from %s:%s", @@ -390,7 +390,7 @@ NS_ASSUME_NONNULL_BEGIN #pragma mark - FSTObjectValue <=> Document proto - (GCFSDocument *)encodedDocumentWithFields:(FSTObjectValue *)objectValue - key:(FSTDocumentKey *)key { + key:(const DocumentKey &)key { GCFSDocument *proto = [GCFSDocument message]; proto.name = [self encodedDocumentKey:key]; proto.fields = [self encodedFields:objectValue]; @@ -412,7 +412,7 @@ NS_ASSUME_NONNULL_BEGIN - (FSTDocument *)decodedFoundDocument:(GCFSBatchGetDocumentsResponse *)response { FSTAssert(!!response.found, @"Tried to deserialize a found document from a deleted document."); - FSTDocumentKey *key = [self decodedDocumentKey:response.found.name]; + const DocumentKey key = [self decodedDocumentKey:response.found.name]; FSTObjectValue *value = [self decodedFields:response.found.fields]; FSTSnapshotVersion *version = [self decodedVersion:response.found.updateTime]; FSTAssert(![version isEqual:[FSTSnapshotVersion noVersion]], @@ -423,7 +423,7 @@ NS_ASSUME_NONNULL_BEGIN - (FSTDeletedDocument *)decodedDeletedDocument:(GCFSBatchGetDocumentsResponse *)response { FSTAssert(!!response.missing, @"Tried to deserialize a deleted document from a found document."); - FSTDocumentKey *key = [self decodedDocumentKey:response.missing]; + const DocumentKey key = [self decodedDocumentKey:response.missing]; FSTSnapshotVersion *version = [self decodedVersion:response.readTime]; FSTAssert(![version isEqual:[FSTSnapshotVersion noVersion]], @"Got a no document response with no snapshot version"); @@ -1052,7 +1052,7 @@ NS_ASSUME_NONNULL_BEGIN - (FSTDocumentWatchChange *)decodedDocumentChange:(GCFSDocumentChange *)change { FSTObjectValue *value = [self decodedFields:change.document.fields]; - FSTDocumentKey *key = [self decodedDocumentKey:change.document.name]; + const DocumentKey key = [self decodedDocumentKey:change.document.name]; FSTSnapshotVersion *version = [self decodedVersion:change.document.updateTime]; FSTAssert(![version isEqual:[FSTSnapshotVersion noVersion]], @"Got a document change with no snapshot version"); @@ -1069,7 +1069,7 @@ NS_ASSUME_NONNULL_BEGIN } - (FSTDocumentWatchChange *)decodedDocumentDelete:(GCFSDocumentDelete *)change { - FSTDocumentKey *key = [self decodedDocumentKey:change.document]; + const DocumentKey key = [self decodedDocumentKey:change.document]; // Note that version might be unset in which case we use [FSTSnapshotVersion noVersion] FSTSnapshotVersion *version = [self decodedVersion:change.readTime]; FSTMaybeDocument *document = [FSTDeletedDocument documentWithKey:key version:version]; @@ -1083,7 +1083,7 @@ NS_ASSUME_NONNULL_BEGIN } - (FSTDocumentWatchChange *)decodedDocumentRemove:(GCFSDocumentRemove *)change { - FSTDocumentKey *key = [self decodedDocumentKey:change.document]; + const DocumentKey key = [self decodedDocumentKey:change.document]; NSArray *removedTargetIds = [self decodedIntegerArray:change.removedTargetIdsArray]; return [[FSTDocumentWatchChange alloc] initWithUpdatedTargetIDs:@[] diff --git a/Firestore/Source/Remote/FSTWatchChange.h b/Firestore/Source/Remote/FSTWatchChange.h index 8ce24fa..8f730de 100644 --- a/Firestore/Source/Remote/FSTWatchChange.h +++ b/Firestore/Source/Remote/FSTWatchChange.h @@ -18,7 +18,8 @@ #import "Firestore/Source/Core/FSTTypes.h" -@class FSTDocumentKey; +#include "Firestore/core/src/firebase/firestore/model/document_key.h" + @class FSTExistenceFilter; @class FSTMaybeDocument; @class FSTSnapshotVersion; @@ -41,21 +42,21 @@ NS_ASSUME_NONNULL_BEGIN - (instancetype)initWithUpdatedTargetIDs:(NSArray *)updatedTargetIDs removedTargetIDs:(NSArray *)removedTargetIDs - documentKey:(FSTDocumentKey *)documentKey + documentKey:(firebase::firestore::model::DocumentKey)documentKey document:(nullable FSTMaybeDocument *)document NS_DESIGNATED_INITIALIZER; - (instancetype)init NS_UNAVAILABLE; +/** The key of the document for this change. */ +- (const firebase::firestore::model::DocumentKey &)documentKey; + /** The new document applies to all of these targets. */ @property(nonatomic, strong, readonly) NSArray *updatedTargetIDs; /** The new document is removed from all of these targets. */ @property(nonatomic, strong, readonly) NSArray *removedTargetIDs; -/** The key of the document for this change. */ -@property(nonatomic, strong, readonly) FSTDocumentKey *documentKey; - /** * The new document or DeletedDocument if it was deleted. Is null if the document went out of * view without the server sending a new document. diff --git a/Firestore/Source/Remote/FSTWatchChange.mm b/Firestore/Source/Remote/FSTWatchChange.mm index 926d027..2c1a916 100644 --- a/Firestore/Source/Remote/FSTWatchChange.mm +++ b/Firestore/Source/Remote/FSTWatchChange.mm @@ -16,31 +16,42 @@ #import "Firestore/Source/Remote/FSTWatchChange.h" +#include + #import "Firestore/Source/Model/FSTDocument.h" -#import "Firestore/Source/Model/FSTDocumentKey.h" #import "Firestore/Source/Remote/FSTExistenceFilter.h" +#include "Firestore/core/src/firebase/firestore/model/document_key.h" + +using firebase::firestore::model::DocumentKey; + NS_ASSUME_NONNULL_BEGIN @implementation FSTWatchChange @end -@implementation FSTDocumentWatchChange +@implementation FSTDocumentWatchChange { + DocumentKey _documentKey; +} - (instancetype)initWithUpdatedTargetIDs:(NSArray *)updatedTargetIDs removedTargetIDs:(NSArray *)removedTargetIDs - documentKey:(FSTDocumentKey *)documentKey + documentKey:(DocumentKey)documentKey document:(nullable FSTMaybeDocument *)document { self = [super init]; if (self) { _updatedTargetIDs = updatedTargetIDs; _removedTargetIDs = removedTargetIDs; - _documentKey = documentKey; + _documentKey = std::move(documentKey); _document = document; } return self; } +- (const firebase::firestore::model::DocumentKey &)documentKey { + return _documentKey; +} + - (BOOL)isEqual:(id)other { if (other == self) { return YES; @@ -59,7 +70,7 @@ NS_ASSUME_NONNULL_BEGIN - (NSUInteger)hash { NSUInteger hash = self.updatedTargetIDs.hash; hash = hash * 31 + self.removedTargetIDs.hash; - hash = hash * 31 + self.documentKey.hash; + hash = hash * 31 + std::hash{}(self.documentKey.ToString()); hash = hash * 31 + self.document.hash; return hash; } -- cgit v1.2.3