21
21
#import < GoogleDataTransport/GDTCOREvent.h>
22
22
#import < GoogleDataTransport/GDTCORLifecycle.h>
23
23
#import < GoogleDataTransport/GDTCORPrioritizer.h>
24
+ #import < GoogleDataTransport/GDTCORStorageEventSelector.h>
24
25
25
26
#import " GDTCORLibrary/Private/GDTCOREvent_Private.h"
26
27
#import " GDTCORLibrary/Private/GDTCORRegistrar_Private.h"
27
28
#import " GDTCORLibrary/Private/GDTCORUploadCoordinator.h"
28
29
30
+ NSString *const gGDTCORFlatFileStorageEventDataPathKey = @" DataPath" ;
31
+
32
+ NSString *const gGDTCORFlatFileStorageMappingIDPathKey = @" MappingIDPath" ;
33
+
34
+ NSString *const gGDTCORFlatFileStorageQoSTierPathKey = @" QoSTierPath" ;
35
+
29
36
@implementation GDTCORFlatFileStorage
30
37
31
38
+ (void )load {
@@ -51,27 +58,118 @@ + (NSString *)archivePath {
51
58
return archivePath;
52
59
}
53
60
54
- + (NSString *)libraryDataPath {
55
- static NSString *libraryDataPath ;
61
+ + (NSString *)baseEventStoragePath {
62
+ static NSString *eventDataPath ;
56
63
static dispatch_once_t onceToken;
57
64
dispatch_once (&onceToken, ^{
58
- libraryDataPath =
65
+ eventDataPath =
59
66
[GDTCORRootDirectory () URLByAppendingPathComponent: NSStringFromClass ([self class ])
60
67
isDirectory: YES ]
61
68
.path ;
62
- libraryDataPath = [libraryDataPath stringByAppendingPathComponent: @" gdt_library_data " ];
63
- if (![[NSFileManager defaultManager ] fileExistsAtPath: libraryDataPath isDirectory: NULL ]) {
69
+ eventDataPath = [eventDataPath stringByAppendingPathComponent: @" gdt_event_data " ];
70
+ if (![[NSFileManager defaultManager ] fileExistsAtPath: eventDataPath isDirectory: NULL ]) {
64
71
NSError *error;
65
- [[NSFileManager defaultManager ] createDirectoryAtPath: libraryDataPath
72
+ [[NSFileManager defaultManager ] createDirectoryAtPath: eventDataPath
66
73
withIntermediateDirectories: YES
67
74
attributes: 0
68
75
error: &error];
69
76
GDTCORAssert (error == nil , @" Creating the library data path failed: %@ " , error);
70
77
}
71
78
});
79
+ return eventDataPath;
80
+ }
81
+
82
+ + (NSString *)libraryDataStoragePath {
83
+ static NSString *libraryDataPath;
84
+ static dispatch_once_t onceToken;
85
+ dispatch_once (&onceToken, ^{
86
+ libraryDataPath =
87
+ [GDTCORRootDirectory () URLByAppendingPathComponent: NSStringFromClass ([self class ])
88
+ isDirectory: YES ]
89
+ .path ;
90
+ libraryDataPath = [libraryDataPath stringByAppendingPathComponent: @" gdt_library_data" ];
91
+ });
92
+ if (![[NSFileManager defaultManager ] fileExistsAtPath: libraryDataPath isDirectory: NULL ]) {
93
+ NSError *error;
94
+ [[NSFileManager defaultManager ] createDirectoryAtPath: libraryDataPath
95
+ withIntermediateDirectories: YES
96
+ attributes: 0
97
+ error: &error];
98
+ GDTCORAssert (error == nil , @" Creating the library data path failed: %@ " , error);
99
+ }
72
100
return libraryDataPath;
73
101
}
74
102
103
+ + (NSDictionary <NSString *, NSString *> *)pathsForEvent : (GDTCOREvent *)event {
104
+ NSString *dataPath =
105
+ [NSString stringWithFormat: @" %@ /%ld /%@ " , [GDTCORFlatFileStorage baseEventStoragePath ],
106
+ (long )event.target, event.eventID];
107
+ NSString *mappingIDPath =
108
+ [NSString stringWithFormat: @" %@ /%ld /%@ /%@ " , [GDTCORFlatFileStorage baseEventStoragePath ],
109
+ (long )event.target, event.mappingID, event.eventID];
110
+ NSString *qosTierPath =
111
+ [NSString stringWithFormat: @" %@ /%ld /%ld /%@ " , [GDTCORFlatFileStorage baseEventStoragePath ],
112
+ (long )event.target, (long )event.qosTier, event.eventID];
113
+ return @{
114
+ gGDTCORFlatFileStorageEventDataPathKey : dataPath,
115
+ gGDTCORFlatFileStorageMappingIDPathKey : mappingIDPath,
116
+ gGDTCORFlatFileStorageQoSTierPathKey : qosTierPath
117
+ };
118
+ }
119
+
120
+ + (NSString *)pathForTarget : (GDTCORTarget)target
121
+ qosTier : (nullable NSNumber *)qosTier
122
+ mappingID : (nullable NSString *)mappingID {
123
+ NSString *baseEventPath = [GDTCORFlatFileStorage baseEventStoragePath ];
124
+ // If only a target was given, return the target path.
125
+ if (qosTier == nil && mappingID == nil ) {
126
+ return [NSString stringWithFormat: @" %@ /%ld " , baseEventPath, (long )target];
127
+ }
128
+
129
+ // If only a target and mappingID were given, return the mapping ID path.
130
+ if (qosTier == nil ) {
131
+ return [NSString stringWithFormat: @" %@ /%ld /%@ " , baseEventPath, (long )target, mappingID];
132
+ }
133
+
134
+ // If only a target and qosTier were given, return the QoS tier path.
135
+ if (mappingID == nil ) {
136
+ return [NSString stringWithFormat: @" %@ /%ld /%@ " , baseEventPath, (long )target, qosTier];
137
+ }
138
+
139
+ // If a target, mappingID, and qosTier were all given, return a single target/qosTier/mappingID
140
+ // directory.
141
+ return
142
+ [NSString stringWithFormat: @" %@ /%ld /%@ /%@ " , baseEventPath, (long )target, qosTier, mappingID];
143
+ }
144
+
145
+ + (NSArray <NSString *> *)searchPathsWithEventSelector : (GDTCORStorageEventSelector *)eventSelector {
146
+ NSMutableArray <NSString *> *searchPaths = [[NSMutableArray alloc ] init ];
147
+ if (eventSelector.selectedQosTiers && eventSelector.selectedQosTiers .count > 0 ) {
148
+ for (NSNumber *qosTier in eventSelector.selectedQosTiers ) {
149
+ NSString *searchPath = [self pathForTarget: eventSelector.selectedTarget
150
+ qosTier: qosTier
151
+ mappingID: eventSelector.selectedMappingID];
152
+ BOOL isDirectory;
153
+ if ([[NSFileManager defaultManager ] fileExistsAtPath: searchPath isDirectory: &isDirectory]) {
154
+ if (isDirectory) {
155
+ [searchPaths addObject: searchPath];
156
+ }
157
+ }
158
+ }
159
+ } else {
160
+ NSString *searchPath = [self pathForTarget: eventSelector.selectedTarget
161
+ qosTier: nil
162
+ mappingID: eventSelector.selectedMappingID];
163
+ BOOL isDirectory;
164
+ if ([[NSFileManager defaultManager ] fileExistsAtPath: searchPath isDirectory: &isDirectory]) {
165
+ if (isDirectory) {
166
+ [searchPaths addObject: searchPath];
167
+ }
168
+ }
169
+ }
170
+ return searchPaths;
171
+ }
172
+
75
173
+ (instancetype )sharedInstance {
76
174
static GDTCORFlatFileStorage *sharedStorage;
77
175
static dispatch_once_t onceToken;
@@ -214,7 +312,7 @@ - (void)libraryDataForKey:(nonnull NSString *)key
214
312
onComplete :
215
313
(nonnull void (^)(NSData *_Nullable, NSError *_Nullable error))onComplete {
216
314
dispatch_async (_storageQueue, ^{
217
- NSString *dataPath = [[[self class ] libraryDataPath ] stringByAppendingPathComponent: key];
315
+ NSString *dataPath = [[[self class ] libraryDataStoragePath ] stringByAppendingPathComponent: key];
218
316
NSError *error;
219
317
NSData *data = [NSData dataWithContentsOfFile: dataPath options: 0 error: &error];
220
318
if (onComplete) {
@@ -234,7 +332,7 @@ - (void)storeLibraryData:(NSData *)data
234
332
}
235
333
dispatch_async (_storageQueue, ^{
236
334
NSError *error;
237
- NSString *dataPath = [[[self class ] libraryDataPath ] stringByAppendingPathComponent: key];
335
+ NSString *dataPath = [[[self class ] libraryDataStoragePath ] stringByAppendingPathComponent: key];
238
336
[data writeToFile: dataPath options: NSDataWritingAtomic error: &error];
239
337
if (onComplete) {
240
338
onComplete (error);
@@ -246,7 +344,7 @@ - (void)removeLibraryDataForKey:(nonnull NSString *)key
246
344
onComplete : (nonnull void (^)(NSError *_Nullable error))onComplete {
247
345
dispatch_async (_storageQueue, ^{
248
346
NSError *error;
249
- NSString *dataPath = [[[self class ] libraryDataPath ] stringByAppendingPathComponent: key];
347
+ NSString *dataPath = [[[self class ] libraryDataStoragePath ] stringByAppendingPathComponent: key];
250
348
if ([[NSFileManager defaultManager ] fileExistsAtPath: dataPath]) {
251
349
[[NSFileManager defaultManager ] removeItemAtPath: dataPath error: &error];
252
350
if (onComplete) {
@@ -310,8 +408,8 @@ - (void)appWillForeground:(GDTCORApplication *)app {
310
408
311
409
- (void )appWillBackground : (GDTCORApplication *)app {
312
410
dispatch_async (_storageQueue, ^{
313
- // Immediately request a background task to run until the end of the current queue of work, and
314
- // cancel it once the work is done.
411
+ // Immediately request a background task to run until the end of the current queue of work,
412
+ // and cancel it once the work is done.
315
413
__block GDTCORBackgroundIdentifier bgID =
316
414
[app beginBackgroundTaskWithName: @" GDTStorage"
317
415
expirationHandler: ^{
0 commit comments