Skip to content

Commit f8fb6f1

Browse files
authored
Enhance the log storage class (#2275)
* Rename fields related to the previous notion of 'backends' to 'uploaders'. Also changes the declaration of the uploader callback block. * Add the ability to delete a set of logs by filename, and a conversion method for hashes to files. * Change the log storage removeLogs method to be log hashes instead of URLS * Style, and change the completion block to include an error * Change to sync, since async to adding more async created race condition opportunity. * Test new storage methods * Fix coordinator method declarations
1 parent 0fc2ff6 commit f8fb6f1

15 files changed

+179
-41
lines changed

GoogleDataLogger/GoogleDataLogger/Classes/GDLLogStorage.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ NS_ASSUME_NONNULL_BEGIN
4545
*/
4646
- (void)removeLog:(NSNumber *)logHash logTarget:(NSNumber *)logTarget;
4747

48+
/** Removes a set of log fields specified by their filenames.
49+
*
50+
* @param logHashes The set of log files to remove.
51+
* @param logTarget The log target the log files correspond to.
52+
*/
53+
- (void)removeLogs:(NSSet<NSNumber *> *)logHashes logTarget:(NSNumber *)logTarget;
54+
55+
/** Converts a set of log hashes to a set of log files.
56+
*
57+
* @param logHashes A set of log hashes to get the files of.
58+
* @return A set of equivalent length, containing all the filenames corresponding to the hashes.
59+
*/
60+
- (NSSet<NSURL *> *)logHashesToFiles:(NSSet<NSNumber *> *)logHashes;
61+
4862
@end
4963

5064
NS_ASSUME_NONNULL_END

GoogleDataLogger/GoogleDataLogger/Classes/GDLLogStorage.m

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ - (instancetype)init {
5656
if (self) {
5757
_storageQueue = dispatch_queue_create("com.google.GDLLogStorage", DISPATCH_QUEUE_SERIAL);
5858
_logHashToLogFile = [[NSMutableDictionary alloc] init];
59-
_logTargetToLogFileSet = [[NSMutableDictionary alloc] init];
59+
_logTargetToLogHashSet = [[NSMutableDictionary alloc] init];
6060
_uploader = [GDLUploadCoordinator sharedInstance];
6161
}
6262
return self;
@@ -90,7 +90,7 @@ - (void)storeLog:(GDLLogEvent *)log {
9090

9191
// Check the QoS, if it's high priority, notify the log target that it has a high priority log.
9292
if (shortLivedLog.qosTier == GDLLogQoSFast) {
93-
NSSet<NSURL *> *allLogsForLogTarget = self.logTargetToLogFileSet[@(logTarget)];
93+
NSSet<NSNumber *> *allLogsForLogTarget = self.logTargetToLogHashSet[@(logTarget)];
9494
[self.uploader forceUploadLogs:allLogsForLogTarget target:logTarget];
9595
}
9696

@@ -117,13 +117,33 @@ - (void)removeLog:(NSNumber *)logHash logTarget:(NSNumber *)logTarget {
117117

118118
// Remove from the tracking collections.
119119
[self.logHashToLogFile removeObjectForKey:logHash];
120-
NSMutableSet<NSURL *> *logFiles = self.logTargetToLogFileSet[logTarget];
121-
GDLAssert(logFiles, @"There wasn't a logSet for this logTarget.");
122-
[logFiles removeObject:logFile];
120+
NSMutableSet<NSNumber *> *logHashes = self.logTargetToLogHashSet[logTarget];
121+
GDLAssert(logHashes, @"There wasn't a logSet for this logTarget.");
122+
[logHashes removeObject:logHash];
123123
// It's fine to not remove the set if it's empty.
124124
});
125125
}
126126

127+
- (void)removeLogs:(NSSet<NSNumber *> *)logHashes logTarget:(NSNumber *)logTarget {
128+
dispatch_sync(_storageQueue, ^{
129+
for (NSNumber *logHash in logHashes) {
130+
[self removeLog:logHash logTarget:logTarget];
131+
}
132+
});
133+
}
134+
135+
- (NSSet<NSURL *> *)logHashesToFiles:(NSSet<NSNumber *> *)logHashes {
136+
NSMutableSet<NSURL *> *logFiles = [[NSMutableSet alloc] init];
137+
dispatch_sync(_storageQueue, ^{
138+
for (NSNumber *hashNumber in logHashes) {
139+
NSURL *logURL = self.logHashToLogFile[hashNumber];
140+
GDLAssert(logURL, @"A log file URL couldn't be found for the given hash");
141+
[logFiles addObject:logURL];
142+
}
143+
});
144+
return logFiles;
145+
}
146+
127147
#pragma mark - Private helper methods
128148

129149
/** Creates the log directory if it does not exist. */
@@ -170,13 +190,15 @@ - (NSURL *)saveLogProtoToDisk:(NSData *)logProtoBytes logHash:(NSUInteger)logHas
170190
*/
171191
- (void)addLogToTrackingCollections:(GDLLogEvent *)log logFile:(NSURL *)logFile {
172192
NSInteger logTarget = log.logTarget;
173-
self.logHashToLogFile[@(log.hash)] = logFile;
174-
NSMutableSet<NSURL *> *logs = self.logTargetToLogFileSet[@(logTarget)];
193+
NSNumber *logHash = @(log.hash);
194+
NSNumber *logTargetNumber = @(logTarget);
195+
self.logHashToLogFile[logHash] = logFile;
196+
NSMutableSet<NSNumber *> *logs = self.logTargetToLogHashSet[logTargetNumber];
175197
if (logs) {
176-
[logs addObject:logFile];
198+
[logs addObject:logHash];
177199
} else {
178-
NSMutableSet<NSURL *> *logSet = [NSMutableSet setWithObject:logFile];
179-
self.logTargetToLogFileSet[@(logTarget)] = logSet;
200+
NSMutableSet<NSNumber *> *logSet = [NSMutableSet setWithObject:logHash];
201+
self.logTargetToLogHashSet[logTargetNumber] = logSet;
180202
}
181203
}
182204

@@ -185,8 +207,8 @@ - (void)addLogToTrackingCollections:(GDLLogEvent *)log logFile:(NSURL *)logFile
185207
/** The NSKeyedCoder key for the logHashToFile property. */
186208
static NSString *const kGDLLogHashToLogFileKey = @"logHashToLogFileKey";
187209

188-
/** The NSKeyedCoder key for the logTargetToLogFileSet property. */
189-
static NSString *const kGDLLogTargetToLogSetKey = @"logTargetToLogFileSetKey";
210+
/** The NSKeyedCoder key for the logTargetToLogHashSet property. */
211+
static NSString *const kGDLLogTargetToLogHashSetKey = @"logTargetToLogHashSetKey";
190212

191213
+ (BOOL)supportsSecureCoding {
192214
return YES;
@@ -199,8 +221,8 @@ - (instancetype)initWithCoder:(NSCoder *)aDecoder {
199221
Class NSMutableDictionaryClass = [NSMutableDictionary class];
200222
sharedInstance->_logHashToLogFile = [aDecoder decodeObjectOfClass:NSMutableDictionaryClass
201223
forKey:kGDLLogHashToLogFileKey];
202-
sharedInstance->_logTargetToLogFileSet =
203-
[aDecoder decodeObjectOfClass:NSMutableDictionaryClass forKey:kGDLLogTargetToLogSetKey];
224+
sharedInstance->_logTargetToLogHashSet =
225+
[aDecoder decodeObjectOfClass:NSMutableDictionaryClass forKey:kGDLLogTargetToLogHashSetKey];
204226
});
205227
return sharedInstance;
206228
}
@@ -209,7 +231,8 @@ - (void)encodeWithCoder:(NSCoder *)aCoder {
209231
GDLLogStorage *sharedInstance = [self.class sharedInstance];
210232
dispatch_sync(sharedInstance.storageQueue, ^{
211233
[aCoder encodeObject:sharedInstance->_logHashToLogFile forKey:kGDLLogHashToLogFileKey];
212-
[aCoder encodeObject:sharedInstance->_logTargetToLogFileSet forKey:kGDLLogTargetToLogSetKey];
234+
[aCoder encodeObject:sharedInstance->_logTargetToLogHashSet
235+
forKey:kGDLLogTargetToLogHashSetKey];
213236
});
214237
}
215238

