FSTMemoryRemoteDocumentCache.m 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #import "Firestore/Source/Model/FSTDocumentKey.h"
  21. #import "Firestore/Source/Model/FSTPath.h"
  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)shutdown {
  35. }
  36. - (void)addEntry:(FSTMaybeDocument *)document group:(FSTWriteGroup *)group {
  37. self.docs = [self.docs dictionaryBySettingObject:document forKey:document.key];
  38. }
  39. - (void)removeEntryForKey:(FSTDocumentKey *)key group:(FSTWriteGroup *)group {
  40. self.docs = [self.docs dictionaryByRemovingObjectForKey:key];
  41. }
  42. - (nullable FSTMaybeDocument *)entryForKey:(FSTDocumentKey *)key {
  43. return self.docs[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 pathByAppendingSegment:@""]];
  50. NSEnumerator<FSTDocumentKey *> *enumerator = [self.docs keyEnumeratorFrom:prefix];
  51. for (FSTDocumentKey *key in enumerator) {
  52. if (![query.path isPrefixOfPath: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. @end
  67. NS_ASSUME_NONNULL_END