GDTCORFlatFileStorage.h 5.7 KB

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