FSTGarbageCollector.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. @class FSTDocumentKey;
  19. @class FSTDocumentReference;
  20. @protocol FSTGarbageCollector;
  21. NS_ASSUME_NONNULL_BEGIN
  22. /**
  23. * A pseudo-collection that maintains references to documents. FSTGarbageSource collections
  24. * notify the FSTGarbageCollector when references to documents change through the
  25. * -addPotentialGarbageKey: message.
  26. */
  27. @protocol FSTGarbageSource
  28. /**
  29. * The garbage collector to which this collection should send -addPotentialGarbageKey: messages.
  30. */
  31. @property(nonatomic, weak, readwrite, nullable) id<FSTGarbageCollector> garbageCollector;
  32. /**
  33. * Checks to see if there are any references to a document with the given key. This can be used by
  34. * garbage collectors to double-check if a key exists in this collection when it was released
  35. * elsewhere.
  36. */
  37. - (BOOL)containsKey:(FSTDocumentKey *)key;
  38. @end
  39. /**
  40. * Tracks different kinds of references to a document, for all the different ways the client
  41. * needs to retain a document.
  42. *
  43. * Usually the local store this means tracking of three different types of references to a
  44. * document:
  45. * 1. RemoteTarget reference identified by a target ID.
  46. * 2. LocalView reference identified also by a target ID.
  47. * 3. Local mutation reference identified by a batch ID.
  48. *
  49. * The idea is that we want to keep a document around at least as long as any remote target or
  50. * local (latency compensated) view is referencing it, or there's an outstanding local mutation to
  51. * that document.
  52. */
  53. @protocol FSTGarbageCollector
  54. /**
  55. * A property that describes whether or not the collector wants to eagerly collect keys.
  56. *
  57. * TODO(b/33384523) Delegate deleting released queries to the GC.
  58. * This flag is a temporary workaround for dealing with a persistent query cache. The collector
  59. * really should have an API for releasing queries that does the right thing for its policy.
  60. */
  61. @property(nonatomic, assign, readonly, getter=isEager) BOOL eager;
  62. /** Adds a garbage source to the collector. */
  63. - (void)addGarbageSource:(id<FSTGarbageSource>)garbageSource;
  64. /** Removes a garbage source from the collector. */
  65. - (void)removeGarbageSource:(id<FSTGarbageSource>)garbageSource;
  66. /**
  67. * Notifies the garbage collector that a document with the given key may have become garbage.
  68. *
  69. * This is useful in both when a document has definitely been released (for example when removed
  70. * from a garbage source) but also when a document has been updated. Documents should be marked in
  71. * this way because the client accepts updates for documents even after the document no longer
  72. * matches any active targets. This behavior allows the client to avoid re-showing an old document
  73. * in the next latency-compensated view.
  74. */
  75. - (void)addPotentialGarbageKey:(FSTDocumentKey *)key;
  76. /** Returns the contents of the garbage bin and clears it. */
  77. - (NSSet<FSTDocumentKey *> *)collectGarbage;
  78. @end
  79. NS_ASSUME_NONNULL_END