FSTMemoryPersistence.mm 3.1 KB

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