FSTMemoryRemoteDocumentCache.mm 2.5 KB

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