FSTPersistence.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. @class FSTUser;
  18. @class FSTWriteGroup;
  19. @protocol FSTMutationQueue;
  20. @protocol FSTQueryCache;
  21. @protocol FSTRemoteDocumentCache;
  22. NS_ASSUME_NONNULL_BEGIN
  23. /**
  24. * FSTPersistence is the lowest-level shared interface to persistent storage in Firestore.
  25. *
  26. * FSTPersistence is used to create FSTMutationQueue and FSTRemoteDocumentCache instances backed
  27. * by persistence (which might be in-memory or LevelDB).
  28. *
  29. * FSTPersistence also exposes an API to create and commit FSTWriteGroup instances.
  30. * Implementations of FSTWriteGroup/FSTPersistence only need to guarantee that writes made
  31. * against the FSTWriteGroup are not made to durable storage until commitGroup:action: is called
  32. * here. Since memory-only storage components do not alter durable storage, they are free to ignore
  33. * the group.
  34. *
  35. * This contract is enough to allow the FSTLocalStore be be written independently of whether or not
  36. * the stored state actually is durably persisted. If persistent storage is enabled, writes are
  37. * grouped together to avoid inconsistent state that could cause crashes.
  38. *
  39. * Concretely, when persistent storage is enabled, the persistent versions of FSTMutationQueue,
  40. * FSTRemoteDocumentCache, and others (the mutators) will defer their writes into an FSTWriteGroup.
  41. * Once the local store has completed one logical operation, it commits the write group using
  42. * [FSTPersistence commitGroup:action:].
  43. *
  44. * When persistent storage is disabled, the non-persistent versions of the mutators ignore the
  45. * FSTWriteGroup and [FSTPersistence commitGroup:action:] is a no-op. This short-cut is allowed
  46. * because memory-only storage leaves no state so it cannot be inconsistent.
  47. *
  48. * This simplifies the implementations of the mutators and allows memory-only implementations to
  49. * supplement the persistent ones without requiring any special dual-store implementation of
  50. * FSTPersistence. The cost is that the FSTLocalStore needs to be slightly careful about the order
  51. * of its reads and writes in order to avoid relying on being able to read back uncommitted writes.
  52. */
  53. @protocol FSTPersistence <NSObject>
  54. /**
  55. * Starts persistent storage, opening the database or similar.
  56. *
  57. * @param error An error object that will be populated if startup fails.
  58. * @return YES if persistent storage started successfully, NO otherwise.
  59. */
  60. - (BOOL)start:(NSError **)error;
  61. /** Releases any resources held during eager shutdown. */
  62. - (void)shutdown;
  63. /**
  64. * Returns an FSTMutationQueue representing the persisted mutations for the given user.
  65. *
  66. * <p>Note: The implementation is free to return the same instance every time this is called for a
  67. * given user. In particular, the memory-backed implementation does this to emulate the persisted
  68. * implementation to the extent possible (e.g. in the case of uid switching from
  69. * sally=>jack=>sally, sally's mutation queue will be preserved).
  70. */
  71. - (id<FSTMutationQueue>)mutationQueueForUser:(FSTUser *)user;
  72. /** Creates an FSTQueryCache representing the persisted cache of queries. */
  73. - (id<FSTQueryCache>)queryCache;
  74. /** Creates an FSTRemoteDocumentCache representing the persisted cache of remote documents. */
  75. - (id<FSTRemoteDocumentCache>)remoteDocumentCache;
  76. /**
  77. * Creates an FSTWriteGroup with the specified action description.
  78. *
  79. * @param action A description of the action performed by this group, used for logging.
  80. * @return The created group.
  81. */
  82. - (FSTWriteGroup *)startGroupWithAction:(NSString *)action;
  83. /**
  84. * Commits all accumulated changes in the given group. If there are no changes this is a no-op.
  85. *
  86. * @param group The group of changes to write as a unit.
  87. */
  88. - (void)commitGroup:(FSTWriteGroup *)group;
  89. @end
  90. NS_ASSUME_NONNULL_END