Skip to content

RUST-527 - Implementation of gridfs #235

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

Closed
wants to merge 1 commit into from
Closed
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: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ default = ["tokio-runtime"]
tokio-runtime = ["tokio/dns", "tokio/macros", "tokio/rt-core", "tokio/tcp", "tokio/rt-threaded", "tokio/time", "reqwest", "serde_bytes"]
async-std-runtime = ["async-std", "async-std/attributes"]
sync = ["async-std-runtime"]
gridfs = []

[dependencies]
async-trait = "0.1.24"
Expand Down
34 changes: 30 additions & 4 deletions src/bson_util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub(crate) mod async_encoding;

use std::time::Duration;
use std::{time::Duration, convert::TryInto};

use serde::{ser, Deserialize, Deserializer, Serialize, Serializer};

Expand Down Expand Up @@ -87,14 +87,40 @@ where
}

#[allow(clippy::trivially_copy_pass_by_ref)]
pub(crate) fn serialize_u32_as_i32<S: Serializer>(
pub(crate) fn serialize_option_u32_as_i32<S: Serializer>(
val: &Option<u32>,
serializer: S,
) -> std::result::Result<S::Ok, S::Error> {
match val {
Some(val) if { *val <= std::i32::MAX as u32 } => serializer.serialize_i32(*val as i32),
Some(val) => serialize_u32_as_i32(val, serializer),
None => serializer.serialize_none(),
_ => Err(ser::Error::custom("u32 specified does not fit into an i32")),
}
}

#[allow(clippy::trivially_copy_pass_by_ref)]
pub(crate) fn serialize_u32_as_i32<S: Serializer>(
val: &u32,
serializer: S,
) -> std::result::Result<S::Ok, S::Error> {
if let Ok(val) = (*val).try_into() {
serializer.serialize_i32(val)
}
else {
Err(ser::Error::custom("u32 specified does not fit into an i32"))
}
}

#[allow(clippy::trivially_copy_pass_by_ref)]
#[cfg(feature = "gridfs")]
pub(crate) fn serialize_u64_as_i64<S: Serializer>(
val: &u64,
serializer: S,
) -> std::result::Result<S::Ok, S::Error> {
if let Ok(val) = (*val).try_into() {
serializer.serialize_i64(val)
}
else {
Err(ser::Error::custom("u32 specified does not fit into an i32"))
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/coll/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
deserialize_duration_from_u64_millis,
serialize_batch_size,
serialize_duration_as_int_millis,
serialize_u32_as_i32,
serialize_option_u32_as_i32,
},
concern::{ReadConcern, WriteConcern},
options::Collation,
Expand Down Expand Up @@ -651,7 +651,7 @@ pub struct FindOptions {
/// number of round trips needed to return the entire set of documents returned by the
/// query.
#[builder(default)]
#[serde(serialize_with = "serialize_u32_as_i32")]
#[serde(serialize_with = "serialize_option_u32_as_i32")]
pub batch_size: Option<u32>,

/// Tags the query with an arbitrary string to help trace the operation through the database
Expand Down
58 changes: 58 additions & 0 deletions src/gridfs/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/// This follow officiel [specification](https://github.com/mongodb/specifications/blob/master/source/gridfs/gridfs-spec.rst)
/// If you found bug according to the specification, please report bug
use serde::{Deserialize, Serialize};

use crate::{
bson::{oid::ObjectId, DateTime, Document},
bson_util::{serialize_u32_as_i32, serialize_u64_as_i64},
};

/// This represent a chunk of a file
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct Chunk {
/// a unique ID for this document of type BSON ObjectId
#[serde(rename = "_id")]
id: ObjectId,
/// the id for this file (the _id from the files collection document). This field takes the
/// type of the corresponding _id in the files collection.
files_id: ObjectId,
/// the index number of this chunk, zero-based.
#[serde(serialize_with = "serialize_u32_as_i32")]
n: u32,
/// a chunk of data from the user file
#[serde(with = "serde_bytes")]
data: Vec<u8>,
}

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct MetaChunk {
/// a unique ID for this document. Usually this will be of type ObjectId, but a custom _id
/// value provided by the application may be of any type.
#[serde(rename = "_id")]
id: ObjectId,
/// the length of this stored file, in bytes
#[serde(serialize_with = "serialize_u64_as_i64")]
length: u64,
/// the size, in bytes, of each data chunk of this file. This value is configurable by file.
/// The default is 255 KiB.
chunk_size: u32,
/// the date and time this file was added to GridFS, stored as a BSON datetime value.
/// The value of this field MUST be the datetime when the upload completed, not the datetime
/// when it was begun.
upload_date: DateTime,
/// DEPRECATED, a hash of the contents of the stored file
#[deprecated]
md5: String,
/// the name of this stored file; this does not need to be unique
filename: String,
/// DEPRECATED, any MIME type, for application use only
#[deprecated]
content_type: String,
/// DEPRECATED, for application use only
#[deprecated]
aliases: Vec<String>,
/// any additional application data the user wishes to store
metadata: Document,
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,6 @@ pub(crate) static RUNTIME: runtime::AsyncRuntime = runtime::AsyncRuntime::Tokio;

#[cfg(all(not(feature = "tokio-runtime"), feature = "async-std-runtime"))]
pub(crate) static RUNTIME: runtime::AsyncRuntime = runtime::AsyncRuntime::AsyncStd;

#[cfg(feature = "gridfs")]
mod gridfs;