linkrs-api 0.1.0

Transport-independent core API for Linkrs
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
//! Vector Index API – Core Layer
//!
//! Provides transport layer independent vector index management and search operations.

use crate::api_core::error::{CoreError, CoreResult};
use linkrs_sync::backend::VectorBackend;
use linkrs_sync::vector_sync::{SearchOptions, VectorIndexLocation, VectorSyncCoordinator};
use simvec::{
    types::{validate_distance_metric, IndexMetadata, PointId},
    CollectionConfig, DistanceMetric, VectorPoint,
};
use std::sync::Arc;

/// Parameters for paginated vector point scanning.
pub struct ScrollQuery<'a> {
    pub space_id: u64,
    pub tag_name: &'a str,
    pub field_name: &'a str,
    pub limit: usize,
    pub offset: Option<&'a str>,
    pub with_payload: Option<bool>,
    pub with_vector: Option<bool>,
}

/// Metrics every backend accepts at index-creation time; anything else is
/// rejected up front instead of failing deep inside one engine.
fn validate_metric(distance: DistanceMetric) -> CoreResult<()> {
    validate_distance_metric(distance).map_err(CoreError::VectorError)
}

/// Vector search result
#[derive(Debug, Clone)]
pub struct VectorSearchResult {
    pub id: PointId,
    pub score: f32,
    pub vector: Option<Vec<f32>>,
    pub payload: Option<std::collections::HashMap<String, serde_json::Value>>,
}

/// Write mode for vector point mutations.
///
/// All writes are transactional through the durable outbox (`WAL + SQLite`)
/// so they participate in the graph transaction's commit/abort and benefit
/// from `read-your-writes` consistency.
#[derive(Debug, Clone)]
pub enum VectorWriteMode {
    /// Transactional write through the durable outbox.
    Transactional {
        txn_id: linkrs_core::types::TransactionId,
        space_id: u64,
        tag: String,
        field: String,
    },
}

/// Vector Index API – Core Layer
pub struct VectorApi {
    backend: VectorBackend,
    coordinator: Arc<VectorSyncCoordinator>,
    sync_manager: Arc<linkrs_sync::SyncManager>,
}

impl VectorApi {
    /// Create a VectorApi with the full assembly the vector subsystem needs.
    ///
    /// The coordinator and sync manager are required, not optional: every
    /// assembly owning a vector backend also owns the sync manager hosting
    /// the vector coordinator, and a partially wired instance would serve
    /// degraded reads or empty index listings instead of failing loudly.
    pub fn new(
        backend: VectorBackend,
        coordinator: Arc<VectorSyncCoordinator>,
        sync_manager: Arc<linkrs_sync::SyncManager>,
    ) -> Self {
        Self {
            backend,
            coordinator,
            sync_manager,
        }
    }

    /// Get the vector backend
    pub fn backend(&self) -> &VectorBackend {
        &self.backend
    }

    /// Get the sync coordinator
    pub fn coordinator(&self) -> &Arc<VectorSyncCoordinator> {
        &self.coordinator
    }

    /// Create a vector index
    ///
    /// Vector indexes are vertex-only: point IDs, payloads, and rebuild
    /// sources are all modeled per vertex, and edge mutations never produce
    /// vector points. `tag_name` must be a vertex tag; edge type names are
    /// rejected by the query and HTTP creation paths before reaching here.
    pub async fn create_index(
        &self,
        space_id: u64,
        tag_name: &str,
        field_name: &str,
        vector_size: usize,
        distance: DistanceMetric,
    ) -> CoreResult<String> {
        self.create_index_with_config(
            space_id,
            tag_name,
            field_name,
            CollectionConfig::new(vector_size, distance),
        )
        .await
    }

