diff options
Diffstat (limited to 'Firestore/Source')
20 files changed, 193 insertions, 116 deletions
diff --git a/Firestore/Source/API/FIRTimestamp+Internal.h b/Firestore/Source/API/FIRTimestamp+Internal.h new file mode 100644 index 0000000..48e38b2 --- /dev/null +++ b/Firestore/Source/API/FIRTimestamp+Internal.h @@ -0,0 +1,35 @@ +/* + * Copyright 2018 Google + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#import "FIRTimestamp.h" + +NS_ASSUME_NONNULL_BEGIN + +/** Internal FIRTimestamp API we don't want exposed in our public header files. */ +@interface FIRTimestamp (Internal) + +/** + * Converts the given date to an ISO 8601 timestamp string, useful for rendering in JSON. + * + * ISO 8601 dates times in UTC look like this: "1912-04-14T23:40:00.000000000Z". + * + * @see http://www.ecma-international.org/ecma-262/6.0/#sec-date-time-string-format + */ +- (NSString *)ISO8601String; + +@end + +NS_ASSUME_NONNULL_END diff --git a/Firestore/Source/Core/FSTTimestamp.mm b/Firestore/Source/API/FIRTimestamp.m index d2b492a..489b921 100644 --- a/Firestore/Source/Core/FSTTimestamp.mm +++ b/Firestore/Source/API/FIRTimestamp.m @@ -14,26 +14,36 @@ * limitations under the License. */ -#import "Firestore/Source/Core/FSTTimestamp.h" - -#include "Firestore/core/src/firebase/firestore/util/comparison.h" - -#import "Firestore/Source/Util/FSTAssert.h" - -using firebase::firestore::util::WrapCompare; +#import "Firestore/Source/API/FIRTimestamp+Internal.h" NS_ASSUME_NONNULL_BEGIN static const int kNanosPerSecond = 1000000000; -@implementation FSTTimestamp +@implementation FIRTimestamp (Internal) -#pragma mark - Constructors +#pragma mark - Internal public methods -+ (instancetype)timestamp { - return [FSTTimestamp timestampWithDate:[NSDate date]]; +- (NSString *)ISO8601String { + NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; + formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss"; + formatter.timeZone = [NSTimeZone timeZoneWithName:@"UTC"]; + NSDate *secondsDate = [NSDate dateWithTimeIntervalSince1970:self.seconds]; + NSString *secondsString = [formatter stringFromDate:secondsDate]; + if (secondsString.length != 19) { + [NSException raise:@"Invalid ISO string" format:@"Invalid ISO string: %@", secondsString]; + } + + NSString *nanosString = [NSString stringWithFormat:@"%09d", self.nanoseconds]; + return [NSString stringWithFormat:@"%@.%@Z", secondsString, nanosString]; } +@end + +@implementation FIRTimestamp + +#pragma mark - Constructors + + (instancetype)timestampWithDate:(NSDate *)date { double secondsDouble; double fraction = modf(date.timeIntervalSince1970, &secondsDouble); @@ -44,21 +54,41 @@ static const int kNanosPerSecond = 1000000000; } int64_t seconds = (int64_t)secondsDouble; int32_t nanos = (int32_t)(fraction * kNanosPerSecond); - return [[FSTTimestamp alloc] initWithSeconds:seconds nanos:nanos]; + return [[FIRTimestamp alloc] initWithSeconds:seconds nanoseconds:nanos]; } -- (instancetype)initWithSeconds:(int64_t)seconds nanos:(int32_t)nanos { ++ (instancetype)timestampWithSeconds:(int64_t)seconds nanoseconds:(int32_t)nanoseconds { + return [[FIRTimestamp alloc] initWithSeconds:seconds nanoseconds:nanoseconds]; +} + ++ (instancetype)timestamp { + return [FIRTimestamp timestampWithDate:[NSDate date]]; +} + +- (instancetype)initWithSeconds:(int64_t)seconds nanoseconds:(int32_t)nanoseconds { self = [super init]; if (self) { - FSTAssert(nanos >= 0, @"timestamp nanoseconds out of range: %d", nanos); - FSTAssert(nanos < 1e9, @"timestamp nanoseconds out of range: %d", nanos); - // Midnight at the beginning of 1/1/1 is the earliest timestamp Firestore supports. - FSTAssert(seconds >= -62135596800L, @"timestamp seconds out of range: %lld", seconds); + if (nanoseconds < 0) { + [NSException raise:@"Invalid timestamp" + format:@"Timestamp nanoseconds out of range: %d", nanoseconds]; + } + if (nanoseconds >= 1e9) { + [NSException raise:@"Invalid timestamp" + format:@"Timestamp nanoseconds out of range: %d", nanoseconds]; + } + // Midnight at the beginning of 1/1/1 is the earliest timestamp supported. + if (seconds < -62135596800L) { + [NSException raise:@"Invalid timestamp" + format:@"Timestamp seconds out of range: %lld", seconds]; + } // This will break in the year 10,000. - FSTAssert(seconds < 253402300800L, @"timestamp seconds out of range: %lld", seconds); + if (seconds >= 253402300800L) { + [NSException raise:@"Invalid timestamp" + format:@"Timestamp seconds out of range: %lld", seconds]; + } _seconds = seconds; - _nanos = nanos; + _nanoseconds = nanoseconds; } return self; } @@ -69,19 +99,19 @@ static const int kNanosPerSecond = 1000000000; if (self == object) { return YES; } - if (![object isKindOfClass:[FSTTimestamp class]]) { + if (![object isKindOfClass:[FIRTimestamp class]]) { return NO; } - return [self isEqualToTimestamp:(FSTTimestamp *)object]; + return [self isEqualToTimestamp:(FIRTimestamp *)object]; } - (NSUInteger)hash { - return (NSUInteger)((self.seconds >> 32) ^ self.seconds ^ self.nanos); + return (NSUInteger)((self.seconds >> 32) ^ self.seconds ^ self.nanoseconds); } - (NSString *)description { - return [NSString - stringWithFormat:@"<FSTTimestamp: seconds=%lld nanos=%d>", self.seconds, self.nanos]; + return [NSString stringWithFormat:@"FIRTimestamp: seconds=%lld nanoseconds=%d>", self.seconds, + self.nanoseconds]; } /** Implements NSCopying without actually copying because timestamps are immutable. */ @@ -92,32 +122,29 @@ static const int kNanosPerSecond = 1000000000; #pragma mark - Public methods - (NSDate *)approximateDateValue { - NSTimeInterval interval = (NSTimeInterval)self.seconds + ((NSTimeInterval)self.nanos) / 1e9; + NSTimeInterval interval = (NSTimeInterval)self.seconds + ((NSTimeInterval)self.nanoseconds) / 1e9; return [NSDate dateWithTimeIntervalSince1970:interval]; } -- (BOOL)isEqualToTimestamp:(FSTTimestamp *)other { - return [self compare:other] == NSOrderedSame; -} - -- (NSString *)ISO8601String { - NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; - formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss"; - formatter.timeZone = [NSTimeZone timeZoneWithName:@"UTC"]; - NSDate *secondsDate = [NSDate dateWithTimeIntervalSince1970:self.seconds]; - NSString *secondsString = [formatter stringFromDate:secondsDate]; - FSTAssert(secondsString.length == 19, @"Invalid ISO string: %@", secondsString); +- (NSComparisonResult)compare:(FIRTimestamp *)other { + if (self.seconds < other.seconds) { + return NSOrderedAscending; + } else if (self.seconds > other.seconds) { + return NSOrderedDescending; + } - NSString *nanosString = [NSString stringWithFormat:@"%09d", self.nanos]; - return [NSString stringWithFormat:@"%@.%@Z", secondsString, nanosString]; + if (self.nanoseconds < other.nanoseconds) { + return NSOrderedAscending; + } else if (self.nanoseconds > other.nanoseconds) { + return NSOrderedDescending; + } + return NSOrderedSame; } -- (NSComparisonResult)compare:(FSTTimestamp *)other { - NSComparisonResult result = WrapCompare<int64_t>(self.seconds, other.seconds); - if (result != NSOrderedSame) { - return result; - } - return WrapCompare<int32_t>(self.nanos, other.nanos); +#pragma mark - Private methods + +- (BOOL)isEqualToTimestamp:(FIRTimestamp *)other { + return [self compare:other] == NSOrderedSame; } @end diff --git a/Firestore/Source/API/FSTUserDataConverter.mm b/Firestore/Source/API/FSTUserDataConverter.mm index 80d4625..f97aea1 100644 --- a/Firestore/Source/API/FSTUserDataConverter.mm +++ b/Firestore/Source/API/FSTUserDataConverter.mm @@ -16,13 +16,14 @@ #import "Firestore/Source/API/FSTUserDataConverter.h" +#import "FIRTimestamp.h" + #import "FIRGeoPoint.h" #import "Firestore/Source/API/FIRDocumentReference+Internal.h" #import "Firestore/Source/API/FIRFieldPath+Internal.h" #import "Firestore/Source/API/FIRFieldValue+Internal.h" #import "Firestore/Source/API/FIRFirestore+Internal.h" #import "Firestore/Source/API/FIRSetOptions+Internal.h" -#import "Firestore/Source/Core/FSTTimestamp.h" #import "Firestore/Source/Model/FSTDocumentKey.h" #import "Firestore/Source/Model/FSTFieldValue.h" #import "Firestore/Source/Model/FSTMutation.h" @@ -536,7 +537,7 @@ typedef NS_ENUM(NSInteger, FSTUserDataSource) { return [FSTStringValue stringValue:input]; } else if ([input isKindOfClass:[NSDate class]]) { - return [FSTTimestampValue timestampValue:[FSTTimestamp timestampWithDate:input]]; + return [FSTTimestampValue timestampValue:[FIRTimestamp timestampWithDate:input]]; } else if ([input isKindOfClass:[FIRGeoPoint class]]) { return [FSTGeoPointValue geoPointValue:input]; diff --git a/Firestore/Source/Core/FSTSnapshotVersion.h b/Firestore/Source/Core/FSTSnapshotVersion.h index b72e4a2..8649d40 100644 --- a/Firestore/Source/Core/FSTSnapshotVersion.h +++ b/Firestore/Source/Core/FSTSnapshotVersion.h @@ -18,7 +18,7 @@ NS_ASSUME_NONNULL_BEGIN -@class FSTTimestamp; +@class FIRTimestamp; /** * A version of a document in Firestore. This corresponds to the version timestamp, such as @@ -30,13 +30,13 @@ NS_ASSUME_NONNULL_BEGIN + (instancetype)noVersion; /** Creates a new version representing the given timestamp. */ -+ (instancetype)versionWithTimestamp:(FSTTimestamp *)timestamp; ++ (instancetype)versionWithTimestamp:(FIRTimestamp *)timestamp; - (instancetype)init NS_UNAVAILABLE; - (NSComparisonResult)compare:(FSTSnapshotVersion *)other; -@property(nonatomic, strong, readonly) FSTTimestamp *timestamp; +@property(nonatomic, strong, readonly) FIRTimestamp *timestamp; @end diff --git a/Firestore/Source/Core/FSTSnapshotVersion.mm b/Firestore/Source/Core/FSTSnapshotVersion.mm index 980ae52..58b2be4 100644 --- a/Firestore/Source/Core/FSTSnapshotVersion.mm +++ b/Firestore/Source/Core/FSTSnapshotVersion.mm @@ -16,7 +16,7 @@ #import "Firestore/Source/Core/FSTSnapshotVersion.h" -#import "Firestore/Source/Core/FSTTimestamp.h" +#import "FIRTimestamp.h" NS_ASSUME_NONNULL_BEGIN @@ -26,17 +26,17 @@ NS_ASSUME_NONNULL_BEGIN static FSTSnapshotVersion *min; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ - FSTTimestamp *timestamp = [[FSTTimestamp alloc] initWithSeconds:0 nanos:0]; + FIRTimestamp *timestamp = [[FIRTimestamp alloc] initWithSeconds:0 nanoseconds:0]; min = [FSTSnapshotVersion versionWithTimestamp:timestamp]; }); return min; } -+ (instancetype)versionWithTimestamp:(FSTTimestamp *)timestamp { ++ (instancetype)versionWithTimestamp:(FIRTimestamp *)timestamp { return [[FSTSnapshotVersion alloc] initWithTimestamp:timestamp]; } -- (instancetype)initWithTimestamp:(FSTTimestamp *)timestamp { +- (instancetype)initWithTimestamp:(FIRTimestamp *)timestamp { self = [super init]; if (self) { _timestamp = timestamp; diff --git a/Firestore/Source/Local/FSTLevelDBMutationQueue.mm b/Firestore/Source/Local/FSTLevelDBMutationQueue.mm index 982e09c..248ef9a 100644 --- a/Firestore/Source/Local/FSTLevelDBMutationQueue.mm +++ b/Firestore/Source/Local/FSTLevelDBMutationQueue.mm @@ -269,7 +269,7 @@ static ReadOptions StandardReadOptions() { } } -- (FSTMutationBatch *)addMutationBatchWithWriteTime:(FSTTimestamp *)localWriteTime +- (FSTMutationBatch *)addMutationBatchWithWriteTime:(FIRTimestamp *)localWriteTime mutations:(NSArray<FSTMutation *> *)mutations group:(FSTWriteGroup *)group { FSTBatchID batchID = self.nextBatchID; diff --git a/Firestore/Source/Local/FSTLocalSerializer.mm b/Firestore/Source/Local/FSTLocalSerializer.mm index c531c77..ec70ca0 100644 --- a/Firestore/Source/Local/FSTLocalSerializer.mm +++ b/Firestore/Source/Local/FSTLocalSerializer.mm @@ -142,7 +142,7 @@ [mutations addObject:[remoteSerializer decodedMutation:write]]; } - FSTTimestamp *localWriteTime = [remoteSerializer decodedTimestamp:batch.localWriteTime]; + FIRTimestamp *localWriteTime = [remoteSerializer decodedTimestamp:batch.localWriteTime]; return [[FSTMutationBatch alloc] initWithBatchID:batchID localWriteTime:localWriteTime diff --git a/Firestore/Source/Local/FSTLocalStore.mm b/Firestore/Source/Local/FSTLocalStore.mm index d12a45b..a5b85cd 100644 --- a/Firestore/Source/Local/FSTLocalStore.mm +++ b/Firestore/Source/Local/FSTLocalStore.mm @@ -16,10 +16,10 @@ #import "Firestore/Source/Local/FSTLocalStore.h" +#import "FIRTimestamp.h" #import "Firestore/Source/Core/FSTListenSequence.h" #import "Firestore/Source/Core/FSTQuery.h" #import "Firestore/Source/Core/FSTSnapshotVersion.h" -#import "Firestore/Source/Core/FSTTimestamp.h" #import "Firestore/Source/Local/FSTGarbageCollector.h" #import "Firestore/Source/Local/FSTLocalDocumentsView.h" #import "Firestore/Source/Local/FSTLocalViewChanges.h" @@ -201,7 +201,7 @@ NS_ASSUME_NONNULL_BEGIN - (FSTLocalWriteResult *)locallyWriteMutations:(NSArray<FSTMutation *> *)mutations { FSTWriteGroup *group = [self.persistence startGroupWithAction:@"Locally write mutations"]; - FSTTimestamp *localWriteTime = [FSTTimestamp timestamp]; + FIRTimestamp *localWriteTime = [FIRTimestamp timestamp]; FSTMutationBatch *batch = [self.mutationQueue addMutationBatchWithWriteTime:localWriteTime mutations:mutations group:group]; diff --git a/Firestore/Source/Local/FSTMemoryMutationQueue.mm b/Firestore/Source/Local/FSTMemoryMutationQueue.mm index 702f614..2a6a1cc 100644 --- a/Firestore/Source/Local/FSTMemoryMutationQueue.mm +++ b/Firestore/Source/Local/FSTMemoryMutationQueue.mm @@ -139,7 +139,7 @@ static const NSComparator NumberComparator = ^NSComparisonResult(NSNumber *left, self.lastStreamToken = streamToken; } -- (FSTMutationBatch *)addMutationBatchWithWriteTime:(FSTTimestamp *)localWriteTime +- (FSTMutationBatch *)addMutationBatchWithWriteTime:(FIRTimestamp *)localWriteTime mutations:(NSArray<FSTMutation *> *)mutations group:(FSTWriteGroup *)group { FSTAssert(mutations.count > 0, @"Mutation batches should not be empty"); diff --git a/Firestore/Source/Local/FSTMutationQueue.h b/Firestore/Source/Local/FSTMutationQueue.h index a1eddd4..12f3284 100644 --- a/Firestore/Source/Local/FSTMutationQueue.h +++ b/Firestore/Source/Local/FSTMutationQueue.h @@ -23,7 +23,7 @@ @class FSTMutation; @class FSTMutationBatch; @class FSTQuery; -@class FSTTimestamp; +@class FIRTimestamp; @class FSTWriteGroup; NS_ASSUME_NONNULL_BEGIN @@ -75,7 +75,7 @@ NS_ASSUME_NONNULL_BEGIN - (void)setLastStreamToken:(nullable NSData *)streamToken group:(FSTWriteGroup *)group; /** Creates a new mutation batch and adds it to this mutation queue. */ -- (FSTMutationBatch *)addMutationBatchWithWriteTime:(FSTTimestamp *)localWriteTime +- (FSTMutationBatch *)addMutationBatchWithWriteTime:(FIRTimestamp *)localWriteTime mutations:(NSArray<FSTMutation *> *)mutations group:(FSTWriteGroup *)group; diff --git a/Firestore/Source/Model/FSTFieldValue.h b/Firestore/Source/Model/FSTFieldValue.h index c8118c6..be8ba45 100644 --- a/Firestore/Source/Model/FSTFieldValue.h +++ b/Firestore/Source/Model/FSTFieldValue.h @@ -22,7 +22,7 @@ @class FSTDocumentKey; @class FSTFieldPath; -@class FSTTimestamp; +@class FIRTimestamp; @class FSTFieldValueOptions; @class FIRGeoPoint; @class FIRSnapshotOptions; @@ -164,8 +164,8 @@ typedef NS_ENUM(NSInteger, FSTServerTimestampBehavior) { * A timestamp value stored in Firestore. */ @interface FSTTimestampValue : FSTFieldValue <NSDate *> -+ (instancetype)timestampValue:(FSTTimestamp *)value; -- (FSTTimestamp *)internalValue; ++ (instancetype)timestampValue:(FIRTimestamp *)value; +- (FIRTimestamp *)internalValue; @end /** @@ -181,10 +181,10 @@ typedef NS_ENUM(NSInteger, FSTServerTimestampBehavior) { * sort by their localWriteTime. */ @interface FSTServerTimestampValue : FSTFieldValue <id> -+ (instancetype)serverTimestampValueWithLocalWriteTime:(FSTTimestamp *)localWriteTime ++ (instancetype)serverTimestampValueWithLocalWriteTime:(FIRTimestamp *)localWriteTime previousValue:(nullable FSTFieldValue *)previousValue; -@property(nonatomic, strong, readonly) FSTTimestamp *localWriteTime; +@property(nonatomic, strong, readonly) FIRTimestamp *localWriteTime; @property(nonatomic, strong, readonly, nullable) FSTFieldValue *previousValue; @end diff --git a/Firestore/Source/Model/FSTFieldValue.mm b/Firestore/Source/Model/FSTFieldValue.mm index 3f3548c..5ef64e1 100644 --- a/Firestore/Source/Model/FSTFieldValue.mm +++ b/Firestore/Source/Model/FSTFieldValue.mm @@ -16,12 +16,13 @@ #import "Firestore/Source/Model/FSTFieldValue.h" +#import "FIRTimestamp.h" + #include "Firestore/core/src/firebase/firestore/util/comparison.h" #include "Firestore/core/src/firebase/firestore/util/string_apple.h" #import "Firestore/Source/API/FIRGeoPoint+Internal.h" #import "Firestore/Source/API/FIRSnapshotOptions+Internal.h" -#import "Firestore/Source/Core/FSTTimestamp.h" #import "Firestore/Source/Model/FSTDocumentKey.h" #import "Firestore/Source/Model/FSTPath.h" #import "Firestore/Source/Util/FSTAssert.h" @@ -424,19 +425,19 @@ struct Comparator<NSString *> { #pragma mark - FSTTimestampValue @interface FSTTimestampValue () -@property(nonatomic, strong, readonly) FSTTimestamp *internalValue; +@property(nonatomic, strong, readonly) FIRTimestamp *internalValue; @end @implementation FSTTimestampValue -+ (instancetype)timestampValue:(FSTTimestamp *)value { ++ (instancetype)timestampValue:(FIRTimestamp *)value { return [[FSTTimestampValue alloc] initWithValue:value]; } -- (id)initWithValue:(FSTTimestamp *)value { +- (id)initWithValue:(FIRTimestamp *)value { self = [super init]; if (self) { - _internalValue = value; // FSTTimestamp is immutable. + _internalValue = value; // FIRTimestamp is immutable. } return self; } @@ -476,13 +477,13 @@ struct Comparator<NSString *> { @implementation FSTServerTimestampValue -+ (instancetype)serverTimestampValueWithLocalWriteTime:(FSTTimestamp *)localWriteTime ++ (instancetype)serverTimestampValueWithLocalWriteTime:(FIRTimestamp *)localWriteTime previousValue:(nullable FSTFieldValue *)previousValue { return [[FSTServerTimestampValue alloc] initWithLocalWriteTime:localWriteTime previousValue:previousValue]; } -- (id)initWithLocalWriteTime:(FSTTimestamp *)localWriteTime +- (id)initWithLocalWriteTime:(FIRTimestamp *)localWriteTime previousValue:(nullable FSTFieldValue *)previousValue { self = [super init]; if (self) { diff --git a/Firestore/Source/Model/FSTMutation.h b/Firestore/Source/Model/FSTMutation.h index 7c5f6de..72f6a25 100644 --- a/Firestore/Source/Model/FSTMutation.h +++ b/Firestore/Source/Model/FSTMutation.h @@ -23,7 +23,7 @@ @class FSTMaybeDocument; @class FSTObjectValue; @class FSTSnapshotVersion; -@class FSTTimestamp; +@class FIRTimestamp; NS_ASSUME_NONNULL_BEGIN @@ -200,7 +200,7 @@ typedef NS_ENUM(NSUInteger, FSTPreconditionExists) { */ - (nullable FSTMaybeDocument *)applyTo:(nullable FSTMaybeDocument *)maybeDoc baseDocument:(nullable FSTMaybeDocument *)baseDoc - localWriteTime:(FSTTimestamp *)localWriteTime + localWriteTime:(FIRTimestamp *)localWriteTime mutationResult:(nullable FSTMutationResult *)mutationResult; /** @@ -209,7 +209,7 @@ typedef NS_ENUM(NSUInteger, FSTPreconditionExists) { */ - (nullable FSTMaybeDocument *)applyTo:(nullable FSTMaybeDocument *)maybeDoc baseDocument:(nullable FSTMaybeDocument *)baseDoc - localWriteTime:(nullable FSTTimestamp *)localWriteTime; + localWriteTime:(nullable FIRTimestamp *)localWriteTime; @property(nonatomic, strong, readonly) FSTDocumentKey *key; diff --git a/Firestore/Source/Model/FSTMutation.mm b/Firestore/Source/Model/FSTMutation.mm index c249138..e702644 100644 --- a/Firestore/Source/Model/FSTMutation.mm +++ b/Firestore/Source/Model/FSTMutation.mm @@ -16,8 +16,9 @@ #import "Firestore/Source/Model/FSTMutation.h" +#import "FIRTimestamp.h" + #import "Firestore/Source/Core/FSTSnapshotVersion.h" -#import "Firestore/Source/Core/FSTTimestamp.h" #import "Firestore/Source/Model/FSTDocument.h" #import "Firestore/Source/Model/FSTDocumentKey.h" #import "Firestore/Source/Model/FSTFieldValue.h" @@ -238,14 +239,14 @@ NS_ASSUME_NONNULL_BEGIN - (nullable FSTMaybeDocument *)applyTo:(nullable FSTMaybeDocument *)maybeDoc baseDocument:(nullable FSTMaybeDocument *)baseDoc - localWriteTime:(FSTTimestamp *)localWriteTime + localWriteTime:(FIRTimestamp *)localWriteTime mutationResult:(nullable FSTMutationResult *)mutationResult { @throw FSTAbstractMethodException(); // NOLINT } - (nullable FSTMaybeDocument *)applyTo:(nullable FSTMaybeDocument *)maybeDoc baseDocument:(nullable FSTMaybeDocument *)baseDoc - localWriteTime:(nullable FSTTimestamp *)localWriteTime { + localWriteTime:(nullable FIRTimestamp *)localWriteTime { return [self applyTo:maybeDoc baseDocument:baseDoc localWriteTime:localWriteTime mutationResult:nil]; } @@ -292,7 +293,7 @@ NS_ASSUME_NONNULL_BEGIN - (nullable FSTMaybeDocument *)applyTo:(nullable FSTMaybeDocument *)maybeDoc baseDocument:(nullable FSTMaybeDocument *)baseDoc - localWriteTime:(FSTTimestamp *)localWriteTime + localWriteTime:(FIRTimestamp *)localWriteTime mutationResult:(nullable FSTMutationResult *)mutationResult { if (mutationResult) { FSTAssert(!mutationResult.transformResults, @"Transform results received by FSTSetMutation."); @@ -368,7 +369,7 @@ NS_ASSUME_NONNULL_BEGIN - (nullable FSTMaybeDocument *)applyTo:(nullable FSTMaybeDocument *)maybeDoc baseDocument:(nullable FSTMaybeDocument *)baseDoc - localWriteTime:(FSTTimestamp *)localWriteTime + localWriteTime:(FIRTimestamp *)localWriteTime mutationResult:(nullable FSTMutationResult *)mutationResult { if (mutationResult) { FSTAssert(!mutationResult.transformResults, @"Transform results received by FSTPatchMutation."); @@ -458,7 +459,7 @@ NS_ASSUME_NONNULL_BEGIN - (nullable FSTMaybeDocument *)applyTo:(nullable FSTMaybeDocument *)maybeDoc baseDocument:(nullable FSTMaybeDocument *)baseDoc - localWriteTime:(FSTTimestamp *)localWriteTime + localWriteTime:(FIRTimestamp *)localWriteTime mutationResult:(nullable FSTMutationResult *)mutationResult { if (mutationResult) { FSTAssert(mutationResult.transformResults, @@ -500,7 +501,7 @@ NS_ASSUME_NONNULL_BEGIN */ - (NSArray<FSTFieldValue *> *)localTransformResultsWithBaseDocument: (FSTMaybeDocument *_Nullable)baseDocument - writeTime:(FSTTimestamp *)localWriteTime { + writeTime:(FIRTimestamp *)localWriteTime { NSMutableArray<FSTFieldValue *> *transformResults = [NSMutableArray array]; for (FSTFieldTransform *fieldTransform in self.fieldTransforms) { if ([fieldTransform.transform isKindOfClass:[FSTServerTimestampTransform class]]) { @@ -570,7 +571,7 @@ NS_ASSUME_NONNULL_BEGIN - (nullable FSTMaybeDocument *)applyTo:(nullable FSTMaybeDocument *)maybeDoc baseDocument:(nullable FSTMaybeDocument *)baseDoc - localWriteTime:(FSTTimestamp *)localWriteTime + localWriteTime:(FIRTimestamp *)localWriteTime mutationResult:(nullable FSTMutationResult *)mutationResult { if (mutationResult) { FSTAssert(!mutationResult.transformResults, diff --git a/Firestore/Source/Model/FSTMutationBatch.h b/Firestore/Source/Model/FSTMutationBatch.h index 145adfa..1de79fe 100644 --- a/Firestore/Source/Model/FSTMutationBatch.h +++ b/Firestore/Source/Model/FSTMutationBatch.h @@ -21,7 +21,7 @@ #import "Firestore/Source/Model/FSTDocumentVersionDictionary.h" @class FSTMutation; -@class FSTTimestamp; +@class FIRTimestamp; @class FSTMutationResult; @class FSTMutationBatchResult; @class FSTSnapshotVersion; @@ -45,7 +45,7 @@ extern const FSTBatchID kFSTBatchIDUnknown; /** Initializes a mutation batch with the given batchID, localWriteTime, and mutations. */ - (instancetype)initWithBatchID:(FSTBatchID)batchID - localWriteTime:(FSTTimestamp *)localWriteTime + localWriteTime:(FIRTimestamp *)localWriteTime mutations:(NSArray<FSTMutation *> *)mutations NS_DESIGNATED_INITIALIZER; - (id)init NS_UNAVAILABLE; @@ -86,7 +86,7 @@ extern const FSTBatchID kFSTBatchIDUnknown; - (FSTDocumentKeySet *)keys; @property(nonatomic, assign, readonly) FSTBatchID batchID; -@property(nonatomic, strong, readonly) FSTTimestamp *localWriteTime; +@property(nonatomic, strong, readonly) FIRTimestamp *localWriteTime; @property(nonatomic, strong, readonly) NSArray<FSTMutation *> *mutations; @end diff --git a/Firestore/Source/Model/FSTMutationBatch.mm b/Firestore/Source/Model/FSTMutationBatch.mm index 01adca7..07aadbb 100644 --- a/Firestore/Source/Model/FSTMutationBatch.mm +++ b/Firestore/Source/Model/FSTMutationBatch.mm @@ -16,8 +16,9 @@ #import "Firestore/Source/Model/FSTMutationBatch.h" +#import "FIRTimestamp.h" + #import "Firestore/Source/Core/FSTSnapshotVersion.h" -#import "Firestore/Source/Core/FSTTimestamp.h" #import "Firestore/Source/Model/FSTDocument.h" #import "Firestore/Source/Model/FSTDocumentKey.h" #import "Firestore/Source/Model/FSTMutation.h" @@ -30,7 +31,7 @@ const FSTBatchID kFSTBatchIDUnknown = -1; @implementation FSTMutationBatch - (instancetype)initWithBatchID:(FSTBatchID)batchID - localWriteTime:(FSTTimestamp *)localWriteTime + localWriteTime:(FIRTimestamp *)localWriteTime mutations:(NSArray<FSTMutation *> *)mutations { self = [super init]; if (self) { diff --git a/Firestore/Source/Core/FSTTimestamp.h b/Firestore/Source/Public/FIRTimestamp.h index f86779d..d0a77f3 100644 --- a/Firestore/Source/Core/FSTTimestamp.h +++ b/Firestore/Source/Public/FIRTimestamp.h @@ -1,5 +1,5 @@ /* - * Copyright 2017 Google + * Copyright 2018 Google * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,40 +19,50 @@ NS_ASSUME_NONNULL_BEGIN /** - * An FSTTimestamp represents an absolute time from the backend at up to nanosecond precision. - * An FSTTimestamp is represented in terms of UTC and does not have an associated timezone. + * A Timestamp represents a point in time independent of any time zone or calendar, represented as + * seconds and fractions of seconds at nanosecond resolution in UTC Epoch time. It is encoded using + * the Proleptic Gregorian Calendar which extends the Gregorian calendar backwards to year one. It + * is encoded assuming all minutes are 60 seconds long, i.e. leap seconds are "smeared" so that no + * leap second table is needed for interpretation. Range is from 0001-01-01T00:00:00Z to + * 9999-12-31T23:59:59.999999999Z. By restricting to that range, we ensure that we can convert to + * and from RFC 3339 date strings. + * + * @see https://github.com/google/protobuf/blob/master/src/google/protobuf/timestamp.proto for the + * reference timestamp definition. */ -@interface FSTTimestamp : NSObject <NSCopying> +NS_SWIFT_NAME(Timestamp) +@interface FIRTimestamp : NSObject <NSCopying> +/** */ - (instancetype)init NS_UNAVAILABLE; /** * Creates a new timestamp. * * @param seconds the number of seconds since epoch. - * @param nanos the number of nanoseconds after the seconds. + * @param nanoseconds the number of nanoseconds after the seconds. */ -- (instancetype)initWithSeconds:(int64_t)seconds nanos:(int32_t)nanos NS_DESIGNATED_INITIALIZER; +- (instancetype)initWithSeconds:(int64_t)seconds + nanoseconds:(int32_t)nanoseconds NS_DESIGNATED_INITIALIZER; -/** Creates a new timestamp with the current date / time. */ -+ (instancetype)timestamp; +/** + * Creates a new timestamp. + * + * @param seconds the number of seconds since epoch. + * @param nanoseconds the number of nanoseconds after the seconds. + */ ++ (instancetype)timestampWithSeconds:(int64_t)seconds nanoseconds:(int32_t)nanoseconds; /** Creates a new timestamp from the given date. */ + (instancetype)timestampWithDate:(NSDate *)date; +/** Creates a new timestamp with the current date / time. */ ++ (instancetype)timestamp; + /** Returns a new NSDate corresponding to this timestamp. This may lose precision. */ - (NSDate *)approximateDateValue; -/** - * Converts the given date to a an ISO 8601 timestamp string, useful for rendering in JSON. - * - * ISO 8601 dates times in UTC look like this: "1912-04-14T23:40:00.000000000Z". - * - * @see http://www.ecma-international.org/ecma-262/6.0/#sec-date-time-string-format - */ -- (NSString *)ISO8601String; - -- (NSComparisonResult)compare:(FSTTimestamp *)other; +- (NSComparisonResult)compare:(FIRTimestamp *)other; /** * Represents seconds of UTC time since Unix epoch 1970-01-01T00:00:00Z. @@ -65,7 +75,7 @@ NS_ASSUME_NONNULL_BEGIN * fractions must still have non-negative nanos values that count forward in time. * Must be from 0 to 999,999,999 inclusive. */ -@property(nonatomic, assign, readonly) int32_t nanos; +@property(nonatomic, assign, readonly) int32_t nanoseconds; @end diff --git a/Firestore/Source/Public/FirebaseFirestore.h b/Firestore/Source/Public/FirebaseFirestore.h index ff110fd..36f9fb7 100644 --- a/Firestore/Source/Public/FirebaseFirestore.h +++ b/Firestore/Source/Public/FirebaseFirestore.h @@ -29,5 +29,6 @@ #import "FIRQuerySnapshot.h" #import "FIRSetOptions.h" #import "FIRSnapshotMetadata.h" +#import "FIRTimestamp.h" #import "FIRTransaction.h" #import "FIRWriteBatch.h" diff --git a/Firestore/Source/Remote/FSTSerializerBeta.h b/Firestore/Source/Remote/FSTSerializerBeta.h index 03e6d14..0f1c3ae 100644 --- a/Firestore/Source/Remote/FSTSerializerBeta.h +++ b/Firestore/Source/Remote/FSTSerializerBeta.h @@ -28,7 +28,7 @@ @class FSTQuery; @class FSTQueryData; @class FSTSnapshotVersion; -@class FSTTimestamp; +@class FIRTimestamp; @class FSTWatchChange; @class GCFSBatchGetDocumentsResponse; @@ -61,8 +61,8 @@ NS_ASSUME_NONNULL_BEGIN - (instancetype)initWithDatabaseID:(const firebase::firestore::model::DatabaseId *)databaseID NS_DESIGNATED_INITIALIZER; -- (GPBTimestamp *)encodedTimestamp:(FSTTimestamp *)timestamp; -- (FSTTimestamp *)decodedTimestamp:(GPBTimestamp *)timestamp; +- (GPBTimestamp *)encodedTimestamp:(FIRTimestamp *)timestamp; +- (FIRTimestamp *)decodedTimestamp:(GPBTimestamp *)timestamp; - (GPBTimestamp *)encodedVersion:(FSTSnapshotVersion *)version; - (FSTSnapshotVersion *)decodedVersion:(GPBTimestamp *)version; diff --git a/Firestore/Source/Remote/FSTSerializerBeta.mm b/Firestore/Source/Remote/FSTSerializerBeta.mm index 35ab637..ceb0501 100644 --- a/Firestore/Source/Remote/FSTSerializerBeta.mm +++ b/Firestore/Source/Remote/FSTSerializerBeta.mm @@ -19,6 +19,7 @@ #include <inttypes.h> #import <GRPCClient/GRPCCall.h> +#import "FIRTimestamp.h" #import "Firestore/Protos/objc/google/firestore/v1beta1/Common.pbobjc.h" #import "Firestore/Protos/objc/google/firestore/v1beta1/Document.pbobjc.h" @@ -32,7 +33,6 @@ #import "FIRGeoPoint.h" #import "Firestore/Source/Core/FSTQuery.h" #import "Firestore/Source/Core/FSTSnapshotVersion.h" -#import "Firestore/Source/Core/FSTTimestamp.h" #import "Firestore/Source/Local/FSTQueryData.h" #import "Firestore/Source/Model/FSTDocument.h" #import "Firestore/Source/Model/FSTDocumentKey.h" @@ -69,15 +69,15 @@ NS_ASSUME_NONNULL_BEGIN #pragma mark - FSTSnapshotVersion <=> GPBTimestamp -- (GPBTimestamp *)encodedTimestamp:(FSTTimestamp *)timestamp { +- (GPBTimestamp *)encodedTimestamp:(FIRTimestamp *)timestamp { GPBTimestamp *result = [GPBTimestamp message]; result.seconds = timestamp.seconds; - result.nanos = timestamp.nanos; + result.nanos = timestamp.nanoseconds; return result; } -- (FSTTimestamp *)decodedTimestamp:(GPBTimestamp *)timestamp { - return [[FSTTimestamp alloc] initWithSeconds:timestamp.seconds nanos:timestamp.nanos]; +- (FIRTimestamp *)decodedTimestamp:(GPBTimestamp *)timestamp { + return [[FIRTimestamp alloc] initWithSeconds:timestamp.seconds nanoseconds:timestamp.nanos]; } - (GPBTimestamp *)encodedVersion:(FSTSnapshotVersion *)version { @@ -287,7 +287,7 @@ NS_ASSUME_NONNULL_BEGIN return result; } -- (GCFSValue *)encodedTimestampValue:(FSTTimestamp *)value { +- (GCFSValue *)encodedTimestampValue:(FIRTimestamp *)value { GCFSValue *result = [GCFSValue message]; result.timestampValue = [self encodedTimestamp:value]; return result; |