use std::sync::Arc;
use futures::FutureExt;
use futures::TryFutureExt;
use futures::future::BoxFuture;
use futures::future::WeakShared;
use vortex_array::buffer::BufferHandle;
use vortex_error::SharedVortexResult;
use vortex_error::VortexError;
use vortex_error::VortexExpect;
use vortex_utils::aliases::dash_map::DashMap;
use vortex_utils::aliases::dash_map::Entry;
use super::SegmentFuture;
use super::SegmentId;
use super::SegmentSource;
pub struct SharedSegmentSource<S> {
inner: S,
in_flight: DashMap<SegmentId, WeakShared<SharedSegmentFuture>>,
}
type SharedSegmentFuture = BoxFuture<'static, SharedVortexResult<BufferHandle>>;
impl<S: SegmentSource> SharedSegmentSource<S> {
pub fn new(inner: S) -> Self {
Self {
inner,
in_flight: DashMap::with_hasher_and_shard_amount(Default::default(), 64),
}
}
}
impl<S: SegmentSource> SegmentSource for SharedSegmentSource<S> {
fn request(&self, id: SegmentId) -> SegmentFuture {
loop {
match self.in_flight.entry(id) {
Entry::Occupied(e) => {
if let Some(shared_future) = e.get().upgrade() {
return shared_future.map_err(VortexError::from).boxed();
} else {
e.remove();
}
}
Entry::Vacant(e) => {
let future = self.inner.request(id).map_err(Arc::new).boxed().shared();
e.insert(
future
.downgrade()
.vortex_expect("just created, cannot be polled to completion"),
);
return future.map_err(VortexError::from).boxed();
}
}
}
}
}