hermes-core 1.8.33

Core async search engine library with WASM support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
//! Dense vector merge strategies
//!
//! Every field always gets a Flat entry (raw vectors for reranking/merge).
//! Optionally, an ANN entry is also written alongside:
//! 1. O(1) cluster merge for homogeneous ANN types (IVF/ScaNN)
//! 2. ANN rebuild with trained structures
//!
//! Raw vectors are read from source segments' lazy flat data (mmap-backed),
//! never from the document store.
//!
//! **Streaming**: ANN blobs are written immediately after serialization and
//! freed, so peak memory is bounded by one ANN blob at a time (not all blobs
//! simultaneously). Doc_id maps are streamed in chunks (384 KB per chunk).

use std::io::Write;

use super::OffsetWriter;
use super::SegmentMerger;
use super::TrainedVectorStructures;
use super::doc_offsets;
use crate::Result;
use crate::directories::{Directory, DirectoryWriter};
use crate::dsl::{DenseVectorQuantization, FieldType, VectorIndexType};
use crate::segment::format::{DenseVectorTocEntry, write_dense_toc_and_footer};
use crate::segment::reader::SegmentReader;
use crate::segment::types::SegmentFiles;
use crate::segment::vector_data::{FlatVectorData, dequantize_raw};

/// Batch size for streaming vector reads (1024 vectors at a time)
const VECTOR_BATCH_SIZE: usize = 1024;

/// Chunk size for streaming flat vector bytes during merge (8 MB)
const FLAT_VECTOR_CHUNK: u64 = 8 * 1024 * 1024;

/// Chunk size for streaming doc_id+ordinal entries during merge.
/// 64K entries × 6 bytes = 384 KB per chunk (vs 60 MB unbounded).
const DOC_ID_CHUNK: usize = 64 * 1024;

/// Streams vectors from a segment's lazy flat data into an add_fn callback.
///
/// Reads vectors in batches of VECTOR_BATCH_SIZE to bound memory usage.
/// Each batch is a single range read via LazyFileSlice. Vectors are
/// dequantized to f32 regardless of storage quantization (f16, u8, f32).
///
/// Returns number of vectors added.
async fn feed_segment(
    segment: &SegmentReader,
    field: crate::dsl::Field,
    doc_id_offset: u32,
    mut add_fn: impl FnMut(u32, u16, &[f32]),
) -> crate::Result<usize> {
    let lazy_flat = match segment.flat_vectors().get(&field.0) {
        Some(f) => f,
        None => return Ok(0),
    };
    let n = lazy_flat.num_vectors;
    if n == 0 {
        return Ok(0);
    }
    let dim = lazy_flat.dim;
    let quant = lazy_flat.quantization;
    let mut count = 0;
    // Only allocate dequantize buffer for non-f32 quantizations
    let needs_dequant = quant != DenseVectorQuantization::F32;
    let mut f32_buf: Vec<f32> = Vec::new();

    for batch_start in (0..n).step_by(VECTOR_BATCH_SIZE) {
        let batch_count = VECTOR_BATCH_SIZE.min(n - batch_start);
        let batch_bytes = lazy_flat
            .read_vectors_batch(batch_start, batch_count)
            .await
            .map_err(crate::Error::Io)?;
        let raw = batch_bytes.as_slice();
        let batch_floats = batch_count * dim;

        // For f32: reinterpret mmap bytes directly (zero-copy).
        // For f16/u8: dequantize into buffer.
        let vectors: &[f32] = if needs_dequant {
            f32_buf.resize(batch_floats, 0.0);
            dequantize_raw(raw, quant, batch_floats, &mut f32_buf);
            &f32_buf
        } else {
            assert!(
                (raw.as_ptr() as usize).is_multiple_of(std::mem::align_of::<f32>()),
                "f32 vector data not 4-byte aligned"
            );
            // Safety: mmap-backed vector data is page-aligned. Assertion above
            // guards against unexpected misalignment.
            unsafe { std::slice::from_raw_parts(raw.as_ptr() as *const f32, batch_floats) }
        };

        for i in 0..batch_count {
            let (doc_id, ordinal) = lazy_flat.get_doc_id(batch_start + i);
            add_fn(
                doc_id_offset + doc_id,
                ordinal,
                &vectors[i * dim..(i + 1) * dim],
            );
            count += 1;
        }
    }
    Ok(count)
}

