Skip to content

Commit 6559fe4

Browse files
CXX-739 Apply string::view_or_value to client, database, and collection
1 parent 17db0bf commit 6559fe4

File tree

11 files changed

+133
-140
lines changed

11 files changed

+133
-140
lines changed

src/mongocxx/client.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,8 @@ stdx::optional<class read_concern> client::read_concern() const {
6969
if (!libmongoc::read_concern_get_level(rc)) {
7070
return stdx::nullopt;
7171
}
72-
7372
return {(class read_concern){
74-
stdx::make_unique<read_concern::impl>(libmongoc::read_concern_copy(rc))}};
73+
stdx::make_unique<read_concern::impl>(libmongoc::read_concern_copy(rc))}};
7574
}
7675

7776
void client::read_preference(class read_preference rp) {
@@ -100,8 +99,8 @@ class write_concern client::write_concern() const {
10099
return wc;
101100
}
102101

103-
class database client::database(stdx::string_view name) const & {
104-
return mongocxx::database(*this, name);
102+
class database client::database(bsoncxx::string::view_or_value name) const & {
103+
return mongocxx::database(*this, std::move(name));
105104
}
106105

107106
cursor client::list_databases() const {
@@ -116,7 +115,7 @@ cursor client::list_databases() const {
116115
}
117116

118117
const client::impl& client::_get_impl() const {
119-
if(!_impl) {
118+
if (!_impl) {
120119
throw logic_error{make_error_code(error_code::k_invalid_client_object)};
121120
}
122121
return *_impl;

src/mongocxx/client.hpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818

1919
#include <memory>
2020

21-
#include <bsoncxx/stdx/string_view.hpp>
22-
21+
#include <bsoncxx/string/view_or_value.hpp>
2322
#include <mongocxx/database.hpp>
2423
#include <mongocxx/options/client.hpp>
2524
#include <mongocxx/read_concern.hpp>
@@ -48,9 +47,7 @@ MONGOCXX_INLINE_NAMESPACE_BEGIN
4847
/// @endcode
4948
///
5049
class MONGOCXX_API client {
51-
5250
public:
53-
5451
///
5552
/// Default constructs a new client. The client is not connected and is equivalent to the
5653
/// state of a moved-from client. The only valid actions to take with a default constructed
@@ -66,10 +63,7 @@ class MONGOCXX_API client {
6663
/// @param options
6764
/// Additional options that cannot be specified via the mongodb_uri
6865
///
69-
client(
70-
const class uri& mongodb_uri,
71-
const options::client& options = options::client()
72-
);
66+
client(const class uri& mongodb_uri, const options::client& options = options::client());
7367

7468
///
7569
/// Move constructs a client.
@@ -171,8 +165,8 @@ class MONGOCXX_API client {
171165
///
172166
/// @return The database
173167
///
174-
class database database(stdx::string_view name) const&;
175-
class database database(stdx::string_view name) const&& = delete;
168+
class database database(bsoncxx::string::view_or_value name) const&;
169+
class database database(bsoncxx::string::view_or_value name) const&& = delete;
176170

177171
///
178172
/// Allows the syntax @c client["db_name"] as a convenient shorthand for the client::database()
@@ -185,8 +179,8 @@ class MONGOCXX_API client {
185179
///
186180
/// @return Client side representation of a server side database
187181
///
188-
MONGOCXX_INLINE class database operator[](stdx::string_view name) const&;
189-
MONGOCXX_INLINE class database operator[](stdx::string_view name) const&& = delete;
182+
MONGOCXX_INLINE class database operator[](bsoncxx::string::view_or_value name) const&;
183+
MONGOCXX_INLINE class database operator[](bsoncxx::string::view_or_value name) const&& = delete;
190184

191185
///
192186
/// Enumerates the databases in the client.
@@ -218,7 +212,7 @@ class MONGOCXX_API client {
218212
std::unique_ptr<impl> _impl;
219213
};
220214

221-
MONGOCXX_INLINE database client::operator[](stdx::string_view name) const & {
215+
MONGOCXX_INLINE database client::operator[](bsoncxx::string::view_or_value name) const & {
222216
return database(name);
223217
}
224218

src/mongocxx/collection.cpp

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ mongocxx::stdx::optional<bsoncxx::document::value> find_and_modify(
7474

7575
if (!r) {
7676
auto gle = mongocxx::libmongoc::collection_get_last_error(collection);
77-
mongocxx::throw_exception<mongocxx::write_exception>(bsoncxx::helpers::value_from_bson_t(gle), error);
77+
mongocxx::throw_exception<mongocxx::write_exception>(
78+
bsoncxx::helpers::value_from_bson_t(gle), error);
7879
}
7980

8081
bsoncxx::document::view result = reply.view();
@@ -124,24 +125,25 @@ collection::operator bool() const noexcept {
124125
}
125126

126127
stdx::string_view collection::name() const {
127-
return stdx::string_view{libmongoc::collection_get_name(_get_impl().collection_t)};
128+
return {libmongoc::collection_get_name(_get_impl().collection_t)};
128129
}
129130

130-
void collection::rename(stdx::string_view new_name, bool drop_target_before_rename) {
131+
void collection::rename(bsoncxx::string::view_or_value new_name, bool drop_target_before_rename) {
131132
bson_error_t error;
132133

133-
auto result = libmongoc::collection_rename(_get_impl().collection_t, _get_impl().database_name.c_str(),
134-
new_name.data(), drop_target_before_rename, &error);
134+
auto result =
135+
libmongoc::collection_rename(_get_impl().collection_t, _get_impl().database_name.c_str(),
136+
new_name.c_str(), drop_target_before_rename, &error);
135137

136138
if (!result) {
137139
throw_exception<operation_exception>(error);
138140
}
139141
}
140142

141-
collection::collection(const database& database, stdx::string_view collection_name)
142-
: _impl(stdx::make_unique<impl>(
143-
libmongoc::database_get_collection(database._get_impl().database_t, collection_name.data()),
144-
database.name(), database._get_impl().client_impl)) {
143+
collection::collection(const database& database, bsoncxx::string::view_or_value collection_name)
144+
: _impl(stdx::make_unique<impl>(libmongoc::database_get_collection(
145+
database._get_impl().database_t, collection_name.c_str()),
146+
database.name(), database._get_impl().client_impl)) {
145147
}
146148

147149
collection::collection(const database& database, void* collection)
@@ -166,6 +168,9 @@ stdx::optional<result::bulk_write> collection::bulk_write(const class bulk_write
166168
mongoc_bulk_operation_t* b = bulk_write._impl->operation_t;
167169
libmongoc::bulk_operation_set_client(b, _get_impl().client_impl->client_t);
168170
libmongoc::bulk_operation_set_database(b, _get_impl().database_name.c_str());
171+
172+
// collection::name() is guaranteed to return a null-terminated string, so it
173+
// is safe to use .data() here.
169174
libmongoc::bulk_operation_set_collection(b, name().data());
170175

171176
scoped_bson_t reply;
@@ -675,8 +680,8 @@ bsoncxx::document::value collection::create_index(view_or_value keys,
675680
opt.geo_options = &geo_opt;
676681
}
677682

678-
auto result =
679-
libmongoc::collection_create_index(_get_impl().collection_t, bson_keys.bson(), &opt, &error);
683+
auto result = libmongoc::collection_create_index(_get_impl().collection_t, bson_keys.bson(),
684+
&opt, &error);
680685

681686
if (!result) {
682687
throw_exception<operation_exception>(error);
@@ -693,10 +698,10 @@ bsoncxx::document::value collection::create_index(view_or_value keys,
693698
}
694699
}
695700

696-
cursor collection::distinct(stdx::string_view field_name, view_or_value query,
701+
cursor collection::distinct(bsoncxx::string::view_or_value field_name, view_or_value query,
697702
const options::distinct& options) {
698703
bsoncxx::builder::stream::document command_builder{};
699-
command_builder << "distinct" << name() << "key" << field_name << "query"
704+
command_builder << "distinct" << name() << "key" << field_name.view() << "query"
700705
<< bsoncxx::types::b_document{query};
701706

702707
if (options.max_time()) {
@@ -705,8 +710,8 @@ cursor collection::distinct(stdx::string_view field_name, view_or_value query,
705710

706711
scoped_bson_t command_bson{command_builder.extract()};
707712

708-
auto database =
709-
libmongoc::client_get_database(_get_impl().client_impl->client_t, _get_impl().database_name.data());
713+
auto database = libmongoc::client_get_database(_get_impl().client_impl->client_t,
714+
_get_impl().database_name.c_str());
710715

711716
auto result = libmongoc::database_command(database, MONGOC_QUERY_NONE, 0, 0, 0,
712717
command_bson.bson(), NULL, NULL);
@@ -744,16 +749,17 @@ stdx::optional<class read_concern> collection::read_concern() const {
744749
if (!libmongoc::read_concern_get_level(rc)) {
745750
return stdx::nullopt;
746751
}
747-
return {(class read_concern){stdx::make_unique<read_concern::impl>(libmongoc::read_concern_copy(rc))}};
752+
return {(class read_concern){
753+
stdx::make_unique<read_concern::impl>(libmongoc::read_concern_copy(rc))}};
748754
}
749755

750756
void collection::read_preference(class read_preference rp) {
751757
libmongoc::collection_set_read_prefs(_get_impl().collection_t, rp._impl->read_preference_t);
752758
}
753759

754760
class read_preference collection::read_preference() const {
755-
class read_preference rp(stdx::make_unique<read_preference::impl>(
756-
libmongoc::read_prefs_copy(libmongoc::collection_get_read_prefs(_get_impl().collection_t))));
761+
class read_preference rp(stdx::make_unique<read_preference::impl>(libmongoc::read_prefs_copy(
762+
libmongoc::collection_get_read_prefs(_get_impl().collection_t))));
757763
return rp;
758764
}
759765

@@ -768,7 +774,7 @@ class write_concern collection::write_concern() const {
768774
}
769775

770776
const collection::impl& collection::_get_impl() const {
771-
if(!_impl) {
777+
if (!_impl) {
772778
throw logic_error{make_error_code(error_code::k_invalid_collection_object)};
773779
}
774780
return *_impl;

src/mongocxx/collection.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include <bsoncxx/builder/stream/document.hpp>
2525
#include <bsoncxx/document/view_or_value.hpp>
2626
#include <bsoncxx/stdx/optional.hpp>
27-
#include <bsoncxx/stdx/string_view.hpp>
27+
#include <bsoncxx/string/view_or_value.hpp>
2828

2929
#include <mongocxx/bulk_write.hpp>
3030
#include <mongocxx/cursor.hpp>
@@ -274,10 +274,9 @@ class MONGOCXX_API collection {
274274
///
275275
/// @see http://docs.mongodb.org/manual/reference/command/distinct/
276276
///
277-
cursor distinct(stdx::string_view name, bsoncxx::document::view_or_value filter,
277+
cursor distinct(bsoncxx::string::view_or_value name, bsoncxx::document::view_or_value filter,
278278
const options::distinct& options = options::distinct());
279279

280-
///
281280
/// Drops this collection and all its contained documents from the database.
282281
///
283282
/// @throws exception::operation if the operation fails.
@@ -458,7 +457,7 @@ class MONGOCXX_API collection {
458457
cursor list_indexes() const;
459458

460459
///
461-
/// Returns the name of this collection.
460+
/// Returns the name of this collection as a view of a null-terminated string.
462461
///
463462
/// @return The name of the collection.
464463
///
@@ -475,7 +474,7 @@ class MONGOCXX_API collection {
475474
///
476475
/// @see https://docs.mongodb.org/manual/reference/command/renameCollection/
477476
///
478-
void rename(stdx::string_view new_name, bool drop_target_before_rename = false);
477+
void rename(bsoncxx::string::view_or_value new_name, bool drop_target_before_rename = false);
479478

480479
///
481480
/// Sets the read_concern for this collection. Changes will not have any effect on existing
@@ -592,7 +591,8 @@ class MONGOCXX_API collection {
592591
private:
593592
friend class database;
594593

595-
MONGOCXX_PRIVATE collection(const database& database, stdx::string_view collection_name);
594+
MONGOCXX_PRIVATE collection(const database& database,
595+
bsoncxx::string::view_or_value collection_name);
596596

597597
MONGOCXX_PRIVATE collection(const database& database, void* collection);
598598

src/mongocxx/database.cpp

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ database& database::operator=(database&&) noexcept = default;
4646

4747
database::~database() = default;
4848

49-
database::database(const class client& client, stdx::string_view name)
49+
database::database(const class client& client, bsoncxx::string::view_or_value name)
5050
: _impl(stdx::make_unique<impl>(
51-
libmongoc::client_get_database(client._get_impl().client_t, name.data()), &client._get_impl(),
52-
name.data())) {
51+
libmongoc::client_get_database(client._get_impl().client_t, name.c_str()),
52+
&client._get_impl(), name.c_str())) {
5353
}
5454

5555
database::database(const database& d) {
@@ -82,6 +82,7 @@ cursor database::list_collections(bsoncxx::document::view_or_value filter) {
8282

8383
return cursor(result);
8484
}
85+
8586
stdx::string_view database::name() const {
8687
return _get_impl().name;
8788
}
@@ -92,8 +93,8 @@ bsoncxx::document::value database::run_command(bsoncxx::document::view_or_value
9293
bson_error_t error;
9394

9495
reply_bson.flag_init();
95-
auto result = libmongoc::database_command_simple(_get_impl().database_t, command_bson.bson(), NULL,
96-
reply_bson.bson(), &error);
96+
auto result = libmongoc::database_command_simple(_get_impl().database_t, command_bson.bson(),
97+
NULL, reply_bson.bson(), &error);
9798

9899
if (!result) {
99100
throw_exception<operation_exception>(std::move(reply_bson.steal()), error);
@@ -109,12 +110,12 @@ bsoncxx::document::value database::modify_collection(stdx::string_view name,
109110
return run_command(doc.view());
110111
}
111112

112-
class collection database::create_collection(stdx::string_view name,
113+
class collection database::create_collection(bsoncxx::string::view_or_value name,
113114
const options::create_collection& options) {
114115
bson_error_t error;
115116

116117
libbson::scoped_bson_t opts_bson{options.to_document()};
117-
auto result = libmongoc::database_create_collection(_get_impl().database_t, name.data(),
118+
auto result = libmongoc::database_create_collection(_get_impl().database_t, name.c_str(),
118119
opts_bson.bson(), &error);
119120

120121
if (!result) {
@@ -140,16 +141,17 @@ stdx::optional<class read_concern> database::read_concern() const {
140141
if (!libmongoc::read_concern_get_level(rc)) {
141142
return stdx::nullopt;
142143
}
143-
return {(class read_concern){stdx::make_unique<read_concern::impl>(libmongoc::read_concern_copy(rc))}};
144+
return {(class read_concern){
145+
stdx::make_unique<read_concern::impl>(libmongoc::read_concern_copy(rc))}};
144146
}
145147

146148
void database::read_preference(class read_preference rp) {
147149
libmongoc::database_set_read_prefs(_get_impl().database_t, rp._impl->read_preference_t);
148150
}
149151

150-
bool database::has_collection(stdx::string_view name) const {
152+
bool database::has_collection(bsoncxx::string::view_or_value name) const {
151153
bson_error_t error;
152-
return libmongoc::database_has_collection(_get_impl().database_t, name.data(), &error);
154+
return libmongoc::database_has_collection(_get_impl().database_t, name.c_str(), &error);
153155
}
154156

155157
class read_preference database::read_preference() const {
@@ -163,17 +165,17 @@ void database::write_concern(class write_concern wc) {
163165
}
164166

165167
class write_concern database::write_concern() const {
166-
class write_concern wc(stdx::make_unique<write_concern::impl>(
167-
libmongoc::write_concern_copy(libmongoc::database_get_write_concern(_get_impl().database_t))));
168+
class write_concern wc(stdx::make_unique<write_concern::impl>(libmongoc::write_concern_copy(
169+
libmongoc::database_get_write_concern(_get_impl().database_t))));
168170
return wc;
169171
}
170172

171-
collection database::collection(stdx::string_view name) const {
172-
return mongocxx::collection(*this, name);
173+
collection database::collection(bsoncxx::string::view_or_value name) const {
174+
return mongocxx::collection(*this, std::move(name));
173175
}
174176

175177
const database::impl& database::_get_impl() const {
176-
if(!_impl) {
178+
if (!_impl) {
177179
throw logic_error{make_error_code(error_code::k_invalid_database_object)};
178180
}
179181
return *_impl;

0 commit comments

Comments
 (0)