FSTLevelDB.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #import "Firestore/Source/Local/FSTPersistence.h"
  18. #ifdef __cplusplus
  19. #include <memory>
  20. namespace leveldb {
  21. class DB;
  22. class Status;
  23. }
  24. #endif
  25. @class FSTDatabaseInfo;
  26. @class FSTLocalSerializer;
  27. NS_ASSUME_NONNULL_BEGIN
  28. /** A LevelDB-backed instance of FSTPersistence. */
  29. // TODO(mikelehen): Rename to FSTLevelDBPersistence.
  30. @interface FSTLevelDB : NSObject <FSTPersistence>
  31. /**
  32. * Initializes the LevelDB in the given directory. Note that all expensive startup work including
  33. * opening any database files is deferred until -[FSTPersistence start] is called.
  34. */
  35. - (instancetype)initWithDirectory:(NSString *)directory
  36. serializer:(FSTLocalSerializer *)serializer NS_DESIGNATED_INITIALIZER;
  37. - (instancetype)init __attribute__((unavailable("Use -initWithDirectory: instead.")));
  38. /** Finds a suitable directory to serve as the root of all Firestore local storage. */
  39. + (NSString *)documentsDirectory;
  40. /**
  41. * Computes a unique storage directory for the given identifying components of local storage.
  42. *
  43. * @param databaseInfo The identifying information for the local storage instance.
  44. * @param documentsDirectory The root document directory relative to which the storage directory
  45. * will be created. Usually just +[FSTLevelDB documentsDir].
  46. * @return A storage directory unique to the instance identified by databaseInfo.
  47. */
  48. + (NSString *)storageDirectoryForDatabaseInfo:(FSTDatabaseInfo *)databaseInfo
  49. documentsDirectory:(NSString *)documentsDirectory;
  50. /**
  51. * Starts LevelDB-backed persistent storage by opening the database files, creating the DB if it
  52. * does not exist.
  53. *
  54. * The leveldb directory is created relative to the appropriate document storage directory for the
  55. * platform: NSDocumentDirectory on iOS or $HOME/.firestore on macOS.
  56. */
  57. - (BOOL)start:(NSError **)error;
  58. #ifdef __cplusplus
  59. // What follows is the Objective-C++ extension to the API.
  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. #endif
  85. @end
  86. NS_ASSUME_NONNULL_END