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<bincode::error::EncodeError> for DiskAnnError {
61    fn from(err: bincode::error::EncodeError) -> Self {
62        DiskAnnError::SerializationError {
63            message: err.to_string(),
64        }
65    }
66}
67
68impl From<bincode::error::DecodeError> for DiskAnnError {
69    fn from(err: bincode::error::DecodeError) -> Self {
70        DiskAnnError::SerializationError {
71            message: err.to_string(),
72        }
73    }
74}
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    #[test]
81    fn test_error_display() {
82        let err = DiskAnnError::VectorNotFound {
83            id: "vec123".to_string(),
84        };
85        assert!(err.to_string().contains("vec123"));
86
87        let err = DiskAnnError::DimensionMismatch {
88            expected: 128,
89            actual: 256,
90        };
91        assert!(err.to_string().contains("128"));
92        assert!(err.to_string().contains("256"));
93    }
94
95    #[test]
96    fn test_io_error_conversion() {
97        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
98        let disk_err: DiskAnnError = io_err.into();
99        assert!(matches!(disk_err, DiskAnnError::IoError { .. }));
100    }
101}