use std::path::{Path, PathBuf};
use crate::common::universal_io::{MmapFile, MmapFs, UniversalRead, UniversalReadFs};
use parking_lot::RwLock;
use rayon::prelude::*;
use crate::segment::common::operation_error::OperationResult;
use crate::segment::segment::update_only::UpdateOnlySegment;
use uuid::Uuid;
use crate::edge::read_only::{LocalSegmentEnumerator, SegmentEnumerator};
use crate::edge::read_view::build_segment_pool;
use crate::edge::update_only::UpdateOnlyEdgeShard;
use crate::edge::update_only::holder::UpdateOnlySegmentHolder;
impl UpdateOnlyEdgeShard<MmapFile> {
pub fn open_mmap(path: &Path) -> OperationResult<Self> {
Self::open(MmapFs, path, LocalSegmentEnumerator::new(path))
}
}
impl<S: UniversalRead + 'static> UpdateOnlyEdgeShard<S> {
pub fn open(
fs: S::Fs,
path: &Path,
enumerator: impl SegmentEnumerator + 'static,
) -> OperationResult<Self>
where
S::Fs: UniversalReadFs<File = S>,
{
let pool = build_segment_pool(
"edge-update",
crate::common::defaults::search_thread_count(0),
None,
)?;
let segments: Vec<(Uuid, PathBuf)> = enumerator.list_segments()?.into_iter().collect();
let opened: Vec<(Uuid, UpdateOnlySegment<S>)> = pool.install(|| {
segments
.into_par_iter()
.map(|(uuid, segment_path)| {
let segment = UpdateOnlySegment::<S>::open(&fs, &segment_path, uuid, None)?;
Ok((uuid, segment))
})
.collect::<OperationResult<Vec<_>>>()
})?;
let mut holder = UpdateOnlySegmentHolder::default();
for (uuid, segment) in opened {
holder.insert(uuid, segment);
}
if holder.is_empty() {
todo!("creating the initial appendable segment needs the append-only components");
}
Ok(Self {
path: path.to_path_buf(),
fs,
segments: RwLock::new(holder),
pool,
})
}
}