Skip to content

Commit ff12ea0

Browse files
bazile-clydekevinAlbs
authored andcommitted
CXX-2007 hidden indexes support (#703)
1 parent 3fe4528 commit ff12ea0

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

src/mongocxx/options/index.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ index& index::unique(bool unique) {
3838
return *this;
3939
}
4040

41+
index& index::hidden(bool hidden) {
42+
_hidden = hidden;
43+
return *this;
44+
}
45+
4146
index& index::name(bsoncxx::string::view_or_value name) {
4247
_name = std::move(name);
4348
return *this;
@@ -131,6 +136,10 @@ const stdx::optional<bool>& index::unique() const {
131136
return _unique;
132137
}
133138

139+
const stdx::optional<bool>& index::hidden() const {
140+
return _hidden;
141+
}
142+
134143
const stdx::optional<bsoncxx::string::view_or_value>& index::name() const {
135144
return _name;
136145
}
@@ -214,6 +223,10 @@ index::operator bsoncxx::document::view_or_value() {
214223
root.append(kvp("unique", *_unique));
215224
}
216225

226+
if (_hidden) {
227+
root.append(kvp("hidden", *_hidden));
228+
}
229+
217230
if (_partial_filter_expression) {
218231
root.append(kvp("partialFilterExpression", *_partial_filter_expression));
219232
}

src/mongocxx/options/index.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,28 @@ class MONGOCXX_API index {
126126
///
127127
const stdx::optional<bool>& unique() const;
128128

129+
///
130+
/// Whether or not the index is hidden from the query planner. A hidden index is not evaluated
131+
/// as part of query plan selection.
132+
///
133+
/// @param hidden
134+
/// Whether or not to create a hidden index.
135+
///
136+
/// @return
137+
/// A reference to the object on which this member function is being called. This facilitates
138+
/// method chaining.
139+
///
140+
/// @see https://docs.mongodb.com/master/core/index-hidden/
141+
///
142+
index& hidden(bool hidden);
143+
144+
///
145+
/// The current hidden setting.
146+
///
147+
/// @return The current hidden.
148+
///
149+
const stdx::optional<bool>& hidden() const;
150+
129151
///
130152
/// The name of the index.
131153
///
@@ -456,6 +478,7 @@ class MONGOCXX_API index {
456478

457479
stdx::optional<bool> _background;
458480
stdx::optional<bool> _unique;
481+
stdx::optional<bool> _hidden;
459482
stdx::optional<bsoncxx::string::view_or_value> _name;
460483
stdx::optional<bsoncxx::document::view> _collation;
461484
stdx::optional<bool> _sparse;

src/mongocxx/options/index_view.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace mongocxx {
2525
MONGOCXX_INLINE_NAMESPACE_BEGIN
2626
namespace options {
2727

28-
index_view::index_view() : _max_time(), _write_concern() {}
28+
index_view::index_view() : _max_time(), _write_concern(), _commit_quorum() {}
2929

3030
const bsoncxx::stdx::optional<mongocxx::write_concern>& index_view::write_concern() const {
3131
return _write_concern;

src/mongocxx/test/collection.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2406,13 +2406,13 @@ TEST_CASE("create_index tests", "[collection]") {
24062406

24072407
options::index options{};
24082408
options.unique(true);
2409+
if (test_util::newer_than(mongodb_client, "4.4"))
2410+
options.hidden(true);
24092411
options.expire_after(std::chrono::seconds(500));
24102412
options.name(index_name);
24112413

24122414
REQUIRE_NOTHROW(coll.create_index(keys.view(), options));
2413-
2414-
bool unique = options.unique().value();
2415-
auto validate = [unique](bsoncxx::document::view index) {
2415+
auto validate = [&](bsoncxx::document::view index) {
24162416
auto expire_after = index["expireAfterSeconds"];
24172417
REQUIRE(expire_after);
24182418
REQUIRE(expire_after.type() == type::k_int32);
@@ -2421,7 +2421,14 @@ TEST_CASE("create_index tests", "[collection]") {
24212421
auto unique_ele = index["unique"];
24222422
REQUIRE(unique_ele);
24232423
REQUIRE(unique_ele.type() == type::k_bool);
2424-
REQUIRE(unique_ele.get_bool() == unique);
2424+
REQUIRE(unique_ele.get_bool() == options.unique().value());
2425+
2426+
if (test_util::newer_than(mongodb_client, "4.4")) {
2427+
auto hidden_ele = index["hidden"];
2428+
REQUIRE(hidden_ele);
2429+
REQUIRE(hidden_ele.type() == type::k_bool);
2430+
REQUIRE(hidden_ele.get_bool() == options.hidden().value());
2431+
}
24252432
};
24262433

24272434
find_index_and_validate(coll, index_name, validate);

0 commit comments

Comments
 (0)