GDTCORFlatFileStorage.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright 2018 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 "GoogleDataTransport/GDTCORLibrary/Internal/GDTCORLifecycle.h"
  18. #import "GoogleDataTransport/GDTCORLibrary/Internal/GDTCORStorageEventSelector.h"
  19. #import "GoogleDataTransport/GDTCORLibrary/Internal/GDTCORStorageProtocol.h"
  20. @class GDTCOREvent;
  21. @class GDTCORUploadCoordinator;
  22. NS_ASSUME_NONNULL_BEGIN
  23. /** The event components eventID dictionary key. */
  24. FOUNDATION_EXPORT NSString *const kGDTCOREventComponentsEventIDKey;
  25. /** The event components qosTier dictionary key. */
  26. FOUNDATION_EXPORT NSString *const kGDTCOREventComponentsQoSTierKey;
  27. /** The event components mappingID dictionary key. */
  28. FOUNDATION_EXPORT NSString *const kGDTCOREventComponentsMappingIDKey;
  29. /** The event components expirationDate dictionary key. */
  30. FOUNDATION_EXPORT NSString *const kGDTCOREventComponentsExpirationKey;
  31. /** The batch components target dictionary key. */
  32. FOUNDATION_EXPORT NSString *const kGDTCORBatchComponentsTargetKey;
  33. /** The batch components batchID dictionary key. */
  34. FOUNDATION_EXPORT NSString *const kGDTCORBatchComponentsBatchIDKey;
  35. /** The batch components expiration dictionary key. */
  36. FOUNDATION_EXPORT NSString *const kGDTCORBatchComponentsExpirationKey;
  37. /** The maximum allowed disk space taken by the stored data. */
  38. FOUNDATION_EXPORT const uint64_t kGDTCORFlatFileStorageSizeLimit;
  39. FOUNDATION_EXPORT NSString *const GDTCORFlatFileStorageErrorDomain;
  40. typedef NS_ENUM(NSInteger, GDTCORFlatFileStorageError) {
  41. GDTCORFlatFileStorageErrorSizeLimitReached = 0
  42. };
  43. /** Manages the storage of events. This class is thread-safe.
  44. *
  45. * Event files will be stored as follows:
  46. * <app cache>/google-sdk-events/<classname>/gdt_event_data/<target>/<eventID>.<qosTier>.<mappingID>
  47. *
  48. * Library data will be stored as follows:
  49. * <app cache>/google-sdk-events/<classname>/gdt_library_data/<libraryDataKey>
  50. *
  51. * Batch data will be stored as follows:
  52. * <app
  53. * cache>/google-sdk-events/<classname>/gdt_batch_data/<target>.<batchID>/<eventID>.<qosTier>.<mappingID>
  54. */
  55. @interface GDTCORFlatFileStorage : NSObject <GDTCORStorageProtocol, GDTCORLifecycleProtocol>
  56. /** The queue on which all storage work will occur. */
  57. @property(nonatomic) dispatch_queue_t storageQueue;
  58. /** The upload coordinator instance used by this storage instance. */
  59. @property(nonatomic) GDTCORUploadCoordinator *uploadCoordinator;
  60. /** Creates and/or returns the storage singleton.
  61. *
  62. * @return The storage singleton.
  63. */
  64. + (instancetype)sharedInstance;
  65. /** Returns the base directory under which all events will be stored.
  66. *
  67. * @return The base directory under which all events will be stored.
  68. */
  69. + (NSString *)eventDataStoragePath;
  70. /** Returns the base directory under which all library data will be stored.
  71. *
  72. * @return The base directory under which all library data will be stored.
  73. */
  74. + (NSString *)libraryDataStoragePath;
  75. /** Returns the base directory under which all batch data will be stored.
  76. *
  77. * @return The base directory under which all batch data will be stored.
  78. */
  79. + (NSString *)batchDataStoragePath;
  80. /** */
  81. + (NSString *)batchPathForTarget:(GDTCORTarget)target
  82. batchID:(NSNumber *)batchID
  83. expirationDate:(NSDate *)expirationDate;
  84. /** Returns a constructed storage path based on the given values. This path may not exist.
  85. *
  86. * @param target The target, which is necessary to be given a path.
  87. * @param eventID The eventID.
  88. * @param qosTier The qosTier.
  89. * @param expirationDate The expirationDate as a 1970-relative time interval.
  90. * @param mappingID The mappingID.
  91. * @return The path representing the combination of the given parameters.
  92. */
  93. + (NSString *)pathForTarget:(GDTCORTarget)target
  94. eventID:(NSString *)eventID
  95. qosTier:(NSNumber *)qosTier
  96. expirationDate:(NSDate *)expirationDate
  97. mappingID:(NSString *)mappingID;
  98. /** Returns extant paths that match all of the given parameters.
  99. *
  100. * @param eventIDs The list of eventIDs to look for, or nil for any.
  101. * @param qosTiers The list of qosTiers to look for, or nil for any.
  102. * @param mappingIDs The list of mappingIDs to look for, or nil for any.
  103. * @param onComplete The completion to call once the paths have been discovered.
  104. */
  105. - (void)pathsForTarget:(GDTCORTarget)target
  106. eventIDs:(nullable NSSet<NSString *> *)eventIDs
  107. qosTiers:(nullable NSSet<NSNumber *> *)qosTiers
  108. mappingIDs:(nullable NSSet<NSString *> *)mappingIDs
  109. onComplete:(void (^)(NSSet<NSString *> *paths))onComplete;
  110. /** Fetches the current batchID counter value from library storage, increments it, and sets the new
  111. * value. Returns nil if a batchID was not able to be created for some reason.
  112. *
  113. * @param onComplete A block to execute when creating the next batchID is complete.
  114. */
  115. - (void)nextBatchID:(void (^)(NSNumber *_Nullable batchID))onComplete;
  116. /** Constructs a dictionary of event filename components.
  117. *
  118. * @param fileName The event filename to split.
  119. * @return The dictionary of event component keys to their values.
  120. */
  121. - (nullable NSDictionary<NSString *, id> *)eventComponentsFromFilename:(NSString *)fileName;
  122. /** Constructs a dictionary of batch filename components.
  123. *
  124. * @param fileName The batch folder name to split.
  125. * @return The dictionary of batch component keys to their values.
  126. */
  127. - (nullable NSDictionary<NSString *, id> *)batchComponentsFromFilename:(NSString *)fileName;
  128. @end
  129. NS_ASSUME_NONNULL_END