Skip to content

Commit 49de84a

Browse files
authored
Migrate FSTMutationBatch to C++ MutationBatch (#3673)
1 parent 593fcef commit 49de84a

28 files changed

+338
-768
lines changed

Firestore/Example/Tests/Integration/FSTDatastoreTests.mm

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
#import "Firestore/Source/API/FIRDocumentReference+Internal.h"
2626
#import "Firestore/Source/API/FSTUserDataConverter.h"
27-
#import "Firestore/Source/Model/FSTMutationBatch.h"
2827

2928
#import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
3029

@@ -57,6 +56,8 @@
5756
using firebase::firestore::model::DocumentKey;
5857
using firebase::firestore::model::DocumentKeySet;
5958
using firebase::firestore::model::FieldValue;
59+
using firebase::firestore::model::MutationBatch;
60+
using firebase::firestore::model::MutationBatchResult;
6061
using firebase::firestore::model::Precondition;
6162
using firebase::firestore::model::OnlineState;
6263
using firebase::firestore::model::TargetId;
@@ -84,18 +85,17 @@ - (void)expectWriteEventWithDescription:(NSString *)description;
8485
- (void)expectListenEventWithDescription:(NSString *)description;
8586

8687
@property(nonatomic, weak, nullable) XCTestCase *testCase;
87-
@property(nonatomic, strong) NSMutableArray<NSObject *> *writeEvents;
8888
@property(nonatomic, strong) NSMutableArray<XCTestExpectation *> *writeEventExpectations;
8989
@property(nonatomic, strong) NSMutableArray<XCTestExpectation *> *listenEventExpectations;
9090
@end
9191

9292
@implementation FSTRemoteStoreEventCapture {
9393
std::vector<RemoteEvent> _listenEvents;
94+
std::vector<MutationBatchResult> _writeEvents;
9495
}
9596

9697
- (instancetype)initWithTestCase:(XCTestCase *_Nullable)testCase {
9798
if (self = [super init]) {
98-
_writeEvents = [NSMutableArray array];
9999
_testCase = testCase;
100100
_writeEventExpectations = [NSMutableArray array];
101101
_listenEventExpectations = [NSMutableArray array];
@@ -125,8 +125,8 @@ - (void)expectListenEventWithDescription:(NSString *)description {
125125
description]]];
126126
}
127127

