Skip to content

Commit

Permalink
Add more docs
Browse files Browse the repository at this point in the history
  • Loading branch information
tustvold committed Apr 9, 2024
1 parent 4f9c5c7 commit ff4618a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
46 changes: 44 additions & 2 deletions object_store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@
//! # }
//! ```
//!
//! # Put Object
//! # Put Object
//!
//! Use the [`ObjectStore::put`] method to atomically write data.
//!
Expand All @@ -266,7 +266,7 @@
//! # }
//! ```
//!
//! # Multipart Upload
//! # Multipart Upload
//!
//! Use the [`ObjectStore::put_multipart`] method to atomically write a large amount of data
//!
Expand Down Expand Up @@ -319,6 +319,48 @@
//! # }
//! ```
//!
//! # Vectored Write
//!
//! When writing data it is often the case that the size of the output is not known ahead of time.
//!
//! A common approach to handling this is to bump-allocate a `Vec`, whereby the underlying
//! allocation is repeatedly reallocated, each time doubling the capacity. The performance of
//! this is suboptimal as reallocating memory will often involve copying it to a new location.
//!
//! Fortunately, as [`PutPayload`] does not require memory regions to be contiguous, it is
//! possible to instead allocate memory in chunks and avoid bump allocating. [`PutPayloadMut`]
//! encapsulates this approach
//!
//! ```
//! # use object_store::local::LocalFileSystem;
//! # use object_store::{ObjectStore, PutPayloadMut};
//! # use std::sync::Arc;
//! # use bytes::Bytes;
//! # use tokio::io::AsyncWriteExt;
//! # use object_store::path::Path;
//! # fn get_object_store() -> Arc<dyn ObjectStore> {
//! # Arc::new(LocalFileSystem::new())
//! # }
//! # async fn multi_upload() {
//! #
//! let object_store: Arc<dyn ObjectStore> = get_object_store();
//! let path = Path::from("data/large_file");
//! let mut buffer = PutPayloadMut::new().with_block_size(8192);
//! for _ in 0..22 {
//! buffer.extend_from_slice(&[0; 1024]);
//! }
//! let payload = buffer.freeze();
//!
//! // Payload consists of 3 separate 8KB allocations
//! assert_eq!(payload.as_ref().len(), 3);
//! assert_eq!(payload.as_ref()[0].len(), 8192);
//! assert_eq!(payload.as_ref()[1].len(), 8192);
//! assert_eq!(payload.as_ref()[2].len(), 6144);
//!
//! object_store.put(&path, payload).await.unwrap();
//! # }
//! ```
//!
//! # Conditional Fetch
//!
//! More complex object retrieval can be supported by [`ObjectStore::get_opts`].
Expand Down
14 changes: 7 additions & 7 deletions object_store/src/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ pub struct PutPayloadMut {
len: usize,
completed: Vec<Bytes>,
in_progress: Vec<u8>,
min_alloc: usize,
block_size: usize,
}

impl Default for PutPayloadMut {
Expand All @@ -199,7 +199,7 @@ impl Default for PutPayloadMut {
completed: vec![],
in_progress: vec![],

min_alloc: 8 * 1024,
block_size: 8 * 1024,
}
}
}
Expand All @@ -210,11 +210,11 @@ impl PutPayloadMut {
Self::default()
}

/// Override the minimum allocation size
/// Allocate data in chunks of `block_size`
///
/// Defaults to 8KB
pub fn with_minimum_allocation_size(self, min_alloc: usize) -> Self {
Self { min_alloc, ..self }
pub fn with_block_size(self, block_size: usize) -> Self {
Self { block_size, ..self }
}

/// Write bytes into this [`PutPayloadMut`]
Expand All @@ -224,7 +224,7 @@ impl PutPayloadMut {

self.in_progress.extend_from_slice(&slice[..to_copy]);
if self.in_progress.capacity() == self.in_progress.len() {
let new_cap = self.min_alloc.max(slice.len() - to_copy);
let new_cap = self.block_size.max(slice.len() - to_copy);
let completed = std::mem::replace(&mut self.in_progress, Vec::with_capacity(new_cap));
if !completed.is_empty() {
self.completed.push(completed.into())
Expand Down Expand Up @@ -277,7 +277,7 @@ mod test {

#[test]
fn test_put_payload() {
let mut chunk = PutPayloadMut::new().with_minimum_allocation_size(23);
let mut chunk = PutPayloadMut::new().with_block_size(23);
chunk.extend_from_slice(&[1; 16]);
chunk.extend_from_slice(&[2; 32]);
chunk.extend_from_slice(&[2; 5]);
Expand Down

0 comments on commit ff4618a

Please sign in to comment.