FSTLevelDBMigrationsTests.mm 7.0 KB

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