/// Write a Flat entry: header + raw vectors (chunked) + doc_ids (chunked).
///
/// Doc_id map is streamed in DOC_ID_CHUNK entries (384 KB) instead of
/// buffering all at once (was 60 MB for 10M vectors).
async fn write_flat_entry(
    field_id: u32,
    dim: usize,
    total_vectors: usize,
    quantization: DenseVectorQuantization,
    segments: &[SegmentReader],
    doc_offs: &[u32],
    writer: &mut OffsetWriter,
) -> Result<()> {
    FlatVectorData::write_binary_header(dim, total_vectors, quantization, writer)?;

    // Pass 1: stream raw vector bytes in chunks
    for segment in segments {
        if let Some(lazy_flat) = segment.flat_vectors().get(&field_id) {
            let total_bytes = lazy_flat.vector_bytes_len();
            let base_offset = lazy_flat.vectors_byte_offset();
            let handle = lazy_flat.handle();
            for chunk_start in (0..total_bytes).step_by(FLAT_VECTOR_CHUNK as usize) {
                let chunk_end = (chunk_start + FLAT_VECTOR_CHUNK).min(total_bytes);
                let bytes = handle
                    .read_bytes_range(base_offset + chunk_start..base_offset + chunk_end)
                    .await
                    .map_err(crate::Error::Io)?;
                writer.write_all(bytes.as_slice())?;
            }
        }
    }

    // Pass 2: stream doc_ids with offset adjustment (chunked, 384 KB per chunk)
    let mut buf = Vec::with_capacity(DOC_ID_CHUNK * 6);
    for (seg_idx, segment) in segments.iter().enumerate() {
        if let Some(lazy_flat) = segment.flat_vectors().get(&field_id) {
            let offset = doc_offs[seg_idx];
            let count = lazy_flat.num_vectors;
            for chunk_start in (0..count).step_by(DOC_ID_CHUNK) {
                buf.clear();
                let chunk_end = (chunk_start + DOC_ID_CHUNK).min(count);
                for i in chunk_start..chunk_end {
                    let (doc_id, ordinal) = lazy_flat.get_doc_id(i);
                    buf.extend_from_slice(&(offset + doc_id).to_le_bytes());
                    buf.extend_from_slice(&ordinal.to_le_bytes());
                }
                writer.write_all(&buf)?;
            }
        }
    }

    Ok(())
}

impl SegmentMerger {
    /// Merge dense vector indexes - returns output size in bytes
    ///
    /// Single-pass streaming: for each field, the ANN blob (if any) is built,
    /// serialized, written to disk, and freed immediately. Then the Flat entry
    /// is streamed. Peak memory = one ANN blob at a time, not all simultaneously.
    ///
    /// No document store reads — all vector data comes from .vectors files.
    pub(super) async fn merge_dense_vectors<D: Directory + DirectoryWriter>(
        &self,
        dir: &D,
        segments: &[SegmentReader],
        files: &SegmentFiles,
        trained: Option<&TrainedVectorStructures>,
    ) -> Result<usize> {
        let doc_offs = doc_offsets(segments)?;

        // Collect fields that need writing (in schema field_id order)
        struct FieldInfo {
            field: crate::dsl::Field,
            dim: usize,
            total_vectors: usize,
            quantization: DenseVectorQuantization,
        }
        let mut fields_to_write: Vec<FieldInfo> = Vec::new();

        for (field, entry) in self.schema.fields() {
            if !matches!(
                entry.field_type,
                FieldType::DenseVector | FieldType::BinaryDenseVector
            ) || !(entry.indexed || entry.stored)
            {
                continue;
            }

            let dim: usize = segments
                .iter()
                .filter_map(|s| s.flat_vectors().get(&field.0).map(|f| f.dim))
                .find(|&d| d > 0)
                .unwrap_or(0);
            if dim == 0 {
                continue;
            }

            let total_vectors: usize = segments
                .iter()
                .filter_map(|s| s.flat_vectors().get(&field.0).map(|f| f.num_vectors))
                .sum();
            if total_vectors == 0 {
                continue;
            }

            let quantization = if entry.field_type == FieldType::BinaryDenseVector {
                DenseVectorQuantization::Binary
            } else {
                entry
                    .dense_vector_config
                    .as_ref()
                    .map(|c| c.quantization)
                    .or_else(|| {
                        segments
                            .iter()
                            .find_map(|s| s.flat_vectors().get(&field.0).map(|f| f.quantization))
                    })
                    .unwrap_or(DenseVectorQuantization::F32)
            };

            fields_to_write.push(FieldInfo {
                field,
                dim,
                total_vectors,
                quantization,
            });
        }

        if fields_to_write.is_empty() {
            return Ok(0);
        }

        let write_start = std::time::Instant::now();
        let mut writer = OffsetWriter::new(dir.streaming_writer(&files.vectors).await?);
        let mut toc: Vec<DenseVectorTocEntry> = Vec::new();

        for fi in &fields_to_write {
            let field = fi.field;
            let entry = self.schema.get_field_entry(field).unwrap();
            let config = entry.dense_vector_config.as_ref();

            // ── ANN entry (written first, index_type < FLAT_TYPE) ────────
            // Skip ANN for binary dense vectors (always brute-force)
            let ann_blob = if entry.field_type == FieldType::BinaryDenseVector {
                None
            } else {
                self.try_build_ann(field, config, segments, &doc_offs, trained)
                    .await?
            };

            if let Some((index_type, bytes)) = ann_blob {
                let data_offset = writer.offset();
                writer.write_all(&bytes)?;
                let data_size = writer.offset() - data_offset;
                toc.push(DenseVectorTocEntry {
                    field_id: field.0,
                    index_type,
                    offset: data_offset,
                    size: data_size,
                });
                // Pad to 8-byte boundary
                let pad = (8 - (writer.offset() % 8)) % 8;
                if pad > 0 {
                    writer.write_all(&[0u8; 8][..pad as usize])?;
                }
                // `bytes` dropped here — frees the ANN blob immediately
            }

            // ── Flat entry (always written, index_type = FLAT_TYPE) ──────
            let data_offset = writer.offset();
            write_flat_entry(
                field.0,
                fi.dim,
                fi.total_vectors,
                fi.quantization,
                segments,
                &doc_offs,
                &mut writer,
            )
            .await?;
            let data_size = writer.offset() - data_offset;
            toc.push(DenseVectorTocEntry {
                field_id: field.0,
                index_type: crate::segment::ann_build::FLAT_TYPE,
                offset: data_offset,
                size: data_size,
            });
            // Pad to 8-byte boundary
            let pad = (8 - (writer.offset() % 8)) % 8;
            if pad > 0 {
                writer.write_all(&[0u8; 8][..pad as usize])?;
            }
        }

        // Write TOC + footer
        let toc_offset = writer.offset();
        write_dense_toc_and_footer(&mut writer, toc_offset, &toc)?;

        let output_size = writer.offset() as usize;
        writer.finish()?;
        log::info!(
            "[merge_vectors] file written: {} ({} entries) in {:.1}s",
            super::format_bytes(output_size),
            toc.len(),
            write_start.elapsed().as_secs_f64()
        );
        Ok(output_size)
    }

