Skip to content

Commit 6c14c21

Browse files
authored
Merge storage refactor back to master (#5322)
* Make mph-gdtsqlite run travis (#3861) * Implement a SQLite helper (#4075) * Add support for in-memory and other types of stores (#4158) * Add support for in-memory and other types of stores * Temporarily add -x to know what's causing check.sh to fail * Remove -x * DO NOT MERGE TO MASTER: Remove commit math from check.sh temporarily * Initial implementation of GDTCORDatabase (#4180) * Initial implementation of GDTCORDatabase * Style * Check result of running a statement * Simplify init Also support >1 :memory: db * Write test docs and fix one tsan issue (#4211) * wip moving storage to sqlite * Revert "wip moving storage to sqlite" This reverts commit ee1895f. * Address tsan issue and document tests * Address implicit self retains Don't know why these aren't surfaced locally, I have the warning on. * Implement bulk sql execution (#4528) * Change custom prioritization dictionary to bytes (#4534) * Change custom prioritization dictionary to bytes * Fix a missed change for customBytes * Refactor GDT's storage to be an interface (#5022) * Refactor GDT's storage to be an interface - Events are now removed by eventID - Events now generate an ID when they are alloc/inited - Copying events also copies their eventID - Event hashing takes into account the eventID - A static method in GDTCOREvent creates event IDs as the concat of <session counter><event counter during session> - Session counters are incremented and stored in user defaults at app start - Storage instances register themselves with GDTCORRegistrar like prioritizers and uploaders - Storage fakes are refactored to be registered with GDTCORRegistrar - Added some missing docs and fixed some typos * Remove an unintended change from assert macro * Use a variable * Additional docs and test fixes * Move saving the counter value to a file * Update docs * Rename GDTCORStorage to GDTCORFlatFileStorage (#5079) * Rename GDTCORStorage to GDTCORFlatFileStorage * Fix the NSCoding keys * Fixes to get building and unit tests passing * Address lack of thread safety affecting lifecycle tests * Style * Transition customPrioritizationDict to customBytes Adds a category on GDTCOREvent to make checking network connection info easier. * Remove all sqlite and database stufff or the purposes of merging back to maste (#5330) * Update .travis.yml
1 parent 9675f8d commit 6c14c21

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+974
-535
lines changed

GoogleDataTransport.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Shared library for iOS SDK data transport needs.
2020
s.tvos.deployment_target = '10.0'
2121
s.watchos.deployment_target = '6.0'
2222

23-
# To develop or run the tests, >= 1.8.0.beta.1 must be installed.
23+
# To develop or run the tests, >= 1.8.0 must be installed.
2424
s.cocoapods_version = '>= 1.4.0'
2525

2626
s.static_framework = true

GoogleDataTransport/GDTCORLibrary/GDTCOREvent.m

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,41 @@
2626

2727
@implementation GDTCOREvent
2828

29+
+ (NSNumber *)nextEventID {
30+
static unsigned long long nextEventID = 0;
31+
static NSString *counterPath;
32+
static dispatch_queue_t eventIDQueue;
33+
static dispatch_once_t onceToken;
34+
dispatch_once(&onceToken, ^{
35+
eventIDQueue = dispatch_queue_create("com.google.GDTCOREventIDQueue", DISPATCH_QUEUE_SERIAL);
36+
counterPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
37+
counterPath = [NSString stringWithFormat:@"%@/google-sdks-events/count", counterPath];
38+
NSError *error;
39+
NSString *countText = [NSString stringWithContentsOfFile:counterPath
40+
encoding:NSUTF8StringEncoding
41+
error:&error];
42+
const char *countChars = [countText UTF8String];
43+
unsigned long long count = -1;
44+
if (countChars) {
45+
count = strtoull([countText UTF8String], NULL, 10);
46+
}
47+
nextEventID = error || count < 0 ? 0 : count;
48+
});
49+
50+
__block NSNumber *result;
51+
dispatch_sync(eventIDQueue, ^{
52+
result = @(nextEventID);
53+
nextEventID++;
54+
NSError *error;
55+
[[result stringValue] writeToFile:counterPath
56+
atomically:YES
57+
encoding:NSUTF8StringEncoding
58+
error:&error];
59+
GDTCORAssert(error == nil, @"There was an error saving the new counter value to disk.");
60+
});
61+
return result;
62+
}
63+
2964
- (nullable instancetype)initWithMappingID:(NSString *)mappingID target:(NSInteger)target {
3065
GDTCORAssert(mappingID.length > 0, @"Please give a valid mapping ID");
3166
GDTCORAssert(target > 0, @"A target cannot be negative or 0");
@@ -34,6 +69,7 @@ - (nullable instancetype)initWithMappingID:(NSString *)mappingID target:(NSInteg
3469
}
3570
self = [super init];
3671
if (self) {
72+
_eventID = [GDTCOREvent nextEventID];
3773
_mappingID = mappingID;
3874
_target = target;
3975
_qosTier = GDTCOREventQosDefault;
@@ -44,23 +80,25 @@ - (nullable instancetype)initWithMappingID:(NSString *)mappingID target:(NSInteg
4480

4581
- (instancetype)copy {
4682
GDTCOREvent *copy = [[GDTCOREvent alloc] initWithMappingID:_mappingID target:_target];
83+
copy->_eventID = _eventID;
4784
copy.dataObject = _dataObject;
4885
copy.qosTier = _qosTier;
4986
copy.clockSnapshot = _clockSnapshot;
50-
copy.customPrioritizationParams = _customPrioritizationParams;
87+
copy.customBytes = _customBytes;
5188
copy->_GDTFilePath = _GDTFilePath;
5289
GDTCORLogDebug("Copying event %@ to event %@", self, copy);
5390
return copy;
5491
}
5592

5693
- (NSUInteger)hash {
5794
// This loses some precision, but it's probably fine.
95+
NSUInteger eventIDHash = [_eventID hash];
5896
NSUInteger mappingIDHash = [_mappingID hash];
5997
NSUInteger timeHash = [_clockSnapshot hash];
6098
NSInteger dataObjectHash = [_dataObject hash];
6199
NSUInteger fileURL = [_GDTFilePath hash];
62100

63-
return mappingIDHash ^ _target ^ _qosTier ^ timeHash ^ dataObjectHash ^ fileURL;
101+
return eventIDHash ^ mappingIDHash ^ _target ^ _qosTier ^ timeHash ^ dataObjectHash ^ fileURL;
64102
}
65103

66104
- (BOOL)isEqual:(id)object {
@@ -109,6 +147,9 @@ - (BOOL)writeToGDTPath:(NSString *)filePath error:(NSError **)error {
109147

110148
#pragma mark - NSSecureCoding and NSCoding Protocols
111149

150+
/** NSCoding key for eventID property. */
151+
static NSString *eventIDKey = @"_eventID";
152+
112153
/** NSCoding key for mappingID property. */
113154
static NSString *mappingIDKey = @"_mappingID";
114155

@@ -127,9 +168,6 @@ - (BOOL)writeToGDTPath:(NSString *)filePath error:(NSError **)error {
127168
/** NSCoding key for GDTFilePath property. */
128169
static NSString *kGDTFilePathKey = @"_GDTFilePath";
129170

130-
/** NSCoding key for customPrioritizationParams property. */
131-
static NSString *customPrioritizationParams = @"_customPrioritizationParams";
132-
133171
/** NSCoding key for backwards compatibility of GDTCORStoredEvent mappingID property.*/
134172
static NSString *kStoredEventMappingIDKey = @"GDTCORStoredEventMappingIDKey";
135173

@@ -145,10 +183,8 @@ - (BOOL)writeToGDTPath:(NSString *)filePath error:(NSError **)error {
145183
/** NSCoding key for backwards compatibility of GDTCORStoredEvent dataFuture property.*/
146184
static NSString *kStoredEventDataFutureKey = @"GDTCORStoredEventDataFutureKey";
147185

148-
/** NSCoding key for backwards compatibility of GDTCORStoredEvent customPrioritizationParams
149-
* property.*/
150-
static NSString *kStoredEventCustomPrioritizationParamsKey =
151-
@"GDTCORStoredEventcustomPrioritizationParamsKey";
186+
/** NSCoding key for customData property. */
187+
static NSString *kCustomDataKey = @"GDTCOREventCustomDataKey";
152188

153189
+ (BOOL)supportsSecureCoding {
154190
return YES;
@@ -165,6 +201,7 @@ - (id)initWithCoder:(NSCoder *)aDecoder {
165201
NSInteger target = [aDecoder decodeIntegerForKey:targetKey];
166202
self = [self initWithMappingID:mappingID target:target];
167203
if (self) {
204+
_eventID = [aDecoder decodeObjectOfClass:[NSNumber class] forKey:eventIDKey];
168205
_qosTier = [aDecoder decodeIntegerForKey:qosTierKey];
169206
_clockSnapshot = [aDecoder decodeObjectOfClass:[GDTCORClock class] forKey:clockSnapshotKey];
170207
NSURL *fileURL = [aDecoder decodeObjectOfClass:[NSURL class] forKey:fileURLKey];
@@ -173,8 +210,7 @@ - (id)initWithCoder:(NSCoder *)aDecoder {
173210
} else {
174211
_GDTFilePath = [aDecoder decodeObjectOfClass:[NSString class] forKey:kGDTFilePathKey];
175212
}
176-
_customPrioritizationParams = [aDecoder decodeObjectOfClass:[NSDictionary class]
177-
forKey:customPrioritizationParams];
213+
_customBytes = [aDecoder decodeObjectOfClass:[NSData class] forKey:kCustomDataKey];
178214
}
179215
return self;
180216
}
@@ -187,6 +223,10 @@ - (id)initWithCoderForStoredEventBackwardCompatibility:(NSCoder *)aDecoder
187223
forKey:kStoredEventTargetKey] integerValue];
188224
self = [self initWithMappingID:mappingID target:target];
189225
if (self) {
226+
_eventID = [aDecoder decodeObjectOfClass:[NSNumber class] forKey:eventIDKey];
227+
if (_eventID == nil) {
228+
_eventID = [GDTCOREvent nextEventID];
229+
}
190230
_qosTier = [[aDecoder decodeObjectOfClass:[NSNumber class]
191231
forKey:kStoredEventQosTierKey] integerValue];
192232
_clockSnapshot = [aDecoder decodeObjectOfClass:[GDTCORClock class]
@@ -196,20 +236,19 @@ - (id)initWithCoderForStoredEventBackwardCompatibility:(NSCoder *)aDecoder
196236
} else {
197237
_GDTFilePath = [aDecoder decodeObjectOfClass:[NSString class] forKey:kGDTFilePathKey];
198238
}
199-
_customPrioritizationParams =
200-
[aDecoder decodeObjectOfClass:[NSDictionary class]
201-
forKey:kStoredEventCustomPrioritizationParamsKey];
239+
_customBytes = [aDecoder decodeObjectOfClass:[NSData class] forKey:kCustomDataKey];
202240
}
203241
return self;
204242
}
205243

206244
- (void)encodeWithCoder:(NSCoder *)aCoder {
245+
[aCoder encodeObject:_eventID forKey:eventIDKey];
207246
[aCoder encodeObject:_mappingID forKey:mappingIDKey];
208247
[aCoder encodeInteger:_target forKey:targetKey];
209248
[aCoder encodeInteger:_qosTier forKey:qosTierKey];
210249
[aCoder encodeObject:_clockSnapshot forKey:clockSnapshotKey];
211250
[aCoder encodeObject:_GDTFilePath forKey:kGDTFilePathKey];
212-
[aCoder encodeObject:_customPrioritizationParams forKey:customPrioritizationParams];
251+
[aCoder encodeObject:_customBytes forKey:kCustomDataKey];
213252
}
214253

215254
@end

0 commit comments

Comments
 (0)