use std::path::Path;
use std::sync::Arc;
use crate::common::universal_io::{MmapFile, MmapFs};
use parking_lot::RwLock;
use crate::segment::common::operation_error::OperationResult;
use crate::segment::data_types::load_profile::LoadProfile;
use crate::segment::index::UniversalReadExt;
use crate::edge::EdgeConfig;
use crate::edge::read_only::ReadOnlyEdgeShard;
use crate::edge::read_only::enumerate::{ManifestSegmentEnumerator, SegmentEnumerator};
use crate::edge::read_only::holder::ReadOnlySegmentHolder;
impl ReadOnlyEdgeShard<MmapFile> {
pub fn open_mmap(path: &Path) -> OperationResult<Self> {
Self::open(MmapFs, path, None, None)
}
}
pub(super) fn merge_follower_config(provided: EdgeConfig, mut derived: EdgeConfig) -> EdgeConfig {
let vectors = std::mem::take(&mut derived.vectors);
let sparse_vectors = std::mem::take(&mut derived.sparse_vectors);
let mut config = provided.fill_unspecified_from(&derived);
config.vectors = vectors;
config.sparse_vectors = sparse_vectors;
config
}
impl<S: UniversalReadExt + 'static> ReadOnlyEdgeShard<S> {
pub fn open(
fs: S::Fs,
path: &Path,
config: Option<EdgeConfig>,
load_profile: Option<LoadProfile>,
) -> OperationResult<Self>
where
S::Fs: Send + Sync + Clone + 'static,
{
let enumerator = ManifestSegmentEnumerator::new(fs.clone(), path);
Self::open_with_enumerator(fs, path, enumerator, config, load_profile)
}
pub(crate) fn open_with_enumerator(
fs: S::Fs,
path: &Path,
enumerator: impl SegmentEnumerator + 'static,
config: Option<EdgeConfig>,
load_profile: Option<LoadProfile>,
) -> OperationResult<Self>
where
S::Fs: Send + Sync + Clone + 'static,
{
let provided_config = config.unwrap_or_default();
let search_pool = crate::edge::read_view::build_segment_pool(
"edge-search",
provided_config.search_thread_count(),
provided_config.search_pool_core,
)?;
let shard = Self {
path: path.to_path_buf(),
fs,
config: RwLock::new(Arc::new(provided_config.clone())),
segments: RwLock::new(ReadOnlySegmentHolder::default()),
enumerator: Box::new(enumerator),
search_pool,
load_profile,
};
shard.refresh()?;
let derived = shard.config.read().clone();
*shard.config.write() = Arc::new(merge_follower_config(
provided_config,
EdgeConfig::clone(&derived),
));
Ok(shard)
}
}