128-
- (void)applySuccessfulWriteWithResult:(FSTMutationBatchResult *)batchResult {
129-
[self.writeEvents addObject:batchResult];
128+
- (void)applySuccessfulWriteWithResult:(const MutationBatchResult &)batchResult {
129+
_writeEvents.push_back(batchResult);
130130
XCTestExpectation *expectation = [self.writeEventExpectations objectAtIndex:0];
131131
[self.writeEventExpectations removeObjectAtIndex:0];
132132
[expectation fulfill];
@@ -223,10 +223,7 @@ - (void)testStreamingWrite {
223223
_remoteStore->set_sync_engine(capture);
224224

225225
auto mutation = testutil::SetMutation("rooms/eros", Map("name", "Eros"));
226-
FSTMutationBatch *batch = [[FSTMutationBatch alloc] initWithBatchID:23
227-
localWriteTime:Timestamp::Now()
228-
baseMutations:{}
229-
mutations:{mutation}];
226+
MutationBatch batch = MutationBatch(23, Timestamp::Now(), {}, {mutation});
230227
_testWorkerQueue->Enqueue([=] {
231228
_remoteStore->AddToWritePipeline(batch);
232229
// The added batch won't be written immediately because write stream wasn't yet open --

Firestore/Example/Tests/Local/FSTLocalSerializerTests.mm

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#import "Firestore/Protos/objc/google/firestore/v1/Query.pbobjc.h"
3232
#import "Firestore/Protos/objc/google/firestore/v1/Write.pbobjc.h"
3333
#import "Firestore/Protos/objc/google/type/Latlng.pbobjc.h"
34-
#import "Firestore/Source/Model/FSTMutationBatch.h"
3534
#import "Firestore/Source/Remote/FSTSerializerBeta.h"
3635

3736
#import "Firestore/Example/Tests/Util/FSTHelpers.h"
@@ -58,6 +57,7 @@
5857
using firebase::firestore::model::FieldMask;
5958
using firebase::firestore::model::MaybeDocument;
6059
using firebase::firestore::model::Mutation;
60+
using firebase::firestore::model::MutationBatch;
6161
using firebase::firestore::model::NoDocument;
6262
using firebase::firestore::model::PatchMutation;
6363
using firebase::firestore::model::Precondition;
@@ -103,10 +103,7 @@ - (void)testEncodesMutationBatch {
103103
Mutation del = testutil::DeleteMutation("baz/quux");
104104

105105
Timestamp writeTime = Timestamp::Now();
106-
FSTMutationBatch *model = [[FSTMutationBatch alloc] initWithBatchID:42
107-
localWriteTime:writeTime
108-
baseMutations:{base}
109-
mutations:{set, patch, del}];
106+
MutationBatch model = MutationBatch(42, writeTime, {base}, {set, patch, del});
110107

111108
GCFSWrite *baseProto = [GCFSWrite message];
112109
baseProto.update.name = @"projects/p/databases/d/documents/bar/baz";
@@ -146,12 +143,12 @@ - (void)testEncodesMutationBatch {
146143
batchProto.localWriteTime = writeTimeProto;
147144

148145
XCTAssertEqualObjects([self.serializer encodedMutationBatch:model], batchProto);
149-
FSTMutationBatch *decoded = [self.serializer decodedMutationBatch:batchProto];
150-
XCTAssertEqual(decoded.batchID, model.batchID);
151-
XCTAssertEqual(decoded.localWriteTime, model.localWriteTime);
152-
XCTAssertEqual(decoded.baseMutations, model.baseMutations);
153-
XCTAssertEqual(decoded.mutations, model.mutations);
154-
XCTAssertEqual([decoded keys], [model keys]);
146+
MutationBatch decoded = [self.serializer decodedMutationBatch:batchProto];
147+
XCTAssertEqual(decoded.batch_id(), model.batch_id());
148+
XCTAssertEqual(decoded.local_write_time(), model.local_write_time());
149+
XCTAssertEqual(decoded.base_mutations(), model.base_mutations());
150+
XCTAssertEqual(decoded.mutations(), model.mutations());
151+
XCTAssertEqual(decoded.keys(), model.keys());
155152
}
156153

157154
- (void)testEncodesDocumentAsMaybeDocument {

Firestore/Example/Tests/Local/FSTLocalStoreTests.mm

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
#import "Firestore/Source/API/FIRFieldValue+Internal.h"
2727
#import "Firestore/Source/Local/FSTPersistence.h"
28-
#import "Firestore/Source/Model/FSTMutationBatch.h"
2928
#import "Firestore/Source/Util/FSTClasses.h"
3029

3130
#import "Firestore/Example/Tests/Local/FSTLocalStoreTests.h"
@@ -38,6 +37,7 @@
3837
#include "Firestore/core/src/firebase/firestore/local/query_data.h"
3938
#include "Firestore/core/src/firebase/firestore/model/document_map.h"
4039
#include "Firestore/core/src/firebase/firestore/model/document_set.h"
40+
#include "Firestore/core/src/firebase/firestore/model/mutation_batch_result.h"
4141
#include "Firestore/core/src/firebase/firestore/remote/remote_event.h"
4242
#include "Firestore/core/src/firebase/firestore/remote/watch_change.h"
4343
#include "Firestore/core/src/firebase/firestore/util/status.h"
@@ -57,6 +57,8 @@
5757
using firebase::firestore::model::ListenSequenceNumber;
5858
using firebase::firestore::model::MaybeDocument;
5959
using firebase::firestore::model::Mutation;
60+
using firebase::firestore::model::MutationBatch;
61+
using firebase::firestore::model::MutationBatchResult;
6062
using firebase::firestore::model::MutationResult;
6163
using firebase::firestore::model::DocumentMap;
6264
using firebase::firestore::model::MaybeDocumentMap;
@@ -106,12 +108,12 @@ @interface FSTLocalStoreTests ()
106108
@property(nonatomic, strong, readwrite) id<FSTPersistence> localStorePersistence;
107109
@property(nonatomic, strong, readwrite) FSTLocalStore *localStore;
108110

109-
@property(nonatomic, strong, readonly) NSMutableArray<FSTMutationBatch *> *batches;
110111
@property(nonatomic, assign, readwrite) TargetId lastTargetID;
111112

112113
@end
113114

114115
@implementation FSTLocalStoreTests {
116+
std::vector<MutationBatch> _batches;
115117
MaybeDocumentMap _lastChanges;
116118
}
117119

@@ -128,7 +130,6 @@ - (void)setUp {
128130
initialUser:User::Unauthenticated()];
129131
[self.localStore start];
130132

131-
_batches = [NSMutableArray array];
132133
_lastTargetID = 0;
133134
}
134135

@@ -156,16 +157,14 @@ - (BOOL)isTestBaseClass {
156157
}
157158

158159
- (void)writeMutation:(Mutation)mutation {
159-
[self writeMutations:{mutation}];
160+
[self writeMutations:{std::move(mutation)}];
160161
}
161162

162163
- (void)writeMutations:(std::vector<Mutation> &&)mutations {
163164
auto mutationsCopy = mutations;
164165
LocalWriteResult result = [self.localStore locallyWriteMutations:std::move(mutationsCopy)];
165-
[self.batches addObject:[[FSTMutationBatch alloc] initWithBatchID:result.batch_id()
166-
localWriteTime:Timestamp::Now()
167-
baseMutations:{}
168-
mutations:std::move(mutations)]];
166+
_batches.emplace_back(result.batch_id(), Timestamp::Now(), std::vector<Mutation>{},
167+
std::move(mutations));
169168
_lastChanges = result.changes();
170169
}
171170

@@ -179,9 +178,12 @@ - (void)notifyLocalViewChanges:(LocalViewChanges)changes {
179178

180179
- (void)acknowledgeMutationWithVersion:(FSTTestSnapshotVersion)documentVersion
181180
transformResult:(id _Nullable)transformResult {
182-
FSTMutationBatch *batch = [self.batches firstObject];
183-
[self.batches removeObjectAtIndex:0];
184-
XCTAssertEqual(batch.mutations.size(), 1, @"Acknowledging more than one mutation not supported.");
181+
XCTAssertGreaterThan(_batches.size(), 0, @"Missing batch to acknowledge.");
182+
MutationBatch batch = _batches.front();
183+
_batches.erase(_batches.begin());
184+
185+
XCTAssertEqual(batch.mutations().size(), 1,
186+
@"Acknowledging more than one mutation not supported.");
185187
SnapshotVersion version = testutil::Version(documentVersion);
186188

187189
absl::optional<std::vector<FieldValue>> mutationTransformResult;
@@ -190,10 +192,7 @@ - (void)acknowledgeMutationWithVersion:(FSTTestSnapshotVersion)documentVersion
190192
}
191193

192194
MutationResult mutationResult(version, mutationTransformResult);
193-
FSTMutationBatchResult *result = [FSTMutationBatchResult resultWithBatch:batch
194-
commitVersion:version
195-
mutationResults:{mutationResult}
196-
streamToken:nil];
195+
MutationBatchResult result(batch, version, {mutationResult}, {});
197196
_lastChanges = [self.localStore acknowledgeBatchWithResult:result];
198197
}
199198

@@ -202,9 +201,9 @@ - (void)acknowledgeMutationWithVersion:(FSTTestSnapshotVersion)documentVersion {
202201
}
203202

204203
- (void)rejectMutation {
205-
FSTMutationBatch *batch = [self.batches firstObject];
206-
[self.batches removeObjectAtIndex:0];
207-
_lastChanges = [self.localStore rejectBatchID:batch.batchID];
204+
MutationBatch batch = _batches.front();
205+
_batches.erase(_batches.begin());
206+
_lastChanges = [self.localStore rejectBatchID:batch.batch_id()];
208207
}
209208

210209
- (TargetId)allocateQuery:(core::Query)query {
@@ -268,11 +267,8 @@ - (void)testMutationBatchKeys {
268267
Mutation base = FSTTestSetMutation(@"foo/ignore", @{@"foo" : @"bar"});
269268
Mutation set1 = FSTTestSetMutation(@"foo/bar", @{@"foo" : @"bar"});
270269
Mutation set2 = FSTTestSetMutation(@"bar/baz", @{@"bar" : @"baz"});
271-
FSTMutationBatch *batch = [[FSTMutationBatch alloc] initWithBatchID:1
272-
localWriteTime:Timestamp::Now()
273-
baseMutations:{base}
274-
mutations:{set1, set2}];
275-
DocumentKeySet keys = [batch keys];
270+
MutationBatch batch = MutationBatch(1, Timestamp::Now(), {base}, {set1, set2});
271+
DocumentKeySet keys = batch.keys();
276272
XCTAssertEqual(keys.size(), 2u);
277273
}
278274

0 commit comments

Comments
 (0)