use std::path::Path;
use std::sync::Arc;
use nodedb_vector::mmap_segment::{MmapVectorSegment, VectorSegmentDropPolicy};
use crate::storage::quarantine::error::QuarantineError;
use crate::storage::quarantine::registry::{QuarantineEngine, QuarantineRegistry, SegmentKey};
pub fn open_vector_segment_with_quarantine(
registry: &Arc<QuarantineRegistry>,
path: &Path,
policy: VectorSegmentDropPolicy,
collection: &str,
segment_id: &str,
) -> Result<MmapVectorSegment, VectorOrQuarantine> {
match MmapVectorSegment::open_with_policy(path, policy) {
Ok(seg) => {
let key = SegmentKey {
engine: QuarantineEngine::Vector,
collection: collection.to_string(),
segment_id: segment_id.to_string(),
};
registry.record_success(&key);
Ok(seg)
}
Err(e) if e.kind() == std::io::ErrorKind::InvalidData => {
let key = SegmentKey {
engine: QuarantineEngine::Vector,
collection: collection.to_string(),
segment_id: segment_id.to_string(),
};
let path_for_rename = if path.exists() { Some(path) } else { None };
registry
.record_failure(key, &e.to_string(), path_for_rename)
.map_err(VectorOrQuarantine::Quarantined)?;
Err(VectorOrQuarantine::Io(e))
}
Err(e) => Err(VectorOrQuarantine::Io(e)),
}
}
#[derive(Debug, thiserror::Error)]
pub enum VectorOrQuarantine {
#[error(transparent)]
Io(#[from] std::io::Error),
#[error(transparent)]
Quarantined(#[from] QuarantineError),
}