GoogleDataLogger/GoogleDataLogger/Classes/GDLRegistrar.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ - (instancetype)init {
3333
self = [super init];
3434
if (self) {
3535
_logTargetToPrioritizer = [[NSMutableDictionary alloc] init];
36-
_logTargetToBackend = [[NSMutableDictionary alloc] init];
36+
_logTargetToUploader = [[NSMutableDictionary alloc] init];
3737
}
3838
return self;
3939
}
4040

4141
- (void)registerBackend:(id<GDLLogUploader>)backend forLogTarget:(GDLLogTarget)logTarget {
42-
self.logTargetToBackend[@(logTarget)] = backend;
42+
self.logTargetToUploader[@(logTarget)] = backend;
4343
}
4444

4545
- (void)registerLogPrioritizer:(id<GDLLogPrioritizer>)prioritizer

GoogleDataLogger/GoogleDataLogger/Classes/GDLUploadCoordinator.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ NS_ASSUME_NONNULL_BEGIN
3333

3434
/** Forces the backend specified by the target to upload the provided set of logs. This should only
3535
* ever happen when the QoS tier of a log requires it.
36+
*
37+
* @param logHashes The set of log hashes to force upload.
38+
* @param logTarget The log target that should force an upload.
3639
*/
37-
- (void)forceUploadLogs:(NSSet<NSURL *> *)logFiles target:(GDLLogTarget)logTarget;
40+
- (void)forceUploadLogs:(NSSet<NSNumber *> *)logHashes target:(GDLLogTarget)logTarget;
3841

