FSTPersistenceTestHelpers.mm 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright 2017 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 "Firestore/Example/Tests/Local/FSTPersistenceTestHelpers.h"
  17. #include <utility>
  18. #import "Firestore/Source/Local/FSTLRUGarbageCollector.h"
  19. #import "Firestore/Source/Local/FSTLevelDB.h"
  20. #import "Firestore/Source/Local/FSTLocalSerializer.h"
  21. #import "Firestore/Source/Local/FSTMemoryPersistence.h"
  22. #import "Firestore/Source/Remote/FSTSerializerBeta.h"
  23. #include "Firestore/core/src/firebase/firestore/model/database_id.h"
  24. #include "Firestore/core/src/firebase/firestore/util/filesystem.h"
  25. #include "Firestore/core/src/firebase/firestore/util/path.h"
  26. #include "Firestore/core/src/firebase/firestore/util/status.h"
  27. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  28. namespace util = firebase::firestore::util;
  29. using firebase::firestore::local::LruParams;
  30. using firebase::firestore::model::DatabaseId;
  31. using firebase::firestore::util::Path;
  32. using firebase::firestore::util::Status;
  33. NS_ASSUME_NONNULL_BEGIN
  34. @implementation FSTPersistenceTestHelpers
  35. + (FSTLocalSerializer *)localSerializer {
  36. // This owns the DatabaseIds since we do not have FirestoreClient instance to own them.
  37. static DatabaseId database_id{"p", "d"};
  38. FSTSerializerBeta *remoteSerializer = [[FSTSerializerBeta alloc] initWithDatabaseID:&database_id];
  39. return [[FSTLocalSerializer alloc] initWithRemoteSerializer:remoteSerializer];
  40. }
  41. + (Path)levelDBDir {
  42. Path dir = util::TempDir().AppendUtf8("FSTPersistenceTestHelpers");
  43. // Delete the directory first to ensure isolation between runs.
  44. util::Status status = util::RecursivelyDelete(dir);
  45. if (!status.ok()) {
  46. [NSException
  47. raise:NSInternalInconsistencyException
  48. format:@"Failed to clean up leveldb path %s: %s", dir.c_str(), status.ToString().c_str()];
  49. }
  50. return dir;
  51. }
  52. + (FSTLevelDB *)levelDBPersistenceWithDir:(Path)dir {
  53. return [self levelDBPersistenceWithDir:dir lruParams:LruParams::Default()];
  54. }
  55. + (FSTLevelDB *)levelDBPersistenceWithDir:(Path)dir lruParams:(LruParams)params {
  56. FSTLocalSerializer *serializer = [self localSerializer];
  57. FSTLevelDB *db =
  58. [[FSTLevelDB alloc] initWithDirectory:std::move(dir) serializer:serializer lruParams:params];
  59. Status status = [db start];
  60. if (!status.ok()) {
  61. [NSException raise:NSInternalInconsistencyException
  62. format:@"Failed to start leveldb persistence: %s", status.ToString().c_str()];
  63. }
  64. return db;
  65. }
  66. + (FSTLevelDB *)levelDBPersistenceWithLruParams:(LruParams)lruParams {
  67. return [self levelDBPersistenceWithDir:[self levelDBDir] lruParams:lruParams];
  68. }
  69. + (FSTLevelDB *)levelDBPersistence {
  70. return [self levelDBPersistenceWithDir:[self levelDBDir]];
  71. }
  72. + (FSTMemoryPersistence *)eagerGCMemoryPersistence {
  73. FSTMemoryPersistence *persistence = [FSTMemoryPersistence persistenceWithEagerGC];
  74. Status status = [persistence start];
  75. if (!status.ok()) {
  76. [NSException raise:NSInternalInconsistencyException
  77. format:@"Failed to start memory persistence: %s", status.ToString().c_str()];
  78. }
  79. return persistence;
  80. }
  81. + (FSTMemoryPersistence *)lruMemoryPersistence {
  82. return [self lruMemoryPersistenceWithLruParams:LruParams::Default()];
  83. }
  84. + (FSTMemoryPersistence *)lruMemoryPersistenceWithLruParams:(LruParams)lruParams {
  85. FSTLocalSerializer *serializer = [self localSerializer];
  86. FSTMemoryPersistence *persistence =
  87. [FSTMemoryPersistence persistenceWithLruParams:lruParams serializer:serializer];
  88. Status status = [persistence start];
  89. if (!status.ok()) {
  90. [NSException raise:NSInternalInconsistencyException
  91. format:@"Failed to start memory persistence: %s", status.ToString().c_str()];
  92. }
  93. return persistence;
  94. }
  95. @end
  96. NS_ASSUME_NONNULL_END