mod bulk_write;
use std::collections::{HashMap, VecDeque};
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use crate::{
bson::{serde_helpers, Binary, Bson, Document, RawDocumentBuf},
change_stream::event::ResumeToken,
db::options::CreateCollectionOptions,
serde_util,
Namespace,
};
pub use bulk_write::*;
#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct InsertOneResult {
pub inserted_id: Bson,
}
impl InsertOneResult {
pub(crate) fn from_insert_many_result(result: InsertManyResult) -> Self {
Self {
inserted_id: result.inserted_ids.get(&0).cloned().unwrap_or(Bson::Null),
}
}
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct InsertManyResult {
pub inserted_ids: HashMap<usize, Bson>,
}
impl InsertManyResult {
pub(crate) fn new() -> Self {
InsertManyResult {
inserted_ids: HashMap::new(),
}
}
}
#[skip_serializing_none]
#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct UpdateResult {
#[serde(serialize_with = "crate::bson::serde_helpers::serialize_u64_as_i64")]
pub matched_count: u64,
#[serde(serialize_with = "crate::bson::serde_helpers::serialize_u64_as_i64")]
pub modified_count: u64,
pub upserted_id: Option<Bson>,
}
#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct DeleteResult {
#[serde(serialize_with = "crate::bson::serde_helpers::serialize_u64_as_i64")]
pub deleted_count: u64,
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct CreateIndexResult {
pub index_name: String,
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct CreateIndexesResult {
pub index_names: Vec<String>,
}
impl CreateIndexesResult {
pub(crate) fn into_create_index_result(self) -> CreateIndexResult {
CreateIndexResult {
index_name: self.index_names.into_iter().next().unwrap(),
}
}
}
#[derive(Debug, Clone)]
pub(crate) struct GetMoreResult {
pub(crate) batch: VecDeque<RawDocumentBuf>,
pub(crate) exhausted: bool,
pub(crate) post_batch_resume_token: Option<ResumeToken>,
pub(crate) ns: Namespace,
pub(crate) id: i64,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub enum CollectionType {
View,
Collection,
Timeseries,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct CollectionSpecificationInfo {
pub read_only: bool,
pub uuid: Option<Binary>,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct CollectionSpecification {
pub name: String,
#[serde(rename = "type")]
pub collection_type: CollectionType,
pub options: CreateCollectionOptions,
pub info: CollectionSpecificationInfo,
pub id_index: Option<Document>,
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct DatabaseSpecification {
pub name: String,
#[serde(
deserialize_with = "serde_util::deserialize_u64_from_bson_number",
serialize_with = "serde_helpers::serialize_u64_as_i64"
)]
pub size_on_disk: u64,
pub empty: bool,
pub shards: Option<Document>,
}