Skip to content

Commit d387784

Browse files
var-consta-maurice
authored andcommitted
Make FirestoreInternal store a pointer to the corresponding public Firestore instance.
The existing setup instead stores a pointer to `App` and uses it to retrieve the corresponding `Firestore` from the "main" cache of Firestore instances. This is unnecessarily tricky and falls apart in tests, which use their own cache of Firestore instances not synchronized with the "main" cache. What happens is that a call to e.g. `DocumentReference::firestore()` tries to retrieve an instance by App from the main cache, but because the main cache is empty, it leads to creation of a new Firestore instance. PiperOrigin-RevId: 295244529
1 parent da52289 commit d387784

File tree

9 files changed

+53
-33
lines changed

9 files changed

+53
-33
lines changed

firestore/src/android/document_reference_android.cc

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,8 @@ METHOD_LOOKUP_DEFINITION(document_reference,
5252
DOCUMENT_REFERENCE_METHODS)
5353

5454
Firestore* DocumentReferenceInternal::firestore() {
55-
App* app = firestore_->app();
56-
FIREBASE_ASSERT(app != nullptr);
57-
Firestore* firestore = Firestore::GetInstance(app);
58-
FIREBASE_ASSERT(firestore != nullptr);
59-
return firestore;
55+
FIREBASE_ASSERT(firestore_->firestore_public() != nullptr);
56+
return firestore_->firestore_public();
6057
}
6158

