FSTLevelDBMigrationsTests.mm 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright 2018 Google
  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. #import <XCTest/XCTest.h>
  17. #include <memory>
  18. #include <string>
  19. #include <vector>
  20. #import "Firestore/Protos/objc/firestore/local/Target.pbobjc.h"
  21. #import "Firestore/Source/Local/FSTLevelDB.h"
  22. #import "Firestore/Source/Local/FSTLevelDBMutationQueue.h"
  23. #import "Firestore/Source/Local/FSTLevelDBQueryCache.h"
  24. #include "Firestore/core/src/firebase/firestore/local/leveldb_key.h"
  25. #include "Firestore/core/src/firebase/firestore/local/leveldb_migrations.h"
  26. #include "Firestore/core/src/firebase/firestore/util/ordered_code.h"
  27. #include "Firestore/core/src/firebase/firestore/util/status.h"
  28. #include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
  29. #include "absl/strings/match.h"
  30. #include "leveldb/db.h"
  31. #import "Firestore/Example/Tests/Local/FSTPersistenceTestHelpers.h"
  32. NS_ASSUME_NONNULL_BEGIN
  33. using firebase::firestore::FirestoreErrorCode;
  34. using firebase::firestore::local::LevelDbDocumentTargetKey;
  35. using firebase::firestore::local::LevelDbMigrations;
  36. using firebase::firestore::local::LevelDbMutationKey;
  37. using firebase::firestore::local::LevelDbMutationQueueKey;
  38. using firebase::firestore::local::LevelDbQueryTargetKey;
  39. using firebase::firestore::local::LevelDbTargetDocumentKey;
  40. using firebase::firestore::local::LevelDbTargetKey;
  41. using firebase::firestore::local::LevelDbTransaction;
  42. using firebase::firestore::model::BatchId;
  43. using firebase::firestore::model::TargetId;
  44. using firebase::firestore::testutil::Key;
  45. using firebase::firestore::util::OrderedCode;
  46. using firebase::firestore::util::Path;
  47. using leveldb::DB;
  48. using leveldb::Options;
  49. using leveldb::Status;
  50. using SchemaVersion = LevelDbMigrations::SchemaVersion;
  51. @interface FSTLevelDBMigrationsTests : XCTestCase
  52. @end
  53. @implementation FSTLevelDBMigrationsTests {
  54. std::unique_ptr<DB> _db;
  55. }
  56. - (void)setUp {
  57. Options options;
  58. options.error_if_exists = true;
  59. options.create_if_missing = true;
  60. Path dir = [FSTPersistenceTestHelpers levelDBDir];
  61. DB *db;
  62. Status status = DB::Open(options, dir.ToUtf8String(), &db);
  63. XCTAssert(status.ok(), @"Failed to create db: %s", status.ToString().c_str());
  64. _db.reset(db);
  65. }
  66. - (void)tearDown {
  67. _db.reset();
  68. }
  69. - (void)testAddsTargetGlobal {
  70. FSTPBTargetGlobal *metadata = [FSTLevelDBQueryCache readTargetMetadataFromDB:_db.get()];
  71. XCTAssertNil(metadata, @"Not expecting metadata yet, we should have an empty db");
  72. LevelDbMigrations::RunMigrations(_db.get());
  73. metadata = [FSTLevelDBQueryCache readTargetMetadataFromDB:_db.get()];
  74. XCTAssertNotNil(metadata, @"Migrations should have added the metadata");
  75. }
  76. - (void)testSetsVersionNumber {
  77. {
  78. LevelDbTransaction transaction(_db.get(), "testSetsVersionNumber before");
  79. SchemaVersion initial = LevelDbMigrations::ReadSchemaVersion(&transaction);
  80. XCTAssertEqual(0, initial, "No version should be equivalent to 0");
  81. }
  82. {
  83. // Pick an arbitrary high migration number and migrate to it.
  84. LevelDbMigrations::RunMigrations(_db.get());
  85. LevelDbTransaction transaction(_db.get(), "testSetsVersionNumber after");
  86. SchemaVersion actual = LevelDbMigrations::ReadSchemaVersion(&transaction);
  87. XCTAssertGreaterThan(actual, 0, @"Expected to migrate to a schema version > 0");
  88. }
  89. }
  90. #define ASSERT_NOT_FOUND(transaction, key) \
  91. do { \
  92. std::string unused_result; \
  93. Status status = transaction.Get(key, &unused_result); \
  94. XCTAssertTrue(status.IsNotFound()); \
  95. } while (0)
  96. #define ASSERT_FOUND(transaction, key) \
  97. do { \
  98. std::string unused_result; \
  99. Status status = transaction.Get(key, &unused_result); \
  100. XCTAssertTrue(status.ok()); \
  101. } while (0)
  102. - (void)testDropsTheQueryCache {
  103. std::string userID{"user"};
  104. BatchId batchID = 1;
  105. TargetId targetID = 2;
  106. FSTDocumentKey *key1 = Key("documents/1");
  107. FSTDocumentKey *key2 = Key("documents/2");
  108. std::string targetKeys[] = {
  109. LevelDbTargetKey::Key(targetID),
  110. LevelDbTargetDocumentKey::Key(targetID, key1),
  111. LevelDbTargetDocumentKey::Key(targetID, key2),
  112. LevelDbDocumentTargetKey::Key(key1, targetID),
  113. LevelDbDocumentTargetKey::Key(key2, targetID),
  114. LevelDbQueryTargetKey::Key("foo.bar.baz", targetID),
  115. };
  116. // Keys that should not be modified by the dropping the query cache
  117. std::string preservedKeys[] = {
  118. [self dummyKeyForTable:"targetA"],
  119. LevelDbMutationQueueKey::Key(userID),
  120. LevelDbMutationKey::Key(userID, batchID),
  121. };
  122. LevelDbMigrations::RunMigrations(_db.get(), 2);
  123. {
  124. // Setup some targets to be counted in the migration.
  125. LevelDbTransaction transaction(_db.get(), "testDropsTheQueryCache setup");
  126. for (const std::string &key : targetKeys) {
  127. transaction.Put(key, "target");
  128. }
  129. for (const std::string &key : preservedKeys) {
  130. transaction.Put(key, "preserved");
  131. }
  132. transaction.Commit();
  133. }
  134. LevelDbMigrations::RunMigrations(_db.get(), 3);
  135. {
  136. LevelDbTransaction transaction(_db.get(), "testDropsTheQueryCache");
  137. for (const std::string &key : targetKeys) {
  138. ASSERT_NOT_FOUND(transaction, key);
  139. }
  140. for (const std::string &key : preservedKeys) {
  141. ASSERT_FOUND(transaction, key);
  142. }
  143. FSTPBTargetGlobal *metadata = [FSTLevelDBQueryCache readTargetMetadataFromDB:_db.get()];
  144. XCTAssertNotNil(metadata, @"Metadata should have been added");
  145. XCTAssertEqual(metadata.targetCount, 0);
  146. }
  147. }
  148. - (void)testDropsTheQueryCacheWithThousandsOfEntries {
  149. LevelDbMigrations::RunMigrations(_db.get(), 2);
  150. {
  151. // Setup some targets to be destroyed.
  152. LevelDbTransaction transaction(_db.get(), "testDropsTheQueryCacheWithThousandsOfEntries setup");
  153. for (int i = 0; i < 10000; ++i) {
  154. transaction.Put(LevelDbTargetKey::Key(i), "");
  155. }
  156. transaction.Commit();
  157. }
  158. LevelDbMigrations::RunMigrations(_db.get(), 3);
  159. {
  160. LevelDbTransaction transaction(_db.get(), "Verify");
  161. std::string prefix = LevelDbTargetKey::KeyPrefix();
  162. auto it = transaction.NewIterator();
  163. std::vector<std::string> found_keys;
  164. for (it->Seek(prefix); it->Valid() && absl::StartsWith(it->key(), prefix); it->Next()) {
  165. found_keys.push_back(std::string{it->key()});
  166. }
  167. XCTAssertEqual(found_keys, std::vector<std::string>{});
  168. }
  169. }
  170. /**
  171. * Creates the name of a dummy entry to make sure the iteration is correctly bounded.
  172. */
  173. - (std::string)dummyKeyForTable:(const char *)tableName {
  174. std::string dummyKey;
  175. // Magic number that indicates a table name follows. Needed to mimic the prefix to the target
  176. // table.
  177. OrderedCode::WriteSignedNumIncreasing(&dummyKey, 5);
  178. OrderedCode::WriteString(&dummyKey, tableName);
  179. return dummyKey;
  180. }
  181. @end
  182. NS_ASSUME_NONNULL_END