FSTPersistenceTestHelpers.mm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 *ldb;
  58. util::Status status = [FSTLevelDB dbWithDirectory:std::move(dir)
  59. serializer:serializer
  60. lruParams:params
  61. ptr:&ldb];
  62. if (!status.ok()) {
  63. [NSException raise:NSInternalInconsistencyException
  64. format:@"Failed to open DB: %s", status.ToString().c_str()];
  65. }
  66. return ldb;
  67. }
  68. + (FSTLevelDB *)levelDBPersistenceWithLruParams:(LruParams)lruParams {
  69. return [self levelDBPersistenceWithDir:[self levelDBDir] lruParams:lruParams];
  70. }
  71. + (FSTLevelDB *)levelDBPersistence {
  72. return [self levelDBPersistenceWithDir:[self levelDBDir]];
  73. }
  74. + (FSTMemoryPersistence *)eagerGCMemoryPersistence {
  75. return [FSTMemoryPersistence persistenceWithEagerGC];
  76. }
  77. + (FSTMemoryPersistence *)lruMemoryPersistence {
  78. return [self lruMemoryPersistenceWithLruParams:LruParams::Default()];
  79. }
  80. + (FSTMemoryPersistence *)lruMemoryPersistenceWithLruParams:(LruParams)lruParams {
  81. FSTLocalSerializer *serializer = [self localSerializer];
  82. return [FSTMemoryPersistence persistenceWithLruParams:lruParams serializer:serializer];
  83. }
  84. @end
  85. NS_ASSUME_NONNULL_END