23
23
#import " Firestore/Source/Local/FSTLevelDB.h"
24
24
#import " Firestore/Source/Local/FSTLevelDBKey.h"
25
25
#import " Firestore/Source/Local/FSTLevelDBMigrations.h"
26
+ #import " Firestore/Source/Local/FSTLevelDBMutationQueue.h"
26
27
#import " Firestore/Source/Local/FSTLevelDBQueryCache.h"
27
28
28
29
#include " Firestore/core/src/firebase/firestore/util/ordered_code.h"
30
+ #include " Firestore/core/src/firebase/firestore/util/status.h"
31
+ #include " Firestore/core/test/firebase/firestore/testutil/testutil.h"
29
32
#include " leveldb/db.h"
30
33
31
34
#import " Firestore/Example/Tests/Local/FSTPersistenceTestHelpers.h"
32
35
33
36
NS_ASSUME_NONNULL_BEGIN
34
37
38
+ using firebase::firestore::FirestoreErrorCode;
35
39
using firebase::firestore::local::LevelDbTransaction;
36
40
using firebase::firestore::util::OrderedCode;
41
+ using firebase::firestore::testutil::Key;
37
42
using leveldb::DB;
38
43
using leveldb::Options;
39
44
using leveldb::Status;
@@ -94,11 +99,7 @@ - (void)testCountsQueries {
94
99
}
95
100
// Add a dummy entry after the targets to make sure the iteration is correctly bounded.
96
101
// Use a table that would sort logically right after that table 'target'.
97
- std::string dummyKey;
98
- // Magic number that indicates a table name follows. Needed to mimic the prefix to the target
99
- // table.
100
- OrderedCode::WriteSignedNumIncreasing (&dummyKey, 5 );
101
- OrderedCode::WriteString (&dummyKey, " targetA" );
102
+ std::string dummyKey = [self dummyKeyForTable: " targetA" ];
102
103
transaction.Put (dummyKey, " dummy" );
103
104
transaction.Commit ();
104
105
}
@@ -112,6 +113,80 @@ - (void)testCountsQueries {
112
113
}
113
114
}
114
115
116
+ #define ASSERT_NOT_FOUND (transaction, key ) \
117
+ do { \
118
+ std::string unused_result; \
119
+ Status status = transaction.Get (key, &unused_result); \
120
+ XCTAssertTrue (status.IsNotFound ()); \
121
+ } while (0 )
122
+
123
+ #define ASSERT_FOUND (transaction, key ) \
124
+ do { \
125
+ std::string unused_result; \
126
+ Status status = transaction.Get (key, &unused_result); \
127
+ XCTAssertTrue (status.ok ()); \
128
+ } while (0 )
129
+
130
+ - (void )testDropsTheQueryCache {
131
+ NSString *userID = @" user" ;
132
+ FSTBatchID batchID = 1 ;
133
+ FSTTargetID targetID = 2 ;
134
+
135
+ FSTDocumentKey *key1 = Key (" documents/1" );
136
+ FSTDocumentKey *key2 = Key (" documents/2" );
137
+
138
+ std::string targetKeys[] = {
139
+ [FSTLevelDBTargetKey keyWithTargetID: targetID],
140
+ [FSTLevelDBTargetDocumentKey keyWithTargetID: targetID documentKey: key1],
141
+ [FSTLevelDBTargetDocumentKey keyWithTargetID: targetID documentKey: key2],
142
+ [FSTLevelDBDocumentTargetKey keyWithDocumentKey: key1 targetID: targetID],
143
+ [FSTLevelDBDocumentTargetKey keyWithDocumentKey: key2 targetID: targetID]};
144
+
145
+ std::string preservedKeys[] = {[self dummyKeyForTable: " targetA" ],
146
+ [FSTLevelDBMutationQueueKey keyWithUserID: userID],
147
+ [FSTLevelDBMutationKey keyWithUserID: userID batchID: batchID]};
148
+
149
+ {
150
+ // Setup some targets to be counted in the migration.
151
+ LevelDbTransaction transaction (_db.get (), " testDropsTheQueryCache setup" );
152
+ [FSTLevelDBMigrations runMigrationsWithTransaction: &transaction upToVersion: 1 ];
153
+
154
+ for (const std::string &key : targetKeys) {
155
+ transaction.Put (key, " target" );
156
+ }
157
+
158
+ for (const std::string &key : preservedKeys) {
159
+ transaction.Put (key, " preserved" );
160
+ }
161
+ transaction.Commit ();
162
+ }
163
+
164
+ {
165
+ LevelDbTransaction transaction (_db.get (), " testDropsTheQueryCache" );
166
+ [FSTLevelDBMigrations runMigrationsWithTransaction: &transaction upToVersion: 2 ];
167
+
168
+ for (const std::string &key : targetKeys) {
169
+ ASSERT_NOT_FOUND (transaction, key);
170
+ }
171
+
172
+ for (const std::string &key : preservedKeys) {
173
+ ASSERT_FOUND (transaction, key);
174
+ }
175
+ }
176
+ }
177
+
178
+ /* *
179
+ * Creates the name of a dummy entry to make sure the iteration is correctly bounded.
180
+ */
181
+ - (std::string)dummyKeyForTable : (const char *)tableName {
182
+ std::string dummyKey;
183
+ // Magic number that indicates a table name follows. Needed to mimic the prefix to the target
184
+ // table.
185
+ OrderedCode::WriteSignedNumIncreasing (&dummyKey, 5 );
186
+ OrderedCode::WriteString (&dummyKey, tableName);
187
+ return dummyKey;
188
+ }
189
+
115
190
@end
116
191
117
192
NS_ASSUME_NONNULL_END
0 commit comments