14
14
* limitations under the License.
15
15
*/
16
16
17
- #import " Firestore/Source/Local/FSTLevelDBMigrations .h"
17
+ #include " Firestore/core/src/firebase/firestore/local/leveldb_migrations .h"
18
18
19
19
#include < string>
20
+ #include < utility>
20
21
21
- #import " Firestore/Protos/objc/firestore/local/Target.pbobjc.h"
22
- #import " Firestore/Source/Local/FSTLevelDBQueryCache.h"
23
-
22
+ #include " Firestore/Protos/nanopb/firestore/local/target.nanopb.h"
24
23
#include " Firestore/core/src/firebase/firestore/local/leveldb_key.h"
25
- #include " Firestore/core/src/firebase/firestore/util/hard_assert.h"
26
- #include " absl/base/macros.h"
27
- #include " absl/memory/memory.h"
24
+ #include " Firestore/core/src/firebase/firestore/nanopb/writer.h"
28
25
#include " absl/strings/match.h"
29
- #include " leveldb/write_batch.h"
30
26
31
- NS_ASSUME_NONNULL_BEGIN
27
+ namespace firebase {
28
+ namespace firestore {
29
+ namespace local {
30
+
31
+ using leveldb::Iterator;
32
+ using leveldb::Slice;
33
+ using leveldb::Status;
34
+ using leveldb::WriteOptions;
35
+ using nanopb::Writer;
36
+
37
+ namespace {
32
38
33
39
/* *
34
40
* Schema version for the iOS client.
35
41
*
36
- * Note that tables aren't a concept in LevelDB. They exist in our schema as just prefixes on keys.
37
- * This means tables don't need to be created but they also can't easily be dropped and re-created.
42
+ * Note that tables aren't a concept in LevelDB. They exist in our schema as
43
+ * just prefixes on keys. This means tables don't need to be created but they
44
+ * also can't easily be dropped and re-created.
38
45
*
39
46
* Migrations:
40
- * * Migration 1 used to ensure the target_global row existed, without clearing it. No longer
41
- * required because migration 3 unconditionally clears it.
42
- * * Migration 2 used to ensure that the target_global row had a correct count of targets. No
43
- * longer required because migration 3 deletes them all.
44
- * * Migration 3 deletes the entire query cache to deal with cache corruption related to
45
- * limbo resolution. Addresses https://github.com/firebase/firebase-ios-sdk/issues/1548.
47
+ * * Migration 1 used to ensure the target_global row existed, without
48
+ * clearing it. No longer required because migration 3 unconditionally
49
+ * clears it.
50
+ * * Migration 2 used to ensure that the target_global row had a correct count
51
+ * of targets. No longer required because migration 3 deletes them all.
52
+ * * Migration 3 deletes the entire query cache to deal with cache corruption
53
+ * related to limbo resolution. Addresses
54
+ * https://github.com/firebase/firebase-ios-sdk/issues/1548.
46
55
*/
47
- static FSTLevelDBSchemaVersion kSchemaVersion = 3 ;
48
-
49
- using firebase::firestore::local::LevelDbDocumentTargetKey;
50
- using firebase::firestore::local::LevelDbQueryTargetKey;
51
- using firebase::firestore::local::LevelDbTargetDocumentKey;
52
- using firebase::firestore::local::LevelDbTargetGlobalKey;
53
- using firebase::firestore::local::LevelDbTargetKey;
54
- using firebase::firestore::local::LevelDbTransaction;
55
- using firebase::firestore::local::LevelDbVersionKey;
56
- using leveldb::Iterator;
57
- using leveldb::Status;
58
- using leveldb::Slice;
59
- using leveldb::WriteOptions;
56
+ const LevelDbMigrations::SchemaVersion kSchemaVersion = 3 ;
60
57
61
58
/* *
62
- * Save the given version number as the current version of the schema of the database.
59
+ * Save the given version number as the current version of the schema of the
60
+ * database.
63
61
* @param version The version to save
64
62
* @param transaction The transaction in which to save the new version number
65
63
*/
66
- static void SaveVersion (FSTLevelDBSchemaVersion version, LevelDbTransaction *transaction) {
64
+ void SaveVersion (LevelDbMigrations::SchemaVersion version,
65
+ LevelDbTransaction* transaction) {
67
66
std::string key = LevelDbVersionKey::Key ();
68
67
std::string version_string = std::to_string (version);
69
68
transaction->Put (key, version_string);
70
69
}
71
70
72
- static void DeleteEverythingWithPrefix (const std::string & prefix, leveldb::DB * db) {
71
+ void DeleteEverythingWithPrefix (const std::string& prefix, leveldb::DB* db) {
73
72
bool more_deletes = true ;
74
73
while (more_deletes) {
75
74
LevelDbTransaction transaction (db, " Delete everything with prefix" );
76
75
auto it = transaction.NewIterator ();
77
76
78
77
more_deletes = false ;
79
- for (it->Seek (prefix); it->Valid () && absl::StartsWith (it->key (), prefix); it->Next ()) {
78
+ for (it->Seek (prefix); it->Valid () && absl::StartsWith (it->key (), prefix);
79
+ it->Next ()) {
80
80
if (transaction.changed_keys () >= 1000 ) {
81
81
more_deletes = true ;
82
82
break ;
@@ -89,7 +89,7 @@ static void DeleteEverythingWithPrefix(const std::string &prefix, leveldb::DB *d
89
89
}
90
90
91
91
/* * Migration 3. */
92
- static void ClearQueryCache (leveldb::DB * db) {
92
+ void ClearQueryCache (leveldb::DB* db) {
93
93
DeleteEverythingWithPrefix (LevelDbTargetKey::KeyPrefix (), db);
94
94
DeleteEverythingWithPrefix (LevelDbDocumentTargetKey::KeyPrefix (), db);
95
95
DeleteEverythingWithPrefix (LevelDbTargetDocumentKey::KeyPrefix (), db);
@@ -98,16 +98,22 @@ static void ClearQueryCache(leveldb::DB *db) {
98
98
LevelDbTransaction transaction (db, " Drop query cache" );
99
99
100
100
// Reset the target global entry too (to reset the target count).
101
- transaction.Put (LevelDbTargetGlobalKey::Key (), [FSTPBTargetGlobal message ]);
101
+ firestore_client_TargetGlobal target_global{};
102
+
103
+ std::string bytes;
104
+ Writer writer = Writer::Wrap (&bytes);
105
+ writer.WriteNanopbMessage (firestore_client_TargetGlobal_fields,
106
+ &target_global);
107
+ transaction.Put (LevelDbTargetGlobalKey::Key (), std::move (bytes));
102
108
103
109
SaveVersion (3 , &transaction);
104
110
transaction.Commit ();
105
111
}
106
112
107
- @implementation FSTLevelDBMigrations
113
+ } // namespace
108
114
109
- + (FSTLevelDBSchemaVersion) schemaVersionWithTransaction :
110
- (firebase::firestore::local:: LevelDbTransaction *) transaction {
115
+ LevelDbMigrations::SchemaVersion LevelDbMigrations::ReadSchemaVersion (
116
+ LevelDbTransaction* transaction) {
111
117
std::string key = LevelDbVersionKey::Key ();
112
118
std::string version_string;
113
119
Status status = transaction->Get (key, &version_string);
@@ -118,22 +124,23 @@ + (FSTLevelDBSchemaVersion)schemaVersionWithTransaction:
118
124
}
119
125
}
120
126
121
- + ( void ) runMigrationsWithDatabase : (leveldb::DB *) database {
122
- [ self runMigrationsWithDatabase: database upToVersion: kSchemaVersion ] ;
127
+ void LevelDbMigrations::RunMigrations (leveldb::DB* db) {
128
+ RunMigrations (db, kSchemaVersion ) ;
123
129
}
124
130
125
- + ( void ) runMigrationsWithDatabase : (leveldb::DB *) database
126
- upToVersion : (FSTLevelDBSchemaVersion) toVersion {
127
- LevelDbTransaction transaction{database , " Read schema version" };
128
- FSTLevelDBSchemaVersion fromVersion = [ self schemaVersionWithTransaction: &transaction] ;
131
+ void LevelDbMigrations::RunMigrations (leveldb::DB* db,
132
+ SchemaVersion to_version) {
133
+ LevelDbTransaction transaction{db , " Read schema version" };
134
+ SchemaVersion from_version = ReadSchemaVersion ( &transaction) ;
129
135
130
- // This must run unconditionally because schema migrations were added to iOS after the first
131
- // release. There may be clients that have never run any migrations that have existing targets.
132
- if (fromVersion < 3 && toVersion >= 3 ) {
133
- ClearQueryCache (database);
136
+ // This must run unconditionally because schema migrations were added to iOS
137
+ // after the first release. There may be clients that have never run any
138
+ // migrations that have existing targets.
139
+ if (from_version < 3 && to_version >= 3 ) {
140
+ ClearQueryCache (db);
134
141
}
135
142
}
136
143
137
- @end
138
-
139
- NS_ASSUME_NONNULL_END
144
+ } // namespace local
145
+ } // namespace firestore
146
+ } // namespace firebase
0 commit comments