FSTLevelDB.h 3.8 KB

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