Skip to content

Commit fd8947c

Browse files
authored
Port FIRSnapshotMetadata to C++ (#2580)
1 parent e4dcc96 commit fd8947c

18 files changed

+234
-80
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ jobs:
8383
script:
8484
# Eliminate the one warning from BoringSSL when CocoaPods 1.6.0 is available.
8585
# The travis_wait is necessary because the command takes more than 10 minutes.
86-
- travis_wait 25 ./scripts/if_changed.sh ./scripts/pod_lib_lint.sh FirebaseFirestore.podspec --platforms=ios --allow-warnings --no-subspecs
86+
- travis_wait 30 ./scripts/if_changed.sh ./scripts/pod_lib_lint.sh FirebaseFirestore.podspec --platforms=ios --allow-warnings --no-subspecs
8787

8888
# pod lib lint to check build and warnings for static library build - only on cron jobs
8989
- stage: test

Firestore/Example/Tests/API/FIRQuerySnapshotTests.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ - (void)testIncludeMetadataChanges {
104104
/*from_cache=*/false,
105105
/*sync_state_changed=*/true,
106106
/*excludes_metadata_changes=*/false};
107-
FIRSnapshotMetadata *metadata = [FIRSnapshotMetadata snapshotMetadataWithPendingWrites:NO
108-
fromCache:NO];
107+
FIRSnapshotMetadata *metadata = [[FIRSnapshotMetadata alloc] initWithPendingWrites:NO
108+
fromCache:NO];
109109
FIRQuerySnapshot *snapshot = [FIRQuerySnapshot snapshotWithFirestore:firestore
110110
originalQuery:query
111111
snapshot:std::move(viewSnapshot)

Firestore/Example/Tests/API/FIRSnapshotMetadataTests.mm

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,11 @@ @interface FIRSnapshotMetadataTests : XCTestCase
2828
@implementation FIRSnapshotMetadataTests
2929

3030
- (void)testEquals {
31-
FIRSnapshotMetadata *foo = [FIRSnapshotMetadata snapshotMetadataWithPendingWrites:YES
32-
fromCache:YES];
33-
FIRSnapshotMetadata *fooDup = [FIRSnapshotMetadata snapshotMetadataWithPendingWrites:YES
34-
fromCache:YES];
35-
FIRSnapshotMetadata *bar = [FIRSnapshotMetadata snapshotMetadataWithPendingWrites:YES
36-
fromCache:NO];
37-
FIRSnapshotMetadata *baz = [FIRSnapshotMetadata snapshotMetadataWithPendingWrites:NO
38-
fromCache:YES];
31+
FIRSnapshotMetadata *foo = [[FIRSnapshotMetadata alloc] initWithPendingWrites:YES fromCache:YES];
32+
FIRSnapshotMetadata *fooDup = [[FIRSnapshotMetadata alloc] initWithPendingWrites:YES
33+
fromCache:YES];
34+
FIRSnapshotMetadata *bar = [[FIRSnapshotMetadata alloc] initWithPendingWrites:YES fromCache:NO];
35+
FIRSnapshotMetadata *baz = [[FIRSnapshotMetadata alloc] initWithPendingWrites:NO fromCache:YES];
3936
XCTAssertEqualObjects(foo, fooDup);
4037
XCTAssertNotEqualObjects(foo, bar);
4138
XCTAssertNotEqualObjects(foo, baz);
@@ -47,6 +44,16 @@ - (void)testEquals {
4744
XCTAssertNotEqual([bar hash], [baz hash]);
4845
}
4946

47+
- (void)testProperties {
48+
FIRSnapshotMetadata *metadata = [[FIRSnapshotMetadata alloc] initWithPendingWrites:YES
49+
fromCache:NO];
50+
XCTAssertTrue(metadata.hasPendingWrites);
51+
XCTAssertTrue(metadata.pendingWrites);
52+
53+
XCTAssertFalse(metadata.isFromCache);
54+
XCTAssertFalse(metadata.fromCache);
55+
}
56+
5057
@end
5158

5259
NS_ASSUME_NONNULL_END

Firestore/Example/Tests/API/FSTAPIHelpers.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
bool hasPendingWrites,
9999
bool fromCache) {
100100
FIRSnapshotMetadata *metadata =
101-
[FIRSnapshotMetadata snapshotMetadataWithPendingWrites:hasPendingWrites fromCache:fromCache];
101+
[[FIRSnapshotMetadata alloc] initWithPendingWrites:hasPendingWrites fromCache:fromCache];
102102
FSTDocumentSet *oldDocuments = FSTTestDocSet(FSTDocumentComparatorByKey, @[]);
103103
DocumentKeySet mutatedKeys;
104104
for (NSString *key in oldDocs) {

Firestore/Source/API/FIRDocumentSnapshot+Internal.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,33 @@
1717
#import "FIRDocumentSnapshot.h"
1818

1919
#include "Firestore/core/src/firebase/firestore/api/document_snapshot.h"
20+
#include "Firestore/core/src/firebase/firestore/api/snapshot_metadata.h"
2021
#include "Firestore/core/src/firebase/firestore/model/document_key.h"
2122

2223
@class FIRFirestore;
2324
@class FSTDocument;
2425

26+
using firebase::firestore::api::DocumentSnapshot;
27+
using firebase::firestore::api::Firestore;
28+
using firebase::firestore::api::SnapshotMetadata;
29+
using firebase::firestore::model::DocumentKey;
30+
2531
NS_ASSUME_NONNULL_BEGIN
2632

2733
@interface FIRDocumentSnapshot (/* Init */)
2834

29-
- (instancetype)initWithSnapshot:(firebase::firestore::api::DocumentSnapshot &&)snapshot
30-
NS_DESIGNATED_INITIALIZER;
35+
- (instancetype)initWithSnapshot:(DocumentSnapshot &&)snapshot NS_DESIGNATED_INITIALIZER;
36+
37+
- (instancetype)initWithFirestore:(Firestore *)firestore
38+
documentKey:(DocumentKey)documentKey
39+
document:(nullable FSTDocument *)document
40+
metadata:(SnapshotMetadata)metadata;
3141

32-
- (instancetype)initWithFirestore:(firebase::firestore::api::Firestore *)firestore
33-
documentKey:(firebase::firestore::model::DocumentKey)documentKey
42+
- (instancetype)initWithFirestore:(Firestore *)firestore
43+
documentKey:(DocumentKey)documentKey
3444
document:(nullable FSTDocument *)document
3545
fromCache:(bool)fromCache
36-
hasPendingWrites:(bool)pendingWrites;
46+
hasPendingWrites:(bool)hasPendingWrites;
3747

3848
@end
3949

Firestore/Source/API/FIRDocumentSnapshot.mm

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ ServerTimestampBehavior InternalServerTimestampBehavior(FIRServerTimestampBehavi
6868

6969
@implementation FIRDocumentSnapshot {
7070
DocumentSnapshot _snapshot;
71+
72+
FIRSnapshotMetadata *_cachedMetadata;
7173
}
7274

7375
- (instancetype)initWithSnapshot:(DocumentSnapshot &&)snapshot {
@@ -80,12 +82,22 @@ - (instancetype)initWithSnapshot:(DocumentSnapshot &&)snapshot {
8082
- (instancetype)initWithFirestore:(Firestore *)firestore
8183
documentKey:(DocumentKey)documentKey
8284
document:(nullable FSTDocument *)document
83-
fromCache:(bool)fromCache
84-
hasPendingWrites:(bool)pendingWrites {
85-
DocumentSnapshot wrapped{firestore, std::move(documentKey), document, fromCache, pendingWrites};
85+
metadata:(SnapshotMetadata)metadata {
86+
DocumentSnapshot wrapped{firestore, std::move(documentKey), document, std::move(metadata)};
8687
return [self initWithSnapshot:std::move(wrapped)];
8788
}
8889

90+
- (instancetype)initWithFirestore:(Firestore *)firestore
91+
documentKey:(DocumentKey)documentKey
92+
document:(nullable FSTDocument *)document
93+
fromCache:(bool)fromCache
94+
hasPendingWrites:(bool)hasPendingWrites {
95+
return [self initWithFirestore:firestore
96+
documentKey:std::move(documentKey)
97+
document:document
98+
metadata:SnapshotMetadata(hasPendingWrites, fromCache)];
99+
}
100+
89101
// NSObject Methods
90102
- (BOOL)isEqual:(nullable id)other {
91103
if (other == self) return YES;
@@ -120,7 +132,10 @@ - (NSString *)documentID {
120132
@dynamic metadata;
121133

122134
- (FIRSnapshotMetadata *)metadata {
123-
return _snapshot.GetMetadata();
135+
if (!_cachedMetadata) {
136+
_cachedMetadata = [[FIRSnapshotMetadata alloc] initWithMetadata:_snapshot.metadata()];
137+
}
138+
return _cachedMetadata;
124139
}
125140

126141
- (nullable NSDictionary<NSString *, id> *)data {

Firestore/Source/API/FIRQuery.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ - (void)getDocumentsWithSource:(FIRFirestoreSource)source
183183
ViewSnapshot snapshot = maybe_snapshot.ValueOrDie();
184184

185185
FIRSnapshotMetadata *metadata =
186-
[FIRSnapshotMetadata snapshotMetadataWithPendingWrites:snapshot.has_pending_writes()
187-
fromCache:snapshot.from_cache()];
186+
[[FIRSnapshotMetadata alloc] initWithPendingWrites:snapshot.has_pending_writes()
187+
fromCache:snapshot.from_cache()];
188188

189189
listener([FIRQuerySnapshot snapshotWithFirestore:firestore
190190
originalQuery:query

Firestore/Source/API/FIRSnapshotMetadata+Internal.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@
1818

1919
#import <Foundation/Foundation.h>
2020

21+
#include "Firestore/core/src/firebase/firestore/api/snapshot_metadata.h"
22+
23+
using firebase::firestore::api::SnapshotMetadata;
24+
2125
NS_ASSUME_NONNULL_BEGIN
2226

23-
@interface FIRSnapshotMetadata (Internal)
27+
@interface FIRSnapshotMetadata (/* Init */)
28+
29+
- (instancetype)initWithMetadata:(SnapshotMetadata)metadata NS_DESIGNATED_INITIALIZER;
2430

25-
+ (instancetype)snapshotMetadataWithPendingWrites:(BOOL)pendingWrites fromCache:(BOOL)fromCache;
31+
- (instancetype)initWithPendingWrites:(bool)pendingWrites fromCache:(bool)fromCache;
2632

2733
@end
2834

Firestore/Source/API/FIRSnapshotMetadata.mm

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,53 +16,49 @@
1616

1717
#import "FIRSnapshotMetadata.h"
1818

19-
#import "Firestore/Source/API/FIRSnapshotMetadata+Internal.h"
20-
21-
NS_ASSUME_NONNULL_BEGIN
22-
23-
@interface FIRSnapshotMetadata ()
19+
#include <utility>
2420

25-
- (instancetype)initWithPendingWrites:(BOOL)pendingWrites fromCache:(BOOL)fromCache;
21+
#import "Firestore/Source/API/FIRSnapshotMetadata+Internal.h"
2622

27-
@end
23+
#include "Firestore/core/src/firebase/firestore/api/snapshot_metadata.h"
2824

29-
@implementation FIRSnapshotMetadata (Internal)
25+
NS_ASSUME_NONNULL_BEGIN
3026

31-
+ (instancetype)snapshotMetadataWithPendingWrites:(BOOL)pendingWrites fromCache:(BOOL)fromCache {
32-
return [[FIRSnapshotMetadata alloc] initWithPendingWrites:pendingWrites fromCache:fromCache];
27+
@implementation FIRSnapshotMetadata {
28+
SnapshotMetadata _metadata;
3329
}
3430

35-
@end
36-
37-
@implementation FIRSnapshotMetadata
38-
39-
- (instancetype)initWithPendingWrites:(BOOL)pendingWrites fromCache:(BOOL)fromCache {
31+
- (instancetype)initWithMetadata:(SnapshotMetadata)metadata {
4032
if (self = [super init]) {
41-
_pendingWrites = pendingWrites;
42-
_fromCache = fromCache;
33+
_metadata = std::move(metadata);
4334
}
4435
return self;
4536
}
4637

38+
- (instancetype)initWithPendingWrites:(bool)pendingWrites fromCache:(bool)fromCache {
39+
SnapshotMetadata wrapped(pendingWrites, fromCache);
40+
return [self initWithMetadata:std::move(wrapped)];
41+
}
42+
4743
// NSObject Methods
4844
- (BOOL)isEqual:(nullable id)other {
4945
if (other == self) return YES;
50-
if (![[other class] isEqual:[self class]]) return NO;
46+
if (![other isKindOfClass:[FIRSnapshotMetadata class]]) return NO;
5147

52-
return [self isEqualToMetadata:other];
48+
FIRSnapshotMetadata *otherMetadata = other;
49+
return _metadata == otherMetadata->_metadata;
5350
}
5451

55-
- (BOOL)isEqualToMetadata:(nullable FIRSnapshotMetadata *)metadata {
56-
if (self == metadata) return YES;
57-
if (metadata == nil) return NO;
52+
- (NSUInteger)hash {
53+
return _metadata.Hash();
54+
}
5855

59-
return self.pendingWrites == metadata.pendingWrites && self.fromCache == metadata.fromCache;
56+
- (BOOL)hasPendingWrites {
57+
return _metadata.pending_writes();
6058
}
6159

62-
- (NSUInteger)hash {
63-
NSUInteger hash = self.pendingWrites ? 1 : 0;
64-
hash = hash * 31u + (self.fromCache ? 1 : 0);
65-
return hash;
60+
- (BOOL)isFromCache {
61+
return _metadata.from_cache();
6662
}
6763

6864
@end

Firestore/Source/Core/FSTFirestoreClient.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,8 @@ - (void)getDocumentsFromLocalCache:(FIRQuery *)query
372372

373373
ViewSnapshot snapshot = std::move(viewChange.snapshot).value();
374374
FIRSnapshotMetadata *metadata =
375-
[FIRSnapshotMetadata snapshotMetadataWithPendingWrites:snapshot.has_pending_writes()
376-
fromCache:snapshot.from_cache()];
375+
[[FIRSnapshotMetadata alloc] initWithPendingWrites:snapshot.has_pending_writes()
376+
fromCache:snapshot.from_cache()];
377377

378378
FIRQuerySnapshot *result = [FIRQuerySnapshot snapshotWithFirestore:query.firestore
379379
originalQuery:query.query

0 commit comments

Comments
 (0)