use std::path::PathBuf;
use rayon::ThreadPool;
use rayon::prelude::*;
use crate::segment::data_types::load_profile::LoadProfile;
use crate::segment::index::UniversalReadExt;
use crate::segment::segment::read_only::ReadOnlySegment;
use uuid::Uuid;
pub(crate) fn load_segments_parallel<S>(
pool: &ThreadPool,
fs: &S::Fs,
segments: impl IntoIterator<Item = (Uuid, PathBuf)>,
load_profile: Option<&LoadProfile>,
) -> Vec<(Uuid, ReadOnlySegment<S>)>
where
S: UniversalReadExt + 'static,
S::Fs: Send + Sync + Clone + 'static,
{
let segments: Vec<(Uuid, PathBuf)> = segments.into_iter().collect();
pool.install(|| {
segments
.into_par_iter()
.filter_map(|(uuid, segment_path)| {
match ReadOnlySegment::<S>::open(fs, &segment_path, uuid, None, load_profile) {
Ok(segment) => Some((uuid, segment)),
Err(err) => {
log::warn!("read-only open: skipping unloadable segment {uuid}: {err}");
None
}
}
})
.collect()
})
}