Skip to content

C++: replace FSTMaybeDocumentDictionary with a C++ equivalent #2139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Dec 10, 2018
Merged
18 changes: 10 additions & 8 deletions Firestore/Example/Benchmarks/FSTLevelDBBenchmarkTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,23 @@

#import "Firestore/Source/Local/FSTLevelDB.h"
#import "Firestore/Source/Local/FSTLocalSerializer.h"
#import "Firestore/Source/Model/FSTDocumentKey.h"
#import "Firestore/Source/Remote/FSTSerializerBeta.h"

#include "Firestore/core/src/firebase/firestore/local/leveldb_key.h"
#include "Firestore/core/src/firebase/firestore/local/leveldb_transaction.h"
#include "Firestore/core/src/firebase/firestore/model/document_key.h"
#include "Firestore/core/src/firebase/firestore/model/types.h"
#include "Firestore/core/src/firebase/firestore/util/string_format.h"

NS_ASSUME_NONNULL_BEGIN

using firebase::firestore::local::LevelDbRemoteDocumentKey;
using firebase::firestore::local::LevelDbTargetDocumentKey;
using firebase::firestore::local::LevelDbTransaction;
using firebase::firestore::model::DatabaseId;
using firebase::firestore::model::DocumentKey;
using firebase::firestore::model::TargetId;
using firebase::firestore::util::StringFormat;

