FSTLevelDB.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 <Foundation/Foundation.h>
  17. #include <memory>
  18. #import "Firestore/Source/Local/FSTPersistence.h"
  19. #include "Firestore/core/src/firebase/firestore/core/database_info.h"
  20. #include "Firestore/core/src/firebase/firestore/local/leveldb_transaction.h"
  21. #include "leveldb/db.h"
  22. @class FSTLocalSerializer;
  23. NS_ASSUME_NONNULL_BEGIN
  24. /** A LevelDB-backed instance of FSTPersistence. */
  25. // TODO(mikelehen): Rename to FSTLevelDBPersistence.
  26. @interface FSTLevelDB : NSObject <FSTPersistence, FSTTransactional>
  27. /**
  28. * Initializes the LevelDB in the given directory. Note that all expensive startup work including
  29. * opening any database files is deferred until -[FSTPersistence start] is called.
  30. */
  31. - (instancetype)initWithDirectory:(NSString *)directory
  32. serializer:(FSTLocalSerializer *)serializer NS_DESIGNATED_INITIALIZER;
  33. - (instancetype)init __attribute__((unavailable("Use -initWithDirectory: instead.")));
  34. /** Finds a suitable directory to serve as the root of all Firestore local storage. */
  35. + (NSString *)documentsDirectory;
  36. /**
  37. * Computes a unique storage directory for the given identifying components of local storage.
  38. *
  39. * @param databaseInfo The identifying information for the local storage instance.
  40. * @param documentsDirectory The root document directory relative to which the storage directory
  41. * will be created. Usually just +[FSTLevelDB documentsDir].
  42. * @return A storage directory unique to the instance identified by databaseInfo.
  43. */
  44. + (NSString *)storageDirectoryForDatabaseInfo:
  45. (const firebase::firestore::core::DatabaseInfo &)databaseInfo
  46. documentsDirectory:(NSString *)documentsDirectory;
  47. /**
  48. * Starts LevelDB-backed persistent storage by opening the database files, creating the DB if it
  49. * does not exist.
  50. *
  51. * The leveldb directory is created relative to the appropriate document storage directory for the
  52. * platform: NSDocumentDirectory on iOS or $HOME/.firestore on macOS.
  53. */
  54. - (BOOL)start:(NSError **)error;
  55. // What follows is the Objective-C++ extension to the API.
  56. /**
  57. * @return A standard set of read options
  58. */
  59. + (const leveldb::ReadOptions)standardReadOptions;
  60. /**
  61. * Creates an NSError based on the given status if the status is not ok.
  62. *
  63. * @param status The status of the preceding LevelDB operation.
  64. * @param description A printf-style format string describing what kind of failure happened if
  65. * @a status is not ok. Additional parameters are substituted into the placeholders in this
  66. * format string.
  67. *
  68. * @return An NSError with its localizedDescription composed from the description format and its
  69. * localizedFailureReason composed from any error message embedded in @a status.
  70. */
  71. + (nullable NSError *)errorWithStatus:(leveldb::Status)status
  72. description:(NSString *)description, ... NS_FORMAT_FUNCTION(2, 3);
  73. /**
  74. * Converts the given @a status to an NSString describing the status condition, suitable for
  75. * logging or inclusion in an NSError.
  76. *
  77. * @param status The status of the preceding LevelDB operation.
  78. *
  79. * @return An NSString describing the status (even if the status was ok).
  80. */
  81. + (NSString *)descriptionOfStatus:(leveldb::Status)status;
  82. /** The native db pointer, allocated during start. */
  83. @property(nonatomic, assign, readonly) std::shared_ptr<leveldb::DB> ptr;
  84. @property(nonatomic, readonly) firebase::firestore::local::LevelDbTransaction *currentTransaction;
  85. @end
  86. NS_ASSUME_NONNULL_END