summaryrefslogtreecommitdiff
path: root/src/mongo/db/timeseries
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/db/timeseries')
-rw-r--r--src/mongo/db/timeseries/SConscript2
-rw-r--r--src/mongo/db/timeseries/bucket_catalog.cpp10
-rw-r--r--src/mongo/db/timeseries/timeseries_options.cpp66
-rw-r--r--src/mongo/db/timeseries/timeseries_options.h10
-rw-r--r--src/mongo/db/timeseries/timeseries_options_test.cpp63
5 files changed, 111 insertions, 40 deletions
diff --git a/src/mongo/db/timeseries/SConscript b/src/mongo/db/timeseries/SConscript
index 866be00d415..8a75cd7fbb9 100644
--- a/src/mongo/db/timeseries/SConscript
+++ b/src/mongo/db/timeseries/SConscript
@@ -76,11 +76,13 @@ env.CppUnitTest(
'minmax_test.cpp',
'timeseries_index_schema_conversion_functions_test.cpp',
'timeseries_update_delete_util_test.cpp',
+ 'timeseries_options_test.cpp'
],
LIBDEPS=[
'$BUILD_DIR/mongo/db/catalog/catalog_test_fixture',
'bucket_catalog',
'timeseries_index_schema_conversion_functions',
+ 'timeseries_options',
'timeseries_update_delete_util',
],
)
diff --git a/src/mongo/db/timeseries/bucket_catalog.cpp b/src/mongo/db/timeseries/bucket_catalog.cpp
index e65da2ce66f..33087511caa 100644
--- a/src/mongo/db/timeseries/bucket_catalog.cpp
+++ b/src/mongo/db/timeseries/bucket_catalog.cpp
@@ -150,13 +150,6 @@ OperationId getOpId(OperationContext* opCtx,
MONGO_UNREACHABLE;
}
-long long roundTimestampDown(const Date_t& time, const TimeseriesOptions& options) {
- int roundingSeconds =
- timeseries::getBucketRoundingSecondsFromGranularity(options.getGranularity());
- long long seconds = durationCount<Seconds>(time.toDurationSinceEpoch());
- return (seconds - (seconds % roundingSeconds));
-}
-
BSONObj buildControlMinTimestampDoc(StringData timeField, long long roundedSeconds) {
BSONObjBuilder builder;
builder.append(timeField, Date_t::fromMillisSinceEpoch(1000 * roundedSeconds));
@@ -654,7 +647,8 @@ const std::shared_ptr<BucketCatalog::ExecutionStats> BucketCatalog::_getExecutio
void BucketCatalog::_setIdTimestamp(Bucket* bucket,
const Date_t& time,
const TimeseriesOptions& options) {
- auto const roundedSeconds = roundTimestampDown(time, options);
+ auto roundedTime = timeseries::roundTimestampToGranularity(time, options.getGranularity());
+ auto const roundedSeconds = durationCount<Seconds>(roundedTime.toDurationSinceEpoch());
bucket->_id.setTimestamp(roundedSeconds);
// Make sure we set the control.min time field to match the rounded _id timestamp.
diff --git a/src/mongo/db/timeseries/timeseries_options.cpp b/src/mongo/db/timeseries/timeseries_options.cpp
index 81a3fc15cb4..e8b1df78f91 100644
--- a/src/mongo/db/timeseries/timeseries_options.cpp
+++ b/src/mongo/db/timeseries/timeseries_options.cpp
@@ -103,33 +103,6 @@ int getMaxSpanSecondsFromGranularity(BucketGranularityEnum granularity) {
MONGO_UNREACHABLE;
}
-int getBucketRoundingSecondsFromGranularity(BucketGranularityEnum granularity) {
- switch (granularity) {
- case BucketGranularityEnum::Seconds:
- // Round down to nearest minute.
- return 60;
- case BucketGranularityEnum::Minutes:
- // Round down to nearest hour.
- return 60 * 60;
- case BucketGranularityEnum::Hours:
- // Round down to hearest day.
- return 60 * 60 * 24;
- }
- MONGO_UNREACHABLE;
-}
-bool optionsAreEqual(const TimeseriesOptions& option1, const TimeseriesOptions& option2) {
- const auto option1BucketSpan = option1.getBucketMaxSpanSeconds()
- ? *option1.getBucketMaxSpanSeconds()
- : getMaxSpanSecondsFromGranularity(option1.getGranularity());
- const auto option2BucketSpan = option2.getBucketMaxSpanSeconds()
- ? *option2.getBucketMaxSpanSeconds()
- : getMaxSpanSecondsFromGranularity(option2.getGranularity());
- return option1.getTimeField() == option1.getTimeField() &&
- option1.getMetaField() == option2.getMetaField() &&
- option1.getGranularity() == option2.getGranularity() &&
- option1BucketSpan == option2BucketSpan;
-}
-
StatusWith<std::pair<TimeseriesOptions, bool>> applyTimeseriesOptionsModifications(
const TimeseriesOptions& currentOptions, const BSONObj& mod) {
TimeseriesOptions newOptions = currentOptions;
@@ -171,5 +144,44 @@ BSONObj generateViewPipeline(const TimeseriesOptions& options, bool asArray) {
<< *options.getBucketMaxSpanSeconds() << "exclude" << BSONArray())));
}
+bool optionsAreEqual(const TimeseriesOptions& option1, const TimeseriesOptions& option2) {
+ const auto option1BucketSpan = option1.getBucketMaxSpanSeconds()
+ ? *option1.getBucketMaxSpanSeconds()
+ : getMaxSpanSecondsFromGranularity(option1.getGranularity());
+ const auto option2BucketSpan = option2.getBucketMaxSpanSeconds()
+ ? *option2.getBucketMaxSpanSeconds()
+ : getMaxSpanSecondsFromGranularity(option2.getGranularity());
+ return option1.getTimeField() == option1.getTimeField() &&
+ option1.getMetaField() == option2.getMetaField() &&
+ option1.getGranularity() == option2.getGranularity() &&
+ option1BucketSpan == option2BucketSpan;
+}
+
+namespace {
+/**
+ * Returns the number of seconds used to round down the bucket ID and control.min timestamp.
+ */
+int getBucketRoundingSecondsFromGranularity(BucketGranularityEnum granularity) {
+ switch (granularity) {
+ case BucketGranularityEnum::Seconds:
+ // Round down to nearest minute.
+ return 60;
+ case BucketGranularityEnum::Minutes:
+ // Round down to nearest hour.
+ return 60 * 60;
+ case BucketGranularityEnum::Hours:
+ // Round down to nearest day.
+ return 60 * 60 * 24;
+ }
+ MONGO_UNREACHABLE;
+}
+} // namespace
+
+Date_t roundTimestampToGranularity(const Date_t& time, BucketGranularityEnum granularity) {
+ int roundingSeconds = getBucketRoundingSecondsFromGranularity(granularity);
+ long long timeSeconds = durationCount<Seconds>(time.toDurationSinceEpoch());
+ long long roundedTimeSeconds = (timeSeconds - (timeSeconds % roundingSeconds));
+ return Date_t::fromDurationSinceEpoch(Seconds{roundedTimeSeconds});
+}
} // namespace timeseries
} // namespace mongo
diff --git a/src/mongo/db/timeseries/timeseries_options.h b/src/mongo/db/timeseries/timeseries_options.h
index feaf7e3cd33..87bf4387473 100644
--- a/src/mongo/db/timeseries/timeseries_options.h
+++ b/src/mongo/db/timeseries/timeseries_options.h
@@ -53,16 +53,16 @@ boost::optional<TimeseriesOptions> getTimeseriesOptions(OperationContext* opCtx,
*/
int getMaxSpanSecondsFromGranularity(BucketGranularityEnum granularity);
-/**
- * Returns the number of seconds used to round down the bucket ID and control.min timestamp.
- */
-int getBucketRoundingSecondsFromGranularity(BucketGranularityEnum granularity);
-
StatusWith<std::pair<TimeseriesOptions, bool>> applyTimeseriesOptionsModifications(
const TimeseriesOptions& current, const BSONObj& mod);
BSONObj generateViewPipeline(const TimeseriesOptions& options, bool asArray);
bool optionsAreEqual(const TimeseriesOptions& option1, const TimeseriesOptions& option2);
+
+/**
+ * Rounds down timestamp to the specified granularity.
+ */
+Date_t roundTimestampToGranularity(const Date_t& time, BucketGranularityEnum granularity);
} // namespace timeseries
} // namespace mongo
diff --git a/src/mongo/db/timeseries/timeseries_options_test.cpp b/src/mongo/db/timeseries/timeseries_options_test.cpp
new file mode 100644
index 00000000000..e3106407cea
--- /dev/null
+++ b/src/mongo/db/timeseries/timeseries_options_test.cpp
@@ -0,0 +1,63 @@
+/**
+ * Copyright (C) 2021-present MongoDB, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the Server Side Public License, version 1,
+ * as published by MongoDB, Inc.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * Server Side Public License for more details.
+ *
+ * You should have received a copy of the Server Side Public License
+ * along with this program. If not, see
+ * <http://www.mongodb.com/licensing/server-side-public-license>.
+ *
+ * As a special exception, the copyright holders give permission to link the
+ * code of portions of this program with the OpenSSL library under certain
+ * conditions as described in each individual source file and distribute
+ * linked combinations including the program with the OpenSSL library. You
+ * must comply with the Server Side Public License in all respects for
+ * all of the code used other than as permitted herein. If you modify file(s)
+ * with this exception, you may extend this exception to your version of the
+ * file(s), but you are not obligated to do so. If you do not wish to do so,
+ * delete this exception statement from your version. If you delete this
+ * exception statement from all source files in the program, then also delete
+ * it in the license file.
+ */
+
+#include "mongo/platform/basic.h"
+
+#include "mongo/db/timeseries/timeseries_options.h"
+#include "mongo/unittest/unittest.h"
+#include "mongo/util/time_support.h"
+
+
+namespace mongo {
+
+TEST(TimeseriesOptionsTest, RoundTimestampToGranularity) {
+ std::vector<std::tuple<BucketGranularityEnum, std::string, std::string>> testCases{
+ {BucketGranularityEnum::Seconds, "2021-01-01T00:00:15.555Z", "2021-01-01T00:00:00.000Z"},
+ {BucketGranularityEnum::Seconds, "2021-01-01T00:00:30.555Z", "2021-01-01T00:00:00.000Z"},
+ {BucketGranularityEnum::Seconds, "2021-01-01T00:00:45.555Z", "2021-01-01T00:00:00.000Z"},
+
+ {BucketGranularityEnum::Minutes, "2021-01-01T00:15:00.000Z", "2021-01-01T00:00:00.000Z"},
+ {BucketGranularityEnum::Minutes, "2021-01-01T00:30:00.000Z", "2021-01-01T00:00:00.000Z"},
+ {BucketGranularityEnum::Minutes, "2021-01-01T00:45:00.000Z", "2021-01-01T00:00:00.000Z"},
+
+ {BucketGranularityEnum::Hours, "2021-01-01T06:00:00.000Z", "2021-01-01T00:00:00.000Z"},
+ {BucketGranularityEnum::Hours, "2021-01-01T12:00:00.000Z", "2021-01-01T00:00:00.000Z"},
+ {BucketGranularityEnum::Hours, "2021-01-01T18:00:00.000Z", "2021-01-01T00:00:00.000Z"},
+ };
+
+ for (const auto& [granularity, input, expectedOutput] : testCases) {
+ auto inputDate = dateFromISOString(input);
+ ASSERT_OK(inputDate);
+ auto roundedDate =
+ timeseries::roundTimestampToGranularity(inputDate.getValue(), granularity);
+ ASSERT_EQ(dateToISOStringUTC(roundedDate), expectedOutput);
+ }
+}
+
+} // namespace mongo