GDTCORStorageProtocol.h 5.3 KB

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