FSTMemoryPersistence.mm 3.4 KB

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