FSTMemoryRemoteDocumentCache.mm 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/FSTMemoryRemoteDocumentCache.h"
  17. #import "Firestore/Source/Core/FSTQuery.h"
  18. #import "Firestore/Source/Local/FSTMemoryPersistence.h"
  19. #import "Firestore/Source/Model/FSTDocument.h"
  20. #import "Firestore/Source/Model/FSTDocumentDictionary.h"
  21. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  22. using firebase::firestore::model::DocumentKey;
  23. NS_ASSUME_NONNULL_BEGIN
  24. @interface FSTMemoryRemoteDocumentCache ()
  25. /** Underlying cache of documents. */
  26. @property(nonatomic, strong) FSTMaybeDocumentDictionary *docs;
  27. @end
  28. @implementation FSTMemoryRemoteDocumentCache
  29. - (instancetype)init {
  30. if (self = [super init]) {
  31. _docs = [FSTMaybeDocumentDictionary maybeDocumentDictionary];
  32. }
  33. return self;
  34. }
  35. - (void)addEntry:(FSTMaybeDocument *)document {
  36. self.docs = [self.docs dictionaryBySettingObject:document forKey:document.key];
  37. }
  38. - (void)removeEntryForKey:(const DocumentKey &)key {
  39. self.docs = [self.docs dictionaryByRemovingObjectForKey:key];
  40. }
  41. - (nullable FSTMaybeDocument *)entryForKey:(const DocumentKey &)key {
  42. return self.docs[static_cast<FSTDocumentKey *>(key)];
  43. }
  44. - (FSTDocumentDictionary *)documentsMatchingQuery:(FSTQuery *)query {
  45. FSTDocumentDictionary *result = [FSTDocumentDictionary documentDictionary];
  46. // Documents are ordered by key, so we can use a prefix scan to narrow down the documents
  47. // we need to match the query against.
  48. FSTDocumentKey *prefix = [FSTDocumentKey keyWithPath:query.path.Append("")];
  49. NSEnumerator<FSTDocumentKey *> *enumerator = [self.docs keyEnumeratorFrom:prefix];
  50. for (FSTDocumentKey *key in enumerator) {
  51. if (!query.path.IsPrefixOf(key.path)) {
  52. break;
  53. }
  54. FSTMaybeDocument *maybeDoc = self.docs[key];
  55. if (![maybeDoc isKindOfClass:[FSTDocument class]]) {
  56. continue;
  57. }
  58. FSTDocument *doc = (FSTDocument *)maybeDoc;
  59. if ([query matchesDocument:doc]) {
  60. result = [result dictionaryBySettingObject:doc forKey:doc.key];
  61. }
  62. }
  63. return result;
  64. }
  65. - (int)removeOrphanedDocuments:(FSTMemoryLRUReferenceDelegate *)referenceDelegate
  66. throughSequenceNumber:(FSTListenSequenceNumber)upperBound {
  67. int count = 0;
  68. FSTMaybeDocumentDictionary *updatedDocs = self.docs;
  69. for (FSTDocumentKey *docKey in [self.docs keyEnumerator]) {
  70. if (![referenceDelegate isPinnedAtSequenceNumber:upperBound document:docKey]) {
  71. updatedDocs = [updatedDocs dictionaryByRemovingObjectForKey:docKey];
  72. NSLog(@"Removing %@", docKey);
  73. count++;
  74. }
  75. }
  76. self.docs = updatedDocs;
  77. return count;
  78. }
  79. @end
  80. NS_ASSUME_NONNULL_END