oxirs_vec/diskann/
types.rs

1//! Core types for DiskANN
2
3use serde::{Deserialize, Serialize};
4use thiserror::Error;
5
6/// Result type for DiskANN operations
7pub type DiskAnnResult<T> = Result<T, DiskAnnError>;
8
9/// Node ID in the Vamana graph
10pub type NodeId = u32;
11
12/// Vector ID (external identifier)
13pub type VectorId = String;
14
15/// Errors that can occur in DiskANN operations
16#[derive(Debug, Error, Clone, Serialize, Deserialize)]
17pub enum DiskAnnError {
18    #[error("Vector not found: {id}")]
19    VectorNotFound { id: VectorId },
20
21    #[error("Invalid dimension: expected {expected}, got {actual}")]
22    DimensionMismatch { expected: usize, actual: usize },
23
24    #[error("Index not built")]
25    IndexNotBuilt,
26
27    #[error("IO error: {message}")]
28    IoError { message: String },
29
30    #[error("Serialization error: {message}")]
31    SerializationError { message: String },
32
33    #[error("Invalid configuration: {message}")]
34    InvalidConfiguration { message: String },
35
36    #[error("Graph error: {message}")]
37    GraphError { message: String },
38
39    #[error("Storage error: {message}")]
40    StorageError { message: String },
41
42    #[error("Memory limit exceeded: {message}")]
43    MemoryLimitExceeded { message: String },
44
45    #[error("Concurrent modification detected")]
46    ConcurrentModification,
47
48    #[error("Internal error: {message}")]
49    InternalError { message: String },
50}
51
52impl From<std::io::Error> for DiskAnnError {
53    fn from(err: std::io::Error) -> Self {
54        DiskAnnError::IoError {
55            message: err.to_string(),
56        }
57    }
58}
59
60impl From<oxicode::Error> for DiskAnnError {
61    fn from(err: oxicode::Error) -> Self {
62        DiskAnnError::SerializationError {
63            message: err.to_string(),
64        }
65    }
66}
67
68#[cfg(test)]
69mod tests {
70    use super::*;
71
72    #[test]
73    fn test_error_display() {
74        let err = DiskAnnError::VectorNotFound {
75            id: "vec123".to_string(),
76        };
77        assert!(err.to_string().contains("vec123"));
78
79        let err = DiskAnnError::DimensionMismatch {
80            expected: 128,
81            actual: 256,
82        };
83        assert!(err.to_string().contains("128"));
84        assert!(err.to_string().contains("256"));
85    }
86
87    #[test]
88    fn test_io_error_conversion() {
89        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
90        let disk_err: DiskAnnError = io_err.into();
91        assert!(matches!(disk_err, DiskAnnError::IoError { .. }));
92    }
93}