Created
April 1, 2021 16:32
-
-
Save DiannaHohensee/e3a97cb8337a91dfb51d3243296711da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/mongo/db/commands/create_indexes.cpp b/src/mongo/db/commands/create_indexes.cpp | |
index b865dab3ab..f3219fef1d 100644 | |
--- a/src/mongo/db/commands/create_indexes.cpp | |
+++ b/src/mongo/db/commands/create_indexes.cpp | |
@@ -637,6 +637,8 @@ std::unique_ptr<CreateIndexesCommand> makeTimeseriesCreateIndexesCommand( | |
for (const auto& origIndex : origIndexes) { | |
BSONObjBuilder builder; | |
for (const auto& elem : origIndex) { | |
+ // Translates the time-series index spec, present in the 'key' field, to an index spec | |
+ // on the underlying buckets collection. | |
if (elem.fieldNameStringData() == NewIndexSpec::kKeyFieldName) { | |
auto bucketsIndexSpecWithStatus = | |
timeseries::createBucketsIndexSpecFromTimeseriesIndexSpec(*timeseriesOptions, | |
@@ -650,6 +652,22 @@ std::unique_ptr<CreateIndexesCommand> makeTimeseriesCreateIndexesCommand( | |
std::move(bucketsIndexSpecWithStatus.getValue())); | |
continue; | |
} | |
+ | |
+ // Translates the 'wildcardProjection' field's time-series index paths to paths on the | |
+ // underlying buckets collection. | |
+ if (elem.fieldNameStringData() == NewIndexSpec::kWildcardProjectionFieldName) { | |
+ auto bucketsWildcardWithStatus = | |
+ timeseries::createMetaFieldOnlyBucketsIndexSpec(*timeseriesOptions, elem.Obj()); | |
+ uassert(ErrorCodes::CannotCreateIndex, | |
+ str::stream() << bucketsWildcardWithStatus.getStatus().toString() | |
+ << " Command request: " << redact(origCmd.toBSON({})), | |
+ bucketsWildcardWithStatus.isOK()); | |
+ | |
+ builder.append(NewIndexSpec::kWildcardProjectionFieldName, | |
+ std::move(bucketsWildcardWithStatus.getValue())); | |
+ continue; | |
+ } | |
+ | |
builder.append(elem); | |
} | |
indexes.push_back(builder.obj()); | |
diff --git a/src/mongo/db/timeseries/timeseries_index_schema_conversion_functions.cpp b/src/mongo/db/timeseries/timeseries_index_schema_conversion_functions.cpp | |
index aad973cf78..ad832773eb 100644 | |
--- a/src/mongo/db/timeseries/timeseries_index_schema_conversion_functions.cpp | |
+++ b/src/mongo/db/timeseries/timeseries_index_schema_conversion_functions.cpp | |
@@ -27,18 +27,53 @@ | |
* it in the license file. | |
*/ | |
+#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kStorage | |
+ | |
#include "mongo/platform/basic.h" | |
#include "mongo/db/timeseries/timeseries_index_schema_conversion_functions.h" | |
#include "mongo/db/timeseries/timeseries_field_names.h" | |
#include "mongo/db/timeseries/timeseries_gen.h" | |
+#include "mongo/logv2/log.h" | |
#include "mongo/logv2/redaction.h" | |
namespace mongo { | |
namespace timeseries { | |
+StatusWith<BSONObj> createMetaFieldOnlyBucketsIndexSpec(const TimeseriesOptions& timeseriesOptions, | |
+ const BSONObj& timeseriesIndexSpecBSON) { | |
+ auto metaField = timeseriesOptions.getMetaField(); | |
+ | |
+ BSONObjBuilder builder; | |
+ for (const auto& elem : timeseriesIndexSpecBSON) { | |
+ if (elem.fieldNameStringData() == *metaField) { | |
+ // The time-series 'metaField' field name always maps to a field named | |
+ // timeseries::kBucketMetaFieldName on the underlying buckets collection. | |
+ builder.appendAs(elem, timeseries::kBucketMetaFieldName); | |
+ continue; | |
+ } | |
+ | |
+ // Lastly, time-series indexes on sub-documents of the 'metaField' are allowed. | |
+ if (elem.fieldNameStringData().startsWith(*metaField + ".")) { | |
+ builder.appendAs(elem, | |
+ str::stream() | |
+ << timeseries::kBucketMetaFieldName << "." | |
+ << elem.fieldNameStringData().substr(metaField->size() + 1)); | |
+ continue; | |
+ } | |
+ | |
+ return {ErrorCodes::BadValue, | |
+ str::stream() << "Invalid index spec for time-series collection: " | |
+ << redact(timeseriesIndexSpecBSON) | |
+ << ", wildcardProjection is only allowed on the '" << *metaField | |
+ << "'. field."}; | |
+ } | |
+ | |
+ return builder.obj(); | |
+} | |
+ | |
StatusWith<BSONObj> createBucketsIndexSpecFromTimeseriesIndexSpec( | |
const TimeseriesOptions& timeseriesOptions, const BSONObj& timeseriesIndexSpecBSON) { | |
auto timeField = timeseriesOptions.getTimeField(); | |
diff --git a/src/mongo/db/timeseries/timeseries_index_schema_conversion_functions.h b/src/mongo/db/timeseries/timeseries_index_schema_conversion_functions.h | |
index 4fbb9c6139..16c01df5b5 100644 | |
--- a/src/mongo/db/timeseries/timeseries_index_schema_conversion_functions.h | |
+++ b/src/mongo/db/timeseries/timeseries_index_schema_conversion_functions.h | |
@@ -43,6 +43,14 @@ class TimeseriesOptions; | |
*/ | |
namespace timeseries { | |
+/** | |
+ * Creates a buckets collection spec from the 'timeseriesIndexSpecBSON' spec provided. Expects specs | |
+ * to only be on the 'metaField' field, or subfields thereof, specified in the provided | |
+ * 'timeseriesOptions'. | |
+ */ | |
+StatusWith<BSONObj> createMetaFieldOnlyBucketsIndexSpec(const TimeseriesOptions& timeseriesOptions, | |
+ const BSONObj& timeseriesIndexSpecBSON); | |
+ | |
/** | |
* Maps the time-series collection index spec 'timeseriesIndexSpecBSON' to the index schema of the | |
* underlying bucket collection using the information provided in 'timeseriesOptions'. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment