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
//! Batch insertion API for HNSW indexes.
//!
//! This module provides the [`BatchInsertable`] trait for efficient
//! insertion of multiple vectors in a single operation.
//!
//! # Performance
//!
//! Batch insertion provides **convenience features** rather than raw throughput gains:
//! - Single API call instead of loop
//! - Built-in progress tracking at ~10% intervals
//! - Best-effort semantics (partial success on non-fatal errors)
//! - Progress callback overhead: <1%
//!
//! **Note:** Throughput is equivalent to sequential insertion since both
//! use the same underlying HNSW algorithm. See `benches/batch_vs_sequential.rs`.
//!
//! # Example
//!
//! ```ignore
//! use edgevec::{HnswConfig, HnswIndex, VectorStorage, batch::BatchInsertable, error::BatchError};
//!
//! fn main() -> Result<(), BatchError> {
//! // Create an HNSW index
//! let config = HnswConfig::new(128);
//! let mut storage = VectorStorage::new(&config, None);
//! let mut index = HnswIndex::new(config, &storage).unwrap();
//!
//! // Prepare vectors for batch insertion
//! let vectors: Vec<(u64, Vec<f32>)> = vec![
//! (1, vec![0.1; 128]),
//! (2, vec![0.2; 128]),
//! ];
//!
//! // Batch insert with progress tracking
//! let ids = index.batch_insert(vectors, &mut storage, Some(|inserted, total| {
//! println!("Progress: {}/{}", inserted, total);
//! }))?;
//!
//! assert_eq!(ids.len(), 2);
//! Ok(())
//! }
//! ```
//!
//! # Error Handling
//!
//! Batch insert uses **best-effort** semantics:
//! - Fatal errors (dimension mismatch on first vector, capacity exceeded) abort immediately
//! - Non-fatal errors (duplicates, invalid vectors mid-batch) are skipped
//! - Partial success is returned via `Ok(Vec<u64>)`
//!
//! See [`BatchError`](crate::error::BatchError) for error types.
use crateBatchError;
use crateVectorStorage;
/// Trait for HNSW indexes supporting batch insertion.
///
/// This trait provides efficient bulk insertion of multiple vectors
/// in a single operation, with optional progress tracking.
///
/// # Performance
///
/// Batch insertion provides **convenience features** rather than raw throughput gains:
/// - Single API call instead of loop
/// - Built-in progress tracking at ~10% intervals
/// - Best-effort semantics (partial success on non-fatal errors)
/// - Progress callback overhead: <1%
///
/// **Note:** Throughput is equivalent to sequential insertion.
/// See `benches/batch_vs_sequential.rs` for benchmark data.
///
/// # Atomicity
///
/// Batch insertion is **not atomic**. It follows best-effort semantics:
/// - If a fatal error occurs (e.g., dimension mismatch on first vector),
/// the operation aborts immediately with `Err(BatchError)`
/// - If a non-fatal error occurs (e.g., duplicate ID mid-batch),
/// the problematic vector is skipped and insertion continues
/// - Returns `Ok(Vec<u64>)` containing IDs of successfully inserted vectors
///
/// # Examples
///
/// Basic usage:
///
/// ```ignore
/// use edgevec::{HnswConfig, HnswIndex, VectorStorage, batch::BatchInsertable};
///
/// fn example() -> Result<(), edgevec::error::BatchError> {
/// let config = HnswConfig::new(128);
/// let mut storage = VectorStorage::new(&config, None);
/// let mut index = HnswIndex::new(config, &storage).unwrap();
///
/// let vectors = vec![
/// (1, vec![1.0; 128]),
/// (2, vec![2.0; 128]),
/// (3, vec![3.0; 128]),
/// ];
///
/// let ids = index.batch_insert(vectors, &mut storage, None)?;
/// println!("Inserted {} vectors", ids.len());
/// Ok(())
/// }
/// ```
///
/// With progress callback:
///
/// ```ignore
/// use edgevec::{HnswConfig, HnswIndex, VectorStorage, batch::BatchInsertable};
///
/// fn example() -> Result<(), edgevec::error::BatchError> {
/// let config = HnswConfig::new(128);
/// let mut storage = VectorStorage::new(&config, None);
/// let mut index = HnswIndex::new(config, &storage).unwrap();
/// let vectors: Vec<(u64, Vec<f32>)> = vec![(1, vec![1.0; 128])];
///
/// let ids = index.batch_insert(vectors, &mut storage, Some(|n, total| {
/// if n % 1000 == 0 || n == total {
/// println!("Progress: {}/{} ({:.1}%)", n, total, (n * 100) as f32 / total as f32);
/// }
/// }))?;
/// Ok(())
/// }
/// ```