Skip to content

C++ migration: port FSTTransaction #2362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 170 commits into from
Feb 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
170 commits
Select commit Hold shift + click to select a range
76b206b
Remove extra imports and includes
var-const Jan 15, 2019
7dcb9ae
Port
var-const Jan 15, 2019
88ba1a7
Forgotten method
var-const Jan 15, 2019
895bf45
style.sh
var-const Jan 15, 2019
b66c5d2
Forgotten the main part!
var-const Jan 15, 2019
3f5fdae
Fixup unit tests
var-const Jan 15, 2019
5ce04b8
Wip
var-const Jan 16, 2019
3dc3170
Step back
var-const Jan 18, 2019
9d1f931
Core compiles
var-const Jan 18, 2019
7ea798e
Merge branch 'master' into varconst/fst-watch-change
var-const Jan 18, 2019
84842be
quick fix
var-const Jan 18, 2019
e5132be
Test helpers
var-const Jan 18, 2019
6c85090
MockDatastore
var-const Jan 18, 2019
f7a90b5
FSTLocalStoreTests
var-const Jan 18, 2019
267963f
wip
var-const Jan 18, 2019
60a5726
wip
var-const Jan 18, 2019
dd9d98d
FSTRemoveEventTests
var-const Jan 18, 2019
aec3f54
FSTSerializerBetaTests
var-const Jan 19, 2019
b012929
wip
var-const Jan 19, 2019
1da0672
wip
var-const Jan 21, 2019
e0f77f2
wip
var-const Jan 21, 2019
df5cd00
wip
var-const Jan 21, 2019
5d3313a
wip
var-const Jan 21, 2019
31e5f3d
wip
var-const Jan 21, 2019
0637243
Ship it!
var-const Jan 21, 2019
26b39c7
use changes
var-const Jan 21, 2019
2cab650
quick fix
var-const Jan 21, 2019
ca88d8c
Unit tests pass
var-const Jan 21, 2019
2c0a7ef
style.sh
var-const Jan 21, 2019
52b131d
Small fixes
var-const Jan 21, 2019
35ce8cc
Get rid of ids alias
var-const Jan 21, 2019
8230fbb
Fixes
var-const Jan 21, 2019
ceb3bee
Move the test
var-const Jan 21, 2019
dd617de
Restore nooutstandingresponses variable
var-const Jan 21, 2019
9d80eea
Small fixes
var-const Jan 21, 2019
836d4de
style.sh
var-const Jan 21, 2019
134acaf
Review feedback
var-const Jan 22, 2019
f8dd5a5
Review feedback
var-const Jan 22, 2019
c02df53
comment
var-const Jan 22, 2019
ce17913
Appease linter
var-const Jan 22, 2019
a87cf7e
Initial
var-const Jan 22, 2019
d6a137a
wip
var-const Jan 23, 2019
1c99679
wip
var-const Jan 23, 2019
b2e0dd1
Initial
var-const Jan 23, 2019
e076f25
style.sh
var-const Jan 23, 2019
359d938
Merge branch 'master' into varconst/fst-document-view-change-type
var-const Jan 23, 2019
4b7c6a7
forgotten file
var-const Jan 23, 2019
7164e2b
Merge branch 'varconst/fst-document-view-change-type' into varconst/f…
var-const Jan 23, 2019
48fbab6
pt. 1 compiles
var-const Jan 23, 2019
0325248
Wip
var-const Jan 23, 2019
acbc626
wip
var-const Jan 24, 2019
b178b00
Merge branch 'master' into varconst/fst-watch-change-aggregator
var-const Jan 24, 2019
78395d8
In theory, all methods implemented
var-const Jan 24, 2019
4fee491
wip
var-const Jan 24, 2019
626efec
Almost compiles
var-const Jan 24, 2019
66db96a
Merge branch 'master' into varconst/fst-watch-change-aggregator
var-const Jan 24, 2019
d5defb1
appease linter
var-const Jan 24, 2019
52b2171
Bring method order into compliance
var-const Jan 24, 2019
e5fcf94
Compliacne, pt.2
var-const Jan 24, 2019
7a48512
core compiles
var-const Jan 25, 2019
66d8af2
unit tests compile
var-const Jan 26, 2019
594a841
Fix unit test -- all pass
var-const Jan 26, 2019
77d9cd7
style.sh
var-const Jan 26, 2019
fdf05a7
Review feedback
var-const Jan 26, 2019
a910ccd
Merge branch 'varconst/fst-watch-change-aggregator' into varconst/fst…
var-const Jan 26, 2019
c71a541
undo formatting
var-const Jan 26, 2019
24a745e
fix
var-const Jan 26, 2019
142f08c
Merge branch 'varconst/fst-watch-change-aggregator' into varconst/fst…
var-const Jan 27, 2019
e402e8b
Merge branch 'master' into varconst/fst-target-change
var-const Jan 27, 2019
e3dd85e
Temp code
var-const Jan 27, 2019
1eb4096
Implementation
var-const Jan 28, 2019
4147c3d
Updating references wip (FSTRemoteEventsTests still to do)
var-const Jan 28, 2019
3b3b012
Unit tests compile
var-const Jan 28, 2019
f1140d9
Unit tests compile and pass
var-const Jan 28, 2019
7ee766f
style.sh
var-const Jan 28, 2019
f00494a
Minor improvements
var-const Jan 28, 2019
766e940
Review feedback
var-const Jan 29, 2019
68ced12
Merge branch 'varconst/fst-target-change' into varconst/fst-remote-ev…
var-const Jan 29, 2019
90450fd
Initial implementation
var-const Jan 29, 2019
ed7ee52
Updating references
var-const Jan 29, 2019
ab6d050
Merge branch 'master' into varconst/fst-remote-event2
var-const Jan 29, 2019
920a921
All compiles
var-const Jan 29, 2019
3d509ec
style.sh
var-const Jan 29, 2019
b2d8ce4
Minor consistency changes
var-const Jan 29, 2019
355ced0
Skeleton
var-const Jan 29, 2019
df4c82e
Pseudochange
var-const Jan 29, 2019
9b494ee
Undo pseudochange
var-const Jan 29, 2019
c5a82f9
unstyle
var-const Jan 29, 2019
65b6ae2
Review feedback
var-const Jan 30, 2019
055372e
Merge branch 'varconst/fst-remote-event2' into varconst/fst-online-st…
var-const Jan 30, 2019
9435faf
Merge branch 'master' into varconst/fst-online-state-tracker
var-const Jan 30, 2019
151f99d
Change extension
var-const Jan 30, 2019
06947a5
style
var-const Jan 30, 2019
26bb8a9
wip
var-const Jan 30, 2019
61f821e
Redundant
var-const Jan 30, 2019
448b1fc
wip
var-const Jan 30, 2019
a7d99ae
wip
var-const Jan 30, 2019
286850f
Merge branch 'varconst/fst-online-state-tracker' into varconst/fst-re…
var-const Jan 30, 2019
6119d54
core actually compiles
var-const Jan 30, 2019
8fa4e74
style.sh
var-const Jan 30, 2019
6702112
wip
var-const Jan 31, 2019
4ec169e
unit tests compile
var-const Jan 31, 2019
6db2ee9
style.sh
var-const Jan 31, 2019
463b652
accidental
var-const Jan 31, 2019
dba5e70
small fixes
var-const Jan 31, 2019
97229a8
initial
var-const Feb 1, 2019
5266f7c
initial cont.
var-const Feb 1, 2019
43c575a
Merge branch 'master' into varconst/fst-online-state-tracker
var-const Feb 1, 2019
53f00fc
Compiles!
var-const Feb 1, 2019
1858aae
all tests pass
var-const Feb 1, 2019
4589e1f
style.sh
var-const Feb 1, 2019
3025ca3
Merge branch 'varconst/fst-online-state-tracker' into varconst/fst-re…
var-const Feb 1, 2019
882a137
Merge branch 'master' into varconst/fst-remote-store1
var-const Feb 1, 2019
5f1ac4b
Merge branch 'varconst/fst-remote-store1' into varconst/fst-remote-st…
var-const Feb 1, 2019
28e0689
android method order
var-const Feb 1, 2019
c803f61
web method order
var-const Feb 1, 2019
aa18e6c
rename
var-const Feb 1, 2019
ce0d114
undo
var-const Feb 1, 2019
771b6d2
Compiles!
var-const Feb 1, 2019
318debc
Review feedback
var-const Feb 1, 2019
0e477c2
Merge branch 'varconst/fst-remote-store1' into varconst/fst-remote-st…
var-const Feb 1, 2019
924b455
Review feedback
var-const Feb 1, 2019
39d980c
Merge branch 'varconst/fst-remote-store2' into varconst/fst-remote-st…
var-const Feb 1, 2019
cd8107b
Small fixes
var-const Feb 1, 2019
6cb2693
Compiles!
var-const Feb 1, 2019
af88702
style.sh
var-const Feb 1, 2019
33da753
fix weird bug
var-const Feb 2, 2019
808e169
Merge branch 'varconst/fst-remote-store1' into varconst/fst-remote-st…
var-const Feb 2, 2019
5f54216
Merge branch 'varconst/fst-remote-store2' into varconst/fst-remote-st…
var-const Feb 2, 2019
5de4789
compilation fix
var-const Feb 2, 2019
971954c
Merge branch 'varconst/fst-remote-store3' into varconst/fst-remote-st…
var-const Feb 2, 2019
229b796
Initial
var-const Feb 6, 2019
de93f9e
undo formatting
var-const Feb 6, 2019
a196232
Merge branch 'master' into varconst/fst-remote-store2
var-const Feb 6, 2019
217a47f
Review feedback
var-const Feb 7, 2019
144dba5
Merge branch 'varconst/fst-remote-store2' into varconst/fst-remote-st…
var-const Feb 7, 2019
5043c6c
Merge branch 'varconst/fst-remote-store3' into varconst/fst-remote-st…
var-const Feb 7, 2019
fa1082e
Fix
var-const Feb 7, 2019
60a5f06
Merge branch 'varconst/fst-remote-store2' into varconst/fst-remote-st…
var-const Feb 7, 2019
5cf9175
Merge branch 'master' into varconst/vector-of-fst-mutations
var-const Feb 7, 2019
d9290fb
Merge branch 'master' into varconst/vector-of-fst-mutations
var-const Feb 7, 2019
38dce72
Merge branch 'master' into varconst/fst-remote-store3
var-const Feb 7, 2019
867f202
pseudocommit
var-const Feb 7, 2019
ec445f0
pseudocommit
var-const Feb 7, 2019
4a7e460
Fix bug
var-const Feb 7, 2019
9e7706e
Merge branch 'varconst/fst-remote-store3' into varconst/fst-remote-st…
var-const Feb 7, 2019
0dd39c3
Merge branch 'master' into varconst/fst-remote-store4
var-const Feb 7, 2019
edf828c
pseudocommit
var-const Feb 7, 2019
6ffd290
pseudocommit
var-const Feb 7, 2019
3cf1447
Fix compilation
var-const Feb 7, 2019
6263612
comment tweak
var-const Feb 7, 2019
ca470d1
Initial
var-const Feb 8, 2019
abea7b2
Review feedback
var-const Feb 8, 2019
3501b44
Merge branch 'master' into varconst/vector-of-fst-mutations
var-const Feb 8, 2019
fcbe4e4
Merge branch 'varconst/vector-of-fst-mutations' into varconst/fst-tra…
var-const Feb 8, 2019
9eccd68
typo
var-const Feb 8, 2019
ffe27d2
Merge branch 'varconst/fst-remote-store4' into varconst/fst-transaction3
var-const Feb 8, 2019
edf51bd
Merge branch 'master' into varconst/fst-transaction3
var-const Feb 8, 2019
2e0a8c3
undo
var-const Feb 8, 2019
15f7613
fix typo
var-const Feb 8, 2019
37b1d46
Minor style change
var-const Feb 8, 2019
32bfb2a
temp code
var-const Feb 8, 2019
3c9a898
Fix
var-const Feb 8, 2019
50f69ed
Conformance
var-const Feb 8, 2019
02b1c80
style.sh
var-const Feb 8, 2019
7f47e36
remove accidental
var-const Feb 8, 2019
9efdd6a
Fix
var-const Feb 8, 2019
e88834e
Fix
var-const Feb 8, 2019
3f0ed3b
Fix
var-const Feb 9, 2019
89e4b14
Review feedback
var-const Feb 11, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Firestore/Example/Tests/Integration/FSTDatastoreTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "Firestore/core/src/firebase/firestore/util/async_queue.h"
#include "Firestore/core/src/firebase/firestore/util/executor_libdispatch.h"
#include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
#include "Firestore/core/src/firebase/firestore/util/status.h"
#include "Firestore/core/src/firebase/firestore/util/string_apple.h"
#include "absl/memory/memory.h"

