summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenety Goh <benety@mongodb.com>2021-06-17 06:01:59 -0400
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2021-06-17 10:56:17 +0000
commitbf7e84984e992b7c994ba090d89fcd528973ff60 (patch)
treeaf7e6031e5e4da495e1c1960bc8eff408a572b6d
parentdf5735431344335eeb8197a8e073aa6967d40a12 (diff)
downloadmongo-bf7e84984e992b7c994ba090d89fcd528973ff60.tar.gz
SERVER-57385 add BSONCollectionCatalogEntry::IndexMetaData::isPresent()
Ignore invalid index metadata when calculating total index count and returning all indexes.
-rw-r--r--src/mongo/db/catalog/collection_impl.cpp10
-rw-r--r--src/mongo/db/storage/bson_collection_catalog_entry.cpp5
-rw-r--r--src/mongo/db/storage/bson_collection_catalog_entry.h14
-rw-r--r--src/mongo/db/storage/durable_catalog_impl.cpp2
4 files changed, 27 insertions, 4 deletions
diff --git a/src/mongo/db/catalog/collection_impl.cpp b/src/mongo/db/catalog/collection_impl.cpp
index 4138de412e7..54c08ad9f33 100644
--- a/src/mongo/db/catalog/collection_impl.cpp
+++ b/src/mongo/db/catalog/collection_impl.cpp
@@ -1965,7 +1965,7 @@ void CollectionImpl::forceSetIndexIsMultikey(OperationContext* opCtx,
}
int CollectionImpl::getTotalIndexCount() const {
- return static_cast<int>(_metadata->indexes.size());
+ return _metadata->getTotalIndexCount();
}
int CollectionImpl::getCompletedIndexCount() const {
@@ -1987,8 +1987,12 @@ BSONObj CollectionImpl::getIndexSpec(StringData indexName) const {
}
void CollectionImpl::getAllIndexes(std::vector<std::string>* names) const {
- for (unsigned i = 0; i < _metadata->indexes.size(); i++) {
- names->push_back(_metadata->indexes[i].spec["name"].String());
+ for (const auto& index : _metadata->indexes) {
+ if (!index.isPresent()) {
+ continue;
+ }
+
+ names->push_back(index.nameStringData().toString());
}
}
diff --git a/src/mongo/db/storage/bson_collection_catalog_entry.cpp b/src/mongo/db/storage/bson_collection_catalog_entry.cpp
index 4fdf2f8baa5..1c954cbce9f 100644
--- a/src/mongo/db/storage/bson_collection_catalog_entry.cpp
+++ b/src/mongo/db/storage/bson_collection_catalog_entry.cpp
@@ -139,6 +139,11 @@ void BSONCollectionCatalogEntry::IndexMetaData::updateHiddenSetting(bool hidden)
// --------------------------
+int BSONCollectionCatalogEntry::MetaData::getTotalIndexCount() const {
+ return std::count_if(
+ indexes.cbegin(), indexes.cend(), [](const auto& index) { return index.isPresent(); });
+}
+
int BSONCollectionCatalogEntry::MetaData::findIndexOffset(StringData name) const {
for (unsigned i = 0; i < indexes.size(); i++)
if (indexes[i].nameStringData() == name)
diff --git a/src/mongo/db/storage/bson_collection_catalog_entry.h b/src/mongo/db/storage/bson_collection_catalog_entry.h
index 189565eaff1..afdfc310e1b 100644
--- a/src/mongo/db/storage/bson_collection_catalog_entry.h
+++ b/src/mongo/db/storage/bson_collection_catalog_entry.h
@@ -69,6 +69,15 @@ public:
multikeyPaths = other.multikeyPaths;
}
+ /**
+ * An index is considered present if it has a non-empty 'spec'.
+ * Invalid indexes by this definition include default constructed instances and
+ * and structs zeroed out due to index drops.
+ */
+ bool isPresent() const {
+ return !spec.isEmpty();
+ }
+
IndexMetaData& operator=(IndexMetaData&& rhs) {
if (&rhs != this) {
spec = std::move(rhs.spec);
@@ -122,6 +131,11 @@ public:
*/
BSONObj toBSON(bool hasExclusiveAccess = false) const;
+ /**
+ * Returns number of valid indexes.
+ */
+ int getTotalIndexCount() const;
+
int findIndexOffset(StringData name) const;
/**
diff --git a/src/mongo/db/storage/durable_catalog_impl.cpp b/src/mongo/db/storage/durable_catalog_impl.cpp
index cd806d06b0a..bf9818d25e9 100644
--- a/src/mongo/db/storage/durable_catalog_impl.cpp
+++ b/src/mongo/db/storage/durable_catalog_impl.cpp
@@ -1070,7 +1070,7 @@ int DurableCatalogImpl::getTotalIndexCount(OperationContext* opCtx, RecordId cat
if (!md)
return 0;
- return static_cast<int>(md->indexes.size());
+ return md->getTotalIndexCount();
}
bool DurableCatalogImpl::isIndexPresent(OperationContext* opCtx,