FSTRemoteDocumentChangeBuffer.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. NS_ASSUME_NONNULL_BEGIN
  18. @protocol FSTRemoteDocumentCache;
  19. @class FSTMaybeDocument;
  20. @class FSTDocumentKey;
  21. @class FSTWriteGroup;
  22. /**
  23. * An in-memory buffer of entries to be written to an FSTRemoteDocumentCache. It can be used to
  24. * batch up a set of changes to be written to the cache, but additionally supports reading entries
  25. * back with the `entryForKey:` method, falling back to the underlying FSTRemoteDocumentCache if
  26. * no entry is buffered. In the absence of LevelDB transactions (that would allow reading back
  27. * uncommitted writes), this greatly simplifies the implementation of complex operations that
  28. * may want to freely read/write entries to the FSTRemoteDocumentCache while still ensuring that
  29. * the final writing of the buffered entries is atomic.
  30. *
  31. * For doing blind writes that don't depend on the current state of the FSTRemoteDocumentCache
  32. * or for plain reads, you can/should still just use the FSTRemoteDocumentCache directly.
  33. */
  34. @interface FSTRemoteDocumentChangeBuffer : NSObject
  35. + (instancetype)changeBufferWithCache:(id<FSTRemoteDocumentCache>)cache;
  36. - (instancetype)init __attribute__((unavailable("Use a static constructor instead")));
  37. /** Buffers an `FSTRemoteDocumentCache addEntry:group:` call. */
  38. - (void)addEntry:(FSTMaybeDocument *)maybeDocument;
  39. // NOTE: removeEntryForKey: is not presently necessary and so is omitted.
  40. /**
  41. * Looks up an entry in the cache. The buffered changes will first be checked, and if no
  42. * buffered change applies, this will forward to `FSTRemoteDocumentCache entryForKey:`.
  43. *
  44. * @param documentKey The key of the entry to look up.
  45. * @return The cached FSTDocument or FSTDeletedDocument entry, or nil if we have nothing cached.
  46. */
  47. - (nullable FSTMaybeDocument *)entryForKey:(FSTDocumentKey *)documentKey;
  48. /**
  49. * Applies buffered changes to the underlying FSTRemoteDocumentCache, using the provided
  50. * FSTWriteGroup.
  51. */
  52. - (void)applyToWriteGroup:(FSTWriteGroup *)group;
  53. @end
  54. NS_ASSUME_NONNULL_END