FSTReferenceSet.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. NS_ASSUME_NONNULL_BEGIN
  22. /**
  23. * A collection of references to a document from some kind of numbered entity (either a targetID or
  24. * batchID). As references are added to or removed from the set corresponding events are emitted to
  25. * a registered garbage collector.
  26. *
  27. * Each reference is represented by a FSTDocumentReference object. Each of them contains enough
  28. * information to uniquely identify the reference. They are all stored primarily in a set sorted
  29. * by key. A document is considered garbage if there's no references in that set (this can be
  30. * efficiently checked thanks to sorting by key).
  31. *
  32. * FSTReferenceSet also keeps a secondary set that contains references sorted by IDs. This one is
  33. * used to efficiently implement removal of all references by some target ID.
  34. */
  35. @interface FSTReferenceSet : NSObject <FSTGarbageSource>
  36. /** Keeps track of keys that have references. */
  37. @property(nonatomic, weak, readwrite, nullable) id<FSTGarbageCollector> garbageCollector;
  38. /** Returns YES if the reference set contains no references. */
  39. - (BOOL)isEmpty;
  40. /** Adds a reference to the given document key for the given ID. */
  41. - (void)addReferenceToKey:(FSTDocumentKey *)key forID:(int)ID;
  42. /** Add references to the given document keys for the given ID. */
  43. - (void)addReferencesToKeys:(FSTDocumentKeySet *)keys forID:(int)ID;
  44. /** Removes a reference to the given document key for the given ID. */
  45. - (void)removeReferenceToKey:(FSTDocumentKey *)key forID:(int)ID;
  46. /** Removes references to the given document keys for the given ID. */
  47. - (void)removeReferencesToKeys:(FSTDocumentKeySet *)keys forID:(int)ID;
  48. /** Clears all references with a given ID. Calls -removeReferenceToKey: for each key removed. */
  49. - (void)removeReferencesForID:(int)ID;
  50. /** Clears all references for all IDs. */
  51. - (void)removeAllReferences;
  52. /** Returns all of the document keys that have had references added for the given ID. */
  53. - (FSTDocumentKeySet *)referencedKeysForID:(int)ID;
  54. @end
  55. NS_ASSUME_NONNULL_END