FSTGarbageCollector.h 3.6 KB

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