FSTPersistenceTestHelpers.mm 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. auto remoteSerializer = [[FSTSerializerBeta alloc] initWithDatabaseID:DatabaseId("p", "d")];
  37. return [[FSTLocalSerializer alloc] initWithRemoteSerializer:remoteSerializer];
  38. }
  39. + (Path)levelDBDir {
  40. Path dir = util::TempDir().AppendUtf8("FSTPersistenceTestHelpers");
  41. // Delete the directory first to ensure isolation between runs.
  42. util::Status status = util::RecursivelyDelete(dir);
  43. if (!status.ok()) {
  44. [NSException
  45. raise:NSInternalInconsistencyException
  46. format:@"Failed to clean up leveldb path %s: %s", dir.c_str(), status.ToString().c_str()];
  47. }
  48. return dir;
  49. }
  50. + (FSTLevelDB *)levelDBPersistenceWithDir:(Path)dir {
  51. return [self levelDBPersistenceWithDir:dir lruParams:LruParams::Default()];
  52. }
  53. + (FSTLevelDB *)levelDBPersistenceWithDir:(Path)dir lruParams:(LruParams)params {
  54. FSTLocalSerializer *serializer = [self localSerializer];
  55. FSTLevelDB *ldb;
  56. util::Status status = [FSTLevelDB dbWithDirectory:std::move(dir)
  57. serializer:serializer
  58. lruParams:params
  59. ptr:&ldb];
  60. if (!status.ok()) {
  61. [NSException raise:NSInternalInconsistencyException
  62. format:@"Failed to open DB: %s", status.ToString().c_str()];
  63. }
  64. return ldb;
  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. return [FSTMemoryPersistence persistenceWithEagerGC];
  74. }
  75. + (FSTMemoryPersistence *)lruMemoryPersistence {
  76. return [self lruMemoryPersistenceWithLruParams:LruParams::Default()];
  77. }
  78. + (FSTMemoryPersistence *)lruMemoryPersistenceWithLruParams:(LruParams)lruParams {
  79. FSTLocalSerializer *serializer = [self localSerializer];
  80. return [FSTMemoryPersistence persistenceWithLruParams:lruParams serializer:serializer];
  81. }
  82. @end
  83. NS_ASSUME_NONNULL_END