FSTMemoryRemoteDocumentCache.mm 3.2 KB

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