FSTQueryCache.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #include "Firestore/core/src/firebase/firestore/model/document_key_set.h"
  20. #include "Firestore/core/src/firebase/firestore/model/snapshot_version.h"
  21. @class FSTDocumentSet;
  22. @class FSTMaybeDocument;
  23. @class FSTQuery;
  24. @class FSTQueryData;
  25. NS_ASSUME_NONNULL_BEGIN
  26. /**
  27. * Represents cached queries received from the remote backend. This contains both a mapping between
  28. * queries and the documents that matched them according to the server, but also metadata about the
  29. * queries.
  30. *
  31. * The cache is keyed by FSTQuery and entries in the cache are FSTQueryData instances.
  32. */
  33. @protocol FSTQueryCache <NSObject, FSTGarbageSource>
  34. /** Starts the query cache up. */
  35. - (void)start;
  36. /**
  37. * Returns the highest target ID of any query in the cache. Typically called during startup to
  38. * seed a target ID generator and avoid collisions with existing queries. If there are no queries
  39. * in the cache, returns zero.
  40. */
  41. - (FSTTargetID)highestTargetID;
  42. /**
  43. * Returns the highest listen sequence number of any query seen by the cache.
  44. */
  45. - (FSTListenSequenceNumber)highestListenSequenceNumber;
  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. - (const firebase::firestore::model::SnapshotVersion &)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:(firebase::firestore::model::SnapshotVersion)snapshotVersion;
  63. /**
  64. * Adds an entry in the cache.
  65. *
  66. * The cache key is extracted from `queryData.query`. The key must not already exist in the cache.
  67. *
  68. * @param queryData A new FSTQueryData instance to put in the cache.
  69. */
  70. - (void)addQueryData:(FSTQueryData *)queryData;
  71. /**
  72. * Updates an entry in the cache.
  73. *
  74. * The cache key is extracted from `queryData.query`. The entry must already exist in the cache,
  75. * and it will be replaced.
  76. * @param queryData An FSTQueryData instance to replace an existing entry in the cache
  77. */
  78. - (void)updateQueryData:(FSTQueryData *)queryData;
  79. /** Removes the cached entry for the given query data (no-op if no entry exists). */
  80. - (void)removeQueryData:(FSTQueryData *)queryData;
  81. /** Returns the number of targets cached. */
  82. - (int32_t)count;
  83. /**
  84. * Looks up an FSTQueryData entry in the cache.
  85. *
  86. * @param query The query corresponding to the entry to look up.
  87. * @return The cached FSTQueryData entry, or nil if the cache has no entry for the query.
  88. */
  89. - (nullable FSTQueryData *)queryDataForQuery:(FSTQuery *)query;
  90. /** Adds the given document keys to cached query results of the given target ID. */
  91. - (void)addMatchingKeys:(const firebase::firestore::model::DocumentKeySet &)keys
  92. forTargetID:(FSTTargetID)targetID;
  93. /** Removes the given document keys from the cached query results of the given target ID. */
  94. - (void)removeMatchingKeys:(const firebase::firestore::model::DocumentKeySet &)keys
  95. forTargetID:(FSTTargetID)targetID;
  96. /** Removes all the keys in the query results of the given target ID. */
  97. - (void)removeMatchingKeysForTargetID:(FSTTargetID)targetID;
  98. - (firebase::firestore::model::DocumentKeySet)matchingKeysForTargetID:(FSTTargetID)targetID;
  99. @end
  100. NS_ASSUME_NONNULL_END