20
20
#import < GoogleDataTransport/GDTCORConsoleLogger.h>
21
21
#import < GoogleDataTransport/GDTCOREvent.h>
22
22
#import < GoogleDataTransport/GDTCORLifecycle.h>
23
- #import < GoogleDataTransport/GDTCORPlatform.h>
24
23
#import < GoogleDataTransport/GDTCORPrioritizer.h>
25
- #import < GoogleDataTransport/GDTCORStorageEventSelector.h>
26
24
27
25
#import " GDTCORLibrary/Private/GDTCOREvent_Private.h"
28
- #import " GDTCORLibrary/Private/GDTCORFlatFileStorageIterator.h"
29
26
#import " GDTCORLibrary/Private/GDTCORRegistrar_Private.h"
30
27
#import " GDTCORLibrary/Private/GDTCORUploadCoordinator.h"
31
28
32
- NSString *const gGDTCORFlatFileStorageEventDataPathKey = @" DataPath" ;
33
-
34
- NSString *const gGDTCORFlatFileStorageMappingIDPathKey = @" MappingIDPath" ;
35
-
36
- NSString *const gGDTCORFlatFileStorageQoSTierPathKey = @" QoSTierPath" ;
37
-
38
29
@implementation GDTCORFlatFileStorage
39
30
40
31
+ (void )load {
@@ -60,115 +51,27 @@ + (NSString *)archivePath {
60
51
return archivePath;
61
52
}
62
53
63
- + (NSString *)baseEventStoragePath {
64
- static NSString *eventDataPath ;
54
+ + (NSString *)libraryDataPath {
55
+ static NSString *libraryDataPath ;
65
56
static dispatch_once_t onceToken;
66
57
dispatch_once (&onceToken, ^{
67
- eventDataPath =
58
+ libraryDataPath =
68
59
[GDTCORRootDirectory () URLByAppendingPathComponent: NSStringFromClass ([self class ])
69
60
isDirectory: YES ]
70
61
.path ;
71
- eventDataPath = [eventDataPath stringByAppendingPathComponent: @" gdt_event_data " ];
72
- if (![[NSFileManager defaultManager ] fileExistsAtPath: eventDataPath isDirectory: NULL ]) {
62
+ libraryDataPath = [libraryDataPath stringByAppendingPathComponent: @" gdt_library_data " ];
63
+ if (![[NSFileManager defaultManager ] fileExistsAtPath: libraryDataPath isDirectory: NULL ]) {
73
64
NSError *error;
74
- [[NSFileManager defaultManager ] createDirectoryAtPath: eventDataPath
65
+ [[NSFileManager defaultManager ] createDirectoryAtPath: libraryDataPath
75
66
withIntermediateDirectories: YES
76
67
attributes: 0
77
68
error: &error];
78
69
GDTCORAssert (error == nil , @" Creating the library data path failed: %@ " , error);
79
70
}
80
71
});
81
- return eventDataPath;
82
- }
83
-
84
- + (NSString *)libraryDataStoragePath {
85
- static NSString *libraryDataPath;
86
- static dispatch_once_t onceToken;
87
- dispatch_once (&onceToken, ^{
88
- libraryDataPath =
89
- [GDTCORRootDirectory () URLByAppendingPathComponent: NSStringFromClass ([self class ])
90
- isDirectory: YES ]
91
- .path ;
92
- libraryDataPath = [libraryDataPath stringByAppendingPathComponent: @" gdt_library_data" ];
93
- });
94
- if (![[NSFileManager defaultManager ] fileExistsAtPath: libraryDataPath isDirectory: NULL ]) {
95
- NSError *error;
96
- [[NSFileManager defaultManager ] createDirectoryAtPath: libraryDataPath
97
- withIntermediateDirectories: YES
98
- attributes: 0
99
- error: &error];
100
- GDTCORAssert (error == nil , @" Creating the library data path failed: %@ " , error);
101
- }
102
72
return libraryDataPath;
103
73
}
104
74
105
- + (NSString *)pathForTarget : (GDTCORTarget)target
106
- eventID : (NSNumber *)eventID
107
- qosTier : (NSNumber *)qosTier
108
- mappingID : (NSString *)mappingID {
109
- return
110
- [NSString stringWithFormat: @" %@ /%ld /%@ .%@ .%@ " , [GDTCORFlatFileStorage baseEventStoragePath ],
111
- (long )target, eventID, qosTier, mappingID];
112
- }
113
-
114
- + (NSSet <NSString *> *)pathsForTarget : (GDTCORTarget)target
115
- eventIDs : (nullable NSSet <NSNumber *> *)eventIDs
116
- qosTiers : (nullable NSSet <NSNumber *> *)qosTiers
117
- mappingIDs : (nullable NSSet <NSString *> *)mappingIDs {
118
- NSMutableSet <NSString *> *paths = [[NSMutableSet alloc ] init ];
119
- NSFileManager *fileManager = [NSFileManager defaultManager ];
120
- NSString *targetPath = [NSString
121
- stringWithFormat: @" %@ /%ld " , [GDTCORFlatFileStorage baseEventStoragePath ], (long )target];
122
- NSError *error;
123
- NSArray <NSString *> *dirPaths = [fileManager contentsOfDirectoryAtPath: targetPath error: &error];
124
- if (error) {
125
- GDTCORLogDebug (@" There was an error reading the contents of the target path: %@ " , error);
126
- return paths;
127
- }
128
- BOOL checkingIDs = eventIDs.count > 0 ;
129
- BOOL checkingQosTiers = qosTiers.count > 0 ;
130
- BOOL checkingMappingIDs = mappingIDs.count > 0 ;
131
- BOOL checkingAnything = checkingIDs == NO && checkingQosTiers == NO && checkingMappingIDs == NO ;
132
- for (NSString *path in dirPaths) {
133
- if (checkingAnything) {
134
- [paths addObject: path];
135
- continue ;
136
- }
137
- NSString *filename = [path lastPathComponent ];
138
- NSArray <NSString *> *components = [filename componentsSeparatedByString: @" ." ];
139
- if (components.count != 3 ) {
140
- GDTCORLogDebug (@" There was an error reading the filename components: %@ " , components);
141
- [[NSFileManager defaultManager ] removeItemAtPath: path error: nil ];
142
- continue ;
143
- }
144
- NSNumber *eventID = @(components[0 ].integerValue );
145
- NSNumber *qosTier = @(components[1 ].integerValue );
146
- NSString *mappingID = components[2 ];
147
- if (eventID == nil || qosTier == nil ) {
148
- GDTCORLogDebug (@" There was an error parsing the filename components: %@ " , components);
149
- [[NSFileManager defaultManager ] removeItemAtPath: path error: nil ];
150
- continue ;
151
- }
152
- NSNumber *eventIDMatch = checkingIDs ? @([eventIDs containsObject: eventID]) : nil ;
153
- NSNumber *qosTierMatch = checkingQosTiers ? @([qosTiers containsObject: qosTier]) : nil ;
154
- NSNumber *mappingIDMatch = checkingMappingIDs ? @([mappingIDs containsObject: mappingID]) : nil ;
155
- if ((eventIDMatch == nil || eventIDMatch.boolValue ) &&
156
- (qosTierMatch == nil || qosTierMatch.boolValue ) &&
157
- (mappingIDMatch == nil || mappingIDMatch.boolValue )) {
158
- [paths addObject: path];
159
- }
160
- }
161
- return paths;
162
- }
163
-
164
- + (NSArray <NSString *> *)searchPathsWithEventSelector : (GDTCORStorageEventSelector *)eventSelector {
165
- NSSet *searchPaths = [GDTCORFlatFileStorage pathsForTarget: eventSelector.selectedTarget
166
- eventIDs: eventSelector.selectedEventIDs
167
- qosTiers: eventSelector.selectedQosTiers
168
- mappingIDs: eventSelector.selectedMappingIDs];
169
- return [searchPaths allObjects ];
170
- }
171
-
172
75
+ (instancetype )sharedInstance {
173
76
static GDTCORFlatFileStorage *sharedStorage;
174
77
static dispatch_once_t onceToken;
@@ -311,7 +214,7 @@ - (void)libraryDataForKey:(nonnull NSString *)key
311
214
onComplete :
312
215
(nonnull void (^)(NSData *_Nullable, NSError *_Nullable error))onComplete {
313
216
dispatch_async (_storageQueue, ^{
314
- NSString *dataPath = [[[self class ] libraryDataStoragePath ] stringByAppendingPathComponent: key];
217
+ NSString *dataPath = [[[self class ] libraryDataPath ] stringByAppendingPathComponent: key];
315
218
NSError *error;
316
219
NSData *data = [NSData dataWithContentsOfFile: dataPath options: 0 error: &error];
317
220
if (onComplete) {
@@ -322,7 +225,7 @@ - (void)libraryDataForKey:(nonnull NSString *)key
322
225
323
226
- (void )storeLibraryData : (NSData *)data
324
227
forKey : (nonnull NSString *)key
325
- onComplete : (nullable void (^)(NSError *_Nullable error))onComplete {
228
+ onComplete : (nonnull void (^)(NSError *_Nullable error))onComplete {
326
229
if (!data || data.length <= 0 ) {
327
230
if (onComplete) {
328
231
onComplete ([NSError errorWithDomain: NSInternalInconsistencyException code: -1 userInfo: nil ]);
@@ -331,7 +234,7 @@ - (void)storeLibraryData:(NSData *)data
331
234
}
332
235
dispatch_async (_storageQueue, ^{
333
236
NSError *error;
334
- NSString *dataPath = [[[self class ] libraryDataStoragePath ] stringByAppendingPathComponent: key];
237
+ NSString *dataPath = [[[self class ] libraryDataPath ] stringByAppendingPathComponent: key];
335
238
[data writeToFile: dataPath options: NSDataWritingAtomic error: &error];
336
239
if (onComplete) {
337
240
onComplete (error);
@@ -343,7 +246,7 @@ - (void)removeLibraryDataForKey:(nonnull NSString *)key
343
246
onComplete : (nonnull void (^)(NSError *_Nullable error))onComplete {
344
247
dispatch_async (_storageQueue, ^{
345
248
NSError *error;
346
- NSString *dataPath = [[[self class ] libraryDataStoragePath ] stringByAppendingPathComponent: key];
249
+ NSString *dataPath = [[[self class ] libraryDataPath ] stringByAppendingPathComponent: key];
347
250
if ([[NSFileManager defaultManager ] fileExistsAtPath: dataPath]) {
348
251
[[NSFileManager defaultManager ] removeItemAtPath: dataPath error: &error];
349
252
if (onComplete) {
@@ -353,61 +256,6 @@ - (void)removeLibraryDataForKey:(nonnull NSString *)key
353
256
});
354
257
}
355
258
356
- - (BOOL )hasEventsForTarget : (GDTCORTarget)target {
357
- // TODO(mikehaney24): Increase the performance of this call.
358
- GDTCORStorageEventSelector *eventSelector =
359
- [[GDTCORStorageEventSelector alloc ] initWithTarget: target
360
- eventIDs: nil
361
- mappingIDs: nil
362
- qosTiers: nil ];
363
- id <GDTCORStorageEventIterator> iter = [self iteratorWithSelector: eventSelector];
364
- return [iter nextEvent ] != nil ;
365
- }
366
-
367
- - (nullable id <GDTCORStorageEventIterator>)iteratorWithSelector :
368
- (nonnull GDTCORStorageEventSelector *)eventSelector {
369
- __block GDTCORFlatFileStorageIterator *iterator;
370
- dispatch_sync (_storageQueue, ^{
371
- NSArray <NSString *> *searchPaths =
372
- [GDTCORFlatFileStorage searchPathsWithEventSelector: eventSelector];
373
- iterator.eventFiles = searchPaths;
374
- });
375
- return iterator;
376
- }
377
-
378
- - (void )purgeEventsFromBefore : (GDTCORClock *)beforeSnapshot
379
- onComplete : (void (^)(NSError *_Nullable error))onComplete {
380
- // TODO(mikehaney24): Figure out how we're going to deal with this.
381
- }
382
-
383
- - (void )storageSizeWithCallback : (void (^)(uint64_t storageSize))onComplete {
384
- dispatch_async (_storageQueue, ^{
385
- unsigned long long totalBytes = 0 ;
386
- NSString *eventDataPath = [GDTCORFlatFileStorage baseEventStoragePath ];
387
- NSString *libraryDataPath = [GDTCORFlatFileStorage libraryDataStoragePath ];
388
- NSDirectoryEnumerator *enumerator =
389
- [[NSFileManager defaultManager ] enumeratorAtPath: eventDataPath];
390
- while ([enumerator nextObject ]) {
391
- NSFileAttributeType fileType = enumerator.fileAttributes [NSFileType ];
392
- if ([fileType isEqual: NSFileTypeRegular ]) {
393
- NSNumber *fileSize = enumerator.fileAttributes [NSFileSize ];
394
- totalBytes += fileSize.unsignedLongLongValue ;
395
- }
396
- }
397
- enumerator = [[NSFileManager defaultManager ] enumeratorAtPath: libraryDataPath];
398
- while ([enumerator nextObject ]) {
399
- NSFileAttributeType fileType = enumerator.fileAttributes [NSFileType ];
400
- if ([fileType isEqual: NSFileTypeRegular ]) {
401
- NSNumber *fileSize = enumerator.fileAttributes [NSFileSize ];
402
- totalBytes += fileSize.unsignedLongLongValue ;
403
- }
404
- }
405
- if (onComplete) {
406
- onComplete (totalBytes);
407
- }
408
- });
409
- }
410
-
411
259
#pragma mark - Private helper methods
412
260
413
261
/* * Saves the event's dataObject to a file using NSData mechanisms.
@@ -462,8 +310,8 @@ - (void)appWillForeground:(GDTCORApplication *)app {
462
310
463
311
- (void )appWillBackground : (GDTCORApplication *)app {
464
312
dispatch_async (_storageQueue, ^{
465
- // Immediately request a background task to run until the end of the current queue of work,
466
- // and cancel it once the work is done.
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.
467
315
__block GDTCORBackgroundIdentifier bgID =
468
316
[app beginBackgroundTaskWithName: @" GDTStorage"
469
317
expirationHandler: ^{
0 commit comments