FSTMemoryPersistence.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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/FSTMemoryPersistence.h"
  17. #import "Firestore/Source/Auth/FSTUser.h"
  18. #import "Firestore/Source/Local/FSTMemoryMutationQueue.h"
  19. #import "Firestore/Source/Local/FSTMemoryQueryCache.h"
  20. #import "Firestore/Source/Local/FSTMemoryRemoteDocumentCache.h"
  21. #import "Firestore/Source/Local/FSTWriteGroup.h"
  22. #import "Firestore/Source/Local/FSTWriteGroupTracker.h"
  23. #import "Firestore/Source/Util/FSTAssert.h"
  24. NS_ASSUME_NONNULL_BEGIN
  25. @interface FSTMemoryPersistence ()
  26. @property(nonatomic, strong, nonnull) FSTWriteGroupTracker *writeGroupTracker;
  27. @property(nonatomic, strong, nonnull)
  28. NSMutableDictionary<FSTUser *, id<FSTMutationQueue>> *mutationQueues;
  29. @property(nonatomic, assign, getter=isStarted) BOOL started;
  30. @end
  31. @implementation FSTMemoryPersistence {
  32. /**
  33. * The FSTQueryCache representing the persisted cache of queries.
  34. *
  35. * Note that this is retained here to make it easier to write tests affecting both the in-memory
  36. * and LevelDB-backed persistence layers. Tests can create a new FSTLocalStore wrapping this
  37. * FSTPersistence instance and this will make the in-memory persistence layer behave as if it
  38. * were actually persisting values.
  39. */
  40. FSTMemoryQueryCache *_queryCache;
  41. /** The FSTRemoteDocumentCache representing the persisted cache of remote documents. */
  42. FSTMemoryRemoteDocumentCache *_remoteDocumentCache;
  43. }
  44. + (instancetype)persistence {
  45. return [[FSTMemoryPersistence alloc] init];
  46. }
  47. - (instancetype)init {
  48. if (self = [super init]) {
  49. _writeGroupTracker = [FSTWriteGroupTracker tracker];
  50. _queryCache = [[FSTMemoryQueryCache alloc] init];
  51. _remoteDocumentCache = [[FSTMemoryRemoteDocumentCache alloc] init];
  52. _mutationQueues = [NSMutableDictionary dictionary];
  53. }
  54. return self;
  55. }
  56. - (BOOL)start:(NSError **)error {
  57. // No durable state to read on startup.
  58. FSTAssert(!self.isStarted, @"FSTMemoryPersistence double-started!");
  59. self.started = YES;
  60. return YES;
  61. }
  62. - (void)shutdown {
  63. // No durable state to ensure is closed on shutdown.
  64. FSTAssert(self.isStarted, @"FSTMemoryPersistence shutdown without start!");
  65. self.started = NO;
  66. }
  67. - (id<FSTMutationQueue>)mutationQueueForUser:(FSTUser *)user {
  68. id<FSTMutationQueue> queue = self.mutationQueues[user];
  69. if (!queue) {
  70. queue = [FSTMemoryMutationQueue mutationQueue];
  71. self.mutationQueues[user] = queue;
  72. }
  73. return queue;
  74. }
  75. - (id<FSTQueryCache>)queryCache {
  76. return _queryCache;
  77. }
  78. - (id<FSTRemoteDocumentCache>)remoteDocumentCache {
  79. return _remoteDocumentCache;
  80. }
  81. - (FSTWriteGroup *)startGroupWithAction:(NSString *)action {
  82. return [self.writeGroupTracker startGroupWithAction:action];
  83. }
  84. - (void)commitGroup:(FSTWriteGroup *)group {
  85. [self.writeGroupTracker endGroup:group];
  86. FSTAssert(group.isEmpty, @"Memory persistence shouldn't use write groups: %@", group.action);
  87. }
  88. @end
  89. NS_ASSUME_NONNULL_END