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. #import "Firestore/Source/Model/FSTDocumentKey.h"
  21. NS_ASSUME_NONNULL_BEGIN
  22. @interface FSTMemoryRemoteDocumentCache ()
  23. /** Underlying cache of documents. */
  24. @property(nonatomic, strong) FSTMaybeDocumentDictionary *docs;
  25. @end
  26. @implementation FSTMemoryRemoteDocumentCache
  27. - (instancetype)init {
  28. if (self = [super init]) {
  29. _docs = [FSTMaybeDocumentDictionary maybeDocumentDictionary];
  30. }
  31. return self;
  32. }
  33. - (void)shutdown {
  34. }
  35. - (void)addEntry:(FSTMaybeDocument *)document group:(FSTWriteGroup *)group {
  36. self.docs = [self.docs dictionaryBySettingObject:document forKey:document.key];
  37. }
  38. - (void)removeEntryForKey:(FSTDocumentKey *)key group:(FSTWriteGroup *)group {
  39. self.docs = [self.docs dictionaryByRemovingObjectForKey:key];
  40. }
  41. - (nullable FSTMaybeDocument *)entryForKey:(FSTDocumentKey *)key {
  42. return self.docs[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. @end
  66. NS_ASSUME_NONNULL_END