use std::default::Default;
use std::fmt;
use std::path::Path;
use std::pin::Pin;
use bytes::{Buf, Bytes};
use crate::io::path_body::PathBody;
use crate::io::path_body::PathBodyBuilder;
use crate::io::size_hint::SizeHint;
use crate::io::PartBuffer;
use crate::memory::BufferPool;
use crate::memory::SegmentedBytes;
#[derive(Debug)]
pub struct InputStream {
pub(super) inner: RawInputStream,
}
impl InputStream {
pub fn from_static(bytes: &'static [u8]) -> Self {
let inner = RawInputStream::Buf(bytes.into());
Self { inner }
}
pub fn size_hint(&self) -> SizeHint {
self.inner.size_hint()
}
pub fn read_from() -> PathBodyBuilder {
PathBodyBuilder::new()
}
pub fn from_path(path: impl AsRef<Path>) -> Result<InputStream, crate::error::Error> {
Self::read_from().path(path).build()
}
pub(crate) fn is_file_backed(&self) -> bool {
matches!(self.inner, RawInputStream::Fs(_))
}
pub(crate) fn check_file_type(&self) -> Result<(), crate::error::Error> {
match &self.inner {
RawInputStream::Fs(path_body) => path_body.check_file_type(),
RawInputStream::Buf(_) | RawInputStream::Dyn(_) => Ok(()),
}
}
pub(crate) fn into_sdk_body(
self,
direct_io: bool,
buffer_pool: BufferPool,
) -> Result<aws_smithy_types::body::SdkBody, crate::error::Error> {
use crate::operation::upload::file_body::{
DirectFileBody, FileBodySource, OffloadedFileBody,
};
use aws_smithy_types::body::SdkBody;
match self.inner {
RawInputStream::Buf(bytes) => Ok(SdkBody::from(bytes)),
RawInputStream::Fs(path_body) => {
let source = FileBodySource::new(path_body.open()?, buffer_pool);
let offset = path_body.offset;
let length = path_body.length;
if direct_io {
Ok(SdkBody::retryable(move || {
SdkBody::from_body_1_x(DirectFileBody::new(source.clone(), offset, length))
}))
} else {
let reader_runtime = tokio::runtime::Handle::current();
Ok(SdkBody::retryable(move || {
SdkBody::from_body_1_x(OffloadedFileBody::new(
source.clone(),
offset,
length,
reader_runtime.clone(),
))
}))
}
}
RawInputStream::Dyn(_) => panic!(
"InputStream::into_sdk_body called on Dyn stream; \
Dyn sources must route through the multipart upload path \
(is_mpu_only() should have been checked upstream)"
),
}
}
pub(crate) fn is_mpu_only(&self) -> bool {
matches!(self.inner, RawInputStream::Dyn(_))
}
pub fn from_part_stream<T: PartStream + Send + Sync + 'static>(stream: T) -> Self {
let inner = RawInputStream::Dyn(BoxStream::new(stream));
Self { inner }
}
}
#[derive(Debug)]
pub(super) enum RawInputStream {
Buf(Bytes),
Fs(PathBody),
Dyn(BoxStream),
}
#[derive(Debug)]
pub struct StreamContext {
part_size: usize,
buffer_pool: BufferPool,
direct_io: bool,
metrics: std::sync::Arc<crate::transfer::MetricsState>,
telemetry: std::sync::Arc<crate::telemetry::Telemetry>,
}
impl StreamContext {
pub(super) fn new(
part_size: usize,
buffer_pool: BufferPool,
direct_io: bool,
metrics: std::sync::Arc<crate::transfer::MetricsState>,
telemetry: std::sync::Arc<crate::telemetry::Telemetry>,
) -> Self {
Self {
part_size,
buffer_pool,
direct_io,
metrics,
telemetry,
}
}
pub fn part_size(&self) -> usize {
self.part_size
}
pub(crate) fn part_buffer(&self) -> PartBuffer {
PartBuffer::new(&self.buffer_pool, self.part_size)
}
pub(crate) fn direct_io(&self) -> bool {
self.direct_io
}
pub(crate) fn record_io(&self, sample: &crate::metrics::IoSample) {
self.metrics.record_io(sample);
self.telemetry.io_counters.record(sample);
}
}
#[derive(Clone, PartialEq, Eq)]
pub struct PartData {
pub(crate) part_number: u64,
pub(crate) data: SegmentedBytes,
pub(crate) checksum: Option<String>,
}
impl std::fmt::Debug for PartData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("PartData")
.field("part_number", &self.part_number)
.field("data_len", &self.data.len())
.field("checksum", &self.checksum)
.finish()
}
}
impl PartData {
pub fn new(part_number: u64, data: impl Into<Bytes>) -> Self {
Self::from_segmented(part_number, SegmentedBytes::from(data.into()))
}
pub fn from_segmented(part_number: u64, data: SegmentedBytes) -> Self {
Self {
part_number,
data,
checksum: None,
}
}
pub fn with_checksum(mut self, checksum: impl Into<String>) -> Self {
self.checksum = Some(checksum.into());
self
}
}
pub trait PartStream {
fn poll_part(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
stream_cx: &StreamContext,
) -> std::task::Poll<Option<std::io::Result<PartData>>>;
fn size_hint(&self) -> crate::io::SizeHint;
fn full_object_checksum(&self) -> Option<String> {
None
}
}
pub(crate) struct BoxStream {
inner: Pin<Box<dyn PartStream + Send + Sync + 'static>>,
}
impl BoxStream {
fn new<T: PartStream + Send + Sync + 'static>(inner: T) -> Self {
BoxStream {
inner: Box::pin(inner),
}
}
pub(crate) fn poll_next(
&mut self,
cx: &mut std::task::Context<'_>,
stream_cx: &StreamContext,
) -> std::task::Poll<Option<std::io::Result<PartData>>> {
self.inner.as_mut().poll_part(cx, stream_cx)
}
pub(crate) fn full_object_checksum(&self) -> Option<String> {
self.inner.full_object_checksum()
}
}
impl fmt::Debug for BoxStream {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("BoxStream(dyn PartStream)").finish()
}
}
impl RawInputStream {
pub(super) fn size_hint(&self) -> SizeHint {
match self {
RawInputStream::Buf(bytes) => SizeHint::exact(bytes.remaining() as u64),
RawInputStream::Fs(path_body) => SizeHint::exact(path_body.length),
RawInputStream::Dyn(box_body) => box_body.inner.size_hint(),
}
}
}
impl Default for InputStream {
fn default() -> Self {
Self {
inner: RawInputStream::Buf(Bytes::default()),
}
}
}
impl From<Bytes> for InputStream {
fn from(value: Bytes) -> Self {
Self {
inner: RawInputStream::Buf(value),
}
}
}
impl From<Vec<u8>> for InputStream {
fn from(value: Vec<u8>) -> Self {
Self::from(Bytes::from(value))
}
}
impl From<&'static [u8]> for InputStream {
fn from(slice: &'static [u8]) -> InputStream {
Self::from(Bytes::from_static(slice))
}
}
impl From<&'static str> for InputStream {
fn from(slice: &'static str) -> InputStream {
Self::from(Bytes::from_static(slice.as_bytes()))
}
}