Skip to content

Commit 3f248b8

Browse files
committed
Add api for auto cache index creation
1 parent 9205183 commit 3f248b8

10 files changed

+302
-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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <utility>
2222

2323
#import "FIRFirestoreSettings+Internal.h"
24+
#import "FIRPersistentCacheSettings+Internal.h"
2425
#import "FIRTransactionOptions+Internal.h"
2526
#import "FIRTransactionOptions.h"
2627

@@ -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,17 @@ @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]
543+
initWithPersistentCacheIndexManager:index_manager];
544+
}
545+
}
546+
return _indexManager;
547+
}
548+
536549
- (const DatabaseId &)databaseID {
537550
return _firestore->database_id();
538551
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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:
63+
(std::shared_ptr<const firebase::firestore::api::PersistentCacheIndexManager>)indexManager
64+
NS_DESIGNATED_INITIALIZER;
65+
66+
@end
67+
68+
NS_ASSUME_NONNULL_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:
31+
(std::shared_ptr<const PersistentCacheIndexManager>)indexManager {
32+
if (self = [super init]) {
33+
_indexManager = indexManager;
34+
}
35+
return self;
36+
}
37+
38+
- (void)enableIndexAutoCreation {
39+
_indexManager->EnableIndexAutoCreation();
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "Firestore/core/src/api/collection_reference.h"
2323
#include "Firestore/core/src/api/document_reference.h"
2424
#include "Firestore/core/src/api/listener_registration.h"
25+
#include "Firestore/core/src/api/persistent_cache_index_manager.h"
2526
#include "Firestore/core/src/api/settings.h"
2627
#include "Firestore/core/src/api/snapshots_in_sync_listener_registration.h"
2728
#include "Firestore/core/src/api/write_batch.h"
@@ -317,6 +318,17 @@ void Firestore::SetIndexConfiguration(const std::string& config,
317318
return;
318319
}
319320

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

Firestore/core/src/api/firestore.h

Lines changed: 8 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,9 @@ 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>
94+
persistent_cache_index_manager();
95+
9196
CollectionReference GetCollection(const std::string& collection_path);
9297
DocumentReference GetDocument(const std::string& document_path);
9398
WriteBatch GetBatch();
@@ -131,6 +136,9 @@ class Firestore : public std::enable_shared_from_this<Firestore> {
131136
auth_credentials_provider_;
132137
std::string persistence_key_;
133138

139+
std::shared_ptr<const PersistentCacheIndexManager>
140+
persistent_cache_index_manager_;
141+
134142
std::shared_ptr<util::Executor> user_executor_;
135143
std::shared_ptr<util::AsyncQueue> worker_queue_;
136144

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 <utility>
20+
21+
#include "Firestore/core/src/core/firestore_client.h"
22+
23+
namespace firebase {
24+
namespace firestore {
25+
namespace api {
26+
27+
PersistentCacheIndexManager::PersistentCacheIndexManager(
28+
std::shared_ptr<core::FirestoreClient> client)
29+
: client_(std::move(client)) {
30+
}
31+
32+
void PersistentCacheIndexManager::EnableIndexAutoCreation() const {
33+
client_->EnableIndexAutoCreation();
34+
}
35+
36+
void PersistentCacheIndexManager::DisableIndexAutoCreation() const {
37+
client_->DisableIndexAutoCreation();
38+
}
39+
40+
void PersistentCacheIndexManager::DeleteAllFieldIndexes() const {
41+
client_->DeleteAllFieldIndexes();
42+
}
43+
44+
} // namespace api
45+
} // namespace firestore
46+
} // namespace firebase
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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
33+
* used for local query execution.
34+
*
35+
* To use, call Firestore::persistent_cache_index_manager() to get an instance.
36+
*/
37+
class PersistentCacheIndexManager {
38+
public:
39+
explicit PersistentCacheIndexManager(
40+
std::shared_ptr<core::FirestoreClient> client);
41+
42+
/**
43+
* Enables SDK to create persistent cache indexes automatically for local
44+
* query execution when SDK believes cache indexes can help improves
45+
* performance.
46+
*
47+
* This feature is disabled by default.
48+
*/
49+
void EnableIndexAutoCreation() const;
50+
51+
/**
52+
* Stops creating persistent cache indexes automatically for local query
53+
* execution. The indexes which have been created by calling
54+
* EnableIndexAutoCreation() still take effect.
55+
*/
56+
void DisableIndexAutoCreation() const;
57+
58+
void DeleteAllFieldIndexes() const;
59+
60+
private:
61+
const std::shared_ptr<core::FirestoreClient> client_;
62+
};
63+
64+
} // namespace api
65+
} // namespace firestore
66+
} // namespace firebase
67+
68+
#endif // FIRESTORE_CORE_SRC_API_PERSISTENT_CACHE_INDEX_MANAGER_H_

Firestore/core/src/core/firestore_client.cc

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

600+
void FirestoreClient::EnableIndexAutoCreation() {
601+
VerifyNotTerminated();
602+
// worker_queue_->Enqueue([this] {
603+
// local_store_->EnableIndexAutoCreation();
604+
//});
605+
}
606+
607+
void FirestoreClient::DisableIndexAutoCreation() {
608+
VerifyNotTerminated();
609+
}
610+
611+
void FirestoreClient::DeleteAllFieldIndexes() {
612+
VerifyNotTerminated();
613+
}
614+
600615
void FirestoreClient::LoadBundle(
601616
std::unique_ptr<util::ByteStream> bundle_data,
602617
std::shared_ptr<api::LoadBundleTask> result_task) {

Firestore/core/src/core/firestore_client.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,24 @@ 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
192+
* query execution when SDK believes cache indexes can help improves
193+
* performance.
194+
*
195+
* This feature is disabled by default.
196+
*/
197+
void EnableIndexAutoCreation();
198+
199+
/**
200+
* Stops creating persistent cache indexes automatically for local query
201+
* execution. The indexes which have been created by calling
202+
* EnableIndexAutoCreation() still take effect.
203+
*/
204+
void DisableIndexAutoCreation();
205+
206+
void DeleteAllFieldIndexes();
207+
190208
void LoadBundle(std::unique_ptr<util::ByteStream> bundle_data,
191209
std::shared_ptr<api::LoadBundleTask> result_task);
192210

0 commit comments

Comments
 (0)