FSTPersistenceTestHelpers.mm 3.4 KB

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