FSTMemoryRemoteDocumentCache.mm 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 <Protobuf/GPBProtocolBuffers.h>
  18. #import "Firestore/Protos/objc/firestore/local/MaybeDocument.pbobjc.h"
  19. #import "Firestore/Source/Core/FSTQuery.h"
  20. #import "Firestore/Source/Local/FSTMemoryPersistence.h"
  21. #import "Firestore/Source/Model/FSTDocument.h"
  22. #import "Firestore/Source/Model/FSTDocumentDictionary.h"
  23. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  24. using firebase::firestore::model::DocumentKey;
  25. using firebase::firestore::model::ListenSequenceNumber;
  26. NS_ASSUME_NONNULL_BEGIN
  27. /**
  28. * Returns an estimate of the number of bytes used to store the given
  29. * document key in memory. This is only an estimate and includes the size
  30. * of the segments of the path, but not any object overhead or path separators.
  31. */
  32. static size_t FSTDocumentKeyByteSize(FSTDocumentKey *key) {
  33. size_t count = 0;
  34. for (const auto &segment : key.path) {
  35. count += segment.size();
  36. }
  37. return count;
  38. }
  39. @interface FSTMemoryRemoteDocumentCache ()
  40. /** Underlying cache of documents. */
  41. @property(nonatomic, strong) FSTMaybeDocumentDictionary *docs;
  42. @end
  43. @implementation FSTMemoryRemoteDocumentCache
  44. - (instancetype)init {
  45. if (self = [super init]) {
  46. _docs = [FSTMaybeDocumentDictionary maybeDocumentDictionary];
  47. }
  48. return self;
  49. }
  50. - (void)addEntry:(FSTMaybeDocument *)document {
  51. self.docs = [self.docs dictionaryBySettingObject:document forKey:document.key];
  52. }
  53. - (void)removeEntryForKey:(const DocumentKey &)key {
  54. self.docs = [self.docs dictionaryByRemovingObjectForKey:key];
  55. }
  56. - (nullable FSTMaybeDocument *)entryForKey:(const DocumentKey &)key {
  57. return self.docs[static_cast<FSTDocumentKey *>(key)];
  58. }
  59. - (FSTDocumentDictionary *)documentsMatchingQuery:(FSTQuery *)query {
  60. FSTDocumentDictionary *result = [FSTDocumentDictionary documentDictionary];
  61. // Documents are ordered by key, so we can use a prefix scan to narrow down the documents
  62. // we need to match the query against.
  63. FSTDocumentKey *prefix = [FSTDocumentKey keyWithPath:query.path.Append("")];
  64. NSEnumerator<FSTDocumentKey *> *enumerator = [self.docs keyEnumeratorFrom:prefix];
  65. for (FSTDocumentKey *key in enumerator) {
  66. if (!query.path.IsPrefixOf(key.path)) {
  67. break;
  68. }
  69. FSTMaybeDocument *maybeDoc = self.docs[key];
  70. if (![maybeDoc isKindOfClass:[FSTDocument class]]) {
  71. continue;
  72. }
  73. FSTDocument *doc = (FSTDocument *)maybeDoc;
  74. if ([query matchesDocument:doc]) {
  75. result = [result dictionaryBySettingObject:doc forKey:doc.key];
  76. }
  77. }
  78. return result;
  79. }
  80. - (std::vector<DocumentKey>)removeOrphanedDocuments:
  81. (FSTMemoryLRUReferenceDelegate *)referenceDelegate
  82. throughSequenceNumber:(ListenSequenceNumber)upperBound {
  83. std::vector<DocumentKey> removed;
  84. FSTMaybeDocumentDictionary *updatedDocs = self.docs;
  85. for (FSTDocumentKey *docKey in [self.docs keyEnumerator]) {
  86. if (![referenceDelegate isPinnedAtSequenceNumber:upperBound document:docKey]) {
  87. updatedDocs = [updatedDocs dictionaryByRemovingObjectForKey:docKey];
  88. removed.push_back(DocumentKey{docKey});
  89. }
  90. }
  91. self.docs = updatedDocs;
  92. return removed;
  93. }
  94. - (size_t)byteSizeWithSerializer:(FSTLocalSerializer *)serializer {
  95. __block size_t count = 0;
  96. [self.docs
  97. enumerateKeysAndObjectsUsingBlock:^(FSTDocumentKey *key, FSTMaybeDocument *doc, BOOL *stop) {
  98. count += FSTDocumentKeyByteSize(key);
  99. count += [[[serializer encodedMaybeDocument:doc] data] length];
  100. }];
  101. return count;
  102. }
  103. @end
  104. NS_ASSUME_NONNULL_END