use std::collections::HashMap;
use std::path::PathBuf;
use crate::common::counter::hardware_counter::HardwareCounterCell;
use crate::common::types::{PointOffsetType, ScoredPointOffset, TelemetryDetail};
use crate::sparse::common::types::DimId;
use super::HNSWIndex;
use crate::segment::common::operation_error::{OperationError, OperationResult};
use crate::segment::data_types::query_context::VectorQueryContext;
use crate::segment::data_types::vectors::{QueryVector, VectorRef};
use crate::segment::index::hnsw_index::config::HnswGraphConfig;
use crate::segment::index::{VectorIndex, VectorIndexRead};
use crate::segment::telemetry::VectorIndexSearchesTelemetry;
use crate::segment::types::{Filter, SearchParams};
use crate::segment::vector_storage::VectorStorageRead;
impl VectorIndexRead for HNSWIndex {
fn search(
&self,
vectors: &[&QueryVector],
filter: Option<&Filter>,
top: usize,
params: Option<&SearchParams>,
query_context: &VectorQueryContext,
) -> OperationResult<Vec<Vec<ScoredPointOffset>>> {
self.with_view(|view| view.search(vectors, filter, top, params, query_context))
}
fn get_telemetry_data(&self, detail: TelemetryDetail) -> VectorIndexSearchesTelemetry {
let tm = &self.searches_telemetry;
VectorIndexSearchesTelemetry {
index_name: None,
unfiltered_plain: tm.unfiltered_plain.lock().get_statistics(detail),
filtered_plain: tm.filtered_plain.lock().get_statistics(detail),
unfiltered_hnsw: tm.unfiltered_hnsw.lock().get_statistics(detail),
filtered_small_cardinality: tm.small_cardinality.lock().get_statistics(detail),
filtered_large_cardinality: tm.large_cardinality.lock().get_statistics(detail),
filtered_exact: tm.exact_filtered.lock().get_statistics(detail),
filtered_sparse: Default::default(),
unfiltered_exact: tm.exact_unfiltered.lock().get_statistics(detail),
unfiltered_sparse: Default::default(),
}
}
fn indexed_vector_count(&self) -> usize {
self.config
.indexed_vector_count
.unwrap_or_else(|| self.graph.num_points())
}
fn size_of_searchable_vectors_in_bytes(&self) -> usize {
self.vector_storage
.borrow()
.size_of_available_vectors_in_bytes()
}
fn fill_idf_statistics(
&self,
_idf: &mut HashMap<DimId, usize>,
corpus: Option<&Filter>,
_is_stopped: &std::sync::atomic::AtomicBool,
_hw_counter: &HardwareCounterCell,
) -> OperationResult<usize> {
Ok(match corpus {
None => self.indexed_vector_count(),
Some(_) => 0,
})
}
fn is_index(&self) -> bool {
true
}
}
impl VectorIndex for HNSWIndex {
fn files(&self) -> Vec<PathBuf> {
let mut files = self.graph.files(&self.path);
let config_path = HnswGraphConfig::get_config_path(&self.path);
if config_path.exists() {
files.push(config_path);
}
files
}
fn immutable_files(&self) -> Vec<PathBuf> {
self.files() }
fn update_vector(
&mut self,
_id: PointOffsetType,
_vector: Option<VectorRef>,
_hw_counter: &HardwareCounterCell,
) -> OperationResult<()> {
Err(OperationError::service_error("Cannot update HNSW index"))
}
fn update_vector_raw(
&mut self,
_id: PointOffsetType,
_vector: Option<&[u8]>,
_hw_counter: &HardwareCounterCell,
) -> OperationResult<()> {
Err(OperationError::service_error("Cannot update HNSW index"))
}
}