Skip to content

Commit 338b6fd

Browse files
committed
Add api for auto cache index creation
1 parent 9205183 commit 338b6fd

10 files changed

+274
-0
lines changed

Firestore/Source/API/FIRFirestore+Internal.h

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

2626
@class FIRApp;
2727
@class FSTUserDataReader;
28+
@class FIRPersistentCacheIndexManager;
2829

2930
namespace firebase {
3031
namespace firestore {
@@ -78,6 +79,9 @@ NS_ASSUME_NONNULL_BEGIN
7879

7980
- (const std::shared_ptr<firebase::firestore::util::AsyncQueue> &)workerQueue;
8081

82+
//TODO(csi): make this function public
83+
@property(nonatomic, readonly) FIRPersistentCacheIndexManager *persistentCacheIndexManager;
84+
8185
@property(nonatomic, assign, readonly) std::shared_ptr<api::Firestore> wrapped;
8286

8387
@property(nonatomic, assign, readonly) const model::DatabaseId &databaseID;

Firestore/Source/API/FIRFirestore.mm

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#import "FIRFirestoreSettings+Internal.h"
2424
#import "FIRTransactionOptions+Internal.h"
2525
#import "FIRTransactionOptions.h"
26+
#import "FIRPersistentCacheSettings+Internal.h"
2627

2728
#import "FirebaseCore/Extension/FIRAppInternal.h"
2829
#import "FirebaseCore/Extension/FIRComponentContainer.h"
@@ -106,6 +107,7 @@ @implementation FIRFirestore {
106107
std::shared_ptr<Firestore> _firestore;
107108
FIRFirestoreSettings *_settings;
108109
__weak id<FSTFirestoreInstanceRegistry> _registry;
110+
FIRPersistentCacheIndexManager *_indexManager;
109111
}
110112

111113
+ (void)initialize {
@@ -533,6 +535,16 @@ @implementation FIRFirestore (Internal)
533535
return _firestore->worker_queue();
534536
}
535537

538+
- (FIRPersistentCacheIndexManager *)persistentCacheIndexManager {
539+
if (!_indexManager) {
540+
auto index_manager = _firestore->persistent_cache_index_manager();
541+
if (index_manager) {
542+
_indexManager = [[FIRPersistentCacheIndexManager alloc] initWithPersistentCacheIndexManager: index_manager];
543+
}
544+
}
545+
return _indexManager;
546+
}
547+
536548
- (const DatabaseId &)databaseID {
537549
return _firestore->database_id();
538550
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#import <Foundation/Foundation.h>
18+
19+
#include <memory>
20+
21+
#include "Firestore/core/src/api/persistent_cache_index_manager.h"
22+
23+
NS_ASSUME_NONNULL_BEGIN
24+
25+
// TODO(sum/avg) move the contents of this category to
26+
// ../Public/FirebaseFirestore/FIRPersistentCacheIndexManager.h
27+
/**
28+
* A PersistentCacheIndexManager which you can config persistent cache indexes used for
29+
* local query execution.
30+
*/
31+
NS_SWIFT_NAME(PersistentCacheIndexManager)
32+
@interface FIRPersistentCacheIndexManager : NSObject
33+
34+
/** :nodoc: */
35+
- (instancetype)init
36+
__attribute__((unavailable("FIRPersistentCacheIndexManager cannot be created directly.")));
37+
38+
/**
39+
* Enables SDK to create persistent cache indexes automatically for local query execution when SDK
40+
* believes cache indexes can help improves performance.
41+
*
42+
* This feature is disabled by default.
43+
*/
44+
- (void)enableIndexAutoCreation NS_SWIFT_NAME(enableIndexAutoCreation);
45+
46+
/**
47+
* Stops creating persistent cache indexes automatically for local query execution. The indexes
48+
* which have been created by calling `enableIndexAutoCreation` still take effect.
49+
*/
50+
- (void)disableIndexAutoCreation NS_SWIFT_NAME(disableIndexAutoCreation);
51+
52+
/**
53+
* Removes all persistent cache indexes. Please note this function will also deletes indexes
54+
* generated by `setIndexConfigurationFromJSON` and `setIndexConfigurationFromStream`.
55+
*/
56+
- (void)deleteAllIndexes NS_SWIFT_NAME(deleteAllIndexes);
57+
58+
@end
59+
60+
@interface FIRPersistentCacheIndexManager (/* Init */)
61+
62+
- (instancetype)initWithPersistentCacheIndexManager:(std::shared_ptr<const firebase::firestore::api::PersistentCacheIndexManager>) indexManager
63+
NS_DESIGNATED_INITIALIZER;
64+
65+
@end
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#import "Firestore/Source/API/FIRFirestore+Internal.h"
18+
#import "Firestore/Source/API/FIRPersistentCacheSettings+Internal.h"
19+
20+
using firebase::firestore::api::Firestore;
21+
using firebase::firestore::api::PersistentCacheIndexManager;
22+
23+
NS_ASSUME_NONNULL_BEGIN
24+
25+
@implementation FIRPersistentCacheIndexManager {
26+
/** The `Firestore` instance that created this index manager. */
27+
std::shared_ptr<const PersistentCacheIndexManager> _indexManager;
28+
}
29+
30+
- (instancetype)initWithPersistentCacheIndexManager: (std::shared_ptr<const PersistentCacheIndexManager>)indexManager {
31+
if (self = [super init]) {
32+
_indexManager = indexManager;
33+
}
34+
return self;
35+
}
36+
37+
- (void)enableIndexAutoCreation {
38+
_indexManager->EnableIndexAutoCreation();
39+
}
40+
41+
42+
- (void)disableIndexAutoCreation {
43+
_indexManager->DisableIndexAutoCreation();
44+
}
45+
46+
- (void)deleteAllIndexes {
47+
_indexManager->DeleteAllFieldIndexes();
48+
}
49+
50+
@end

Firestore/core/src/api/firestore.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "Firestore/core/src/api/settings.h"
2626
#include "Firestore/core/src/api/snapshots_in_sync_listener_registration.h"
2727
#include "Firestore/core/src/api/write_batch.h"
28+
#include "Firestore/core/src/api/persistent_cache_index_manager.h"
2829
#include "Firestore/core/src/core/event_listener.h"
2930
#include "Firestore/core/src/core/firestore_client.h"
3031
#include "Firestore/core/src/core/query.h"
@@ -317,6 +318,15 @@ void Firestore::SetIndexConfiguration(const std::string& config,
317318
return;
318319
}
319320

321+
std::shared_ptr<const PersistentCacheIndexManager> Firestore::persistent_cache_index_manager() {
322+
EnsureClientConfigured();
323+
if (settings_.persistence_enabled() && !persistent_cache_index_manager_) {
324+
persistent_cache_index_manager_ = std::make_shared<const PersistentCacheIndexManager>(client_);
325+
}
326+
327+
return persistent_cache_index_manager_;
328+
}
329+
320330
std::shared_ptr<LoadBundleTask> Firestore::LoadBundle(
321331
std::unique_ptr<util::ByteStream> bundle_data) {
322332
EnsureClientConfigured();

Firestore/core/src/api/firestore.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ struct Empty;
4646

4747
namespace api {
4848

49+
class PersistentCacheIndexManager;
50+
4951
extern const int kDefaultTransactionMaxAttempts;
5052

5153
class Firestore : public std::enable_shared_from_this<Firestore> {
@@ -88,6 +90,8 @@ class Firestore : public std::enable_shared_from_this<Firestore> {
8890

8991
void set_user_executor(std::unique_ptr<util::Executor> user_executor);
9092

93+
std::shared_ptr<const PersistentCacheIndexManager> persistent_cache_index_manager();
94+
9195
CollectionReference GetCollection(const std::string& collection_path);
9296
DocumentReference GetDocument(const std::string& document_path);
9397
WriteBatch GetBatch();
@@ -131,6 +135,8 @@ class Firestore : public std::enable_shared_from_this<Firestore> {
131135
auth_credentials_provider_;
132136
std::string persistence_key_;
133137

138+
std::shared_ptr<const PersistentCacheIndexManager> persistent_cache_index_manager_;
139+
134140
std::shared_ptr<util::Executor> user_executor_;
135141
std::shared_ptr<util::AsyncQueue> worker_queue_;
136142

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "Firestore/core/src/api/persistent_cache_index_manager.h"
18+
19+
#include "Firestore/core/src/core/firestore_client.h"
20+
21+
namespace firebase {
22+
namespace firestore {
23+
namespace api {
24+
25+
PersistentCacheIndexManager::PersistentCacheIndexManager(
26+
std::shared_ptr<core::FirestoreClient> client)
27+
: client_(std::move(client)) {}
28+
29+
void PersistentCacheIndexManager::EnableIndexAutoCreation() const {
30+
client_->EnableIndexAutoCreation();
31+
}
32+
33+
void PersistentCacheIndexManager::DisableIndexAutoCreation() const {
34+
client_->DisableIndexAutoCreation();
35+
}
36+
37+
void PersistentCacheIndexManager::DeleteAllFieldIndexes() const {
38+
client_->DeleteAllFieldIndexes();
39+
}
40+
41+
} // namespace api
42+
} // namespace firestore
43+
} // namespace firebase
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef FIRESTORE_CORE_SRC_API_PERSISTENT_CACHE_INDEX_MANAGER_H_
18+
#define FIRESTORE_CORE_SRC_API_PERSISTENT_CACHE_INDEX_MANAGER_H_
19+
20+
#include <memory>
21+
22+
namespace firebase {
23+
namespace firestore {
24+
25+
namespace core {
26+
class FirestoreClient;
27+
} // namespace core
28+
29+
namespace api {
30+
31+
/**
32+
* A PersistentCacheIndexManager which you can config persistent cache indexes used for local query execution.
33+
*
34+
* To use, call Firestore::persistent_cache_index_manager() to get an instance.
35+
*/
36+
class PersistentCacheIndexManager {
37+
public:
38+
PersistentCacheIndexManager(std::shared_ptr<core::FirestoreClient> client);
39+
40+
/**
41+
* Enables SDK to create persistent cache indexes automatically for local query execution when SDK believes cache indexes can help improves performance.
42+
*
43+
* This feature is disabled by default.
44+
*/
45+
void EnableIndexAutoCreation() const;
46+
47+
/**
48+
* Stops creating persistent cache indexes automatically for local query execution. The indexes which have been created by calling EnableIndexAutoCreation() still take effect.
49+
*/
50+
void DisableIndexAutoCreation() const;
51+
52+
void DeleteAllFieldIndexes() const;
53+
54+
private:
55+
const std::shared_ptr<core::FirestoreClient> client_;
56+
};
57+
58+
} // namespace api
59+
} // namespace firestore
60+
} // namespace firebase
61+
62+
63+
#endif // FIRESTORE_CORE_SRC_API_PERSISTENT_CACHE_INDEX_MANAGER_H_

Firestore/core/src/core/firestore_client.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,13 @@ void FirestoreClient::ConfigureFieldIndexes(
597597
});
598598
}
599599

600+
void FirestoreClient::EnableIndexAutoCreation() {
601+
VerifyNotTerminated();
602+
//worker_queue_->Enqueue([this] {
603+
//local_store_->EnableIndexAutoCreation();
604+
//});
605+
}
606+
600607
void FirestoreClient::LoadBundle(
601608
std::unique_ptr<util::ByteStream> bundle_data,
602609
std::shared_ptr<api::LoadBundleTask> result_task) {

Firestore/core/src/core/firestore_client.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,20 @@ class FirestoreClient : public std::enable_shared_from_this<FirestoreClient> {
187187

188188
void ConfigureFieldIndexes(std::vector<model::FieldIndex> parsed_indexes);
189189

190+
/**
191+
* Enables SDK to create persistent cache indexes automatically for local query execution when SDK believes cache indexes can help improves performance.
192+
*
193+
* This feature is disabled by default.
194+
*/
195+
void EnableIndexAutoCreation();
196+
197+
/**
198+
* Stops creating persistent cache indexes automatically for local query execution. The indexes which have been created by calling EnableIndexAutoCreation() still take effect.
199+
*/
200+
void DisableIndexAutoCreation();
201+
202+
void DeleteAllFieldIndexes();
203+
190204
void LoadBundle(std::unique_ptr<util::ByteStream> bundle_data,
191205
std::shared_ptr<api::LoadBundleTask> result_task);
192206

0 commit comments

Comments
 (0)