Skip to content

Commit

Permalink
CXX-2007 hidden indexes support (#703)
Browse files Browse the repository at this point in the history
  • Loading branch information
bazile-clyde authored and kevinAlbs committed Jul 30, 2020
1 parent 3fe4528 commit ff12ea0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
13 changes: 13 additions & 0 deletions src/mongocxx/options/index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ index& index::unique(bool unique) {
return *this;
}

index& index::hidden(bool hidden) {
_hidden = hidden;
return *this;
}

index& index::name(bsoncxx::string::view_or_value name) {
_name = std::move(name);
return *this;
Expand Down Expand Up @@ -131,6 +136,10 @@ const stdx::optional<bool>& index::unique() const {
return _unique;
}

const stdx::optional<bool>& index::hidden() const {
return _hidden;
}

const stdx::optional<bsoncxx::string::view_or_value>& index::name() const {
return _name;
}
Expand Down Expand Up @@ -214,6 +223,10 @@ index::operator bsoncxx::document::view_or_value() {
root.append(kvp("unique", *_unique));
}

if (_hidden) {
root.append(kvp("hidden", *_hidden));
}

if (_partial_filter_expression) {
root.append(kvp("partialFilterExpression", *_partial_filter_expression));
}
Expand Down
23 changes: 23 additions & 0 deletions src/mongocxx/options/index.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,28 @@ class MONGOCXX_API index {
///
const stdx::optional<bool>& unique() const;

///
/// Whether or not the index is hidden from the query planner. A hidden index is not evaluated
/// as part of query plan selection.
///
/// @param hidden
/// Whether or not to create a hidden index.
///
/// @return
/// A reference to the object on which this member function is being called. This facilitates
/// method chaining.
///
/// @see https://docs.mongodb.com/master/core/index-hidden/
///
index& hidden(bool hidden);

///
/// The current hidden setting.
///
/// @return The current hidden.
///
const stdx::optional<bool>& hidden() const;

///
/// The name of the index.
///
Expand Down Expand Up @@ -456,6 +478,7 @@ class MONGOCXX_API index {

stdx::optional<bool> _background;
stdx::optional<bool> _unique;
stdx::optional<bool> _hidden;
stdx::optional<bsoncxx::string::view_or_value> _name;
stdx::optional<bsoncxx::document::view> _collation;
stdx::optional<bool> _sparse;
Expand Down
2 changes: 1 addition & 1 deletion src/mongocxx/options/index_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace mongocxx {
MONGOCXX_INLINE_NAMESPACE_BEGIN
namespace options {

index_view::index_view() : _max_time(), _write_concern() {}
index_view::index_view() : _max_time(), _write_concern(), _commit_quorum() {}

const bsoncxx::stdx::optional<mongocxx::write_concern>& index_view::write_concern() const {
return _write_concern;
Expand Down
15 changes: 11 additions & 4 deletions src/mongocxx/test/collection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2406,13 +2406,13 @@ TEST_CASE("create_index tests", "[collection]") {

options::index options{};
options.unique(true);
if (test_util::newer_than(mongodb_client, "4.4"))
options.hidden(true);
options.expire_after(std::chrono::seconds(500));
options.name(index_name);

REQUIRE_NOTHROW(coll.create_index(keys.view(), options));

bool unique = options.unique().value();
auto validate = [unique](bsoncxx::document::view index) {
auto validate = [&](bsoncxx::document::view index) {
auto expire_after = index["expireAfterSeconds"];
REQUIRE(expire_after);
REQUIRE(expire_after.type() == type::k_int32);
Expand All @@ -2421,7 +2421,14 @@ TEST_CASE("create_index tests", "[collection]") {
auto unique_ele = index["unique"];
REQUIRE(unique_ele);
REQUIRE(unique_ele.type() == type::k_bool);
REQUIRE(unique_ele.get_bool() == unique);
REQUIRE(unique_ele.get_bool() == options.unique().value());

if (test_util::newer_than(mongodb_client, "4.4")) {
auto hidden_ele = index["hidden"];
REQUIRE(hidden_ele);
REQUIRE(hidden_ele.type() == type::k_bool);
REQUIRE(hidden_ele.get_bool() == options.hidden().value());
}
};

find_index_and_validate(coll, index_name, validate);
Expand Down

0 comments on commit ff12ea0

Please sign in to comment.