Skip to content

Commit 065a5bb

Browse files
Public Auto Persistent Index Creation API (#11757)
Co-authored-by: Nick Cooke <[email protected]>
1 parent 26563cc commit 065a5bb

15 files changed

+404
-53
lines changed

Firestore/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Unreleased
2+
- [feature] Add the option to allow the SDK to create cache indexes automatically to
3+
improve query execution locally. (#11596)
4+
15
# 10.12.0
26
- [feature] Implemented an optimization in the local cache synchronization logic
37
that reduces the number of billed document reads when documents were deleted

Firestore/Example/Firestore.xcodeproj/project.pbxproj

Lines changed: 25 additions & 1 deletion
Large diffs are not rendered by default.

Firestore/Example/Tests/Integration/API/FIRDatabaseTests.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1844,7 +1844,7 @@ - (void)testGetValidPersistentCacheIndexManager {
18441844
XCTAssertNotNil(db2.persistentCacheIndexManager);
18451845

18461846
// Disable persistent disk cache
1847-
FIRFirestore *db3 = [FIRFirestore firestoreForDatabase:@"PersistentCacheIndexManagerDB3"];
1847+
FIRFirestore *db3 = [FIRFirestore firestoreForDatabase:@"MemoryCacheIndexManagerDB1"];
18481848
FIRFirestoreSettings *settings3 = [db3 settings];
18491849
[settings3 setCacheSettings:[[FIRMemoryCacheSettings alloc] init]];
18501850
[db3 setSettings:settings3];

Firestore/Example/Tests/Integration/API/FIRIndexingTests.mm

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@
1414
* limitations under the License.
1515
*/
1616

17+
// TODO(csi): Delete this once setIndexConfigurationFromJSON and setIndexConfigurationFromStream
18+
// are removed.
19+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
20+
1721
#import <FirebaseFirestore/FirebaseFirestore.h>
1822

1923
#import <XCTest/XCTest.h>
2024

21-
#import "Firestore/Source/API/FIRFirestore+Internal.h"
22-
#import "Firestore/Source/API/FIRPersistentCacheIndexManager+Internal.h"
23-
2425
#import "Firestore/Example/Tests/Util/FSTHelpers.h"
2526
#import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
27+
#import "Firestore/Source/Public/FirebaseFirestore/FIRPersistentCacheIndexManager.h"
2628

2729
@interface FIRIndexingTests : FSTIntegrationTestCase
2830
@end
@@ -194,13 +196,13 @@ - (void)testAutoIndexCreationSetSuccessfullyUsingDefault {
194196
- (void)testAutoIndexCreationAfterFailsTermination {
195197
[self terminateFirestore:self.db];
196198

197-
FSTAssertThrows([self.db.persistentCacheIndexManager enableIndexAutoCreation],
199+
XCTAssertThrows([self.db.persistentCacheIndexManager enableIndexAutoCreation],
198200
@"The client has already been terminated.");
199201

200-
FSTAssertThrows([self.db.persistentCacheIndexManager disableIndexAutoCreation],
202+
XCTAssertThrows([self.db.persistentCacheIndexManager disableIndexAutoCreation],
201203
@"The client has already been terminated.");
202204

203-
FSTAssertThrows([self.db.persistentCacheIndexManager deleteAllIndexes],
205+
XCTAssertThrows([self.db.persistentCacheIndexManager deleteAllIndexes],
204206
@"The client has already been terminated.");
205207
}
206208

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#import <Foundation/Foundation.h>
16+
17+
NS_ASSUME_NONNULL_BEGIN
18+
19+
@interface FSTExceptionCatcher : NSObject
20+
21+
typedef void (^ThrowingBlock)(void);
22+
23+
+ (BOOL)catchException:(ThrowingBlock)block error:(__autoreleasing NSError **)error;
24+
25+
@end
26+
27+
NS_ASSUME_NONNULL_END
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#import "Firestore/Example/Tests/Util/FSTExceptionCatcher.h"
16+
17+
@implementation FSTExceptionCatcher
18+
19+
+ (BOOL)catchException:(ThrowingBlock)block error:(__autoreleasing NSError **)error {
20+
@try {
21+
block();
22+
return YES;
23+
} @catch (NSException *exception) {
24+
NSMutableDictionary *info = [NSMutableDictionary dictionary];
25+
[info setValue:exception.name forKey:@"ExceptionName"];
26+
[info setValue:exception.reason forKey:@"ExceptionReason"];
27+
[info setValue:exception.callStackReturnAddresses forKey:@"ExceptionCallStackReturnAddresses"];
28+
[info setValue:exception.callStackSymbols forKey:@"ExceptionCallStackSymbols"];
29+
[info setValue:exception.userInfo forKey:@"ExceptionUserInfo"];
30+
31+
// Just using error code `FIRErrorCodeConfigFailed` for now
32+
NSInteger FIRErrorCodeConfigFailed = -114;
33+
*error = [[NSError alloc] initWithDomain:exception.name
34+
code:FIRErrorCodeConfigFailed
35+
userInfo:info];
36+
return NO;
37+
}
38+
}
39+
40+
@end

Firestore/Source/API/FIRFirestore+Internal.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,6 @@ NS_ASSUME_NONNULL_BEGIN
7979

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

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

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

Firestore/Source/API/FIRFirestore.mm

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17+
// TODO(csi): Delete this once setIndexConfigurationFromJSON and setIndexConfigurationFromStream
18+
// are removed.
19+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
20+
1721
#import "FIRFirestore+Internal.h"
1822

1923
#include <memory>
@@ -535,12 +539,14 @@ @implementation FIRFirestore (Internal)
535539
return _firestore->worker_queue();
536540
}
537541

538-
- (FIRPersistentCacheIndexManager *)persistentCacheIndexManager {
542+
- (nullable FIRPersistentCacheIndexManager *)persistentCacheIndexManager {
539543
if (!_indexManager) {
540544
auto index_manager = _firestore->persistent_cache_index_manager();
541545
if (index_manager) {
542546
_indexManager = [[FIRPersistentCacheIndexManager alloc]
543547
initWithPersistentCacheIndexManager:index_manager];
548+
} else {
549+
return nil;
544550
}
545551
}
546552
return _indexManager;

Firestore/Source/API/FIRPersistentCacheIndexManager+Internal.h

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,51 +13,15 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
#import "FIRPersistentCacheIndexManager.h"
1617

1718
#import <Foundation/Foundation.h>
1819

1920
#include <memory>
20-
2121
#include "Firestore/core/src/api/persistent_cache_index_manager.h"
2222

2323
NS_ASSUME_NONNULL_BEGIN
2424

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 [[FIRFirestore firestore] setIndexConfigurationFromJSON] and [[FIRFirestore
55-
* firestore] setIndexConfigurationFromStream], which are deprecated.
56-
*/
57-
- (void)deleteAllIndexes NS_SWIFT_NAME(deleteAllIndexes());
58-
59-
@end
60-
6125
@interface FIRPersistentCacheIndexManager (/* Init */)
6226

6327
- (instancetype)initWithPersistentCacheIndexManager:

Firestore/Source/Public/FirebaseFirestore/FIRFirestore.h

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
@class FIRTransaction;
2929
@class FIRTransactionOptions;
3030
@class FIRWriteBatch;
31+
@class FIRPersistentCacheIndexManager;
3132

3233
NS_ASSUME_NONNULL_BEGIN
3334

@@ -103,7 +104,16 @@ NS_SWIFT_NAME(Firestore)
103104
#pragma mark - Configure FieldIndexes
104105

105106
/**
106-
* This method is in preview. API signature and functionality are subject to change.
107+
* A PersistentCacheIndexManager which you can config persistent cache indexes used for
108+
* local query execution.
109+
*/
110+
@property(nonatomic, readonly, nullable)
111+
FIRPersistentCacheIndexManager *persistentCacheIndexManager;
112+
113+
/**
114+
* NOTE: This preview method will be deprecated in a future major release. Consider using
115+
* `PersistentCacheIndexManager.enableIndexAutoCreation()` to let the SDK decide whether to create
116+
* cache indexes for queries running locally.
107117
*
108118
* Configures indexing for local query execution. Any previous index configuration is overridden.
109119
*
@@ -121,10 +131,15 @@ NS_SWIFT_NAME(Firestore)
121131
*/
122132
- (void)setIndexConfigurationFromJSON:(NSString *)json
123133
completion:(nullable void (^)(NSError *_Nullable error))completion
124-
NS_SWIFT_NAME(setIndexConfiguration(_:completion:));
134+
NS_SWIFT_NAME(setIndexConfiguration(_:completion:)) DEPRECATED_MSG_ATTRIBUTE(
135+
"Instead of creating cache indexes manually, consider using "
136+
"`PersistentCacheIndexManager.enableIndexAutoCreation()` to let the SDK decide whether to "
137+
"create cache indexes for queries running locally.");
125138

126139
/**
127-
* This method is in preview. API signature and functionality are subject to change.
140+
* NOTE: This preview method will be deprecated in a future major release. Consider using
141+
* `PersistentCacheIndexManager.enableIndexAutoCreation()` to let the SDK decide whether to create
142+
* cache indexes for queries running locally.
128143
*
129144
* Configures indexing for local query execution. Any previous index configuration is overridden.
130145
*
@@ -145,7 +160,10 @@ NS_SWIFT_NAME(Firestore)
145160
*/
146161
- (void)setIndexConfigurationFromStream:(NSInputStream *)stream
147162
completion:(nullable void (^)(NSError *_Nullable error))completion
148-
NS_SWIFT_NAME(setIndexConfiguration(_:completion:));
163+
NS_SWIFT_NAME(setIndexConfiguration(_:completion:)) DEPRECATED_MSG_ATTRIBUTE(
164+
"Instead of creating cache indexes manually, consider using "
165+
"`PersistentCacheIndexManager.enableIndexAutoCreation()` to let the SDK decide whether to "
166+
"create cache indexes for queries running locally.");
149167

150168
#pragma mark - Collections and Documents
151169

0 commit comments

Comments
 (0)