    /// Create a vector index with full collection config (quantization/hnsw)
    ///
    /// Vertex-only, like [`VectorApi::create_index`]: `tag_name` must be a
    /// vertex tag, never an edge type name.
    pub async fn create_index_with_config(
        &self,
        space_id: u64,
        tag_name: &str,
        field_name: &str,
        config: CollectionConfig,
    ) -> CoreResult<String> {
        validate_metric(config.distance)?;
        if let Some(qc) = &config.quantization_config {
            qc.validate(config.vector_size)
                .map_err(|e| CoreError::VectorError(e.to_string()))?;
        }

        if config.quantization_config.is_some() || config.hnsw_config.is_some() {
            return self
                .coordinator
                .create_index_with_config(space_id, tag_name, field_name, config)
                .await
                .map_err(|e| CoreError::VectorError(e.to_string()));
        }
        self.coordinator
            .create_vector_index(
                space_id,
                tag_name,
                field_name,
                config.vector_size,
                config.distance,
            )
            .await
            .map_err(|e| CoreError::VectorError(e.to_string()))
    }

    /// Drop a vector index
    pub async fn drop_index(
        &self,
        space_id: u64,
        tag_name: &str,
        field_name: &str,
    ) -> CoreResult<()> {
        self.coordinator
            .drop_vector_index(space_id, tag_name, field_name)
            .await
            .map_err(|e| CoreError::VectorError(e.to_string()))
    }

    /// Get vector index info (from logical index metadata)
    pub fn get_index_info(
        &self,
        space_id: u64,
        tag_name: &str,
        field_name: &str,
    ) -> CoreResult<Option<IndexMetadata>> {
        Ok(self.coordinator.index_info(space_id, tag_name, field_name))
    }

    /// List all vector indexes
    pub fn list_indexes(&self) -> Vec<String> {
        self.coordinator
            .list_indexes()
            .into_iter()
            .map(|w| w.collection_name)
            .collect()
    }

    /// List vector indexes whose last rebuild failed.
    ///
    /// These indexes still serve live data but need operator attention: retry
    /// [`VectorApi::rebuild_collection`].
    pub fn inconsistent_indexes(&self) -> Vec<linkrs_core::index_events::RebuildProgress> {
        self.coordinator.inconsistent_vector_indexes()
    }

    /// Insert a vector point with explicit write mode.
    pub async fn insert_vector_with_mode(
        &self,
        _space_id: u64,
        _tag_name: &str,
        _field_name: &str,
        point: VectorPoint,
        mode: VectorWriteMode,
    ) -> CoreResult<()> {
        let VectorWriteMode::Transactional {
            txn_id,
            space_id: txn_space,
            tag,
            field,
        } = mode;
        let manager = &self.sync_manager;
        let vector = point.vector.clone();
        let payload = point.payload.clone().unwrap_or_default();
        let mut properties: Vec<(Arc<str>, linkrs_core::Value)> = Vec::new();
        for (k, v) in payload {
            if let Ok(val) = serde_json::from_value::<linkrs_core::Value>(v) {
                properties.push((Arc::from(k.as_str()), val));
            }
        }
        properties.push((
            Arc::from(field.as_str()),
            linkrs_core::Value::vector(vector),
        ));
        let vertex_id = linkrs_core::Value::string(point.id.to_string());
        manager
            .on_vertex_change_with_txn(
                txn_id,
                txn_space,
                &tag,
                &vertex_id,
                &properties,
                linkrs_sync::types::ChangeType::Insert,
            )
            .map_err(|e| CoreError::VectorError(e.to_string()))?;
        Ok(())
    }

    /// Insert vector points in batch with explicit write mode.
    pub async fn insert_vector_batch_with_mode(
        &self,
        space_id: u64,
        _tag_name: &str,
        _field_name: &str,
        points: Vec<VectorPoint>,
        mode: VectorWriteMode,
    ) -> CoreResult<()> {
        let VectorWriteMode::Transactional {
            txn_id,
            space_id: txn_space,
            tag,
            field,
        } = mode;
        let manager = &self.sync_manager;
        for point in points {
            let vector = point.vector.clone();
            let payload = point.payload.clone().unwrap_or_default();
            let mut properties: Vec<(Arc<str>, linkrs_core::Value)> = Vec::new();
            for (k, v) in payload {
                if let Ok(val) = serde_json::from_value::<linkrs_core::Value>(v) {
                    properties.push((Arc::from(k.as_str()), val));
                }
            }
            properties.push((
                Arc::from(field.as_str()),
                linkrs_core::Value::vector(vector),
            ));
            let vertex_id = linkrs_core::Value::string(point.id.to_string());
            manager
                .on_vertex_change_with_txn(
                    txn_id,
                    txn_space,
                    &tag,
                    &vertex_id,
                    &properties,
                    linkrs_sync::types::ChangeType::Insert,
                )
                .map_err(|e| CoreError::VectorError(e.to_string()))?;
        }
        let _ = space_id;
        Ok(())
    }

