FSTQueryCache.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 <Foundation/Foundation.h>
  17. #import "Firestore/Source/Core/FSTTypes.h"
  18. #import "Firestore/Source/Local/FSTGarbageCollector.h"
  19. #import "Firestore/Source/Model/FSTDocumentKeySet.h"
  20. @class FSTDocumentKey;
  21. @class FSTDocumentSet;
  22. @class FSTMaybeDocument;
  23. @class FSTQuery;
  24. @class FSTQueryData;
  25. @class FSTWriteGroup;
  26. @class FSTSnapshotVersion;
  27. NS_ASSUME_NONNULL_BEGIN
  28. /**
  29. * Represents cached queries received from the remote backend. This contains both a mapping between
  30. * queries and the documents that matched them according to the server, but also metadata about the
  31. * queries.
  32. *
  33. * The cache is keyed by FSTQuery and entries in the cache are FSTQueryData instances.
  34. */
  35. @protocol FSTQueryCache <NSObject, FSTGarbageSource>
  36. /** Starts the query cache up. */
  37. - (void)start;
  38. /** Shuts this cache down, closing open files, etc. */
  39. - (void)shutdown;
  40. /**
  41. * Returns the highest target ID of any query in the cache. Typically called during startup to
  42. * seed a target ID generator and avoid collisions with existing queries. If there are no queries
  43. * in the cache, returns zero.
  44. */
  45. - (FSTTargetID)highestTargetID;
  46. /**
  47. * A global snapshot version representing the last consistent snapshot we received from the
  48. * backend. This is monotonically increasing and any snapshots received from the backend prior to
  49. * this version (e.g. for targets resumed with a resume_token) should be suppressed (buffered)
  50. * until the backend has caught up to this snapshot version again. This prevents our cache from
  51. * ever going backwards in time.
  52. *
  53. * This is updated whenever our we get a TargetChange with a read_time and empty target_ids.
  54. */
  55. - (FSTSnapshotVersion *)lastRemoteSnapshotVersion;
  56. /**
  57. * Set the snapshot version representing the last consistent snapshot received from the backend.
  58. * (see -lastRemoteSnapshotVersion for more details).
  59. *
  60. * @param snapshotVersion The new snapshot version.
  61. */
  62. - (void)setLastRemoteSnapshotVersion:(FSTSnapshotVersion *)snapshotVersion
  63. group:(FSTWriteGroup *)group;
  64. /**
  65. * Adds or replaces an entry in the cache.
  66. *
  67. * The cache key is extracted from `queryData.query`. If there is already a cache entry for the
  68. * key, it will be replaced.
  69. *
  70. * @param queryData An FSTQueryData instance to put in the cache.
  71. */
  72. - (void)addQueryData:(FSTQueryData *)queryData group:(FSTWriteGroup *)group;
  73. /** Removes the cached entry for the given query data (no-op if no entry exists). */
  74. - (void)removeQueryData:(FSTQueryData *)queryData group:(FSTWriteGroup *)group;
  75. /**
  76. * Looks up an FSTQueryData entry in the cache.
  77. *
  78. * @param query The query corresponding to the entry to look up.
  79. * @return The cached FSTQueryData entry, or nil if the cache has no entry for the query.
  80. */
  81. - (nullable FSTQueryData *)queryDataForQuery:(FSTQuery *)query;
  82. /** Adds the given document keys to cached query results of the given target ID. */
  83. - (void)addMatchingKeys:(FSTDocumentKeySet *)keys
  84. forTargetID:(FSTTargetID)targetID
  85. group:(FSTWriteGroup *)group;
  86. /** Removes the given document keys from the cached query results of the given target ID. */
  87. - (void)removeMatchingKeys:(FSTDocumentKeySet *)keys
  88. forTargetID:(FSTTargetID)targetID
  89. group:(FSTWriteGroup *)group;
  90. /** Removes all the keys in the query results of the given target ID. */
  91. - (void)removeMatchingKeysForTargetID:(FSTTargetID)targetID group:(FSTWriteGroup *)group;
  92. - (FSTDocumentKeySet *)matchingKeysForTargetID:(FSTTargetID)targetID;
  93. @end
  94. NS_ASSUME_NONNULL_END