#[cfg(feature = "native")]
use crate::dsl::Schema;
#[cfg(feature = "native")]
use crate::error::Result;
#[cfg(feature = "sync")]
use std::collections::HashMap;
#[cfg(feature = "native")]
use std::sync::Arc;
#[cfg(feature = "native")]
use std::sync::{OnceLock, Weak};
mod searcher;
pub use searcher::Searcher;
#[cfg(feature = "native")]
mod primary_key;
#[cfg(feature = "native")]
mod reader;
#[cfg(feature = "native")]
mod vector_builder;
#[cfg(all(feature = "wasm", not(feature = "native")))]
mod wasm_writer;
#[cfg(feature = "native")]
mod writer;
#[cfg(feature = "native")]
pub use primary_key::PrimaryKeyIndex;
#[cfg(feature = "native")]
pub use reader::IndexReader;
#[cfg(all(feature = "wasm", not(feature = "native")))]
pub use wasm_writer::IndexWriter as WasmIndexWriter;
#[cfg(feature = "native")]
pub use writer::{IndexWriter, PreparedCommit, WRITER_LOCK_FILENAME};
mod metadata;
pub use metadata::{
FieldVectorMeta, INDEX_META_FILENAME, IndexMetadata, SegmentMetaInfo, VectorIndexState,
};
#[cfg(feature = "native")]
mod helpers;
#[cfg(feature = "native")]
pub use helpers::{
IndexingStats, SchemaConfig, SchemaFieldConfig, create_index_at_path, create_index_from_sdl,
index_documents_from_reader, index_json_document, parse_schema,
};
pub const SLICE_CACHE_FILENAME: &str = "index.slicecache";
#[cfg(feature = "native")]
pub const MAX_CONCURRENT_REORDER_PASSES: usize = 2;
#[cfg(feature = "native")]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum ReorderPriority {
Optimizer,
AutomaticMerge,
Foreground,
}
#[cfg(feature = "native")]
#[derive(Debug)]
pub struct ReorderConcurrencyGate {
permits: Arc<tokio::sync::Semaphore>,
automatic_merge_permits: Arc<tokio::sync::Semaphore>,
limit: usize,
foreground_lock: Arc<tokio::sync::Mutex<()>>,
foreground_active: std::sync::atomic::AtomicBool,
foreground_finished: tokio::sync::Notify,
}
#[cfg(feature = "native")]
#[derive(Debug)]
pub(crate) struct BmpIoGate {
limit: usize,
active: parking_lot::Mutex<usize>,
available: parking_lot::Condvar,
async_available: tokio::sync::Notify,
}
#[cfg(feature = "native")]
impl BmpIoGate {
fn new(limit: usize) -> Self {
Self {
limit,
active: parking_lot::Mutex::new(0),
available: parking_lot::Condvar::new(),
async_available: tokio::sync::Notify::new(),
}
}
fn acquire(&self) -> BmpIoPermit<'_> {
let mut active = self.active.lock();
while *active >= self.limit {
self.available.wait(&mut active);
}
*active += 1;
BmpIoPermit { gate: self }
}
async fn acquire_async(&self) -> BmpIoPermit<'_> {
loop {
let notified = self.async_available.notified();
{
let mut active = self.active.lock();
if *active < self.limit {
*active += 1;
return BmpIoPermit { gate: self };
}
}
notified.await;
}
}
}
#[cfg(feature = "native")]
struct BmpIoPermit<'a> {
gate: &'a BmpIoGate,
}
#[cfg(feature = "native")]
impl Drop for BmpIoPermit<'_> {
fn drop(&mut self) {
let mut active = self.gate.active.lock();
*active -= 1;
self.gate.available.notify_one();
self.gate.async_available.notify_one();
}
}
#[cfg(feature = "native")]
impl ReorderConcurrencyGate {
pub fn new(requested_limit: usize) -> Self {
let limit = requested_limit.clamp(1, MAX_CONCURRENT_REORDER_PASSES);
let automatic_merge_limit = limit.saturating_sub(1).max(1);
Self {
permits: Arc::new(tokio::sync::Semaphore::new(limit)),
automatic_merge_permits: Arc::new(tokio::sync::Semaphore::new(automatic_merge_limit)),
limit,
foreground_lock: Arc::new(tokio::sync::Mutex::new(())),
foreground_active: std::sync::atomic::AtomicBool::new(false),
foreground_finished: tokio::sync::Notify::new(),
}
}
pub fn limit(&self) -> usize {
self.limit
}
pub(crate) async fn acquire(
self: &Arc<Self>,
priority: ReorderPriority,
) -> std::result::Result<ReorderPermit, tokio::sync::AcquireError> {
match priority {
ReorderPriority::Optimizer => self.acquire_background(None).await,
ReorderPriority::AutomaticMerge => {
let merge_permit = Arc::clone(&self.automatic_merge_permits)
.acquire_owned()
.await?;
self.acquire_background(Some(merge_permit)).await
}
ReorderPriority::Foreground => self.acquire_foreground().await,
}
}
async fn acquire_background(
self: &Arc<Self>,
automatic_merge: Option<tokio::sync::OwnedSemaphorePermit>,
) -> std::result::Result<ReorderPermit, tokio::sync::AcquireError> {
loop {
if self
.foreground_active
.load(std::sync::atomic::Ordering::Acquire)
{
let notified = self.foreground_finished.notified();
if self
.foreground_active
.load(std::sync::atomic::Ordering::Acquire)
{
notified.await;
continue;
}
}
let permit = Arc::clone(&self.permits).acquire_owned().await?;
if !self
.foreground_active
.load(std::sync::atomic::Ordering::Acquire)
{
return Ok(ReorderPermit {
_permit: permit,
_automatic_merge: automatic_merge,
});
}
drop(permit);
}
}
async fn acquire_foreground(
self: &Arc<Self>,
) -> std::result::Result<ReorderPermit, tokio::sync::AcquireError> {
let permit = Arc::clone(&self.permits).acquire_owned().await?;
Ok(ReorderPermit {
_permit: permit,
_automatic_merge: None,
})
}
pub(crate) async fn begin_foreground(
self: &Arc<Self>,
) -> std::result::Result<ForegroundReorderGuard, tokio::sync::AcquireError> {
let exclusive = Arc::clone(&self.foreground_lock).lock_owned().await;
self.foreground_active
.store(true, std::sync::atomic::Ordering::Release);
let mut guard = ForegroundReorderGuard {
gate: Arc::clone(self),
reserved: None,
_exclusive: exclusive,
};
if self.limit > 1 {
guard.reserved = Some(
Arc::clone(&self.permits)
.acquire_many_owned((self.limit - 1) as u32)
.await?,
);
}
Ok(guard)
}
}
#[cfg(feature = "native")]
pub(crate) struct ReorderPermit {
_permit: tokio::sync::OwnedSemaphorePermit,
_automatic_merge: Option<tokio::sync::OwnedSemaphorePermit>,
}
#[cfg(feature = "native")]
pub(crate) struct ForegroundReorderGuard {
gate: Arc<ReorderConcurrencyGate>,
reserved: Option<tokio::sync::OwnedSemaphorePermit>,
_exclusive: tokio::sync::OwnedMutexGuard<()>,
}
#[cfg(feature = "native")]
impl Drop for ForegroundReorderGuard {
fn drop(&mut self) {
drop(self.reserved.take());
self.gate
.foreground_active
.store(false, std::sync::atomic::Ordering::Release);
self.gate.foreground_finished.notify_waiters();
}
}
#[derive(Debug, Clone)]
pub struct IndexConfig {
pub num_threads: usize,
pub bmp_io_concurrency: usize,
pub num_indexing_threads: usize,
pub num_compression_threads: usize,
pub term_cache_blocks: usize,
pub store_cache_budget_bytes: usize,
pub max_indexing_memory_bytes: usize,
pub vector_training_max_samples: usize,
pub vector_training_memory_bytes: usize,
pub merge_policy: Box<dyn crate::merge::MergePolicy>,
pub optimization: crate::structures::IndexOptimization,
pub reload_interval_ms: u64,
pub max_concurrent_merges: usize,
#[cfg(feature = "native")]
pub background_merge_permits: Arc<tokio::sync::Semaphore>,
pub merge_bp_time_budget: Option<std::time::Duration>,
pub bp_memory_budget_bytes: usize,
#[cfg(feature = "native")]
pub background_reorder_permits: Arc<ReorderConcurrencyGate>,
#[cfg(feature = "native")]
pub background_reorder_pool: Option<Arc<rayon::ThreadPool>>,
}
#[cfg(feature = "sync")]
static SEARCH_CPU_POOLS: OnceLock<parking_lot::Mutex<HashMap<usize, Weak<rayon::ThreadPool>>>> =
OnceLock::new();
#[cfg(feature = "native")]
static STORE_CACHE_POOLS: OnceLock<
parking_lot::Mutex<std::collections::HashMap<usize, Weak<crate::segment::SharedStoreCache>>>,
> = OnceLock::new();
#[cfg(feature = "native")]
static BMP_IO_GATES: OnceLock<
parking_lot::Mutex<std::collections::HashMap<usize, Weak<BmpIoGate>>>,
> = OnceLock::new();
#[cfg(feature = "native")]
pub(crate) fn shared_bmp_io_gate(limit: usize) -> Arc<BmpIoGate> {
let mut gates = BMP_IO_GATES
.get_or_init(|| parking_lot::Mutex::new(std::collections::HashMap::new()))
.lock();
if let Some(gate) = gates.get(&limit).and_then(Weak::upgrade) {
return gate;
}
let gate = Arc::new(BmpIoGate::new(limit));
gates.retain(|_, gate| gate.strong_count() > 0);
gates.insert(limit, Arc::downgrade(&gate));
log::info!("[bmp] process-wide random-I/O concurrency={limit}");
gate
}
#[cfg(feature = "native")]
pub(crate) fn shared_store_cache(budget_bytes: usize) -> Arc<crate::segment::SharedStoreCache> {
let mut caches = STORE_CACHE_POOLS
.get_or_init(|| parking_lot::Mutex::new(std::collections::HashMap::new()))
.lock();
if let Some(cache) = caches.get(&budget_bytes).and_then(Weak::upgrade) {
return cache;
}
let cache = Arc::new(crate::segment::SharedStoreCache::new(budget_bytes));
caches.retain(|_, cache| cache.strong_count() > 0);
caches.insert(budget_bytes, Arc::downgrade(&cache));
log::info!(
"[store_cache] process-wide budget={}",
crate::format_bytes(budget_bytes as u64)
);
cache
}
#[cfg(feature = "sync")]
fn shared_search_pool(num_threads: usize) -> Result<Arc<rayon::ThreadPool>> {
if num_threads == 0 {
return Err(crate::Error::Internal(
"IndexConfig.num_threads must be greater than zero".into(),
));
}
let mut pools = SEARCH_CPU_POOLS
.get_or_init(|| parking_lot::Mutex::new(HashMap::new()))
.lock();
if let Some(pool) = pools.get(&num_threads).and_then(Weak::upgrade) {
return Ok(pool);
}
let pool = Arc::new(
rayon::ThreadPoolBuilder::new()
.num_threads(num_threads)
.thread_name(move |idx| format!("hermes-search-{}-{}", num_threads, idx))
.build()
.map_err(|error| {
crate::Error::Internal(format!(
"failed to create {num_threads}-thread search pool: {error}"
))
})?,
);
pools.retain(|_, pool| pool.strong_count() > 0);
pools.insert(num_threads, Arc::downgrade(&pool));
log::info!("[search] process-wide CPU pool: {} thread(s)", num_threads);
Ok(pool)
}
impl Default for IndexConfig {
fn default() -> Self {
#[cfg(feature = "native")]
let compression_threads = crate::default_compression_threads();
#[cfg(not(feature = "native"))]
let compression_threads = 1;
#[cfg(feature = "native")]
let search_threads = crate::default_search_threads();
#[cfg(not(feature = "native"))]
let search_threads = 1;
Self {
num_threads: search_threads,
bmp_io_concurrency: 4,
num_indexing_threads: 1, num_compression_threads: compression_threads,
term_cache_blocks: 256,
#[cfg(target_pointer_width = "64")]
store_cache_budget_bytes: 2 * 1024 * 1024 * 1024,
#[cfg(not(target_pointer_width = "64"))]
store_cache_budget_bytes: 32 * 1024 * 1024,
max_indexing_memory_bytes: 256 * 1024 * 1024, vector_training_max_samples: 10_000_000,
#[cfg(target_pointer_width = "64")]
vector_training_memory_bytes: 4 * 1024 * 1024 * 1024,
#[cfg(not(target_pointer_width = "64"))]
vector_training_memory_bytes: usize::MAX,
merge_policy: Box::new(crate::merge::TieredMergePolicy::large_scale()),
optimization: crate::structures::IndexOptimization::default(),
reload_interval_ms: 1000, max_concurrent_merges: 4,
#[cfg(feature = "native")]
background_merge_permits: Arc::new(tokio::sync::Semaphore::new(4)),
merge_bp_time_budget: Some(std::time::Duration::from_secs(600)),
#[cfg(target_pointer_width = "64")]
bp_memory_budget_bytes: 24 * 1024 * 1024 * 1024,
#[cfg(not(target_pointer_width = "64"))]
bp_memory_budget_bytes: usize::MAX,
#[cfg(feature = "native")]
background_reorder_permits: Arc::new(ReorderConcurrencyGate::new(2)),
#[cfg(feature = "native")]
background_reorder_pool: None,
}
}
}
#[cfg(feature = "native")]
pub struct Index<D: crate::directories::DirectoryWriter + 'static> {
directory: Arc<D>,
schema: Arc<Schema>,
config: IndexConfig,
search_resources: searcher::SearcherResources,
segment_manager: Arc<crate::merge::SegmentManager<D>>,
cached_reader: tokio::sync::OnceCell<IndexReader<D>>,
}
#[cfg(feature = "native")]
impl<D: crate::directories::DirectoryWriter + 'static> Index<D> {
pub async fn create(directory: D, schema: Schema, config: IndexConfig) -> Result<Self> {
let search_resources = searcher::SearcherResources::new(
config.term_cache_blocks,
config.store_cache_budget_bytes,
config.num_threads,
config.bmp_io_concurrency,
)?;
let directory = Arc::new(directory);
let schema = Arc::new(schema);
directory.set_index_label(schema.index_label());
if directory
.exists(std::path::Path::new(INDEX_META_FILENAME))
.await?
{
return Err(crate::Error::Internal(format!(
"refusing to create index: {} already exists in this directory; \
use Index::open to open the existing index, or delete the \
directory first if you really want to start over",
INDEX_META_FILENAME
)));
}
let metadata = IndexMetadata::new((*schema).clone());
let segment_manager = Arc::new(crate::merge::SegmentManager::new(
Arc::clone(&directory),
Arc::clone(&schema),
metadata,
config.merge_policy.clone_box(),
config.term_cache_blocks,
config.max_concurrent_merges,
Arc::clone(&config.background_merge_permits),
config.merge_bp_time_budget,
config.bp_memory_budget_bytes,
Arc::clone(&config.background_reorder_permits),
config.background_reorder_pool.clone(),
));
segment_manager.update_metadata(|_| {}).await?;
Ok(Self {
directory,
schema,
config,
search_resources,
segment_manager,
cached_reader: tokio::sync::OnceCell::new(),
})
}
pub async fn open(directory: D, config: IndexConfig) -> Result<Self> {
let search_resources = searcher::SearcherResources::new(
config.term_cache_blocks,
config.store_cache_budget_bytes,
config.num_threads,
config.bmp_io_concurrency,
)?;
let directory = Arc::new(directory);
let metadata = IndexMetadata::load(directory.as_ref()).await?;
let schema = Arc::new(metadata.schema.clone());
directory.set_index_label(schema.index_label());
let segment_manager = Arc::new(crate::merge::SegmentManager::new(
Arc::clone(&directory),
Arc::clone(&schema),
metadata,
config.merge_policy.clone_box(),
config.term_cache_blocks,
config.max_concurrent_merges,
Arc::clone(&config.background_merge_permits),
config.merge_bp_time_budget,
config.bp_memory_budget_bytes,
Arc::clone(&config.background_reorder_permits),
config.background_reorder_pool.clone(),
));
segment_manager.try_load_and_publish_trained().await?;
Ok(Self {
directory,
schema,
config,
search_resources,
segment_manager,
cached_reader: tokio::sync::OnceCell::new(),
})
}
pub fn schema(&self) -> &Schema {
&self.schema
}
pub fn schema_arc(&self) -> &Arc<Schema> {
&self.schema
}
pub fn directory(&self) -> &D {
&self.directory
}
pub fn segment_manager(&self) -> &Arc<crate::merge::SegmentManager<D>> {
&self.segment_manager
}
pub async fn reader(&self) -> Result<&IndexReader<D>> {
self.cached_reader
.get_or_try_init(|| async {
IndexReader::from_segment_manager_with_resources(
Arc::clone(&self.schema),
Arc::clone(&self.segment_manager),
self.config.reload_interval_ms,
self.search_resources.clone(),
)
.await
})
.await
}
pub fn config(&self) -> &IndexConfig {
&self.config
}
pub async fn segment_readers(&self) -> Result<Vec<Arc<crate::segment::SegmentReader>>> {
let reader = self.reader().await?;
let searcher = reader.searcher().await?;
Ok(searcher.segment_readers().to_vec())
}
pub async fn num_docs(&self) -> Result<u32> {
let reader = self.reader().await?;
let searcher = reader.searcher().await?;
Ok(searcher.num_docs())
}
pub fn default_fields(&self) -> Vec<crate::Field> {
if !self.schema.default_fields().is_empty() {
self.schema.default_fields().to_vec()
} else {
self.schema
.fields()
.filter(|(_, entry)| {
entry.indexed && entry.field_type == crate::dsl::FieldType::Text
})
.map(|(field, _)| field)
.collect()
}
}
pub fn tokenizers(&self) -> Arc<crate::tokenizer::TokenizerRegistry> {
Arc::new(crate::tokenizer::TokenizerRegistry::default())
}
pub fn query_parser(&self) -> crate::dsl::QueryLanguageParser {
let default_fields = self.default_fields();
let tokenizers = self.tokenizers();
let query_routers = self.schema.query_routers();
if !query_routers.is_empty()
&& let Ok(router) = crate::dsl::QueryFieldRouter::from_rules(query_routers)
{
return crate::dsl::QueryLanguageParser::with_router(
Arc::clone(&self.schema),
default_fields,
tokenizers,
router,
);
}
crate::dsl::QueryLanguageParser::new(Arc::clone(&self.schema), default_fields, tokenizers)
}
pub async fn query(
&self,
query_str: &str,
limit: usize,
) -> Result<crate::query::SearchResponse> {
self.query_offset(query_str, limit, 0).await
}
pub async fn query_offset(
&self,
query_str: &str,
limit: usize,
offset: usize,
) -> Result<crate::query::SearchResponse> {
let parser = self.query_parser();
let query = parser
.parse(query_str)
.map_err(crate::error::Error::Query)?;
self.search_offset(query.as_ref(), limit, offset).await
}
pub async fn search(
&self,
query: &dyn crate::query::Query,
limit: usize,
) -> Result<crate::query::SearchResponse> {
self.search_offset(query, limit, 0).await
}
pub async fn search_offset(
&self,
query: &dyn crate::query::Query,
limit: usize,
offset: usize,
) -> Result<crate::query::SearchResponse> {
let reader = self.reader().await?;
let searcher = reader.searcher().await?;
#[cfg(feature = "sync")]
let (results, total_seen) = {
let runtime_flavor = tokio::runtime::Handle::current().runtime_flavor();
if runtime_flavor == tokio::runtime::RuntimeFlavor::MultiThread {
tokio::task::block_in_place(|| {
searcher.search_with_offset_and_count_sync(query, limit, offset)
})?
} else {
searcher.search_with_offset_and_count_sync(query, limit, offset)?
}
};
#[cfg(not(feature = "sync"))]
let (results, total_seen) = {
searcher
.search_with_offset_and_count(query, limit, offset)
.await?
};
let total_hits = total_seen;
let hits: Vec<crate::query::SearchHit> = results
.into_iter()
.map(|result| crate::query::SearchHit {
address: crate::query::DocAddress::new(result.segment_id, result.doc_id),
score: result.score,
matched_fields: result.extract_ordinals(),
})
.collect();
Ok(crate::query::SearchResponse { hits, total_hits })
}
pub async fn get_document(
&self,
address: &crate::query::DocAddress,
) -> Result<Option<crate::dsl::Document>> {
let reader = self.reader().await?;
let searcher = reader.searcher().await?;
searcher.get_document(address).await
}
pub async fn get_postings(
&self,
field: crate::Field,
term: &[u8],
) -> Result<
Vec<(
Arc<crate::segment::SegmentReader>,
crate::structures::BlockPostingList,
)>,
> {
let segments = self.segment_readers().await?;
let mut results = Vec::new();
for segment in segments {
if let Some(postings) = segment.get_postings(field, term).await? {
results.push((segment, postings));
}
}
Ok(results)
}
}
#[cfg(feature = "native")]
impl<D: crate::directories::DirectoryWriter + 'static> Index<D> {
pub fn writer(&self) -> writer::IndexWriter<D> {
writer::IndexWriter::from_index(self)
}
}
#[cfg(test)]
mod tests;