    /// Delete a vector point with explicit write mode.
    pub async fn delete_vector_with_mode(
        &self,
        space_id: u64,
        tag_name: &str,
        field_name: &str,
        point_id: &str,
        mode: VectorWriteMode,
    ) -> CoreResult<()> {
        let VectorWriteMode::Transactional {
            txn_id,
            space_id: txn_space,
            tag,
            field,
        } = mode;
        let manager = &self.sync_manager;
        let vertex_id = linkrs_core::Value::string(point_id);
        manager
            .on_vertex_change_with_txn(
                txn_id,
                txn_space,
                &tag,
                &vertex_id,
                &[],
                linkrs_sync::types::ChangeType::Delete,
            )
            .map_err(|e| CoreError::VectorError(e.to_string()))?;
        let _ = (space_id, tag_name, field_name, field);
        Ok(())
    }

    /// Delete vector points in batch with explicit write mode.
    pub async fn delete_vector_batch_with_mode(
        &self,
        space_id: u64,
        tag_name: &str,
        field_name: &str,
        point_ids: Vec<&str>,
        mode: VectorWriteMode,
    ) -> CoreResult<()> {
        let VectorWriteMode::Transactional {
            txn_id,
            space_id: txn_space,
            tag,
            field,
        } = mode;
        let manager = &self.sync_manager;
        for point_id in point_ids {
            let vertex_id = linkrs_core::Value::string(point_id);
            manager
                .on_vertex_change_with_txn(
                    txn_id,
                    txn_space,
                    &tag,
                    &vertex_id,
                    &[],
                    linkrs_sync::types::ChangeType::Delete,
                )
                .map_err(|e| CoreError::VectorError(e.to_string()))?;
        }
        let _ = (space_id, tag_name, field_name, field);
        Ok(())
    }

    /// Rebuild a vector index from primary storage without dropping it.
    ///
    /// Backfill and catch-up replay converge a temp collection while the
    /// live slice keeps serving; a fenced publish then swaps the temp in.
    /// Requires a configured `SyncManager` (durable outbox + vector
    /// coordinator). Returns the number of applied point operations.
    pub async fn rebuild_collection(
        &self,
        space_id: u64,
        tag_name: &str,
        field_name: &str,
        source: &mut dyn linkrs_sync::VectorDocSource,
        options: linkrs_sync::VectorRebuildOptions,
    ) -> CoreResult<u64> {
        let manager = &self.sync_manager;
        manager
            .rebuild_vector_index(space_id, tag_name, field_name, source, options)
            .await
            .map_err(|e| CoreError::VectorError(e.to_string()))
    }

    /// Clear a vector index: drop all indexed points without backfill.
    ///
    /// This is an explicit destructive operation, not a rebuild. The caller
    /// must pass `force = true` to confirm the data loss; the call is audit
    /// logged. To repair a damaged index without data loss, use
    /// [`VectorApi::rebuild_collection`] (or drop and recreate the index).
    pub async fn clear_collection(
        &self,
        space_id: u64,
        tag_name: &str,
        field_name: &str,
        force: bool,
    ) -> CoreResult<()> {
        if !force {
            return Err(CoreError::VectorError(
                "Refusing to clear vector index without explicit confirmation: retry with force = true"
                    .to_string(),
            ));
        }
        log::warn!(
            "Clearing vector index explicitly confirmed: space {} tag {} field {}",
            space_id,
            tag_name,
            field_name
        );
        self.coordinator
            .index_manager()
            .purge_index_data(space_id, tag_name, field_name)
            .await
            .map_err(|e| CoreError::VectorError(e.to_string()))
    }

