FSTRemoteDocumentChangeBuffer.m 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 "Firestore/Source/Local/FSTRemoteDocumentChangeBuffer.h"
  17. #import "Firestore/Source/Local/FSTRemoteDocumentCache.h"
  18. #import "Firestore/Source/Model/FSTDocument.h"
  19. #import "Firestore/Source/Model/FSTDocumentKey.h"
  20. #import "Firestore/Source/Util/FSTAssert.h"
  21. NS_ASSUME_NONNULL_BEGIN
  22. @interface FSTRemoteDocumentChangeBuffer ()
  23. - (instancetype)initWithCache:(id<FSTRemoteDocumentCache>)cache;
  24. /** The underlying cache we're buffering changes for. */
  25. @property(nonatomic, strong, nonnull) id<FSTRemoteDocumentCache> remoteDocumentCache;
  26. /** The buffered changes, stored as a dictionary for easy lookups. */
  27. @property(nonatomic, strong, nullable)
  28. NSMutableDictionary<FSTDocumentKey *, FSTMaybeDocument *> *changes;
  29. @end
  30. @implementation FSTRemoteDocumentChangeBuffer
  31. + (instancetype)changeBufferWithCache:(id<FSTRemoteDocumentCache>)cache {
  32. return [[FSTRemoteDocumentChangeBuffer alloc] initWithCache:cache];
  33. }
  34. - (instancetype)initWithCache:(id<FSTRemoteDocumentCache>)cache {
  35. if (self = [super init]) {
  36. _remoteDocumentCache = cache;
  37. _changes = [NSMutableDictionary dictionary];
  38. }
  39. return self;
  40. }
  41. - (void)addEntry:(FSTMaybeDocument *)maybeDocument {
  42. [self assertValid];
  43. self.changes[maybeDocument.key] = maybeDocument;
  44. }
  45. - (nullable FSTMaybeDocument *)entryForKey:(FSTDocumentKey *)documentKey {
  46. [self assertValid];
  47. FSTMaybeDocument *bufferedEntry = self.changes[documentKey];
  48. if (bufferedEntry) {
  49. return bufferedEntry;
  50. } else {
  51. return [self.remoteDocumentCache entryForKey:documentKey];
  52. }
  53. }
  54. - (void)applyToWriteGroup:(FSTWriteGroup *)group {
  55. [self assertValid];
  56. [self.changes enumerateKeysAndObjectsUsingBlock:^(FSTDocumentKey *key, FSTMaybeDocument *value,
  57. BOOL *stop) {
  58. [self.remoteDocumentCache addEntry:value group:group];
  59. }];
  60. // We should not be used to buffer any more changes.
  61. self.changes = nil;
  62. }
  63. - (void)assertValid {
  64. FSTAssert(self.changes, @"Changes have already been applied.");
  65. }
  66. @end
  67. NS_ASSUME_NONNULL_END