GDTCORStorageProtocol.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright 2020 Google LLC
  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 "GDTCORLifecycle.h"
  18. #import "GDTCORStorageEventSelector.h"
  19. #import "GDTCORTargets.h"
  20. @class GDTCOREvent;
  21. @class GDTCORClock;
  22. NS_ASSUME_NONNULL_BEGIN
  23. typedef void (^GDTCORStorageBatchBlock)(NSNumber *_Nullable newBatchID,
  24. NSSet<GDTCOREvent *> *_Nullable batchEvents);
  25. /** Defines the interface a storage subsystem is expected to implement. */
  26. @protocol GDTCORStorageProtocol <NSObject, GDTCORLifecycleProtocol>
  27. @required
  28. /** Stores an event and calls onComplete with a non-nil error if anything went wrong.
  29. *
  30. * @param event The event to store
  31. * @param completion The completion block to call after an attempt to store the event has been made.
  32. */
  33. - (void)storeEvent:(GDTCOREvent *)event
  34. onComplete:(void (^_Nullable)(BOOL wasWritten, NSError *_Nullable error))completion;
  35. /** Returns YES if some events have been stored for the given target, NO otherwise.
  36. *
  37. * @param onComplete The completion block to invoke when determining if there are events is done.
  38. */
  39. - (void)hasEventsForTarget:(GDTCORTarget)target onComplete:(void (^)(BOOL hasEvents))onComplete;
  40. /** Constructs an event batch with the given event selector. Events in this batch will not be
  41. * returned in any queries or other batches until the batch is removed.
  42. *
  43. * @param eventSelector The event selector used to find the events.
  44. * @param expiration The expiration time of the batch. If removeBatchWithID:deleteEvents:onComplete:
  45. * is not called within this time frame, the batch will be removed with its events deleted.
  46. * @param onComplete The completion handler to be called when the events have been fetched.
  47. */
  48. - (void)batchWithEventSelector:(nonnull GDTCORStorageEventSelector *)eventSelector
  49. batchExpiration:(nonnull NSDate *)expiration
  50. onComplete:(nonnull GDTCORStorageBatchBlock)onComplete;
  51. /** Removes the event batch.
  52. *
  53. * @param batchID The batchID to remove.
  54. * @param deleteEvents If YES, the events in this batch are deleted.
  55. * @param onComplete The completion handler to call when the batch removal process has completed.
  56. */
  57. - (void)removeBatchWithID:(NSNumber *)batchID
  58. deleteEvents:(BOOL)deleteEvents
  59. onComplete:(void (^_Nullable)(void))onComplete;
  60. /** Finds the batchIDs for the given target and calls the callback block.
  61. *
  62. * @param target The target.
  63. * @param onComplete The block to invoke with the set of current batchIDs.
  64. */
  65. - (void)batchIDsForTarget:(GDTCORTarget)target
  66. onComplete:(void (^)(NSSet<NSNumber *> *_Nullable batchIDs))onComplete;
  67. /** Checks the storage for expired events and batches, deletes them if they're expired. */
  68. - (void)checkForExpirations;
  69. /** Persists the given data with the given key.
  70. *
  71. * @param data The data to store.
  72. * @param key The unique key to store it to.
  73. * @param onComplete An block to be run when storage of the data is complete.
  74. */
  75. - (void)storeLibraryData:(NSData *)data
  76. forKey:(NSString *)key
  77. onComplete:(nullable void (^)(NSError *_Nullable error))onComplete;
  78. /** Retrieves the stored data for the given key and optionally sets a new value.
  79. *
  80. * @param key The key corresponding to the desired data.
  81. * @param onFetchComplete The callback to invoke with the data once it's retrieved.
  82. * @param setValueBlock This optional block can provide a new value to set.
  83. */
  84. - (void)libraryDataForKey:(nonnull NSString *)key
  85. onFetchComplete:(nonnull void (^)(NSData *_Nullable data,
  86. NSError *_Nullable error))onFetchComplete
  87. setNewValue:(NSData *_Nullable (^_Nullable)(void))setValueBlock;
  88. /** Removes data from storage and calls the callback when complete.
  89. *
  90. * @param key The key of the data to remove.
  91. * @param onComplete The callback that will be invoked when removing the data is complete.
  92. */
  93. - (void)removeLibraryDataForKey:(NSString *)key
  94. onComplete:(void (^)(NSError *_Nullable error))onComplete;
  95. /** Calculates and returns the total disk size that this storage consumes.
  96. *
  97. * @param onComplete The callback that will be invoked once storage size calculation is complete.
  98. */
  99. - (void)storageSizeWithCallback:(void (^)(uint64_t storageSize))onComplete;
  100. @end
  101. /** Retrieves the storage instance for the given target.
  102. *
  103. * @param target The target.
  104. * * @return The storage instance registered for the target, or nil if there is none.
  105. */
  106. FOUNDATION_EXPORT
  107. id<GDTCORStorageProtocol> _Nullable GDTCORStorageInstanceForTarget(GDTCORTarget target);
  108. NS_ASSUME_NONNULL_END