Skip to content
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
35 changes: 35 additions & 0 deletions src/db/options.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::time::Duration;

use bson::doc;
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use typed_builder::TypedBuilder;
Expand Down Expand Up @@ -106,6 +107,9 @@ pub struct CreateCollectionOptions {

/// Options for supporting change stream pre- and post-images.
pub change_stream_pre_and_post_images: Option<ChangeStreamPreAndPostImages>,

/// Options for clustered collections.
pub clustered_index: Option<ClusteredIndex>,
}

/// Specifies how strictly the database should apply validation rules to existing documents during
Expand Down Expand Up @@ -135,6 +139,37 @@ pub enum ValidationAction {
Warn,
}

/// Specifies options for a clustered collection. Some fields have required values; the `Default`
/// impl uses those values.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ClusteredIndex {
/// Key pattern; currently required to be `{_id: 1}`.
pub key: Document,

/// Currently required to be `true`.
pub unique: bool,

/// Optional; will be automatically generated if not provided.
pub name: Option<String>,

/// Optional; currently must be `2` if provided.
#[serde(skip_serializing_if = "Option::is_none")]
pub v: Option<i32>,
}

impl Default for ClusteredIndex {
fn default() -> Self {
Self {
key: doc! { "_id": 1 },
unique: true,
name: None,
v: None,
}
}
}

/// Specifies default configuration for indexes created on a collection, including the _id index.
#[derive(Clone, Debug, TypedBuilder, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
Expand Down
14 changes: 14 additions & 0 deletions src/index/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,20 @@ pub struct IndexOptions {
/// A flag that determines whether the index is hidden from the query planner. A
/// hidden index is not evaluated as part of the query plan selection.
pub hidden: Option<bool>,

#[builder(default, setter(skip))]
clustered: Option<bool>,
}

impl IndexOptions {
/// Optionally specifies that this index is clustered. This is not a valid option to provide to
/// 'create_indexes', but can appear in the options returned for an index via 'list_indexes'.
/// To create a clustered index, create a new collection using the 'clustered_index' option.
///
/// This options is only supported by servers >= 6.0.
pub fn clustered(&self) -> Option<bool> {
self.clustered
}
}

/// The version of the index. Version 0 Indexes are disallowed as of MongoDB 3.2.
Expand Down
3 changes: 1 addition & 2 deletions src/test/spec/json/collection-management/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ 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.
in the `Unified Test Format <../../unified-test-format/unified-test-format.rst>`__.
291 changes: 291 additions & 0 deletions src/test/spec/json/collection-management/clustered-indexes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,291 @@
{
"description": "clustered-indexes",
"schemaVersion": "1.4",
"runOnRequirements": [
{
"minServerVersion": "5.3",
"serverless": "forbid"
}
],
"createEntities": [
{
"client": {
"id": "client0",
"observeEvents": [
"commandStartedEvent"
]
}
},
{
"database": {
"id": "database0",
"client": "client0",
"databaseName": "ci-tests"
}
},
{
"collection": {
"id": "collection0",
"database": "database0",
"collectionName": "test"
}
}
],
"initialData": [
{
"collectionName": "test",
"databaseName": "ci-tests",
"documents": []
}
],
"tests": [
{
"description": "createCollection with clusteredIndex",
"operations": [
{
"name": "dropCollection",
"object": "database0",
"arguments": {
"collection": "test"
}
},
{
"name": "createCollection",
"object": "database0",
"arguments": {
"collection": "test",
"clusteredIndex": {
"key": {
"_id": 1
},
"unique": true,
"name": "test index"
}
}
},
{
"name": "assertCollectionExists",
"object": "testRunner",
"arguments": {
"databaseName": "ci-tests",
"collectionName": "test"
}
}
],
"expectEvents": [
{
"client": "client0",
"events": [
{
"commandStartedEvent": {
"command": {
"drop": "test"
},
"databaseName": "ci-tests"
}
},
{
"commandStartedEvent": {
"command": {
"create": "test",
"clusteredIndex": {
"key": {
"_id": 1
},
"unique": true,
"name": "test index"
}
},
"databaseName": "ci-tests"
}
}
]
}
]
},
{
"description": "listCollections includes clusteredIndex",
"operations": [
{
"name": "dropCollection",
"object": "database0",
"arguments": {
"collection": "test"
}
},
{
"name": "createCollection",
"object": "database0",
"arguments": {
"collection": "test",
"clusteredIndex": {
"key": {
"_id": 1
},
"unique": true,
"name": "test index"
}
}
},
{
"name": "listCollections",
"object": "database0",
"arguments": {
"filter": {
"name": {
"$eq": "test"
}
}
},
"expectResult": [
{
"name": "test",
"options": {
"clusteredIndex": {
"key": {
"_id": 1
},
"unique": true,
"name": "test index",
"v": {
"$$type": [
"int",
"long"
]
}
}
}
}
]
}
],
"expectEvents": [
{
"client": "client0",
"events": [
{
"commandStartedEvent": {
"command": {
"drop": "test"
},
"databaseName": "ci-tests"
}
},
{
"commandStartedEvent": {
"command": {
"create": "test",
"clusteredIndex": {
"key": {
"_id": 1
},
"unique": true,
"name": "test index"
}
},
"databaseName": "ci-tests"
}
},
{
"commandStartedEvent": {
"command": {
"listCollections": 1,
"filter": {
"name": {
"$eq": "test"
}
}
},
"databaseName": "ci-tests"
}
}
]
}
]
},
{
"description": "listIndexes returns the index",
"operations": [
{
"name": "dropCollection",
"object": "database0",
"arguments": {
"collection": "test"
}
},
{
"name": "createCollection",
"object": "database0",
"arguments": {
"collection": "test",
"clusteredIndex": {
"key": {
"_id": 1
},
"unique": true,
"name": "test index"
}
}
},
{
"name": "listIndexes",
"object": "collection0",
"expectResult": [
{
"key": {
"_id": 1
},
"name": "test index",
"clustered": true,
"unique": true,
"v": {
"$$type": [
"int",
"long"
]
}
}
]
}
],
"expectEvents": [
{
"client": "client0",
"events": [
{
"commandStartedEvent": {
"command": {
"drop": "test"
},
"databaseName": "ci-tests"
}
},
{
"commandStartedEvent": {
"command": {
"create": "test",
"clusteredIndex": {
"key": {
"_id": 1
},
"unique": true,
"name": "test index"
}
},
"databaseName": "ci-tests"
}
},
{
"commandStartedEvent": {
"command": {
"listIndexes": "test"
},
"databaseName": "ci-tests"
}
}
]
}
]
}
]
}
Loading