3942
@end
4043

GoogleDataLogger/GoogleDataLogger/Classes/GDLUploadCoordinator.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ + (instancetype)sharedInstance {
2929
return sharedUploader;
3030
}
3131

32-
- (void)forceUploadLogs:(NSSet<NSURL *> *)logFiles target:(GDLLogTarget)logTarget {
32+
- (void)forceUploadLogs:(NSSet<NSNumber *> *)logHashes target:(GDLLogTarget)logTarget {
3333
// Ask the prioritizer of the target for some logs
3434
}
3535

GoogleDataLogger/GoogleDataLogger/Classes/Private/GDLLogStorage_Private.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
/** A map of logTargets to a set of log hash values. */
3030
@property(nonatomic)
31-
NSMutableDictionary<NSNumber *, NSMutableSet<NSURL *> *> *logTargetToLogFileSet;
31+
NSMutableDictionary<NSNumber *, NSMutableSet<NSNumber *> *> *logTargetToLogHashSet;
3232

3333
/** The log uploader instance to use. */
3434
@property(nonatomic) GDLUploadCoordinator *uploader;

GoogleDataLogger/GoogleDataLogger/Classes/Private/GDLRegistrar_Private.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
@interface GDLRegistrar ()
2020

2121
/** A map of logTargets to backend implementations. */
22-
@property(nonatomic) NSMutableDictionary<NSNumber *, id<GDLLogUploader>> *logTargetToBackend;
22+
@property(nonatomic) NSMutableDictionary<NSNumber *, id<GDLLogUploader>> *logTargetToUploader;
2323

2424
/** A map of logTargets to prioritizer implementations. */
2525
@property(nonatomic) NSMutableDictionary<NSNumber *, id<GDLLogPrioritizer>> *logTargetToPrioritizer;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2019 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+
17+
#import <Foundation/Foundation.h>
18+
19+
/** The list of targets supported by the shared logging infrastructure. If adding a new target,
20+
* please use the previous value +1, and increment GDLLogTargetLast by 1.
21+
*/
22+
typedef NS_ENUM(NSInteger, GDLLogTarget) {
23+
24+
/** The CCT log target. */
25+
kGDLLogTargetCCT = 1000,
26+
27+
GDLLogTargetLast = 1001
28+
};

GoogleDataLogger/GoogleDataLogger/Classes/Public/GDLLogUploader.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,21 @@
1616

1717
#import <Foundation/Foundation.h>
1818

19+
#import <GoogleDataLogger/GDLClock.h>
20+
#import <GoogleDataLogger/GDLLogTargets.h>
21+
1922
NS_ASSUME_NONNULL_BEGIN
2023

2124
/** A convenient typedef to define the block to be called upon completion of an upload to the
2225
* backend.
26+
*
27+
* target: The log target that was uploading.
28+
* nextUploadAttemptUTC: The desired next upload attempt time.
29+
* uploadError: Populated with any upload error. If non-nil, a retry will be attempted.
2330
*/
24-
typedef void (^GDLBackendCompletionBlock)(NSSet<NSURL *> *_Nullable successfulUploads,
25-
NSSet<NSURL *> *_Nullable unsuccessfulUploads);
31+
typedef void (^GDLUploaderCompletionBlock)(GDLLogTarget target,
32+
GDLClock *nextUploadAttemptUTC,
33+
NSError *_Nullable uploadError);
2634

2735
/** This protocol defines the common interface for logging backend implementations. */
2836
@protocol GDLLogUploader <NSObject>
@@ -36,7 +44,7 @@ typedef void (^GDLBackendCompletionBlock)(NSSet<NSURL *> *_Nullable successfulUp
3644
* - successfulUploads: The set of filenames uploaded successfully.
3745
* - unsuccessfulUploads: The set of filenames not uploaded successfully.
3846
*/
39-
- (void)uploadLogs:(NSSet<NSURL *> *)logFiles onComplete:(GDLBackendCompletionBlock)onComplete;
47+
- (void)uploadLogs:(NSSet<NSURL *> *)logFiles onComplete:(GDLUploaderCompletionBlock)onComplete;
4048

4149
@end
4250

GoogleDataLogger/GoogleDataLogger/Classes/Public/GDLRegistrar.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,11 @@
1717
#import <Foundation/Foundation.h>
1818

1919
#import <GoogleDataLogger/GDLLogPrioritizer.h>
20+
#import <GoogleDataLogger/GDLLogTargets.h>
2021
#import <GoogleDataLogger/GDLLogUploader.h>
2122

2223
NS_ASSUME_NONNULL_BEGIN
2324

24-
/** The list of targets supported by the shared logging infrastructure. */
25-
typedef NS_ENUM(NSInteger, GDLLogTarget) {
26-
27-
/** The CCT log target. */
28-
kGDLLogTargetCCT = 1000
29-
};
30-
3125
/** Manages the registration of log targets with the logging SDK. */
3226
@interface GDLRegistrar : NSObject
3327

GoogleDataLogger/Tests/Unit/Categories/GDLLogStorage+Testing.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ @implementation GDLLogStorage (Testing)
2020

2121
- (void)reset {
2222
dispatch_sync(self.storageQueue, ^{
23-
[self.logTargetToLogFileSet removeAllObjects];
23+
[self.logTargetToLogHashSet removeAllObjects];
2424
[self.logHashToLogFile removeAllObjects];
2525
});
2626
}

GoogleDataLogger/Tests/Unit/Categories/GDLRegistrar+Testing.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ @implementation GDLRegistrar (Testing)
2222

2323
- (void)reset {
2424
[self.logTargetToPrioritizer removeAllObjects];
25-
[self.logTargetToBackend removeAllObjects];
25+
[self.logTargetToUploader removeAllObjects];
2626
}
2727

2828
@end

0 commit comments

Comments
 (0)