Skip to content

Commit

Permalink
Avoid wrapping and rewrapping NSStrings when constructing DatabaseId (
Browse files Browse the repository at this point in the history
…firebase#833)

* Avoid wrapping and rewrapping NSStrings when constructing DatabaseId

* Shorten DatabaseId::kDefaultDatabaseId
  • Loading branch information
wilhuff committed Feb 22, 2018
1 parent 6ce954a commit 935f3ca
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 47 deletions.
4 changes: 2 additions & 2 deletions Firestore/Example/Tests/API/FSTAPIHelpers.mm
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnonnull"
dispatch_once(&onceToken, ^{
sharedInstance = [[FIRFirestore alloc] initWithProjectID:@"abc"
database:@"abc"
sharedInstance = [[FIRFirestore alloc] initWithProjectID:"abc"
database:"abc"
persistenceKey:@"db123"
credentialsProvider:nil
workerDispatchQueue:nil
Expand Down
3 changes: 1 addition & 2 deletions Firestore/Example/Tests/Core/FSTQueryTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,6 @@ - (void)testSortsDocumentsInTheCorrectOrder {
[query queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:FSTTestFieldPath(@"sort")
ascending:YES]];

NSString *defaultDatabaseID = util::WrapNSStringNoCopy(DatabaseId::kDefaultDatabaseId);
// clang-format off
NSArray<FSTDocument *> *docs = @[
FSTTestDoc(@"collection/1", 0, @{@"sort": [NSNull null]}, NO),
Expand All @@ -326,7 +325,7 @@ - (void)testSortsDocumentsInTheCorrectOrder {
FSTTestDoc(@"collection/1", 0, @{@"sort": @"ab"}, NO),
FSTTestDoc(@"collection/1", 0, @{@"sort": @"b"}, NO),
FSTTestDoc(@"collection/1", 0, @{@"sort":
FSTTestRef(@"project", defaultDatabaseID, @"collection/id1")}, NO),
FSTTestRef("project", DatabaseId::kDefault, @"collection/id1")}, NO),
];
// clang-format on

Expand Down
2 changes: 1 addition & 1 deletion Firestore/Example/Tests/Integration/FSTDatastoreTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ - (void)setUp {
[GRPCCall useInsecureConnectionsForHost:settings.host];
}

DatabaseId database_id(util::MakeStringView(projectID), DatabaseId::kDefaultDatabaseId);
DatabaseId database_id(util::MakeStringView(projectID), DatabaseId::kDefault);

_databaseInfo = DatabaseInfo(database_id, "test-key", util::MakeStringView(settings.host),
settings.sslEnabled);
Expand Down
2 changes: 1 addition & 1 deletion Firestore/Example/Tests/Integration/FSTStreamTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ - (void)setUp {

FIRFirestoreSettings *settings = [FSTIntegrationTestCase settings];
DatabaseId database_id(util::MakeStringView([FSTIntegrationTestCase projectID]),
DatabaseId::kDefaultDatabaseId);
DatabaseId::kDefault);

_testQueue = dispatch_queue_create("FSTStreamTestWorkerQueue", DISPATCH_QUEUE_SERIAL);
_workerDispatchQueue = [[FSTDispatchQueue alloc] initWithQueue:_testQueue];
Expand Down
17 changes: 8 additions & 9 deletions Firestore/Example/Tests/Model/FSTFieldValueTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ - (void)testWrapBlobs {

- (void)testWrapResourceNames {
NSArray *values = @[
FSTTestRef(@"project", util::WrapNSStringNoCopy(DatabaseId::kDefaultDatabaseId), @"foo/bar"),
FSTTestRef(@"project", util::WrapNSStringNoCopy(DatabaseId::kDefaultDatabaseId), @"foo/baz")
FSTTestRef("project", DatabaseId::kDefault, @"foo/bar"),
FSTTestRef("project", DatabaseId::kDefault, @"foo/baz")
];
for (FSTDocumentKeyReference *value in values) {
FSTFieldValue *wrapped = FSTTestFieldValue(value);
Expand Down Expand Up @@ -423,7 +423,7 @@ - (void)testArrays {
}

- (void)testValueEquality {
DatabaseId database_id = DatabaseId("project", DatabaseId::kDefaultDatabaseId);
DatabaseId database_id = DatabaseId("project", DatabaseId::kDefault);
NSArray *groups = @[
@[ FSTTestFieldValue(@YES), [FSTBooleanValue booleanValue:YES] ],
@[ FSTTestFieldValue(@NO), [FSTBooleanValue booleanValue:NO] ],
Expand Down Expand Up @@ -467,10 +467,9 @@ - (void)testValueEquality {
@[ FSTTestFieldValue(FSTTestGeoPoint(1, 0)) ],
@[
[FSTReferenceValue referenceValue:FSTTestDocKey(@"coll/doc1") databaseID:&database_id],
FSTTestFieldValue(FSTTestRef(
@"project", util::WrapNSStringNoCopy(DatabaseId::kDefaultDatabaseId), @"coll/doc1"))
FSTTestFieldValue(FSTTestRef("project", DatabaseId::kDefault, @"coll/doc1"))
],
@[ FSTTestRef(@"project", @"(default)", @"coll/doc2") ],
@[ FSTTestRef("project", "(default)", @"coll/doc2") ],
@[ FSTTestFieldValue(@[ @"foo", @"bar" ]), FSTTestFieldValue(@[ @"foo", @"bar" ]) ],
@[ FSTTestFieldValue(@[ @"foo", @"bar", @"baz" ]) ], @[ FSTTestFieldValue(@[ @"foo" ]) ],
@[
Expand Down Expand Up @@ -531,9 +530,9 @@ - (void)testValueOrdering {
@[ FSTTestData(0, 1, 2, 4, 3, -1) ], @[ FSTTestData(255, -1) ],

// resource names
@[ FSTTestRef(@"p1", @"d1", @"c1/doc1") ], @[ FSTTestRef(@"p1", @"d1", @"c1/doc2") ],
@[ FSTTestRef(@"p1", @"d1", @"c10/doc1") ], @[ FSTTestRef(@"p1", @"d1", @"c2/doc1") ],
@[ FSTTestRef(@"p1", @"d2", @"c1/doc1") ], @[ FSTTestRef(@"p2", @"d1", @"c1/doc1") ],
@[ FSTTestRef("p1", "d1", @"c1/doc1") ], @[ FSTTestRef("p1", "d1", @"c1/doc2") ],
@[ FSTTestRef("p1", "d1", @"c10/doc1") ], @[ FSTTestRef("p1", "d1", @"c2/doc1") ],
@[ FSTTestRef("p1", "d2", @"c1/doc1") ], @[ FSTTestRef("p2", "d1", @"c1/doc1") ],

// Geo points
@[ FSTTestGeoPoint(-90, -180) ], @[ FSTTestGeoPoint(-90, 0) ], @[ FSTTestGeoPoint(-90, 180) ],
Expand Down
5 changes: 2 additions & 3 deletions Firestore/Example/Tests/Remote/FSTSerializerBetaTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,8 @@ - (void)testEncodesBlobs {
}

- (void)testEncodesResourceNames {
FSTDocumentKeyReference *reference =
FSTTestRef(@"project", util::WrapNSStringNoCopy(DatabaseId::kDefaultDatabaseId), @"foo/bar");
_databaseId = DatabaseId("project", DatabaseId::kDefaultDatabaseId);
FSTDocumentKeyReference *reference = FSTTestRef("project", DatabaseId::kDefault, @"foo/bar");
_databaseId = DatabaseId("project", DatabaseId::kDefault);
GCFSValue *proto = [GCFSValue message];
proto.referenceValue = @"projects/project/databases/(default)/documents/foo/bar";

Expand Down
6 changes: 5 additions & 1 deletion Firestore/Example/Tests/Util/FSTHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#import "Firestore/Source/Model/FSTDocumentDictionary.h"
#import "Firestore/Source/Model/FSTDocumentKeySet.h"

#include "absl/strings/string_view.h"

@class FIRGeoPoint;
@class FSTDeleteMutation;
@class FSTDeletedDocument;
Expand Down Expand Up @@ -190,7 +192,9 @@ FSTResourcePath *FSTTestPath(NSString *path);
/**
* A convenience method for creating a document reference from a path string.
*/
FSTDocumentKeyReference *FSTTestRef(NSString *projectID, NSString *databaseID, NSString *path);
FSTDocumentKeyReference *FSTTestRef(const absl::string_view projectID,
const absl::string_view databaseID,
NSString *path);

/** A convenience method for creating a query for the given path (without any other filters). */
FSTQuery *FSTTestQuery(NSString *path);
Expand Down
8 changes: 5 additions & 3 deletions Firestore/Example/Tests/Util/FSTHelpers.mm
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@

FSTFieldValue *FSTTestFieldValue(id _Nullable value) {
// This owns the DatabaseIds since we do not have FirestoreClient instance to own them.
static DatabaseId database_id{"project", DatabaseId::kDefaultDatabaseId};
static DatabaseId database_id{"project", DatabaseId::kDefault};
FSTUserDataConverter *converter =
[[FSTUserDataConverter alloc] initWithDatabaseID:&database_id
preConverter:^id _Nullable(id _Nullable input) {
Expand Down Expand Up @@ -172,10 +172,12 @@
return [FSTResourcePath pathWithSegments:FSTTestSplitPath(path)];
}

FSTDocumentKeyReference *FSTTestRef(NSString *projectID, NSString *database, NSString *path) {
FSTDocumentKeyReference *FSTTestRef(const absl::string_view projectID,
const absl::string_view database,
NSString *path) {
// This owns the DatabaseIds since we do not have FirestoreClient instance to own them.
static std::list<DatabaseId> database_ids;
database_ids.emplace_back(util::MakeStringView(projectID), util::MakeStringView(database));
database_ids.emplace_back(projectID, database);
return [[FSTDocumentKeyReference alloc] initWithKey:FSTTestDocKey(path)
databaseID:&database_ids.back()];
}
Expand Down
13 changes: 6 additions & 7 deletions Firestore/Example/Tests/Util/FSTIntegrationTestCase.mm
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,12 @@ - (FIRFirestore *)firestoreWithProjectID:(NSString *)projectID {
FIRSetLoggerLevel(FIRLoggerLevelDebug);
// HACK: FIRFirestore expects a non-nil app, but for tests we cheat.
FIRApp *app = nil;
FIRFirestore *firestore = [[FIRFirestore alloc]
initWithProjectID:projectID
database:util::WrapNSStringNoCopy(DatabaseId::kDefaultDatabaseId)
persistenceKey:persistenceKey
credentialsProvider:credentialsProvider
workerDispatchQueue:workerDispatchQueue
firebaseApp:app];
FIRFirestore *firestore = [[FIRFirestore alloc] initWithProjectID:util::MakeStringView(projectID)
database:DatabaseId::kDefault
persistenceKey:persistenceKey
credentialsProvider:credentialsProvider
workerDispatchQueue:workerDispatchQueue
firebaseApp:app];

firestore.settings = [FSTIntegrationTestCase settings];

Expand Down
5 changes: 3 additions & 2 deletions Firestore/Source/API/FIRFirestore+Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#import "FIRFirestore.h"

#include "Firestore/core/src/firebase/firestore/model/database_id.h"
#include "absl/strings/string_view.h"

NS_ASSUME_NONNULL_BEGIN

Expand All @@ -31,8 +32,8 @@ NS_ASSUME_NONNULL_BEGIN
* Initializes a Firestore object with all the required parameters directly. This exists so that
* tests can create FIRFirestore objects without needing FIRApp.
*/
- (instancetype)initWithProjectID:(NSString *)projectID
database:(NSString *)database
- (instancetype)initWithProjectID:(const absl::string_view)projectID
database:(const absl::string_view)database
persistenceKey:(NSString *)persistenceKey
credentialsProvider:(id<FSTCredentialsProvider>)credentialsProvider
workerDispatchQueue:(FSTDispatchQueue *)workerDispatchQueue
Expand Down
18 changes: 8 additions & 10 deletions Firestore/Source/API/FIRFirestore.mm
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,11 @@ + (instancetype)firestore {
@"Failed to get FirebaseApp instance. Please call FirebaseApp.configure() "
@"before using Firestore");
}
return
[self firestoreForApp:app database:util::WrapNSStringNoCopy(DatabaseId::kDefaultDatabaseId)];
return [self firestoreForApp:app database:util::WrapNSStringNoCopy(DatabaseId::kDefault)];
}

+ (instancetype)firestoreForApp:(FIRApp *)app {
return
[self firestoreForApp:app database:util::WrapNSStringNoCopy(DatabaseId::kDefaultDatabaseId)];
return [self firestoreForApp:app database:util::WrapNSStringNoCopy(DatabaseId::kDefault)];
}

// TODO(b/62410906): make this public
Expand All @@ -137,7 +135,7 @@ + (instancetype)firestoreForApp:(FIRApp *)app database:(NSString *)database {
FSTThrowInvalidArgument(
@"database identifier may not be nil. Use '%@' if you want the default "
"database",
util::WrapNSStringNoCopy(DatabaseId::kDefaultDatabaseId));
util::WrapNSStringNoCopy(DatabaseId::kDefault));
}

// Note: If the key format changes, please change the code that detects FIRApps being deleted
Expand All @@ -159,8 +157,8 @@ + (instancetype)firestoreForApp:(FIRApp *)app database:(NSString *)database {

NSString *persistenceKey = app.name;

firestore = [[FIRFirestore alloc] initWithProjectID:projectID
database:database
firestore = [[FIRFirestore alloc] initWithProjectID:util::MakeStringView(projectID)
database:util::MakeStringView(database)
persistenceKey:persistenceKey
credentialsProvider:credentialsProvider
workerDispatchQueue:workerDispatchQueue
Expand All @@ -172,14 +170,14 @@ + (instancetype)firestoreForApp:(FIRApp *)app database:(NSString *)database {
}
}

- (instancetype)initWithProjectID:(NSString *)projectID
database:(NSString *)database
- (instancetype)initWithProjectID:(const absl::string_view)projectID
database:(const absl::string_view)database
persistenceKey:(NSString *)persistenceKey
credentialsProvider:(id<FSTCredentialsProvider>)credentialsProvider
workerDispatchQueue:(FSTDispatchQueue *)workerDispatchQueue
firebaseApp:(FIRApp *)app {
if (self = [super init]) {
_databaseID = DatabaseId(util::MakeStringView(projectID), util::MakeStringView(database));
_databaseID = DatabaseId(projectID, database);
FSTPreConverterBlock block = ^id _Nullable(id _Nullable input) {
if ([input isKindOfClass:[FIRDocumentReference class]]) {
FIRDocumentReference *documentReference = (FIRDocumentReference *)input;
Expand Down
2 changes: 1 addition & 1 deletion Firestore/core/src/firebase/firestore/model/database_id.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace firebase {
namespace firestore {
namespace model {

constexpr const char* DatabaseId::kDefaultDatabaseId;
constexpr const char* DatabaseId::kDefault;

DatabaseId::DatabaseId(const absl::string_view project_id,
const absl::string_view database_id)
Expand Down
4 changes: 2 additions & 2 deletions Firestore/core/src/firebase/firestore/model/database_id.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace model {
class DatabaseId {
public:
/** The default name for "unset" database ID in resource names. */
static constexpr const char* kDefaultDatabaseId = "(default)";
static constexpr const char* kDefault = "(default)";

#if defined(__OBJC__)
// For objective-c++ initialization; to be removed after migration.
Expand All @@ -58,7 +58,7 @@ class DatabaseId {

/** Whether this is the default database of the project. */
bool IsDefaultDatabase() const {
return database_id_ == kDefaultDatabaseId;
return database_id_ == kDefault;
}

#if defined(__OBJC__)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ TEST(DatabaseInfo, Getter) {
}

TEST(DatabaseInfo, DefaultDatabase) {
DatabaseInfo info(DatabaseId("project id", DatabaseId::kDefaultDatabaseId),
"key", "http://host", false);
DatabaseInfo info(DatabaseId("project id", DatabaseId::kDefault), "key",
"http://host", false);
EXPECT_EQ("project id", info.database_id().project_id());
EXPECT_EQ("(default)", info.database_id().database_id());
EXPECT_EQ("key", info.persistence_key());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ TEST(DatabaseId, Constructor) {
}

TEST(DatabaseId, DefaultDb) {
DatabaseId id("p", DatabaseId::kDefaultDatabaseId);
DatabaseId id("p", DatabaseId::kDefault);
EXPECT_EQ("p", id.project_id());
EXPECT_EQ("(default)", id.database_id());
EXPECT_TRUE(id.IsDefaultDatabase());
Expand Down

0 comments on commit 935f3ca

Please sign in to comment.