FSTQueryCache.h 4.9 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. #include "Firestore/core/src/firebase/firestore/model/document_key_set.h"
  18. #include "Firestore/core/src/firebase/firestore/model/snapshot_version.h"
  19. #include "Firestore/core/src/firebase/firestore/model/types.h"
  20. @class FSTDocumentSet;
  21. @class FSTMaybeDocument;
  22. @class FSTQuery;
  23. @class FSTQueryData;
  24. NS_ASSUME_NONNULL_BEGIN
  25. /**
  26. * Represents cached queries received from the remote backend. This contains both a mapping between
  27. * queries and the documents that matched them according to the server, but also metadata about the
  28. * queries.
  29. *
  30. * The cache is keyed by FSTQuery and entries in the cache are FSTQueryData instances.
  31. */
  32. @protocol FSTQueryCache <NSObject>
  33. /**
  34. * Returns the highest target ID of any query in the cache. Typically called during startup to
  35. * seed a target ID generator and avoid collisions with existing queries. If there are no queries
  36. * in the cache, returns zero.
  37. */
  38. - (firebase::firestore::model::TargetId)highestTargetID;
  39. /**
  40. * Returns the highest listen sequence number of any query seen by the cache.
  41. */
  42. - (firebase::firestore::model::ListenSequenceNumber)highestListenSequenceNumber;
  43. /**
  44. * A global snapshot version representing the last consistent snapshot we received from the
  45. * backend. This is monotonically increasing and any snapshots received from the backend prior to
  46. * this version (e.g. for targets resumed with a resume_token) should be suppressed (buffered)
  47. * until the backend has caught up to this snapshot version again. This prevents our cache from
  48. * ever going backwards in time.
  49. *
  50. * This is updated whenever our we get a TargetChange with a read_time and empty target_ids.
  51. */
  52. - (const firebase::firestore::model::SnapshotVersion &)lastRemoteSnapshotVersion;
  53. /**
  54. * Set the snapshot version representing the last consistent snapshot received from the backend.
  55. * (see -lastRemoteSnapshotVersion for more details).
  56. *
  57. * @param snapshotVersion The new snapshot version.
  58. */
  59. - (void)setLastRemoteSnapshotVersion:(firebase::firestore::model::SnapshotVersion)snapshotVersion;
  60. /**
  61. * Adds an entry in the cache.
  62. *
  63. * The cache key is extracted from `queryData.query`. The key must not already exist in the cache.
  64. *
  65. * @param queryData A new FSTQueryData instance to put in the cache.
  66. */
  67. - (void)addQueryData:(FSTQueryData *)queryData;
  68. /**
  69. * Updates an entry in the cache.
  70. *
  71. * The cache key is extracted from `queryData.query`. The entry must already exist in the cache,
  72. * and it will be replaced.
  73. * @param queryData An FSTQueryData instance to replace an existing entry in the cache
  74. */
  75. - (void)updateQueryData:(FSTQueryData *)queryData;
  76. /** Removes the cached entry for the given query data (no-op if no entry exists). */
  77. - (void)removeQueryData:(FSTQueryData *)queryData;
  78. - (void)enumerateTargetsUsingBlock:(void (^)(FSTQueryData *queryData, BOOL *stop))block;
  79. - (int)removeQueriesThroughSequenceNumber:
  80. (firebase::firestore::model::ListenSequenceNumber)sequenceNumber
  81. liveQueries:(NSDictionary<NSNumber *, FSTQueryData *> *)liveQueries;
  82. /** Returns the number of targets cached. */
  83. - (int32_t)count;
  84. /**
  85. * Looks up an FSTQueryData entry in the cache.
  86. *
  87. * @param query The query corresponding to the entry to look up.
  88. * @return The cached FSTQueryData entry, or nil if the cache has no entry for the query.
  89. */
  90. - (nullable FSTQueryData *)queryDataForQuery:(FSTQuery *)query;
  91. /** Adds the given document keys to cached query results of the given target ID. */
  92. - (void)addMatchingKeys:(const firebase::firestore::model::DocumentKeySet &)keys
  93. forTargetID:(firebase::firestore::model::TargetId)targetID;
  94. /** Removes the given document keys from the cached query results of the given target ID. */
  95. - (void)removeMatchingKeys:(const firebase::firestore::model::DocumentKeySet &)keys
  96. forTargetID:(firebase::firestore::model::TargetId)targetID;
  97. /** Removes all the keys in the query results of the given target ID. */
  98. - (void)removeMatchingKeysForTargetID:(firebase::firestore::model::TargetId)targetID;
  99. - (firebase::firestore::model::DocumentKeySet)matchingKeysForTargetID:
  100. (firebase::firestore::model::TargetId)targetID;
  101. /**
  102. * Checks to see if there are any references to a document with the given key.
  103. */
  104. - (BOOL)containsKey:(const firebase::firestore::model::DocumentKey &)key;
  105. @end
  106. NS_ASSUME_NONNULL_END