6259
const std::string& DocumentReferenceInternal::id() const {

firestore/src/android/document_snapshot_android.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ METHOD_LOOKUP_DEFINITION(document_snapshot,
4444
DOCUMENT_SNAPSHOT_METHODS)
4545

4646
Firestore* DocumentSnapshotInternal::firestore() const {
47-
return Firestore::GetInstance(firestore_->app());
47+
FIREBASE_ASSERT(firestore_->firestore_public() != nullptr);
48+
return firestore_->firestore_public();
4849
}
4950

5051
const std::string& DocumentSnapshotInternal::id() const {

firestore/src/android/firestore_android.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222
namespace firebase {
2323
namespace firestore {
2424

25+
class Firestore;
26+
class ListenerRegistrationInternal;
2527
class Transaction;
2628
class TransactionFunction;
2729
class WriteBatch;
28-
class ListenerRegistrationInternal;
2930

3031
// Used for registering global callbacks. See
3132
// firebase::util::RegisterCallbackOnTask for context.
@@ -52,6 +53,7 @@ class FirestoreInternal {
5253
public:
5354
using ApiType = Firestore;
5455

56+
// Note: call `set_firestore_public` immediately after construction.
5557
explicit FirestoreInternal(App* app);
5658
~FirestoreInternal();
5759

@@ -142,6 +144,13 @@ class FirestoreInternal {
142144
return static_cast<InternalType*>(value.internal_);
143145
}
144146

147+
void set_firestore_public(Firestore* firestore_public) {
148+
firestore_public_ = firestore_public;
149+
}
150+
151+
Firestore* firestore_public() { return firestore_public_; }
152+
const Firestore* firestore_public() const { return firestore_public_; }
153+
145154
private:
146155
// Gets the reference-counted Future implementation of this instance, which
147156
// can be used to create a Future.
@@ -165,6 +174,7 @@ class FirestoreInternal {
165174
static int initialize_count_;
166175

167176
App* app_ = nullptr;
177+
Firestore* firestore_public_ = nullptr;
168178
// Java Firestore global ref.
169179
jobject obj_;
170180

firestore/src/android/query_android.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ METHOD_LOOKUP_DEFINITION(query,
2424
QUERY_METHODS)
2525

2626
Firestore* QueryInternal::firestore() {
27-
return Firestore::GetInstance(firestore_->app());
27+
FIREBASE_ASSERT(firestore_->firestore_public() != nullptr);
28+
return firestore_->firestore_public();
2829
}
2930

3031
Query QueryInternal::OrderBy(const FieldPath& field,

firestore/src/common/firestore.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ Firestore::Firestore(::firebase::App* app)
121121
Firestore::Firestore(FirestoreInternal* internal)
122122
// TODO(wuandy): use make_unique once it is supported for our build here.
123123
: internal_(internal) {
124+
internal_->set_firestore_public(this);
125+
124126
if (internal_->initialized()) {
125127
CleanupNotifier* app_notifier = CleanupNotifier::FindByOwner(app());
126128
assert(app_notifier);

firestore/src/ios/firestore_ios.cc

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ FirestoreInternal::FirestoreInternal(App* app)
5252
FirestoreInternal::FirestoreInternal(
5353
App* app, std::unique_ptr<CredentialsProvider> credentials)
5454
: app_(NOT_NULL(app)),
55-
firestore_(CreateFirestore(app, std::move(credentials))) {
55+
firestore_core_(CreateFirestore(app, std::move(credentials))) {
5656
ApplyDefaultSettings();
5757
}
5858

@@ -68,25 +68,25 @@ std::shared_ptr<api::Firestore> FirestoreInternal::CreateFirestore(
6868

6969
CollectionReference FirestoreInternal::Collection(
7070
const char* collection_path) const {
71-
auto result = firestore_->GetCollection(collection_path);
71+
auto result = firestore_core_->GetCollection(collection_path);
7272
return MakePublic(std::move(result));
7373
}
7474

7575
DocumentReference FirestoreInternal::Document(const char* document_path) const {
76-
auto result = firestore_->GetDocument(document_path);
76+
auto result = firestore_core_->GetDocument(document_path);
7777
return MakePublic(std::move(result));
7878
}
7979

8080
Query FirestoreInternal::CollectionGroup(const char* collection_id) const {
81-
core::Query core_query = firestore_->GetCollectionGroup(collection_id);
82-
api::Query api_query(std::move(core_query), firestore_);
81+
core::Query core_query = firestore_core_->GetCollectionGroup(collection_id);
82+
api::Query api_query(std::move(core_query), firestore_core_);
8383
return MakePublic(std::move(api_query));
8484
}
8585

8686
Settings FirestoreInternal::settings() const {
8787
Settings result;
8888

89-
const api::Settings& from = firestore_->settings();
89+
const api::Settings& from = firestore_core_->settings();
9090
result.set_host(from.host());
9191
result.set_ssl_enabled(from.ssl_enabled());
9292
result.set_persistence_enabled(from.persistence_enabled());
@@ -105,14 +105,14 @@ void FirestoreInternal::set_settings(const Settings& from) {
105105
// TODO(varconst): implement `cache_size_bytes` in public `Settings` and
106106
// uncomment.
107107
// settings.set_cache_size_bytes(from.cache_size_bytes());
108-
firestore_->set_settings(settings);
108+
firestore_core_->set_settings(settings);
109109

110110
std::unique_ptr<Executor> user_executor = from.CreateExecutor();
111-
firestore_->set_user_executor(std::move(user_executor));
111+
firestore_core_->set_user_executor(std::move(user_executor));
112112
}
113113

114114
WriteBatch FirestoreInternal::batch() const {
115-
return MakePublic(firestore_->GetBatch());
115+
return MakePublic(firestore_core_->GetBatch());
116116
}
117117

118118
Future<void> FirestoreInternal::RunTransaction(TransactionFunction* update) {
@@ -170,8 +170,8 @@ Future<void> FirestoreInternal::RunTransaction(
170170
}
171171
};
172172

173-
firestore_->RunTransaction(std::move(update_callback),
174-
std::move(final_result_callback));
173+
firestore_core_->RunTransaction(std::move(update_callback),
174+
std::move(final_result_callback));
175175

176176
return promise.future();
177177
}
@@ -183,7 +183,7 @@ Future<void> FirestoreInternal::RunTransactionLastResult() {
183183
Future<void> FirestoreInternal::DisableNetwork() {
184184
auto promise =
185185
promise_factory_.CreatePromise<void>(AsyncApi::kDisableNetwork);
186-
firestore_->DisableNetwork(StatusCallbackWithPromise(promise));
186+
firestore_core_->DisableNetwork(StatusCallbackWithPromise(promise));
187187
return promise.future();
188188
}
189189

@@ -193,7 +193,7 @@ Future<void> FirestoreInternal::DisableNetworkLastResult() {
193193

194194
Future<void> FirestoreInternal::EnableNetwork() {
195195
auto promise = promise_factory_.CreatePromise<void>(AsyncApi::kEnableNetwork);
196-
firestore_->EnableNetwork(StatusCallbackWithPromise(promise));
196+
firestore_core_->EnableNetwork(StatusCallbackWithPromise(promise));
197197
return promise.future();
198198
}
199199

@@ -204,7 +204,7 @@ Future<void> FirestoreInternal::EnableNetworkLastResult() {
204204
Future<void> FirestoreInternal::Terminate() {
205205
auto promise = promise_factory_.CreatePromise<void>(AsyncApi::kTerminate);
206206
ClearListeners();
207-
firestore_->Terminate(StatusCallbackWithPromise(promise));
207+
firestore_core_->Terminate(StatusCallbackWithPromise(promise));
208208
return promise.future();
209209
}
210210

@@ -215,7 +215,7 @@ Future<void> FirestoreInternal::TerminateLastResult() {
215215
Future<void> FirestoreInternal::WaitForPendingWrites() {
216216
auto promise =
217217
promise_factory_.CreatePromise<void>(AsyncApi::kWaitForPendingWrites);
218-
firestore_->WaitForPendingWrites(StatusCallbackWithPromise(promise));
218+
firestore_core_->WaitForPendingWrites(StatusCallbackWithPromise(promise));
219219
return promise.future();
220220
}
221221

@@ -226,7 +226,7 @@ Future<void> FirestoreInternal::WaitForPendingWritesLastResult() {
226226
Future<void> FirestoreInternal::ClearPersistence() {
227227
auto promise =
228228
promise_factory_.CreatePromise<void>(AsyncApi::kClearPersistence);
229-
firestore_->ClearPersistence(StatusCallbackWithPromise(promise));
229+
firestore_core_->ClearPersistence(StatusCallbackWithPromise(promise));
230230
return promise.future();
231231
}
232232

@@ -247,14 +247,14 @@ ListenerRegistration FirestoreInternal::AddSnapshotsInSyncListener(
247247
std::function<void()> listener_function = [listener] {
248248
listener->OnEvent(Error::Ok);
249249
};
250-
auto result = firestore_->AddSnapshotsInSyncListener(
250+
auto result = firestore_core_->AddSnapshotsInSyncListener(
251251
ListenerWithCallback(std::move(listener_function)));
252252
return MakePublic(std::move(result), this);
253253
}
254254

255255
ListenerRegistration FirestoreInternal::AddSnapshotsInSyncListener(
256256
std::function<void()> callback) {
257-
auto result = firestore_->AddSnapshotsInSyncListener(
257+
auto result = firestore_core_->AddSnapshotsInSyncListener(
258258
ListenerWithCallback(std::move(callback)));
259259
return MakePublic(std::move(result), this);
260260
}

firestore/src/ios/firestore_ios.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121
namespace firebase {
2222
namespace firestore {
2323

24+
class Firestore;
2425
class ListenerRegistrationInternal;
2526
class Transaction;
2627
class TransactionFunction;
2728
class WriteBatch;
2829

2930
class FirestoreInternal {
3031
public:
32+
// Note: call `set_firestore_public` immediately after construction.
3133
explicit FirestoreInternal(App* app);
3234
~FirestoreInternal();
3335

@@ -83,18 +85,23 @@ class FirestoreInternal {
8385
std::function<void()> callback);
8486

8587
const model::DatabaseId& database_id() const {
86-
return firestore_->database_id();
88+
return firestore_core_->database_id();
8789
}
8890

8991
// Manages the ListenerRegistrationInternal objects.
9092
void RegisterListenerRegistration(ListenerRegistrationInternal* registration);
9193
void UnregisterListenerRegistration(
9294
ListenerRegistrationInternal* registration);
9395

94-
// TODO(varconst): this method should become unnecessary once
95-
// `api::Transaction::Lookup()` starts returning C++ documents.
96+
void set_firestore_public(Firestore* firestore_public) {
97+
firestore_public_ = firestore_public;
98+
}
99+
100+
Firestore* firestore_public() { return firestore_public_; }
101+
const Firestore* firestore_public() const { return firestore_public_; }
102+
96103
const std::shared_ptr<api::Firestore>& firestore_core() const {
97-
return firestore_;
104+
return firestore_core_;
98105
}
99106

100107
private:
@@ -127,7 +134,8 @@ class FirestoreInternal {
127134
void ApplyDefaultSettings();
128135

129136
App* app_ = nullptr;
130-
std::shared_ptr<api::Firestore> firestore_;
137+
Firestore* firestore_public_ = nullptr;
138+
std::shared_ptr<api::Firestore> firestore_core_;
131139

132140
CleanupNotifier cleanup_;
133141

firestore/src/ios/util_ios.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ FirestoreInternal* GetFirestoreInternal(T* object) {
1616

1717
template <typename T>
1818
Firestore* GetFirestore(T* object) {
19-
FirestoreInternal* internal = GetFirestoreInternal(object);
20-
return Firestore::GetInstance(internal->app());
19+
return GetFirestoreInternal(object)->firestore_public();
2120
}
2221

2322
} // namespace firestore

firestore/src/stub/firestore_stub.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ class FirestoreInternal {
120120
return static_cast<InternalType*>(value.internal_);
121121
}
122122

123+
void set_firestore_public(Firestore*) {}
124+
123125
private:
124126
CleanupNotifier cleanup_;
125127
App* app_;

0 commit comments

Comments
 (0)