FSTQueryCache.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. * Returns the highest listen sequence number of any query seen by the cache.
  48. */
  49. - (FSTListenSequenceNumber)highestListenSequenceNumber;
  50. /**
  51. * A global snapshot version representing the last consistent snapshot we received from the
  52. * backend. This is monotonically increasing and any snapshots received from the backend prior to
  53. * this version (e.g. for targets resumed with a resume_token) should be suppressed (buffered)
  54. * until the backend has caught up to this snapshot version again. This prevents our cache from
  55. * ever going backwards in time.
  56. *
  57. * This is updated whenever our we get a TargetChange with a read_time and empty target_ids.
  58. */
  59. - (FSTSnapshotVersion *)lastRemoteSnapshotVersion;
  60. /**
  61. * Set the snapshot version representing the last consistent snapshot received from the backend.
  62. * (see -lastRemoteSnapshotVersion for more details).
  63. *
  64. * @param snapshotVersion The new snapshot version.
  65. */
  66. - (void)setLastRemoteSnapshotVersion:(FSTSnapshotVersion *)snapshotVersion
  67. group:(FSTWriteGroup *)group;
  68. /**
  69. * Adds an entry in the cache.
  70. *
  71. * The cache key is extracted from `queryData.query`. The key must not already exist in the cache.
  72. *
  73. * @param queryData A new FSTQueryData instance to put in the cache.
  74. */
  75. - (void)addQueryData:(FSTQueryData *)queryData group:(FSTWriteGroup *)group;
  76. /**
  77. * Updates an entry in the cache.
  78. *
  79. * The cache key is extracted from `queryData.query`. The entry must already exist in the cache,
  80. * and it will be replaced.
  81. * @param queryData An FSTQueryData instance to replace an existing entry in the cache
  82. */
  83. - (void)updateQueryData:(FSTQueryData *)queryData group:(FSTWriteGroup *)group;
  84. /** Removes the cached entry for the given query data (no-op if no entry exists). */
  85. - (void)removeQueryData:(FSTQueryData *)queryData group:(FSTWriteGroup *)group;
  86. /** Returns the number of targets cached. */
  87. - (int32_t)count;
  88. /**
  89. * Looks up an FSTQueryData entry in the cache.
  90. *
  91. * @param query The query corresponding to the entry to look up.
  92. * @return The cached FSTQueryData entry, or nil if the cache has no entry for the query.
  93. */
  94. - (nullable FSTQueryData *)queryDataForQuery:(FSTQuery *)query;
  95. /** Adds the given document keys to cached query results of the given target ID. */
  96. - (void)addMatchingKeys:(FSTDocumentKeySet *)keys
  97. forTargetID:(FSTTargetID)targetID
  98. group:(FSTWriteGroup *)group;
  99. /** Removes the given document keys from the cached query results of the given target ID. */
  100. - (void)removeMatchingKeys:(FSTDocumentKeySet *)keys
  101. forTargetID:(FSTTargetID)targetID
  102. group:(FSTWriteGroup *)group;
  103. /** Removes all the keys in the query results of the given target ID. */
  104. - (void)removeMatchingKeysForTargetID:(FSTTargetID)targetID group:(FSTWriteGroup *)group;
  105. - (FSTDocumentKeySet *)matchingKeysForTargetID:(FSTTargetID)targetID;
  106. @end
  107. NS_ASSUME_NONNULL_END