Expand All @@ -63,6 +64,7 @@
using firebase::firestore::remote::RemoteStore;
using firebase::firestore::util::AsyncQueue;
using firebase::firestore::util::ExecutorLibdispatch;
using firebase::firestore::util::Status;

NS_ASSUME_NONNULL_BEGIN

Expand Down Expand Up @@ -202,8 +204,8 @@ - (void)tearDown {
- (void)testCommit {
XCTestExpectation *expectation = [self expectationWithDescription:@"commitWithCompletion"];

_datastore->CommitMutations({}, ^(NSError *_Nullable error) {
XCTAssertNil(error, @"Failed to commit");
_datastore->CommitMutations({}, [self, expectation](const Status &status) {
XCTAssertTrue(status.ok(), @"Failed to commit");
[expectation fulfill];
});

Expand Down
2 changes: 1 addition & 1 deletion Firestore/Example/Tests/Integration/FSTTransactionTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ - (void)testReadingADocTwiceWithDifferentVersions {
// The get itself will fail, because we already read an earlier version of this document.
// TODO(klimt): Perhaps we shouldn't fail reads for this, but should wait and fail the
// whole transaction? It's an edge-case anyway, as developers shouldn't be reading the same
// do multiple times. But they need to handle read errors anyway.
// doc multiple times. But they need to handle read errors anyway.
XCTAssertNotNil(*error);
return nil;
}
Expand Down
9 changes: 6 additions & 3 deletions Firestore/Source/API/FIRFirestore.mm
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "Firestore/core/src/firebase/firestore/auth/credentials_provider.h"
#include "Firestore/core/src/firebase/firestore/auth/firebase_credentials_provider_apple.h"
#include "Firestore/core/src/firebase/firestore/core/database_info.h"
#include "Firestore/core/src/firebase/firestore/core/transaction.h"
#include "Firestore/core/src/firebase/firestore/model/database_id.h"
#include "Firestore/core/src/firebase/firestore/model/resource_path.h"
#include "Firestore/core/src/firebase/firestore/util/async_queue.h"
Expand All @@ -53,6 +54,7 @@
using firebase::firestore::auth::CredentialsProvider;
using firebase::firestore::auth::FirebaseCredentialsProvider;
using firebase::firestore::core::DatabaseInfo;
using firebase::firestore::core::Transaction;
using firebase::firestore::model::DatabaseId;
using firebase::firestore::model::ResourcePath;
using util::AsyncQueue;
Expand Down Expand Up @@ -275,18 +277,19 @@ - (void)runTransactionWithBlock:(id _Nullable (^)(FIRTransaction *, NSError **))
completion:
(void (^)(id _Nullable result, NSError *_Nullable error))completion {
// We wrap the function they provide in order to use internal implementation classes for
// FSTTransaction, and to run the user callback block on the proper queue.
// transaction, and to run the user callback block on the proper queue.
if (!updateBlock) {
FSTThrowInvalidArgument(@"Transaction block cannot be nil.");
} else if (!completion) {
FSTThrowInvalidArgument(@"Transaction completion block cannot be nil.");
}

FSTTransactionBlock wrappedUpdate =
^(FSTTransaction *internalTransaction,
^(std::shared_ptr<Transaction> internalTransaction,
void (^internalCompletion)(id _Nullable, NSError *_Nullable)) {
FIRTransaction *transaction =
[FIRTransaction transactionWithFSTTransaction:internalTransaction firestore:self];
[FIRTransaction transactionWithInternalTransaction:std::move(internalTransaction)
firestore:self];
dispatch_async(queue, ^{
NSError *_Nullable error = nil;
id _Nullable result = updateBlock(transaction, &error);
Expand Down
10 changes: 7 additions & 3 deletions Firestore/Source/API/FIRTransaction+Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@

#import "FIRTransaction.h"

#include <memory>

#include "Firestore/core/src/firebase/firestore/core/transaction.h"

@class FIRFirestore;
@class FSTTransaction;

@interface FIRTransaction (Internal)

+ (instancetype)transactionWithFSTTransaction:(FSTTransaction *)transaction
firestore:(FIRFirestore *)firestore;
+ (instancetype)transactionWithInternalTransaction:
(std::shared_ptr<firebase::firestore::core::Transaction>)transaction
firestore:(FIRFirestore *)firestore;

@end
96 changes: 51 additions & 45 deletions Firestore/Source/API/FIRTransaction.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,59 @@

#import "FIRTransaction.h"

#include <memory>
#include <utility>
#include <vector>

#import "Firestore/Source/API/FIRDocumentReference+Internal.h"
#import "Firestore/Source/API/FIRDocumentSnapshot+Internal.h"
#import "Firestore/Source/API/FIRFirestore+Internal.h"
#import "Firestore/Source/API/FIRTransaction+Internal.h"
#import "Firestore/Source/API/FSTUserDataConverter.h"
#import "Firestore/Source/Core/FSTTransaction.h"
#import "Firestore/Source/Model/FSTDocument.h"
#import "Firestore/Source/Util/FSTUsageValidation.h"

#include "Firestore/core/src/firebase/firestore/core/transaction.h"
#include "Firestore/core/src/firebase/firestore/util/error_apple.h"
#include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
#include "Firestore/core/src/firebase/firestore/util/status.h"

using firebase::firestore::core::ParsedSetData;
using firebase::firestore::core::ParsedUpdateData;
using firebase::firestore::core::Transaction;
using firebase::firestore::util::MakeNSError;
using firebase::firestore::util::Status;

NS_ASSUME_NONNULL_BEGIN

#pragma mark - FIRTransaction

@interface FIRTransaction ()

- (instancetype)initWithTransaction:(FSTTransaction *)transaction
- (instancetype)initWithTransaction:(std::shared_ptr<Transaction>)transaction
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we actually want to have shared ownership here? I expect the FIRTransaction will have sole ownership, maybe?

firestore:(FIRFirestore *)firestore NS_DESIGNATED_INITIALIZER;

@property(nonatomic, strong, readonly) FSTTransaction *internalTransaction;
@property(nonatomic, strong, readonly) FIRFirestore *firestore;
@end

@implementation FIRTransaction (Internal)

+ (instancetype)transactionWithFSTTransaction:(FSTTransaction *)transaction
firestore:(FIRFirestore *)firestore {
return [[FIRTransaction alloc] initWithTransaction:transaction firestore:firestore];
+ (instancetype)transactionWithInternalTransaction:(std::shared_ptr<Transaction>)transaction
firestore:(FIRFirestore *)firestore {
return [[FIRTransaction alloc] initWithTransaction:std::move(transaction) firestore:firestore];
}

@end

@implementation FIRTransaction
@implementation FIRTransaction {
std::shared_ptr<Transaction> _internalTransaction;
}

- (instancetype)initWithTransaction:(FSTTransaction *)transaction
- (instancetype)initWithTransaction:(std::shared_ptr<Transaction>)transaction
firestore:(FIRFirestore *)firestore {
self = [super init];
if (self) {
_internalTransaction = transaction;
_internalTransaction = std::move(transaction);
_firestore = firestore;
}
return self;
Expand All @@ -77,7 +85,7 @@ - (FIRTransaction *)setData:(NSDictionary<NSString *, id> *)data
[self validateReference:document];
ParsedSetData parsed = merge ? [self.firestore.dataConverter parsedMergeData:data fieldMask:nil]
: [self.firestore.dataConverter parsedSetData:data];
[self.internalTransaction setData:std::move(parsed) forDocument:document.key];
_internalTransaction->Set(document.key, std::move(parsed));
return self;
}

Expand All @@ -86,60 +94,58 @@ - (FIRTransaction *)setData:(NSDictionary<NSString *, id> *)data
mergeFields:(NSArray<id> *)mergeFields {
[self validateReference:document];
ParsedSetData parsed = [self.firestore.dataConverter parsedMergeData:data fieldMask:mergeFields];
[self.internalTransaction setData:std::move(parsed) forDocument:document.key];
_internalTransaction->Set(document.key, std::move(parsed));
return self;
}

- (FIRTransaction *)updateData:(NSDictionary<id, id> *)fields
forDocument:(FIRDocumentReference *)document {
[self validateReference:document];
ParsedUpdateData parsed = [self.firestore.dataConverter parsedUpdateData:fields];
[self.internalTransaction updateData:std::move(parsed) forDocument:document.key];
_internalTransaction->Update(document.key, std::move(parsed));
return self;
}

- (FIRTransaction *)deleteDocument:(FIRDocumentReference *)document {
[self validateReference:document];
[self.internalTransaction deleteDocument:document.key];
_internalTransaction->Delete(document.key);
return self;
}

- (void)getDocument:(FIRDocumentReference *)document
completion:(void (^)(FIRDocumentSnapshot *_Nullable document,
NSError *_Nullable error))completion {
[self validateReference:document];
[self.internalTransaction
lookupDocumentsForKeys:{document.key}
completion:^(NSArray<FSTMaybeDocument *> *_Nullable documents,
NSError *_Nullable error) {
if (error) {
completion(nil, error);
return;
}
HARD_ASSERT(documents.count == 1,
"Mismatch in docs returned from document lookup.");
FSTMaybeDocument *internalDoc = documents.firstObject;
if ([internalDoc isKindOfClass:[FSTDeletedDocument class]]) {
FIRDocumentSnapshot *doc =
[FIRDocumentSnapshot snapshotWithFirestore:self.firestore
documentKey:document.key
document:nil
fromCache:NO
hasPendingWrites:NO];
completion(doc, nil);
} else if ([internalDoc isKindOfClass:[FSTDocument class]]) {
FIRDocumentSnapshot *doc =
[FIRDocumentSnapshot snapshotWithFirestore:self.firestore
documentKey:internalDoc.key
document:(FSTDocument *)internalDoc
fromCache:NO
hasPendingWrites:NO];
completion(doc, nil);
} else {
HARD_FAIL("BatchGetDocumentsRequest returned unexpected document type: %s",
NSStringFromClass([internalDoc class]));
}
}];
_internalTransaction->Lookup(
{document.key}, [self, document, completion](const std::vector<FSTMaybeDocument *> &documents,
const Status &status) {
if (!status.ok()) {
completion(nil, MakeNSError(status));
return;
}

HARD_ASSERT(documents.size() == 1, "Mismatch in docs returned from document lookup.");
FSTMaybeDocument *internalDoc = documents.front();
if ([internalDoc isKindOfClass:[FSTDeletedDocument class]]) {
FIRDocumentSnapshot *doc = [FIRDocumentSnapshot snapshotWithFirestore:self.firestore
documentKey:document.key
document:nil
fromCache:NO
hasPendingWrites:NO];
completion(doc, nil);
} else if ([internalDoc isKindOfClass:[FSTDocument class]]) {
FIRDocumentSnapshot *doc =
[FIRDocumentSnapshot snapshotWithFirestore:self.firestore
documentKey:internalDoc.key
document:(FSTDocument *)internalDoc
fromCache:NO
hasPendingWrites:NO];
completion(doc, nil);
} else {
HARD_FAIL("BatchGetDocumentsRequest returned unexpected document type: %s",
NSStringFromClass([internalDoc class]));
}
});
}

- (FIRDocumentSnapshot *_Nullable)getDocument:(FIRDocumentReference *)document
Expand Down
1 change: 0 additions & 1 deletion Firestore/Source/Core/FSTFirestoreClient.mm
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#import "Firestore/Source/Core/FSTEventManager.h"
#import "Firestore/Source/Core/FSTQuery.h"
#import "Firestore/Source/Core/FSTSyncEngine.h"
#import "Firestore/Source/Core/FSTTransaction.h"
#import "Firestore/Source/Core/FSTView.h"
#import "Firestore/Source/Local/FSTLRUGarbageCollector.h"
#import "Firestore/Source/Local/FSTLevelDB.h"
Expand Down
21 changes: 15 additions & 6 deletions Firestore/Source/Core/FSTSyncEngine.mm
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
#import "Firestore/Source/Core/FSTSyncEngine.h"

#include <map>
#include <memory>
#include <set>
#include <unordered_map>
#include <utility>

#import "FIRFirestoreErrors.h"
#import "Firestore/Source/Core/FSTQuery.h"
#import "Firestore/Source/Core/FSTTransaction.h"
#import "Firestore/Source/Core/FSTView.h"
#import "Firestore/Source/Core/FSTViewSnapshot.h"
#import "Firestore/Source/Local/FSTLocalStore.h"
Expand All @@ -36,18 +36,22 @@

#include "Firestore/core/src/firebase/firestore/auth/user.h"
#include "Firestore/core/src/firebase/firestore/core/target_id_generator.h"
#include "Firestore/core/src/firebase/firestore/core/transaction.h"
#include "Firestore/core/src/firebase/firestore/local/reference_set.h"
#include "Firestore/core/src/firebase/firestore/model/document_key.h"
#include "Firestore/core/src/firebase/firestore/model/document_map.h"
#include "Firestore/core/src/firebase/firestore/model/snapshot_version.h"
#include "Firestore/core/src/firebase/firestore/remote/remote_event.h"
#include "Firestore/core/src/firebase/firestore/util/error_apple.h"
#include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
#include "Firestore/core/src/firebase/firestore/util/log.h"
#include "Firestore/core/src/firebase/firestore/util/status.h"
#include "absl/types/optional.h"

using firebase::firestore::auth::HashUser;
using firebase::firestore::auth::User;
using firebase::firestore::core::TargetIdGenerator;
using firebase::firestore::core::Transaction;
using firebase::firestore::local::ReferenceSet;
using firebase::firestore::model::BatchId;
using firebase::firestore::model::DocumentKey;
Expand All @@ -62,6 +66,8 @@
using firebase::firestore::remote::RemoteStore;
using firebase::firestore::remote::TargetChange;
using firebase::firestore::util::AsyncQueue;
using firebase::firestore::util::MakeNSError;
using firebase::firestore::util::Status;

NS_ASSUME_NONNULL_BEGIN

Expand Down Expand Up @@ -287,27 +293,30 @@ - (void)transactionWithRetries:(int)retries
completion:(FSTVoidIDErrorBlock)completion {
workerQueue->VerifyIsCurrentQueue();
HARD_ASSERT(retries >= 0, "Got negative number of retries for transaction");
FSTTransaction *transaction = _remoteStore->CreateTransaction();

std::shared_ptr<Transaction> transaction = _remoteStore->CreateTransaction();
updateBlock(transaction, ^(id _Nullable result, NSError *_Nullable error) {
workerQueue->Enqueue(
[self, retries, workerQueue, updateBlock, completion, transaction, result, error] {
if (error) {
completion(nil, error);
return;
}
[transaction commitWithCompletion:^(NSError *_Nullable transactionError) {
if (!transactionError) {
transaction->Commit([self, retries, workerQueue, updateBlock, completion,
result](const Status &status) {
if (status.ok()) {
completion(result, nil);
return;
}

// TODO(b/35201829): Only retry on real transaction failures.
if (retries == 0) {
NSError *wrappedError =
[NSError errorWithDomain:FIRFirestoreErrorDomain
code:FIRFirestoreErrorCodeFailedPrecondition
userInfo:@{
NSLocalizedDescriptionKey : @"Transaction failed all retries.",
NSUnderlyingErrorKey : transactionError
NSUnderlyingErrorKey : MakeNSError(status)
}];
completion(nil, wrappedError);
return;
Expand All @@ -317,7 +326,7 @@ - (void)transactionWithRetries:(int)retries
workerQueue:workerQueue
updateBlock:updateBlock
completion:completion];
}];
});
});
});
}
Expand Down
Loading