    /// Try to build an ANN index for a field. Returns (index_type, serialized_bytes)
    /// or None if no ANN path is available.
    ///
    /// Tries in order: O(1) ScaNN merge → O(1) IVF merge → rebuild from trained.
    async fn try_build_ann(
        &self,
        field: crate::dsl::Field,
        config: Option<&crate::dsl::DenseVectorConfig>,
        segments: &[SegmentReader],
        doc_offs: &[u32],
        trained: Option<&TrainedVectorStructures>,
    ) -> Result<Option<(u8, Vec<u8>)>> {
        let segments_with_flat = segments
            .iter()
            .filter(|s| s.flat_vectors().contains_key(&field.0))
            .count();

        // --- Try O(1) cluster merge for homogeneous ScaNN ---
        let scann_indexes: Vec<_> = segments
            .iter()
            .filter_map(|s| s.get_scann_vector_index(field))
            .collect();

        if scann_indexes.len() == segments_with_flat && !scann_indexes.is_empty() {
            let refs: Vec<&crate::structures::IVFPQIndex> =
                scann_indexes.iter().map(|(idx, _)| idx.as_ref()).collect();
            let codebook = scann_indexes.first().map(|(_, cb)| cb);

            match (
                crate::structures::IVFPQIndex::merge(&refs, doc_offs),
                codebook,
            ) {
                (Ok(merged), Some(codebook)) => {
                    let index_data = crate::segment::ScaNNIndexData {
                        codebook: (**codebook).clone(),
                        index: merged,
                    };
                    let bytes = index_data
                        .to_bytes()
                        .map_err(|e| crate::Error::Serialization(e.to_string()))?;
                    return Ok(Some((crate::segment::ann_build::SCANN_TYPE, bytes)));
                }
                (Err(e), _) => {
                    log::warn!("ScaNN merge failed: {}, falling back to rebuild", e);
                }
                (_, None) => {
                    log::warn!("ScaNN merge: missing codebook, falling back to rebuild");
                }
            }
        }

        // --- Try O(1) cluster merge for homogeneous IVF-RaBitQ ---
        let ivf_indexes: Vec<_> = segments
            .iter()
            .filter_map(|s| s.get_ivf_vector_index(field))
            .collect();

        if ivf_indexes.len() == segments_with_flat && !ivf_indexes.is_empty() {
            let refs: Vec<&crate::structures::IVFRaBitQIndex> =
                ivf_indexes.iter().map(|(idx, _)| idx.as_ref()).collect();
            let codebook = ivf_indexes.first().map(|(_, cb)| cb);

            match (
                crate::structures::IVFRaBitQIndex::merge(&refs, doc_offs),
                codebook,
            ) {
                (Ok(merged), Some(codebook)) => {
                    let index_data = crate::segment::IVFRaBitQIndexData {
                        codebook: (**codebook).clone(),
                        index: merged,
                    };
                    let bytes = index_data
                        .to_bytes()
                        .map_err(|e| crate::Error::Serialization(e.to_string()))?;
                    return Ok(Some((crate::segment::ann_build::IVF_RABITQ_TYPE, bytes)));
                }
                (Err(e), _) => {
                    log::warn!("IVF merge failed: {}, falling back to rebuild", e);
                }
                (_, None) => {
                    log::warn!("IVF merge: missing codebook, falling back to rebuild");
                }
            }
        }

        // --- Try ANN rebuild from lazy flat vectors using trained structures ---
        let ann_type = trained
            .zip(config)
            .and_then(|(trained, config)| match config.index_type {
                VectorIndexType::IvfRaBitQ if trained.centroids.contains_key(&field.0) => {
                    Some(VectorIndexType::IvfRaBitQ)
                }
                VectorIndexType::ScaNN
                    if trained.centroids.contains_key(&field.0)
                        && trained.codebooks.contains_key(&field.0) =>
                {
                    Some(VectorIndexType::ScaNN)
                }
                _ => None,
            });

        let ann = match ann_type {
            Some(ann) => ann,
            None => {
                log::debug!(
                    "[merge_vectors] field {}: no ANN path available (trained={}, config={}, ivf={}/{}, scann={}/{})",
                    field.0,
                    trained.is_some(),
                    config
                        .map(|c| format!("{:?}", c.index_type))
                        .unwrap_or_else(|| "None".into()),
                    ivf_indexes.len(),
                    segments_with_flat,
                    scann_indexes.len(),
                    segments_with_flat,
                );
                return Ok(None);
            }
        };

        let trained = trained.unwrap();
        let dim = segments
            .iter()
            .filter_map(|s| s.flat_vectors().get(&field.0).map(|f| f.dim))
            .find(|&d| d > 0)
            .unwrap();
        let mut total_fed = 0usize;
        let ann_start = std::time::Instant::now();

        let (index_type, bytes) = match ann {
            VectorIndexType::Flat | VectorIndexType::RaBitQ => unreachable!(),
            VectorIndexType::IvfRaBitQ => {
                let centroids = &trained.centroids[&field.0];
                let (mut index, codebook) =
                    crate::segment::ann_build::new_ivf_rabitq(dim, centroids);

                for (seg_idx, segment) in segments.iter().enumerate() {
                    let offset = doc_offs[seg_idx];
                    let fed = feed_segment(segment, field, offset, |doc_id, ordinal, vec| {
                        index.add_vector(centroids, &codebook, doc_id, ordinal, vec);
                    })
                    .await?;
                    total_fed += fed;
                    if fed > 0 {
                        log::debug!(
                            "[merge_vectors] field {} IVF: fed {} vectors from segment {} ({} total, {:.1}s)",
                            field.0,
                            fed,
                            seg_idx,
                            total_fed,
                            ann_start.elapsed().as_secs_f64()
                        );
                    }
                }

                log::info!(
                    "[merge_vectors] field {} IVF: serializing index ({} vectors, {:.1}s elapsed)",
                    field.0,
                    total_fed,
                    ann_start.elapsed().as_secs_f64()
                );
                let bytes = crate::segment::ann_build::serialize_ivf_rabitq(index, codebook)?;
                (crate::segment::ann_build::IVF_RABITQ_TYPE, bytes)
            }
            VectorIndexType::ScaNN => {
                let centroids = &trained.centroids[&field.0];
                let codebook = &trained.codebooks[&field.0];
                let mut index = crate::segment::ann_build::new_scann(dim, centroids, codebook);

                for (seg_idx, segment) in segments.iter().enumerate() {
                    let offset = doc_offs[seg_idx];
                    let fed = feed_segment(segment, field, offset, |doc_id, ordinal, vec| {
                        index.add_vector(centroids, codebook, doc_id, ordinal, vec);
                    })
                    .await?;
                    total_fed += fed;
                    if fed > 0 {
                        log::debug!(
                            "[merge_vectors] field {} ScaNN: fed {} vectors from segment {} ({} total, {:.1}s)",
                            field.0,
                            fed,
                            seg_idx,
                            total_fed,
                            ann_start.elapsed().as_secs_f64()
                        );
                    }
                }

                log::info!(
                    "[merge_vectors] field {} ScaNN: serializing index ({} vectors, {:.1}s elapsed)",
                    field.0,
                    total_fed,
                    ann_start.elapsed().as_secs_f64()
                );
                let bytes = crate::segment::ann_build::serialize_scann(index, codebook)?;
                (crate::segment::ann_build::SCANN_TYPE, bytes)
            }
        };

        log::info!(
            "[merge_vectors] field {} ANN(type={}) rebuilt: {} vectors, blob={}, {:.1}s",
            field.0,
            index_type,
            total_fed,
            super::format_bytes(bytes.len()),
            ann_start.elapsed().as_secs_f64()
        );
        Ok(Some((index_type, bytes)))
    }
}