pub struct InMemoryStorage { /* private fields */ }Expand description
In-memory storage with automatic spillover to tempfiles for large objects.
Thread-safe: uses DashMap for concurrent access. Objects larger than
the configured max_memory_size are transparently written to
temporary files and read back on demand.
§Examples
use bytes::Bytes;
use rustack_s3_core::storage::InMemoryStorage;
let storage = InMemoryStorage::new(1024);
let result = storage
.write_object("my-bucket", "hello.txt", "null", Bytes::from("hello"))
.await
.unwrap();
assert_eq!(result.size, 5);
let data = storage
.read_object("my-bucket", "hello.txt", "null", None)
.await
.unwrap();
assert_eq!(data.as_ref(), b"hello");Implementations§
Source§impl InMemoryStorage
impl InMemoryStorage
Sourcepub fn new(max_memory_size: usize) -> Self
pub fn new(max_memory_size: usize) -> Self
Create a new storage backend with the given memory threshold.
Objects larger than max_memory_size bytes are spilled to temporary
files on disk.
Sourcepub fn default_max_memory_size() -> usize
pub fn default_max_memory_size() -> usize
Return the default maximum in-memory object size (512 KiB).
Sourcepub async fn write_object(
&self,
bucket: &str,
key: &str,
version_id: &str,
data: Bytes,
) -> Result<WriteResult, S3ServiceError>
pub async fn write_object( &self, bucket: &str, key: &str, version_id: &str, data: Bytes, ) -> Result<WriteResult, S3ServiceError>
Store object data. Computes MD5 and returns a WriteResult.
If the data exceeds the configured memory threshold, it is spilled to a temporary file on disk.
§Errors
Returns S3ServiceError::Internal if the temporary file cannot be
created or written.
Sourcepub async fn read_object(
&self,
bucket: &str,
key: &str,
version_id: &str,
range: Option<(u64, u64)>,
) -> Result<Bytes, S3ServiceError>
pub async fn read_object( &self, bucket: &str, key: &str, version_id: &str, range: Option<(u64, u64)>, ) -> Result<Bytes, S3ServiceError>
Read object data. Returns the full Bytes for the object.
If range is specified as (start, end) (inclusive on both ends),
only that byte range is returned.
§Errors
S3ServiceError::NoSuchKeyif the object is not found.S3ServiceError::InvalidRangeif the range is out of bounds.S3ServiceError::Internalif the on-disk file cannot be read.
Sourcepub async fn copy_object(
&self,
src_bucket: &str,
src_key: &str,
src_version_id: &str,
dst_bucket: &str,
dst_key: &str,
dst_version_id: &str,
) -> Result<WriteResult, S3ServiceError>
pub async fn copy_object( &self, src_bucket: &str, src_key: &str, src_version_id: &str, dst_bucket: &str, dst_key: &str, dst_version_id: &str, ) -> Result<WriteResult, S3ServiceError>
Copy object data from one location to another.
Reads the source object data, then writes it to the destination
location. Returns a WriteResult for the destination object.
§Errors
S3ServiceError::NoSuchKeyif the source object is not found.S3ServiceError::Internalif disk I/O fails.
Sourcepub fn delete_object(&self, bucket: &str, key: &str, version_id: &str)
pub fn delete_object(&self, bucket: &str, key: &str, version_id: &str)
Delete object data.
Removes the stored data for the given object key. If the data was on
disk, the temporary file is cleaned up via the Drop implementation.
This is a no-op if the object does not exist.
Sourcepub async fn write_part(
&self,
bucket: &str,
upload_id: &str,
part_number: u32,
data: Bytes,
) -> Result<WriteResult, S3ServiceError>
pub async fn write_part( &self, bucket: &str, upload_id: &str, part_number: u32, data: Bytes, ) -> Result<WriteResult, S3ServiceError>
Store a multipart part.
Computes MD5 and returns a WriteResult. If the part data exceeds
the memory threshold, it is spilled to a temporary file.
§Errors
Returns S3ServiceError::Internal if the temporary file cannot be
created or written.
Sourcepub async fn read_part(
&self,
bucket: &str,
upload_id: &str,
part_number: u32,
) -> Result<Bytes, S3ServiceError>
pub async fn read_part( &self, bucket: &str, upload_id: &str, part_number: u32, ) -> Result<Bytes, S3ServiceError>
Read a multipart part’s data.
§Errors
S3ServiceError::InvalidPartif the part does not exist.S3ServiceError::Internalif the on-disk file cannot be read.
Sourcepub async fn complete_multipart(
&self,
bucket: &str,
upload_id: &str,
key: &str,
version_id: &str,
part_numbers: &[u32],
) -> Result<(WriteResult, Vec<String>), S3ServiceError>
pub async fn complete_multipart( &self, bucket: &str, upload_id: &str, key: &str, version_id: &str, part_numbers: &[u32], ) -> Result<(WriteResult, Vec<String>), S3ServiceError>
Assemble parts into a final object. Concatenates part data in order.
Returns a tuple of (WriteResult, Vec<String>) where the vector
contains the individual (unquoted) MD5 hex digests for each part.
The WriteResult::etag is a composite ETag in the format
"<md5>-<part_count>".
§Errors
S3ServiceError::InvalidPartif any requested part does not exist.S3ServiceError::Internalif disk I/O fails.
Sourcepub fn abort_multipart(&self, bucket: &str, upload_id: &str)
pub fn abort_multipart(&self, bucket: &str, upload_id: &str)
Delete all parts for a multipart upload.
Removes all stored part data associated with the given upload ID.
Temporary files are cleaned up automatically via Drop.
Sourcepub fn delete_bucket_data(&self, bucket: &str)
pub fn delete_bucket_data(&self, bucket: &str)
Delete all data (objects and parts) for a bucket.
This removes both object data and any in-progress multipart part data associated with the bucket.