Skip to content

Commit 60054f3

Browse files
CXX-739 Apply string::view_or_value to index options
1 parent 586794c commit 60054f3

File tree

4 files changed

+43
-24
lines changed

4 files changed

+43
-24
lines changed

src/bsoncxx/string/view_or_value.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ class BSONCXX_API view_or_value : public bsoncxx::view_or_value<stdx::string_vie
4040
///
4141
using bsoncxx::view_or_value<stdx::string_view, std::string>::view_or_value;
4242

43+
///
44+
/// Default constructor, equivalent to using an empty string.
45+
///
46+
BSONCXX_INLINE view_or_value() = default;
47+
4348
///
4449
/// Construct a string::view_or_value using a null-terminated const char *.
4550
/// The resulting view_or_value will keep a string_view of 'str', so it is

src/mongocxx/collection.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <cstdint>
1919
#include <limits>
2020
#include <tuple>
21+
#include <unordered_set>
2122
#include <utility>
2223

2324
#include <bsoncxx/builder/stream/document.hpp>
@@ -591,6 +592,12 @@ bsoncxx::document::value collection::create_index(view_or_value keys,
591592
::mongoc_index_opt_wt_t wt_opt{};
592593
libmongoc::index_opt_init(&opt);
593594

595+
// keep our safe copies alive
596+
bsoncxx::string::view_or_value name_copy{};
597+
bsoncxx::string::view_or_value wt_config_copy{};
598+
bsoncxx::string::view_or_value default_language_copy{};
599+
bsoncxx::string::view_or_value language_override_copy{};
600+
594601
if (options.background()) {
595602
opt.background = *options.background();
596603
}
@@ -600,7 +607,8 @@ bsoncxx::document::value collection::create_index(view_or_value keys,
600607
}
601608

602609
if (options.name()) {
603-
opt.name = options.name()->c_str();
610+
name_copy = options.name()->terminated();
611+
opt.name = name_copy.data();
604612
}
605613

606614
if (options.sparse()) {
@@ -617,7 +625,8 @@ bsoncxx::document::value collection::create_index(view_or_value keys,
617625
options.storage_options().get());
618626

619627
if (wt_options->config_string()) {
620-
wt_opt.config_str = wt_options->config_string()->c_str();
628+
wt_config_copy = wt_options->config_string()->terminated();
629+
wt_opt.config_str = wt_config_copy.data();
621630
}
622631
opt.storage_options = reinterpret_cast<mongoc_index_opt_storage_t*>(&wt_opt);
623632
}
@@ -637,11 +646,13 @@ bsoncxx::document::value collection::create_index(view_or_value keys,
637646
}
638647

639648
if (options.default_language()) {
640-
opt.default_language = options.default_language()->c_str();
649+
default_language_copy = options.default_language()->terminated();
650+
opt.default_language = default_language_copy.data();
641651
}
642652

643653
if (options.language_override()) {
644-
opt.language_override = options.language_override()->c_str();
654+
language_override_copy = options.language_override()->terminated();
655+
opt.language_override = language_override_copy.data();
645656
}
646657

647658
if (options.partial_filter_expression()) {

src/mongocxx/options/index.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void index::unique(bool unique) {
3131
_unique = unique;
3232
}
3333

34-
void index::name(std::string name) {
34+
void index::name(bsoncxx::string::view_or_value name) {
3535
_name = std::move(name);
3636
}
3737

@@ -60,11 +60,11 @@ void index::weights(bsoncxx::document::view weights) {
6060
_weights = weights;
6161
}
6262

63-
void index::default_language(std::string default_language) {
63+
void index::default_language(bsoncxx::string::view_or_value default_language) {
6464
_default_language = std::move(default_language);
6565
}
6666

67-
void index::language_override(std::string language_override) {
67+
void index::language_override(bsoncxx::string::view_or_value language_override) {
6868
_language_override = std::move(language_override);
6969
}
7070

@@ -100,7 +100,7 @@ const stdx::optional<bool>& index::unique() const {
100100
return _unique;
101101
}
102102

103-
const stdx::optional<std::string>& index::name() const {
103+
const stdx::optional<bsoncxx::string::view_or_value>& index::name() const {
104104
return _name;
105105
}
106106

@@ -124,11 +124,11 @@ const stdx::optional<bsoncxx::document::view>& index::weights() const {
124124
return _weights;
125125
}
126126

127-
const stdx::optional<std::string>& index::default_language() const {
127+
const stdx::optional<bsoncxx::string::view_or_value>& index::default_language() const {
128128
return _default_language;
129129
}
130130

131-
const stdx::optional<std::string>& index::language_override() const {
131+
const stdx::optional<bsoncxx::string::view_or_value>& index::language_override() const {
132132
return _language_override;
133133
}
134134

@@ -160,11 +160,13 @@ index::base_storage_options::~base_storage_options() = default;
160160

161161
index::wiredtiger_storage_options::~wiredtiger_storage_options() = default;
162162

163-
void index::wiredtiger_storage_options::config_string(std::string config_string) {
163+
void index::wiredtiger_storage_options::config_string(
164+
bsoncxx::string::view_or_value config_string) {
164165
_config_string = std::move(config_string);
165166
}
166167

167-
const stdx::optional<std::string>& index::wiredtiger_storage_options::config_string() const {
168+
const stdx::optional<bsoncxx::string::view_or_value>&
169+
index::wiredtiger_storage_options::config_string() const {
168170
return _config_string;
169171
}
170172

src/mongocxx/options/index.hpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include <bsoncxx/document/view.hpp>
2222
#include <bsoncxx/stdx/optional.hpp>
23+
#include <bsoncxx/string/view_or_value.hpp>
2324
#include <mongocxx/stdx.hpp>
2425

2526
namespace mongocxx {
@@ -61,19 +62,19 @@ class MONGOCXX_API index {
6162
/// @param config_string
6263
/// The WiredTiger configuration string.
6364
///
64-
void config_string(std::string config_string);
65+
void config_string(bsoncxx::string::view_or_value config_string);
6566

6667
///
6768
/// The current config_string setting.
6869
///
6970
/// @return The current config_string.
7071
///
71-
const stdx::optional<std::string>& config_string() const;
72+
const stdx::optional<bsoncxx::string::view_or_value>& config_string() const;
7273

7374
private:
7475
friend collection;
7576
MONGOCXX_PRIVATE int type() const override;
76-
stdx::optional<std::string> _config_string;
77+
stdx::optional<bsoncxx::string::view_or_value> _config_string;
7778
};
7879

7980
index();
@@ -120,14 +121,14 @@ class MONGOCXX_API index {
120121
/// @param name
121122
/// The name of the index.
122123
///
123-
void name(std::string name);
124+
void name(bsoncxx::string::view_or_value name);
124125

125126
///
126127
/// The current name setting.
127128
///
128129
/// @return The current name.
129130
///
130-
const stdx::optional<std::string>& name() const;
131+
const stdx::optional<bsoncxx::string::view_or_value>& name() const;
131132

132133
///
133134
/// Whether or not to create a sparse index. Sparse indexes only reference documents with the
@@ -213,14 +214,14 @@ class MONGOCXX_API index {
213214
/// @param default_language
214215
/// The default language used when creating text indexes.
215216
///
216-
void default_language(std::string default_language);
217+
void default_language(bsoncxx::string::view_or_value default_language);
217218

218219
///
219220
/// The current default_language setting.
220221
///
221222
/// @return The current default_language.
222223
///
223-
const stdx::optional<std::string>& default_language() const;
224+
const stdx::optional<bsoncxx::string::view_or_value>& default_language() const;
224225

225226
///
226227
/// For text indexes, the name of the field, in the collection’s documents, that contains the
@@ -229,14 +230,14 @@ class MONGOCXX_API index {
229230
/// @param language_override
230231
/// The name of the field that contains the override language for text indexes.
231232
///
232-
void language_override(std::string language_override);
233+
void language_override(bsoncxx::string::view_or_value language_override);
233234

234235
///
235236
/// The current name of the field that contains the override language for text indexes.
236237
///
237238
/// @return The name of the field that contains the override language for text indexes.
238239
///
239-
const stdx::optional<std::string>& language_override() const;
240+
const stdx::optional<bsoncxx::string::view_or_value>& language_override() const;
240241

241242
///
242243
/// Sets the document for the partial filter expression for partial indexes.
@@ -337,14 +338,14 @@ class MONGOCXX_API index {
337338

338339
stdx::optional<bool> _background;
339340
stdx::optional<bool> _unique;
340-
stdx::optional<std::string> _name;
341+
stdx::optional<bsoncxx::string::view_or_value> _name;
341342
stdx::optional<bool> _sparse;
342343
std::unique_ptr<base_storage_options> _storage_options;
343344
stdx::optional<std::int32_t> _expire_after_seconds;
344345
stdx::optional<std::int32_t> _version;
345346
stdx::optional<bsoncxx::document::view> _weights;
346-
stdx::optional<std::string> _default_language;
347-
stdx::optional<std::string> _language_override;
347+
stdx::optional<bsoncxx::string::view_or_value> _default_language;
348+
stdx::optional<bsoncxx::string::view_or_value> _language_override;
348349
stdx::optional<bsoncxx::document::view> _partial_filter_expression;
349350
stdx::optional<std::uint8_t> _twod_sphere_version;
350351
stdx::optional<std::uint8_t> _twod_bits_precision;

0 commit comments

Comments
 (0)