Skip to content

RUST-666 Add options for timeseries collection creation #381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/bson_util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ pub(crate) fn serialize_duration_as_int_millis<S: Serializer>(
}
}

#[cfg(test)]
pub(crate) fn serialize_duration_option_as_int_secs<S: Serializer>(
val: &Option<Duration>,
serializer: S,
Expand Down
47 changes: 47 additions & 0 deletions src/db/options.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::time::Duration;

use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use typed_builder::TypedBuilder;
Expand Down Expand Up @@ -83,6 +85,18 @@ pub struct CreateCollectionOptions {

/// The default configuration for indexes created on this collection, including the _id index.
pub index_option_defaults: Option<IndexOptionDefaults>,

/// Specifies options for creating a timeseries collection. This feature is only available on
/// server versions 5.0 and above.
pub timeseries: Option<TimeseriesOptions>,

/// Duration indicating after how long old time-series data should be deleted.
#[serde(
default,
Copy link
Contributor Author

@abr-egn abr-egn Jun 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why this field needs to be explicitly marked as using the default when it's not present when others don't, but without this annotation some tests fail in deserialization complaining about missing field "expireAfterSeconds".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's because we provide special serialization / deserialization helpers for this field which override the default behavior.

deserialize_with = "bson_util::deserialize_duration_from_u64_seconds",
serialize_with = "bson_util::serialize_duration_option_as_int_secs"
)]
pub expire_after_seconds: Option<Duration>,
}

/// Specifies how strictly the database should apply validation rules to existing documents during
Expand Down Expand Up @@ -123,6 +137,39 @@ pub struct IndexOptionDefaults {
pub storage_engine: Document,
}

/// Specifies options for creating a timeseries collection.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct TimeseriesOptions {
/// Name of the top-level field to be used for time. Inserted documents must have this field,
/// and the field must be of the BSON UTC datetime type.
pub time_field: String,

/// Name of the top-level field describing the series. This field is used to group related data
/// and may be of any BSON type, except for array. This name may not be the same as the
/// timeField or _id.
pub meta_field: Option<String>,

/// The units you'd use to describe the expected interval between subsequent measurements for a
/// time-series. Defaults to `TimeseriesGranularity::Seconds` if unset.
pub granularity: Option<TimeseriesGranularity>,
}

/// The units you'd use to describe the expected interval between subsequent measurements for a
/// time-series.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub enum TimeseriesGranularity {
/// The expected interval between subsequent measurements is in seconds.
Seconds,
/// The expected interval between subsequent measurements is in minutes.
Minutes,
/// The expected interval between subsequent measurements is in hours.
Hours,
}

/// Specifies the options to a [`Database::drop`](../struct.Database.html#method.drop) operation.
#[derive(Debug, Default, TypedBuilder, Serialize)]
#[serde(rename_all = "camelCase")]
Expand Down
10 changes: 10 additions & 0 deletions src/test/spec/collection_management.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use crate::test::{run_spec_test, LOCK};

use super::run_unified_format_test;

#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
async fn run() {
let _guard = LOCK.run_exclusively().await;
run_spec_test(&["collection-management"], run_unified_format_test).await;
}
7 changes: 7 additions & 0 deletions src/test/spec/json/collection-management/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
===========================
Collection Management Tests
===========================

This directory contains tests for collection management. They are implemented
in the `Unified Test Format <../../unified-test-format/unified-test-format.rst>`__
and require schema version 1.0.
255 changes: 255 additions & 0 deletions src/test/spec/json/collection-management/timeseries-collection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
{
"description": "timeseries-collection",
"schemaVersion": "1.0",
"runOnRequirements": [
{
"minServerVersion": "5.0"
}
],
"createEntities": [
{
"client": {
"id": "client0",
"observeEvents": [
"commandStartedEvent"
]
}
},
{
"database": {
"id": "database0",
"client": "client0",
"databaseName": "ts-tests"
}
},
{
"collection": {
"id": "collection0",
"database": "database0",
"collectionName": "test"
}
}
],
"initialData": [
{
"collectionName": "test",
"databaseName": "ts-tests",
"documents": []
}
],
"tests": [
{
"description": "createCollection with all options",
"operations": [
{
"name": "dropCollection",
"object": "database0",
"arguments": {
"collection": "test"
}
},
{
"name": "createCollection",
"object": "database0",
"arguments": {
"collection": "test",
"expireAfterSeconds": 604800,
"timeseries": {
"timeField": "time",
"metaField": "meta",
"granularity": "minutes"
}
}
},
{
"name": "assertCollectionExists",
"object": "testRunner",
"arguments": {
"databaseName": "ts-tests",
"collectionName": "test"
}
}
],
"expectEvents": [
{
"client": "client0",
"events": [
{
"commandStartedEvent": {
"command": {
"drop": "test"
},
"databaseName": "ts-tests"
}
},
{
"commandStartedEvent": {
"command": {
"create": "test",
"expireAfterSeconds": 604800,
"timeseries": {
"timeField": "time",
"metaField": "meta",
"granularity": "minutes"
}
},
"databaseName": "ts-tests"
}
}
]
}
]
},
{
"description": "insertMany with duplicate ids",
"operations": [
{
"name": "dropCollection",
"object": "database0",
"arguments": {
"collection": "test"
}
},
{
"name": "createCollection",
"object": "database0",
"arguments": {
"collection": "test",
"expireAfterSeconds": 604800,
"timeseries": {
"timeField": "time",
"metaField": "meta",
"granularity": "minutes"
}
}
},
{
"name": "assertCollectionExists",
"object": "testRunner",
"arguments": {
"databaseName": "ts-tests",
"collectionName": "test"
}
},
{
"name": "insertMany",
"object": "collection0",
"arguments": {
"documents": [
{
"_id": 1,
"time": {
"$date": {
"$numberLong": "1552949630482"
}
}
},
{
"_id": 1,
"time": {
"$date": {
"$numberLong": "1552949630483"
}
}
}
]
}
},
{
"name": "find",
"object": "collection0",
"arguments": {
"filter": {},
"sort": {
"time": 1
}
},
"expectResult": [
{
"_id": 1,
"time": {
"$date": {
"$numberLong": "1552949630482"
}
}
},
{
"_id": 1,
"time": {
"$date": {
"$numberLong": "1552949630483"
}
}
}
]
}
],
"expectEvents": [
{
"client": "client0",
"events": [
{
"commandStartedEvent": {
"command": {
"drop": "test"
},
"databaseName": "ts-tests"
}
},
{
"commandStartedEvent": {
"command": {
"create": "test",
"expireAfterSeconds": 604800,
"timeseries": {
"timeField": "time",
"metaField": "meta",
"granularity": "minutes"
}
},
"databaseName": "ts-tests"
}
},
{
"commandStartedEvent": {
"command": {
"insert": "test",
"documents": [
{
"_id": 1,
"time": {
"$date": {
"$numberLong": "1552949630482"
}
}
},
{
"_id": 1,
"time": {
"$date": {
"$numberLong": "1552949630483"
}
}
}
]
}
}
},
{
"commandStartedEvent": {
"command": {
"find": "test",
"filter": {},
"sort": {
"time": 1
}
},
"databaseName": "ts-tests"
}
}
]
}
]
}
]
}
Loading