namespace {

Expand Down Expand Up @@ -100,9 +104,8 @@ void FillDB() {
LevelDbTransaction txn(db_.ptr, "benchmark");

for (int i = 0; i < numDocuments_; i++) {
FSTDocumentKey *docKey =
[FSTDocumentKey keyWithPathString:[NSString stringWithFormat:@"docs/doc_%i", i]];
std::string docKeyString = [FSTLevelDBRemoteDocumentKey keyWithDocumentKey:docKey];
auto docKey = DocumentKey::FromPathString(StringFormat("docs/doc_%i", i));
std::string docKeyString = LevelDbRemoteDocumentKey::Key(docKey);
txn.Put(docKeyString, DocumentData());
WriteIndex(txn, docKey);
}
Expand All @@ -112,7 +115,7 @@ void FillDB() {
}

protected:
void WriteIndex(LevelDbTransaction &txn, FSTDocumentKey *docKey) {
void WriteIndex(LevelDbTransaction &txn, const DocumentKey &docKey) {
// Arbitrary target ID
TargetId targetID = 1;
txn.Put(LevelDbDocumentTargetKey::Key(docKey, targetID), emptyBuffer_);
Expand All @@ -136,10 +139,9 @@ void WriteIndex(LevelDbTransaction &txn, FSTDocumentKey *docKey) {
for (const auto &_ : state) {
LevelDbTransaction txn(db_.ptr, "benchmark");
for (int i = 0; i < docsToUpdate; i++) {
FSTDocumentKey *docKey =
[FSTDocumentKey keyWithPathString:[NSString stringWithFormat:@"docs/doc_%i", i]];
auto docKey = DocumentKey::FromPathString(StringFormat("docs/doc_%i", i));
if (writeIndexes) WriteIndex(txn, docKey);
std::string docKeyString = [FSTLevelDBRemoteDocumentKey keyWithDocumentKey:docKey];
std::string docKeyString = LevelDbRemoteDocumentKey::Key(docKey);
txn.Put(docKeyString, documentUpdate);
}
txn.Commit();
Expand Down
1 change: 0 additions & 1 deletion Firestore/Example/Tests/API/FSTAPIHelpers.mm
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#import "Firestore/Source/Core/FSTQuery.h"
#import "Firestore/Source/Core/FSTViewSnapshot.h"
#import "Firestore/Source/Model/FSTDocument.h"
#import "Firestore/Source/Model/FSTDocumentKey.h"
#import "Firestore/Source/Model/FSTDocumentSet.h"

#include "Firestore/core/src/firebase/firestore/util/string_apple.h"
Expand Down
1 change: 0 additions & 1 deletion Firestore/Example/Tests/Core/FSTQueryTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

#import "Firestore/Source/API/FIRFirestore+Internal.h"
#import "Firestore/Source/Model/FSTDocument.h"
#import "Firestore/Source/Model/FSTDocumentKey.h"

#import "Firestore/Example/Tests/Util/FSTHelpers.h"

Expand Down
1 change: 0 additions & 1 deletion Firestore/Example/Tests/Core/FSTViewTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#import "Firestore/Source/Core/FSTQuery.h"
#import "Firestore/Source/Core/FSTViewSnapshot.h"
#import "Firestore/Source/Model/FSTDocument.h"
#import "Firestore/Source/Model/FSTDocumentKey.h"
#import "Firestore/Source/Model/FSTDocumentSet.h"
#import "Firestore/Source/Model/FSTFieldValue.h"
#import "Firestore/Source/Remote/FSTRemoteEvent.h"
Expand Down
5 changes: 3 additions & 2 deletions Firestore/Example/Tests/Integration/FSTDatastoreTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#import "Firestore/Source/Core/FSTFirestoreClient.h"
#import "Firestore/Source/Core/FSTQuery.h"
#import "Firestore/Source/Local/FSTQueryData.h"
#import "Firestore/Source/Model/FSTDocumentKey.h"
#import "Firestore/Source/Model/FSTFieldValue.h"
#import "Firestore/Source/Model/FSTMutation.h"
#import "Firestore/Source/Model/FSTMutationBatch.h"
Expand All @@ -39,6 +38,7 @@
#include "Firestore/core/src/firebase/firestore/auth/empty_credentials_provider.h"
#include "Firestore/core/src/firebase/firestore/core/database_info.h"
#include "Firestore/core/src/firebase/firestore/model/database_id.h"
#include "Firestore/core/src/firebase/firestore/model/document_key.h"
#include "Firestore/core/src/firebase/firestore/model/precondition.h"
#include "Firestore/core/src/firebase/firestore/util/async_queue.h"
#include "Firestore/core/src/firebase/firestore/util/executor_libdispatch.h"
Expand All @@ -51,6 +51,7 @@
using firebase::firestore::core::DatabaseInfo;
using firebase::firestore::model::BatchId;
using firebase::firestore::model::DatabaseId;
using firebase::firestore::model::DocumentKey;
using firebase::firestore::model::DocumentKeySet;
using firebase::firestore::model::Precondition;
using firebase::firestore::model::TargetId;
Expand Down Expand Up @@ -243,7 +244,7 @@ - (void)awaitExpectations {

- (FSTSetMutation *)setMutation {
return [[FSTSetMutation alloc]
initWithKey:[FSTDocumentKey keyWithPathString:@"rooms/eros"]
initWithKey:DocumentKey::FromPathString("rooms/eros")
value:[[FSTObjectValue alloc]
initWithDictionary:@{@"name" : [FSTStringValue stringValue:@"Eros"]}]
precondition:Precondition::None()];
Expand Down
4 changes: 2 additions & 2 deletions Firestore/Example/Tests/Local/FSTLevelDBMigrationsTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ - (void)testDropsTheQueryCache {
BatchId batchID = 1;
TargetId targetID = 2;

FSTDocumentKey *key1 = Key("documents/1");
FSTDocumentKey *key2 = Key("documents/2");
DocumentKey key1 = Key("documents/1");
DocumentKey key2 = Key("documents/2");

std::string targetKeys[] = {
LevelDbTargetKey::Key(targetID),
Expand Down
1 change: 0 additions & 1 deletion Firestore/Example/Tests/Local/FSTLocalSerializerTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#import "Firestore/Source/Core/FSTQuery.h"
#import "Firestore/Source/Local/FSTQueryData.h"
#import "Firestore/Source/Model/FSTDocument.h"
#import "Firestore/Source/Model/FSTDocumentKey.h"
#import "Firestore/Source/Model/FSTFieldValue.h"
#import "Firestore/Source/Model/FSTMutation.h"
#import "Firestore/Source/Model/FSTMutationBatch.h"
Expand Down
90 changes: 49 additions & 41 deletions Firestore/Example/Tests/Local/FSTLocalStoreTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#import "Firestore/Source/Local/FSTQueryCache.h"
#import "Firestore/Source/Local/FSTQueryData.h"
#import "Firestore/Source/Model/FSTDocument.h"
#import "Firestore/Source/Model/FSTDocumentKey.h"
#import "Firestore/Source/Model/FSTDocumentSet.h"
#import "Firestore/Source/Model/FSTMutation.h"
#import "Firestore/Source/Model/FSTMutationBatch.h"
Expand All @@ -40,15 +39,27 @@
#import "Firestore/third_party/Immutable/Tests/FSTImmutableSortedSet+Testing.h"

#include "Firestore/core/src/firebase/firestore/auth/user.h"
#include "Firestore/core/src/firebase/firestore/model/document_map.h"
#include "Firestore/core/test/firebase/firestore/testutil/testutil.h"

namespace testutil = firebase::firestore::testutil;
using firebase::firestore::auth::User;
using firebase::firestore::model::DocumentKey;
using firebase::firestore::model::DocumentKeySet;
using firebase::firestore::model::ListenSequenceNumber;
using firebase::firestore::model::DocumentMap;
using firebase::firestore::model::MaybeDocumentMap;
using firebase::firestore::model::SnapshotVersion;
using firebase::firestore::model::TargetId;

static NSArray<FSTDocument *> *docMapToArray(const DocumentMap &docs) {
NSMutableArray<FSTDocument *> *result = [NSMutableArray array];
for (const auto &kv : docs.underlying_map()) {
[result addObject:static_cast<FSTDocument *>(kv.second)];
}
return result;
}

NS_ASSUME_NONNULL_BEGIN

@interface FSTLocalStoreTests ()
Expand All @@ -57,12 +68,13 @@ @interface FSTLocalStoreTests ()
@property(nonatomic, strong, readwrite) FSTLocalStore *localStore;

@property(nonatomic, strong, readonly) NSMutableArray<FSTMutationBatch *> *batches;
@property(nonatomic, strong, readwrite, nullable) FSTMaybeDocumentDictionary *lastChanges;
@property(nonatomic, assign, readwrite) TargetId lastTargetID;

@end

@implementation FSTLocalStoreTests
@implementation FSTLocalStoreTests {
MaybeDocumentMap _lastChanges;
}

- (void)setUp {
[super setUp];
Expand All @@ -78,7 +90,6 @@ - (void)setUp {
[self.localStore start];

_batches = [NSMutableArray array];
_lastChanges = nil;
_lastTargetID = 0;
}

Expand Down Expand Up @@ -115,11 +126,11 @@ - (void)writeMutations:(NSArray<FSTMutation *> *)mutations {
[self.batches addObject:[[FSTMutationBatch alloc] initWithBatchID:result.batchID
localWriteTime:[FIRTimestamp timestamp]
mutations:mutations]];
self.lastChanges = result.changes;
_lastChanges = result.changes;
}

- (void)applyRemoteEvent:(FSTRemoteEvent *)event {
self.lastChanges = [self.localStore applyRemoteEvent:event];
_lastChanges = [self.localStore applyRemoteEvent:event];
}

- (void)notifyLocalViewChanges:(FSTLocalViewChanges *)changes {
Expand All @@ -137,13 +148,13 @@ - (void)acknowledgeMutationWithVersion:(FSTTestSnapshotVersion)documentVersion {
commitVersion:version
mutationResults:@[ mutationResult ]
streamToken:nil];
self.lastChanges = [self.localStore acknowledgeBatchWithResult:result];
_lastChanges = [self.localStore acknowledgeBatchWithResult:result];
}

- (void)rejectMutation {
FSTMutationBatch *batch = [self.batches firstObject];
[self.batches removeObjectAtIndex:0];
self.lastChanges = [self.localStore rejectBatchID:batch.batchID];
_lastChanges = [self.localStore rejectBatchID:batch.batchID];
}

- (TargetId)allocateQuery:(FSTQuery *)query {
Expand All @@ -159,34 +170,31 @@ - (TargetId)allocateQuery:(FSTQuery *)query {
} while (0)

/** Asserts that a the lastChanges contain the docs in the given array. */
#define FSTAssertChanged(documents) \
XCTAssertNotNil(self.lastChanges); \
do { \
FSTMaybeDocumentDictionary *actual = self.lastChanges; \
NSArray<FSTMaybeDocument *> *expected = (documents); \
XCTAssertEqual(actual.count, expected.count); \
NSEnumerator<FSTMaybeDocument *> *enumerator = expected.objectEnumerator; \
[actual enumerateKeysAndObjectsUsingBlock:^(FSTDocumentKey * key, FSTMaybeDocument * value, \
BOOL * stop) { \
XCTAssertEqualObjects(value, [enumerator nextObject]); \
}]; \
self.lastChanges = nil; \
#define FSTAssertChanged(documents) \
do { \
NSArray<FSTMaybeDocument *> *expected = (documents); \
XCTAssertEqual(_lastChanges.size(), expected.count); \
NSEnumerator<FSTMaybeDocument *> *enumerator = expected.objectEnumerator; \
for (const auto &kv : _lastChanges) { \
FSTMaybeDocument *value = kv.second; \
XCTAssertEqualObjects(value, [enumerator nextObject]); \
} \
_lastChanges = MaybeDocumentMap{}; \
} while (0)

/** Asserts that the given keys were removed. */
#define FSTAssertRemoved(keyPaths) \
XCTAssertNotNil(self.lastChanges); \
do { \
FSTMaybeDocumentDictionary *actual = self.lastChanges; \
XCTAssertEqual(actual.count, keyPaths.count); \
NSEnumerator<NSString *> *keyPathEnumerator = keyPaths.objectEnumerator; \
[actual enumerateKeysAndObjectsUsingBlock:^(FSTDocumentKey * actualKey, \
FSTMaybeDocument * value, BOOL * stop) { \
FSTDocumentKey *expectedKey = FSTTestDocKey([keyPathEnumerator nextObject]); \
XCTAssertEqualObjects(actualKey, expectedKey); \
XCTAssertTrue([value isKindOfClass:[FSTDeletedDocument class]]); \
}]; \
self.lastChanges = nil; \
#define FSTAssertRemoved(keyPaths) \
do { \
XCTAssertEqual(_lastChanges.size(), keyPaths.count); \
NSEnumerator<NSString *> *keyPathEnumerator = keyPaths.objectEnumerator; \
for (const auto &kv : _lastChanges) { \
const DocumentKey &actualKey = kv.first; \
FSTMaybeDocument *value = kv.second; \
DocumentKey expectedKey = FSTTestDocKey([keyPathEnumerator nextObject]); \
XCTAssertEqual(actualKey, expectedKey); \
XCTAssertTrue([value isKindOfClass:[FSTDeletedDocument class]]); \
} \
_lastChanges = MaybeDocumentMap{}; \
} while (0)

/** Asserts that the given local store contains the given document. */
Expand All @@ -200,7 +208,7 @@ - (TargetId)allocateQuery:(FSTQuery *)query {
/** Asserts that the given local store does not contain the given document. */
#define FSTAssertNotContains(keyPathString) \
do { \
FSTDocumentKey *key = FSTTestDocKey(keyPathString); \
DocumentKey key = FSTTestDocKey(keyPathString); \
FSTMaybeDocument *actual = [self.localStore readDocument:key]; \
XCTAssertNil(actual); \
} while (0)
Expand Down Expand Up @@ -834,9 +842,9 @@ - (void)testCanExecuteDocumentQueries {
FSTTestSetMutation(@"foo/bar/Foo/Bar", @{@"Foo" : @"Bar"})
]];
FSTQuery *query = FSTTestQuery("foo/bar");
FSTDocumentDictionary *docs = [self.localStore executeQuery:query];
XCTAssertEqualObjects([docs values], @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"},
FSTDocumentStateLocalMutations) ]);
DocumentMap docs = [self.localStore executeQuery:query];
XCTAssertEqualObjects(docMapToArray(docs), @[ FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"},
FSTDocumentStateLocalMutations) ]);
}

- (void)testCanExecuteCollectionQueries {
Expand All @@ -850,9 +858,9 @@ - (void)testCanExecuteCollectionQueries {
FSTTestSetMutation(@"fooo/blah", @{@"fooo" : @"blah"})
]];
FSTQuery *query = FSTTestQuery("foo");
FSTDocumentDictionary *docs = [self.localStore executeQuery:query];
DocumentMap docs = [self.localStore executeQuery:query];
XCTAssertEqualObjects(
[docs values], (@[
docMapToArray(docs), (@[
FSTTestDoc("foo/bar", 0, @{@"foo" : @"bar"}, FSTDocumentStateLocalMutations),
FSTTestDoc("foo/baz", 0, @{@"foo" : @"baz"}, FSTDocumentStateLocalMutations)
]));
Expand All @@ -874,8 +882,8 @@ - (void)testCanExecuteMixedCollectionQueries {

[self.localStore locallyWriteMutations:@[ FSTTestSetMutation(@"foo/bonk", @{@"a" : @"b"}) ]];

FSTDocumentDictionary *docs = [self.localStore executeQuery:query];
XCTAssertEqualObjects([docs values], (@[
DocumentMap docs = [self.localStore executeQuery:query];
XCTAssertEqualObjects(docMapToArray(docs), (@[
FSTTestDoc("foo/bar", 20, @{@"a" : @"b"}, FSTDocumentStateSynced),
FSTTestDoc("foo/baz", 10, @{@"a" : @"b"}, FSTDocumentStateSynced),
FSTTestDoc("foo/bonk", 0, @{@"a" : @"b"}, FSTDocumentStateLocalMutations)
Expand Down
13 changes: 8 additions & 5 deletions Firestore/Example/Tests/Local/FSTReferenceSetTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
#import <XCTest/XCTest.h>

#import "Firestore/Example/Tests/Util/FSTHelpers.h"
#import "Firestore/Source/Model/FSTDocumentKey.h"

#include "Firestore/core/src/firebase/firestore/model/document_key.h"

using firebase::firestore::model::DocumentKey;

NS_ASSUME_NONNULL_BEGIN

Expand All @@ -29,7 +32,7 @@ @interface FSTReferenceSetTests : XCTestCase
@implementation FSTReferenceSetTests

- (void)testAddOrRemoveReferences {
FSTDocumentKey *key = FSTTestDocKey(@"foo/bar");
DocumentKey key = FSTTestDocKey(@"foo/bar");

FSTReferenceSet *referenceSet = [[FSTReferenceSet alloc] init];
XCTAssertTrue([referenceSet isEmpty]);
Expand All @@ -54,9 +57,9 @@ - (void)testAddOrRemoveReferences {
}

- (void)testRemoveAllReferencesForTargetID {
FSTDocumentKey *key1 = FSTTestDocKey(@"foo/bar");
FSTDocumentKey *key2 = FSTTestDocKey(@"foo/baz");
FSTDocumentKey *key3 = FSTTestDocKey(@"foo/blah");
DocumentKey key1 = FSTTestDocKey(@"foo/bar");
DocumentKey key2 = FSTTestDocKey(@"foo/baz");
DocumentKey key3 = FSTTestDocKey(@"foo/blah");
FSTReferenceSet *referenceSet = [[FSTReferenceSet alloc] init];

[referenceSet addReferenceToKey:key1 forID:1];
Expand Down
Loading