Skip to content

Commit bbbc867

Browse files
RUST-994 Only return index names from CreateIndex(es)Result (#442)
1 parent 17c0f87 commit bbbc867

File tree

3 files changed

+5
-103
lines changed

3 files changed

+5
-103
lines changed

src/operation/create_indexes/mod.rs

Lines changed: 5 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ mod test;
44
use crate::{
55
bson::{doc, Document},
66
cmap::{Command, StreamDescription},
7-
coll::options::CommitQuorum,
87
error::Result,
98
index::IndexModel,
109
operation::{append_options, Operation},
@@ -13,12 +12,7 @@ use crate::{
1312
Namespace,
1413
};
1514

16-
use super::CommandResponse;
17-
use serde::{
18-
de::{Error, Unexpected},
19-
Deserialize,
20-
Deserializer,
21-
};
15+
use super::{CommandResponse, WriteConcernOnlyBody};
2216

2317
#[derive(Debug)]
2418
pub(crate) struct CreateIndexes {
@@ -56,7 +50,7 @@ impl CreateIndexes {
5650
impl Operation for CreateIndexes {
5751
type O = CreateIndexesResult;
5852
type Command = Document;
59-
type Response = CommandResponse<Response>;
53+
type Response = CommandResponse<WriteConcernOnlyBody>;
6054
const NAME: &'static str = "createIndexes";
6155

6256
fn build(&mut self, _description: &StreamDescription) -> Result<Command> {
@@ -77,19 +71,12 @@ impl Operation for CreateIndexes {
7771

7872
fn handle_response(
7973
&self,
80-
response: Response,
74+
response: WriteConcernOnlyBody,
8175
_description: &StreamDescription,
8276
) -> Result<Self::O> {
77+
response.validate()?;
8378
let index_names = self.indexes.iter().filter_map(|i| i.get_name()).collect();
84-
let Response(response) = response;
85-
Ok(CreateIndexesResult {
86-
index_names,
87-
created_collection_automatically: response.created_collection_automatically,
88-
num_indexes_before: response.num_indexes_before,
89-
num_indexes_after: response.num_indexes_after,
90-
note: response.note,
91-
commit_quorum: response.commit_quorum,
92-
})
79+
Ok(CreateIndexesResult { index_names })
9380
}
9481

9582
fn write_concern(&self) -> Option<&WriteConcern> {
@@ -98,47 +85,3 @@ impl Operation for CreateIndexes {
9885
.and_then(|opts| opts.write_concern.as_ref())
9986
}
10087
}
101-
#[derive(Debug, Deserialize)]
102-
#[serde(rename_all = "camelCase")]
103-
struct ResponseBody {
104-
created_collection_automatically: Option<bool>,
105-
num_indexes_before: u32,
106-
num_indexes_after: u32,
107-
note: Option<String>,
108-
commit_quorum: Option<CommitQuorum>,
109-
}
110-
111-
#[derive(Debug)]
112-
pub(crate) struct Response(ResponseBody);
113-
114-
impl<'de> Deserialize<'de> for Response {
115-
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
116-
where
117-
D: Deserializer<'de>,
118-
{
119-
#[derive(Deserialize)]
120-
#[serde(untagged)]
121-
enum ResponseHelper {
122-
PlainResponse(ResponseBody),
123-
ShardedResponse { raw: Document },
124-
}
125-
126-
match ResponseHelper::deserialize(deserializer)? {
127-
ResponseHelper::PlainResponse(body) => Ok(Response(body)),
128-
ResponseHelper::ShardedResponse { raw } => {
129-
let len = raw.values().count();
130-
if len != 1 {
131-
return Err(Error::invalid_length(len, &"a single result"));
132-
}
133-
134-
let (_, v) = raw.into_iter().next().unwrap(); // Safe unwrap because of length check above.
135-
bson::from_bson(v).map(Response).map_err(|_| {
136-
Error::invalid_type(
137-
Unexpected::Other("Unknown bson"),
138-
&"a createIndexes response",
139-
)
140-
})
141-
}
142-
}
143-
}
144-
}

src/operation/create_indexes/test.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,6 @@ async fn handle_success() {
8181

8282
let expected_values = CreateIndexesResult {
8383
index_names: vec!["a".to_string(), "b".to_string()],
84-
created_collection_automatically: Some(false),
85-
num_indexes_before: 1,
86-
num_indexes_after: 3,
87-
note: None,
88-
commit_quorum: Some(CommitQuorum::VotingMembers),
8984
};
9085
let actual_values = handle_response_test(&op, response).unwrap();
9186
assert_eq!(actual_values, expected_values);

src/results.rs

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::collections::{HashMap, VecDeque};
55
use crate::{
66
bson::{Bson, Document},
77
bson_util,
8-
coll::options::CommitQuorum,
98
db::options::CreateCollectionOptions,
109
};
1110

@@ -84,21 +83,6 @@ pub struct DeleteResult {
8483
pub struct CreateIndexResult {
8584
/// The name of the index created in the `createIndex` command.
8685
pub index_name: String,
87-
88-
/// Whether or not the collection was created implicitly by the command.
89-
pub created_collection_automatically: Option<bool>,
90-
91-
/// The number of indexes on the collection prior to running the command.
92-
pub num_indexes_before: u32,
93-
94-
/// The number of indexes on the collection after the command was run.
95-
pub num_indexes_after: u32,
96-
97-
/// Any information pertinent to the creation of the indexes. For more information, see the [documentation](https://docs.mongodb.com/manual/reference/command/createIndexes/#output).
98-
pub note: Option<String>,
99-
100-
/// The commit quorum for the operation.
101-
pub commit_quorum: Option<CommitQuorum>,
10286
}
10387

10488
/// Information about the indexes created as a result of a
@@ -108,32 +92,12 @@ pub struct CreateIndexResult {
10892
pub struct CreateIndexesResult {
10993
/// The list containing the names of all indexes created in the `createIndexes` command.
11094
pub index_names: Vec<String>,
111-
112-
/// Whether or not the collection was created implicitly by the command.
113-
pub created_collection_automatically: Option<bool>,
114-
115-
/// The number of indexes on the collection prior to running the command.
116-
pub num_indexes_before: u32,
117-
118-
/// The number of indexes on the collection after the command was run.
119-
pub num_indexes_after: u32,
120-
121-
/// Any information pertinent to the creation of the indexes. For more information, see the [documentation](https://docs.mongodb.com/manual/reference/command/createIndexes/#output).
122-
pub note: Option<String>,
123-
124-
/// The commit quorum for the operation.
125-
pub commit_quorum: Option<CommitQuorum>,
12695
}
12796

12897
impl CreateIndexesResult {
12998
pub(crate) fn into_create_index_result(self) -> CreateIndexResult {
13099
CreateIndexResult {
131100
index_name: self.index_names.into_iter().next().unwrap(),
132-
created_collection_automatically: self.created_collection_automatically,
133-
num_indexes_before: self.num_indexes_before,
134-
num_indexes_after: self.num_indexes_after,
135-
note: self.note,
136-
commit_quorum: self.commit_quorum,
137101
}
138102
}
139103
}

0 commit comments

Comments
 (0)