    /// Search vectors with options
    pub async fn search_with_options(
        &self,
        options: SearchOptions,
    ) -> CoreResult<Vec<VectorSearchResult>> {
        self.coordinator
            .search_with_options(options)
            .await
            .map(|results| {
                results
                    .into_iter()
                    .map(|r| VectorSearchResult {
                        id: r.id,
                        score: r.score,
                        vector: r.vector.map(|v| v.to_vec()),
                        payload: r.payload.map(|p| p.into_iter().collect()),
                    })
                    .collect()
            })
            .map_err(|e| CoreError::VectorError(e.to_string()))
    }

    /// Get a vector point by ID
    pub async fn get_vector(
        &self,
        space_id: u64,
        tag_name: &str,
        field_name: &str,
        point_id: &str,
    ) -> CoreResult<Option<VectorPoint>> {
        let collection_name =
            VectorIndexLocation::new(space_id, tag_name, field_name).to_collection_name();
        self.backend
            .get_vector(&collection_name, point_id)
            .await
            .map_err(|e| CoreError::VectorError(e.to_string()))
    }

    /// Get vector index count
    pub async fn count(&self, space_id: u64, tag_name: &str, field_name: &str) -> CoreResult<u64> {
        let collection_name =
            VectorIndexLocation::new(space_id, tag_name, field_name).to_collection_name();
        self.backend
            .count(&collection_name)
            .await
            .map_err(|e| CoreError::VectorError(e.to_string()))
    }

    /// Replace the entire payload for the given points.
    pub async fn set_payload(
        &self,
        space_id: u64,
        tag_name: &str,
        field_name: &str,
        point_ids: Vec<&str>,
        payload: simvec::types::Payload,
    ) -> CoreResult<()> {
        let collection_name =
            VectorIndexLocation::new(space_id, tag_name, field_name).to_collection_name();
        self.backend
            .set_payload(&collection_name, point_ids, payload)
            .await
            .map_err(|e| CoreError::VectorError(e.to_string()))
    }

    /// Merge the given fields into the payload of the given points.
    /// Only the supplied keys are updated; other existing keys are preserved.
    pub async fn set_payload_fields(
        &self,
        space_id: u64,
        tag_name: &str,
        field_name: &str,
        point_ids: Vec<&str>,
        fields: simvec::types::Payload,
    ) -> CoreResult<()> {
        let collection_name =
            VectorIndexLocation::new(space_id, tag_name, field_name).to_collection_name();
        self.backend
            .set_payload_fields(&collection_name, point_ids, fields)
            .await
            .map_err(|e| CoreError::VectorError(e.to_string()))
    }

    /// Remove specific keys from the payload of the given points.
    pub async fn delete_payload(
        &self,
        space_id: u64,
        tag_name: &str,
        field_name: &str,
        point_ids: Vec<&str>,
        keys: Vec<&str>,
    ) -> CoreResult<()> {
        let collection_name =
            VectorIndexLocation::new(space_id, tag_name, field_name).to_collection_name();
        self.backend
            .delete_payload(&collection_name, point_ids, keys)
            .await
            .map_err(|e| CoreError::VectorError(e.to_string()))
    }

    /// Paginated scan over points in a collection.
    pub async fn scroll(
        &self,
        query: ScrollQuery<'_>,
    ) -> CoreResult<(Vec<VectorPoint>, Option<String>)> {
        let collection_name =
            VectorIndexLocation::new(query.space_id, query.tag_name, query.field_name)
                .to_collection_name();
        self.backend
            .scroll(
                &collection_name,
                query.limit,
                query.offset,
                query.with_payload,
                query.with_vector,
            )
            .await
            .map_err(|e| CoreError::VectorError(e.to_string()))
    }
}