FSTLevelDB.h 4.1 KB

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