-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy pathgridfs.rs
200 lines (170 loc) · 6.21 KB
/
gridfs.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//! Contains the functionality for GridFS operations.
mod download;
pub(crate) mod options;
mod upload;
use std::sync::{atomic::AtomicBool, Arc};
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use crate::{
bson::{doc, oid::ObjectId, Bson, DateTime, Document, RawBinaryRef},
checked::Checked,
error::Error,
options::{CollectionOptions, ReadConcern, SelectionCriteria, WriteConcern},
Collection,
Database,
};
pub use download::GridFsDownloadStream;
pub(crate) use options::*;
pub use upload::GridFsUploadStream;
const DEFAULT_BUCKET_NAME: &str = "fs";
const DEFAULT_CHUNK_SIZE_BYTES: u32 = 255 * 1024;
/// A model for the documents stored in the chunks collection.
#[derive(Debug, Deserialize, Serialize)]
pub(crate) struct Chunk<'a> {
#[serde(rename = "_id")]
id: ObjectId,
files_id: Bson,
#[serde(serialize_with = "bson::serde_helpers::serialize_u32_as_i32")]
n: u32,
#[serde(borrow)]
data: RawBinaryRef<'a>,
}
/// A model for the documents stored in a GridFS bucket's files
/// collection.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[skip_serializing_none]
#[non_exhaustive]
pub struct FilesCollectionDocument {
/// The file's unique identifier.
#[serde(rename = "_id")]
pub id: Bson,
/// The length of the file in bytes.
pub length: u64,
/// The size of the file's chunks in bytes.
#[serde(
rename = "chunkSize",
serialize_with = "bson::serde_helpers::serialize_u32_as_i32"
)]
pub chunk_size_bytes: u32,
/// The time at which the file was uploaded.
pub upload_date: DateTime,
/// The name of the file.
pub filename: Option<String>,
/// User-provided metadata associated with the file.
pub metadata: Option<Document>,
}
impl FilesCollectionDocument {
/// Returns the total number of chunks expected to be in the file.
fn n(&self) -> u32 {
Self::n_from_vals(self.length, self.chunk_size_bytes)
}
fn n_from_vals(length: u64, chunk_size_bytes: u32) -> u32 {
let chunk_size_bytes = chunk_size_bytes as u64;
let n = Checked::new(length) / chunk_size_bytes + u64::from(length % chunk_size_bytes != 0);
n.try_into().unwrap()
}
fn expected_chunk_length_from_vals(length: u64, chunk_size_bytes: u32, n: u32) -> u32 {
let remainder = length % (chunk_size_bytes as u64);
if n == Self::n_from_vals(length, chunk_size_bytes) - 1 && remainder != 0 {
Checked::new(remainder).try_into().unwrap()
} else {
chunk_size_bytes
}
}
}
#[derive(Debug)]
struct GridFsBucketInner {
db: Database,
options: GridFsBucketOptions,
files: Collection<FilesCollectionDocument>,
chunks: Collection<Chunk<'static>>,
created_indexes: AtomicBool,
}
/// A `GridFsBucket` provides the functionality for storing and retrieving binary BSON data that
/// exceeds the 16 MiB size limit of a MongoDB document. Users may upload and download large amounts
/// of data, called files, to the bucket. When a file is uploaded, its contents are divided into
/// chunks and stored in a chunks collection. A corresponding [`FilesCollectionDocument`] is also
/// stored in a files collection. When a user downloads a file, the bucket finds and returns the
/// data stored in its chunks.
///
/// `GridFsBucket` uses [`std::sync::Arc`] internally, so it can be shared safely across threads or
/// async tasks.
#[derive(Debug, Clone)]
pub struct GridFsBucket {
inner: Arc<GridFsBucketInner>,
}
impl GridFsBucket {
pub(crate) fn new(db: Database, mut options: GridFsBucketOptions) -> GridFsBucket {
if options.read_concern.is_none() {
options.read_concern = db.read_concern().cloned();
}
if options.write_concern.is_none() {
options.write_concern = db.write_concern().cloned();
}
if options.selection_criteria.is_none() {
options.selection_criteria = db.selection_criteria().cloned();
}
let bucket_name = options
.bucket_name
.as_deref()
.unwrap_or(DEFAULT_BUCKET_NAME);
let collection_options = CollectionOptions::builder()
.read_concern(options.read_concern.clone())
.write_concern(options.write_concern.clone())
.selection_criteria(options.selection_criteria.clone())
.build();
let files = db.collection_with_options::<FilesCollectionDocument>(
&format!("{}.files", bucket_name),
collection_options.clone(),
);
let chunks = db.collection_with_options::<Chunk>(
&format!("{}.chunks", bucket_name),
collection_options,
);
GridFsBucket {
inner: Arc::new(GridFsBucketInner {
db: db.clone(),
options,
files,
chunks,
created_indexes: AtomicBool::new(false),
}),
}
}
pub(crate) fn client(&self) -> &crate::Client {
self.inner.files.client()
}
/// Gets the read concern of the bucket.
pub fn read_concern(&self) -> Option<&ReadConcern> {
self.inner.options.read_concern.as_ref()
}
/// Gets the write concern of the bucket.
pub fn write_concern(&self) -> Option<&WriteConcern> {
self.inner.options.write_concern.as_ref()
}
/// Gets the selection criteria of the bucket.
pub fn selection_criteria(&self) -> Option<&SelectionCriteria> {
self.inner.options.selection_criteria.as_ref()
}
/// Gets the chunk size in bytes for the bucket.
pub(crate) fn chunk_size_bytes(&self) -> u32 {
self.inner
.options
.chunk_size_bytes
.unwrap_or(DEFAULT_CHUNK_SIZE_BYTES)
}
/// Gets a handle to the files collection for the bucket.
pub(crate) fn files(&self) -> &Collection<FilesCollectionDocument> {
&self.inner.files
}
/// Gets a handle to the chunks collection for the bucket.
pub(crate) fn chunks(&self) -> &Collection<Chunk<'static>> {
&self.inner.chunks
}
}
impl Error {
fn into_futures_io_error(self) -> futures_io::Error {
futures_io::Error::new(futures_io::ErrorKind::Other, self)
}
}