2017-10-03 08:55:22 -07:00
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-11-16 20:52:01 -08:00
|
|
|
#import "Firestore/Source/Local/FSTLocalSerializer.h"
|
2017-10-03 08:55:22 -07:00
|
|
|
|
2018-04-09 11:54:39 -07:00
|
|
|
#include <cinttypes>
|
2019-02-07 22:03:17 -05:00
|
|
|
#include <utility>
|
|
|
|
|
#include <vector>
|
2018-01-31 11:23:55 -08:00
|
|
|
|
2018-05-09 15:04:50 -07:00
|
|
|
#import "FIRTimestamp.h"
|
2017-11-16 20:52:01 -08:00
|
|
|
#import "Firestore/Protos/objc/firestore/local/MaybeDocument.pbobjc.h"
|
|
|
|
|
#import "Firestore/Protos/objc/firestore/local/Mutation.pbobjc.h"
|
|
|
|
|
#import "Firestore/Protos/objc/firestore/local/Target.pbobjc.h"
|
2018-12-21 11:28:48 -08:00
|
|
|
#import "Firestore/Protos/objc/google/firestore/v1/Document.pbobjc.h"
|
2017-11-16 20:52:01 -08:00
|
|
|
#import "Firestore/Source/Remote/FSTSerializerBeta.h"
|
2017-10-03 08:55:22 -07:00
|
|
|
|
2018-04-25 14:13:57 -04:00
|
|
|
#include "Firestore/core/include/firebase/firestore/timestamp.h"
|
2019-08-02 11:35:33 -07:00
|
|
|
#include "Firestore/core/src/firebase/firestore/core/query.h"
|
2019-08-20 14:56:32 -07:00
|
|
|
#include "Firestore/core/src/firebase/firestore/local/query_data.h"
|
2019-06-20 09:39:19 -07:00
|
|
|
#include "Firestore/core/src/firebase/firestore/model/document.h"
|
2018-03-23 17:52:01 -04:00
|
|
|
#include "Firestore/core/src/firebase/firestore/model/document_key.h"
|
2019-08-25 17:40:31 -07:00
|
|
|
#include "Firestore/core/src/firebase/firestore/model/mutation_batch.h"
|
2019-08-13 13:59:00 -07:00
|
|
|
#include "Firestore/core/src/firebase/firestore/model/no_document.h"
|
2018-04-25 14:05:36 -04:00
|
|
|
#include "Firestore/core/src/firebase/firestore/model/snapshot_version.h"
|
2019-08-13 13:59:00 -07:00
|
|
|
#include "Firestore/core/src/firebase/firestore/model/unknown_document.h"
|
2019-08-19 12:22:56 -07:00
|
|
|
#include "Firestore/core/src/firebase/firestore/nanopb/nanopb_util.h"
|
2018-05-24 11:00:23 -07:00
|
|
|
#include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
|
2018-03-23 17:52:01 -04:00
|
|
|
|
2018-04-25 14:13:57 -04:00
|
|
|
using firebase::Timestamp;
|
2019-08-02 11:35:33 -07:00
|
|
|
using firebase::firestore::core::Query;
|
2019-08-20 14:56:32 -07:00
|
|
|
using firebase::firestore::local::QueryData;
|
|
|
|
|
using firebase::firestore::local::QueryPurpose;
|
2019-08-13 13:59:00 -07:00
|
|
|
using firebase::firestore::model::Document;
|
2018-03-23 17:52:01 -04:00
|
|
|
using firebase::firestore::model::DocumentKey;
|
2019-06-20 09:39:19 -07:00
|
|
|
using firebase::firestore::model::DocumentState;
|
2018-08-16 14:02:03 -07:00
|
|
|
using firebase::firestore::model::ListenSequenceNumber;
|
2019-08-13 13:59:00 -07:00
|
|
|
using firebase::firestore::model::MaybeDocument;
|
2019-08-14 23:18:47 -07:00
|
|
|
using firebase::firestore::model::Mutation;
|
2019-08-25 17:40:31 -07:00
|
|
|
using firebase::firestore::model::MutationBatch;
|
2019-08-13 13:59:00 -07:00
|
|
|
using firebase::firestore::model::NoDocument;
|
2019-06-18 16:21:17 -07:00
|
|
|
using firebase::firestore::model::ObjectValue;
|
2018-04-25 14:05:36 -04:00
|
|
|
using firebase::firestore::model::SnapshotVersion;
|
2018-08-15 15:56:45 -07:00
|
|
|
using firebase::firestore::model::TargetId;
|
2019-08-13 13:59:00 -07:00
|
|
|
using firebase::firestore::model::UnknownDocument;
|
2019-08-19 12:22:56 -07:00
|
|
|
using firebase::firestore::nanopb::ByteString;
|
|
|
|
|
using firebase::firestore::nanopb::MakeByteString;
|
|
|
|
|
using firebase::firestore::nanopb::MakeNSData;
|
2018-03-23 17:52:01 -04:00
|
|
|
|
2017-10-03 08:55:22 -07:00
|
|
|
@interface FSTLocalSerializer ()
|
|
|
|
|
|
|
|
|
|
@property(nonatomic, strong, readonly) FSTSerializerBeta *remoteSerializer;
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
/** Serializer for values stored in the LocalStore. */
|
|
|
|
|
@implementation FSTLocalSerializer
|
|
|
|
|
|
|
|
|
|
- (instancetype)initWithRemoteSerializer:(FSTSerializerBeta *)remoteSerializer {
|
|
|
|
|
self = [super init];
|
|
|
|
|
if (self) {
|
|
|
|
|
_remoteSerializer = remoteSerializer;
|
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-13 13:59:00 -07:00
|
|
|
- (FSTPBMaybeDocument *)encodedMaybeDocument:(const MaybeDocument &)document {
|
2017-10-03 08:55:22 -07:00
|
|
|
FSTPBMaybeDocument *proto = [FSTPBMaybeDocument message];
|
|
|
|
|
|
2019-08-13 13:59:00 -07:00
|
|
|
if (document.is_no_document()) {
|
|
|
|
|
NoDocument deletedDocument(document);
|
2018-11-01 09:44:30 -07:00
|
|
|
proto.noDocument = [self encodedDeletedDocument:deletedDocument];
|
2019-08-13 13:59:00 -07:00
|
|
|
proto.hasCommittedMutations = deletedDocument.has_committed_mutations();
|
|
|
|
|
} else if (document.is_document()) {
|
|
|
|
|
Document existingDocument(document);
|
|
|
|
|
const absl::any &docProto = existingDocument.proto();
|
|
|
|
|
if (docProto.has_value()) {
|
|
|
|
|
proto.document = absl::any_cast<GCFSDocument *>(docProto);
|
2018-12-10 14:39:22 -08:00
|
|
|
} else {
|
|
|
|
|
proto.document = [self encodedDocument:existingDocument];
|
|
|
|
|
}
|
2019-08-13 13:59:00 -07:00
|
|
|
proto.hasCommittedMutations = existingDocument.has_committed_mutations();
|
|
|
|
|
} else if (document.is_unknown_document()) {
|
|
|
|
|
UnknownDocument unknownDocument(document);
|
2018-11-01 09:44:30 -07:00
|
|
|
proto.unknownDocument = [self encodedUnknownDocument:unknownDocument];
|
|
|
|
|
proto.hasCommittedMutations = YES;
|
2017-10-03 08:55:22 -07:00
|
|
|
} else {
|
2019-08-13 13:59:00 -07:00
|
|
|
HARD_FAIL("Unknown document type %s", document.type());
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return proto;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-13 13:59:00 -07:00
|
|
|
- (MaybeDocument)decodedMaybeDocument:(FSTPBMaybeDocument *)proto {
|
2017-10-03 08:55:22 -07:00
|
|
|
switch (proto.documentTypeOneOfCase) {
|
|
|
|
|
case FSTPBMaybeDocument_DocumentType_OneOfCase_Document:
|
2019-01-02 09:27:31 -08:00
|
|
|
return [self decodedDocument:proto.document
|
|
|
|
|
withCommittedMutations:proto.hasCommittedMutations];
|
2017-10-03 08:55:22 -07:00
|
|
|
|
|
|
|
|
case FSTPBMaybeDocument_DocumentType_OneOfCase_NoDocument:
|
2018-11-01 09:44:30 -07:00
|
|
|
return [self decodedDeletedDocument:proto.noDocument
|
|
|
|
|
withCommittedMutations:proto.hasCommittedMutations];
|
|
|
|
|
|
|
|
|
|
case FSTPBMaybeDocument_DocumentType_OneOfCase_UnknownDocument:
|
|
|
|
|
return [self decodedUnknownDocument:proto.unknownDocument];
|
2017-10-03 08:55:22 -07:00
|
|
|
|
|
|
|
|
default:
|
2018-05-24 11:00:23 -07:00
|
|
|
HARD_FAIL("Unknown MaybeDocument %s", proto);
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-12-21 11:28:48 -08:00
|
|
|
* Encodes a Document for local storage. This differs from the v1 RPC serializer for Documents in
|
|
|
|
|
* that it preserves the updateTime, which is considered an output only value by the server.
|
2017-10-03 08:55:22 -07:00
|
|
|
*/
|
2019-08-13 13:59:00 -07:00
|
|
|
- (GCFSDocument *)encodedDocument:(const Document &)document {
|
2017-10-03 08:55:22 -07:00
|
|
|
FSTSerializerBeta *remoteSerializer = self.remoteSerializer;
|
|
|
|
|
|
|
|
|
|
GCFSDocument *proto = [GCFSDocument message];
|
2019-08-13 13:59:00 -07:00
|
|
|
proto.name = [remoteSerializer encodedDocumentKey:document.key()];
|
|
|
|
|
proto.fields = [remoteSerializer encodedFields:document.data()];
|
|
|
|
|
proto.updateTime = [remoteSerializer encodedVersion:document.version()];
|
2017-10-03 08:55:22 -07:00
|
|
|
|
|
|
|
|
return proto;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Decodes a Document proto to the equivalent model. */
|
2019-08-13 13:59:00 -07:00
|
|
|
- (Document)decodedDocument:(GCFSDocument *)document
|
|
|
|
|
withCommittedMutations:(BOOL)committedMutations {
|
2017-10-03 08:55:22 -07:00
|
|
|
FSTSerializerBeta *remoteSerializer = self.remoteSerializer;
|
|
|
|
|
|
2019-06-18 16:21:17 -07:00
|
|
|
ObjectValue data = [remoteSerializer decodedFields:document.fields];
|
2018-05-01 13:37:53 -04:00
|
|
|
DocumentKey key = [remoteSerializer decodedDocumentKey:document.name];
|
|
|
|
|
SnapshotVersion version = [remoteSerializer decodedVersion:document.updateTime];
|
2019-08-13 13:59:00 -07:00
|
|
|
DocumentState state =
|
|
|
|
|
committedMutations ? DocumentState::kCommittedMutations : DocumentState::kSynced;
|
|
|
|
|
return Document(std::move(data), std::move(key), version, state);
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Encodes a NoDocument value to the equivalent proto. */
|
2019-08-13 13:59:00 -07:00
|
|
|
- (FSTPBNoDocument *)encodedDeletedDocument:(const NoDocument &)document {
|
2017-10-03 08:55:22 -07:00
|
|
|
FSTSerializerBeta *remoteSerializer = self.remoteSerializer;
|
|
|
|
|
|
|
|
|
|
FSTPBNoDocument *proto = [FSTPBNoDocument message];
|
2019-08-13 13:59:00 -07:00
|
|
|
proto.name = [remoteSerializer encodedDocumentKey:document.key()];
|
|
|
|
|
proto.readTime = [remoteSerializer encodedVersion:document.version()];
|
2017-10-03 08:55:22 -07:00
|
|
|
return proto;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Decodes a NoDocument proto to the equivalent model. */
|
2019-08-13 13:59:00 -07:00
|
|
|
- (NoDocument)decodedDeletedDocument:(FSTPBNoDocument *)proto
|
|
|
|
|
withCommittedMutations:(BOOL)committedMutations {
|
2017-10-03 08:55:22 -07:00
|
|
|
FSTSerializerBeta *remoteSerializer = self.remoteSerializer;
|
|
|
|
|
|
2018-05-01 13:37:53 -04:00
|
|
|
DocumentKey key = [remoteSerializer decodedDocumentKey:proto.name];
|
|
|
|
|
SnapshotVersion version = [remoteSerializer decodedVersion:proto.readTime];
|
2019-08-13 13:59:00 -07:00
|
|
|
return NoDocument(std::move(key), version, committedMutations);
|
2018-11-01 09:44:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Encodes an UnknownDocument value to the equivalent proto. */
|
2019-08-13 13:59:00 -07:00
|
|
|
- (FSTPBUnknownDocument *)encodedUnknownDocument:(const UnknownDocument &)document {
|
2018-11-01 09:44:30 -07:00
|
|
|
FSTSerializerBeta *remoteSerializer = self.remoteSerializer;
|
|
|
|
|
|
|
|
|
|
FSTPBUnknownDocument *proto = [FSTPBUnknownDocument message];
|
2019-08-13 13:59:00 -07:00
|
|
|
proto.name = [remoteSerializer encodedDocumentKey:document.key()];
|
|
|
|
|
proto.version = [remoteSerializer encodedVersion:document.version()];
|
2018-11-01 09:44:30 -07:00
|
|
|
return proto;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Decodes an UnknownDocument proto to the equivalent model. */
|
2019-08-13 13:59:00 -07:00
|
|
|
- (UnknownDocument)decodedUnknownDocument:(FSTPBUnknownDocument *)proto {
|
2018-11-01 09:44:30 -07:00
|
|
|
FSTSerializerBeta *remoteSerializer = self.remoteSerializer;
|
|
|
|
|
|
|
|
|
|
DocumentKey key = [remoteSerializer decodedDocumentKey:proto.name];
|
|
|
|
|
SnapshotVersion version = [remoteSerializer decodedVersion:proto.version];
|
2019-08-13 13:59:00 -07:00
|
|
|
return UnknownDocument(std::move(key), version);
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
2019-08-25 17:40:31 -07:00
|
|
|
- (FSTPBWriteBatch *)encodedMutationBatch:(const MutationBatch &)batch {
|
2017-10-03 08:55:22 -07:00
|
|
|
FSTSerializerBeta *remoteSerializer = self.remoteSerializer;
|
|
|
|
|
|
|
|
|
|
FSTPBWriteBatch *proto = [FSTPBWriteBatch message];
|
2019-08-25 17:40:31 -07:00
|
|
|
proto.batchId = batch.batch_id();
|
|
|
|
|
proto.localWriteTime = [remoteSerializer encodedTimestamp:batch.local_write_time()];
|
2017-10-03 08:55:22 -07:00
|
|
|
|
2019-03-06 14:51:30 -08:00
|
|
|
NSMutableArray<GCFSWrite *> *baseWrites = proto.baseWritesArray;
|
2019-08-25 17:40:31 -07:00
|
|
|
for (const Mutation &baseMutation : batch.base_mutations()) {
|
2019-03-06 14:51:30 -08:00
|
|
|
[baseWrites addObject:[remoteSerializer encodedMutation:baseMutation]];
|
|
|
|
|
}
|
2017-10-03 08:55:22 -07:00
|
|
|
NSMutableArray<GCFSWrite *> *writes = proto.writesArray;
|
2019-08-25 17:40:31 -07:00
|
|
|
for (const Mutation &mutation : batch.mutations()) {
|
2017-10-03 08:55:22 -07:00
|
|
|
[writes addObject:[remoteSerializer encodedMutation:mutation]];
|
|
|
|
|
}
|
|
|
|
|
return proto;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-25 17:40:31 -07:00
|
|
|
- (MutationBatch)decodedMutationBatch:(FSTPBWriteBatch *)batch {
|
2017-10-03 08:55:22 -07:00
|
|
|
FSTSerializerBeta *remoteSerializer = self.remoteSerializer;
|
|
|
|
|
|
|
|
|
|
int batchID = batch.batchId;
|
2019-03-06 14:51:30 -08:00
|
|
|
|
2019-08-14 23:18:47 -07:00
|
|
|
std::vector<Mutation> baseMutations;
|
2019-03-06 14:51:30 -08:00
|
|
|
for (GCFSWrite *write in batch.baseWritesArray) {
|
|
|
|
|
baseMutations.push_back([remoteSerializer decodedMutation:write]);
|
|
|
|
|
}
|
2019-08-14 23:18:47 -07:00
|
|
|
std::vector<Mutation> mutations;
|
2017-10-03 08:55:22 -07:00
|
|
|
for (GCFSWrite *write in batch.writesArray) {
|
2019-02-07 22:03:17 -05:00
|
|
|
mutations.push_back([remoteSerializer decodedMutation:write]);
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
2018-04-25 14:13:57 -04:00
|
|
|
Timestamp localWriteTime = [remoteSerializer decodedTimestamp:batch.localWriteTime];
|
2017-10-03 08:55:22 -07:00
|
|
|
|
2019-08-25 17:40:31 -07:00
|
|
|
return MutationBatch(batchID, localWriteTime, std::move(baseMutations), std::move(mutations));
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
2019-08-20 14:56:32 -07:00
|
|
|
- (FSTPBTarget *)encodedQueryData:(const QueryData &)queryData {
|
2017-10-03 08:55:22 -07:00
|
|
|
FSTSerializerBeta *remoteSerializer = self.remoteSerializer;
|
|
|
|
|
|
2019-08-20 14:56:32 -07:00
|
|
|
HARD_ASSERT(queryData.purpose() == QueryPurpose::Listen,
|
|
|
|
|
"only queries with purpose %s may be stored, got %s", QueryPurpose::Listen,
|
|
|
|
|
queryData.purpose());
|
2017-10-03 08:55:22 -07:00
|
|
|
|
|
|
|
|
FSTPBTarget *proto = [FSTPBTarget message];
|
2019-08-20 14:56:32 -07:00
|
|
|
proto.targetId = queryData.target_id();
|
|
|
|
|
proto.lastListenSequenceNumber = queryData.sequence_number();
|
|
|
|
|
proto.snapshotVersion = [remoteSerializer encodedVersion:queryData.snapshot_version()];
|
2019-08-22 15:18:11 -07:00
|
|
|
proto.resumeToken = MakeNullableNSData(queryData.resume_token());
|
2017-10-03 08:55:22 -07:00
|
|
|
|
2019-08-20 14:56:32 -07:00
|
|
|
const Query &query = queryData.query();
|
2019-08-02 11:35:33 -07:00
|
|
|
if (query.IsDocumentQuery()) {
|
2017-10-03 08:55:22 -07:00
|
|
|
proto.documents = [remoteSerializer encodedDocumentsTarget:query];
|
|
|
|
|
} else {
|
|
|
|
|
proto.query = [remoteSerializer encodedQueryTarget:query];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return proto;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-20 14:56:32 -07:00
|
|
|
- (QueryData)decodedQueryData:(FSTPBTarget *)target {
|
2017-10-03 08:55:22 -07:00
|
|
|
FSTSerializerBeta *remoteSerializer = self.remoteSerializer;
|
|
|
|
|
|
2018-08-15 15:56:45 -07:00
|
|
|
TargetId targetID = target.targetId;
|
2018-08-16 14:02:03 -07:00
|
|
|
ListenSequenceNumber sequenceNumber = target.lastListenSequenceNumber;
|
2018-05-01 13:37:53 -04:00
|
|
|
SnapshotVersion version = [remoteSerializer decodedVersion:target.snapshotVersion];
|
2019-08-19 12:22:56 -07:00
|
|
|
ByteString resumeToken = MakeByteString(target.resumeToken);
|
2017-10-03 08:55:22 -07:00
|
|
|
|
2019-08-02 11:35:33 -07:00
|
|
|
Query query;
|
2017-10-03 08:55:22 -07:00
|
|
|
switch (target.targetTypeOneOfCase) {
|
|
|
|
|
case FSTPBTarget_TargetType_OneOfCase_Documents:
|
|
|
|
|
query = [remoteSerializer decodedQueryFromDocumentsTarget:target.documents];
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case FSTPBTarget_TargetType_OneOfCase_Query:
|
|
|
|
|
query = [remoteSerializer decodedQueryFromQueryTarget:target.query];
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
2018-05-24 11:00:23 -07:00
|
|
|
HARD_FAIL("Unknown Target.targetType %s", target.targetTypeOneOfCase);
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
2019-08-20 14:56:32 -07:00
|
|
|
return QueryData(std::move(query), targetID, sequenceNumber, QueryPurpose::Listen, version,
|
|
|
|
|
std::move(resumeToken));
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
2018-04-25 14:05:36 -04:00
|
|
|
- (GPBTimestamp *)encodedVersion:(const SnapshotVersion &)version {
|
2017-10-03 08:55:22 -07:00
|
|
|
return [self.remoteSerializer encodedVersion:version];
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-25 14:05:36 -04:00
|
|
|
- (SnapshotVersion)decodedVersion:(GPBTimestamp *)version {
|
2017-10-03 08:55:22 -07:00
|
|
|
return [self.remoteSerializer decodedVersion:version];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|