|
1 | | -use defs::DbError; |
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use defs::{DbError, Dimension, PointId, Similarity}; |
| 4 | +use serde::{Deserialize, Serialize}; |
2 | 5 | use storage::StorageEngine; |
3 | 6 |
|
4 | | -use crate::{IndexSnapshot, SerializableIndex, hnsw::HnswIndex}; |
| 7 | +use crate::{IndexSnapshot, IndexType, SerializableIndex, hnsw::{HNSW_MAGIC_BYTES, HnswIndex, types::{LevelGenerator, Node, PointIndexation}}}; |
| 8 | + |
| 9 | +#[repr(packed)] |
| 10 | +#[derive(Serialize, Deserialize)] |
| 11 | +pub struct HnswMetadataPack{ |
| 12 | + pub ef_construction: usize, |
| 13 | + pub data_dimension: Dimension, |
| 14 | + pub ef: usize, |
| 15 | + pub similarity: Similarity, |
| 16 | +} |
| 17 | + |
| 18 | +#[derive(Serialize, Deserialize)] |
| 19 | +pub struct HnswIndexPack { |
| 20 | + pub max_connections: usize, |
| 21 | + pub max_connections_0: usize, |
| 22 | + pub max_layer: usize, |
| 23 | + pub points_by_layer: Vec<Vec<PointId>>, |
| 24 | + pub nodes: Vec<Node>, |
| 25 | + pub entry_point: Option<PointId>, |
| 26 | + pub level_scale: f64, |
| 27 | +} |
| 28 | + |
5 | 29 |
|
6 | 30 | impl SerializableIndex for HnswIndex { |
7 | 31 | fn serialize_topology(&self) -> Result<Vec<u8>, DbError> { |
8 | | - return Err(DbError::SerializationError("not implemented".to_string())); |
| 32 | + let mut buffer = Vec::new(); |
| 33 | + |
| 34 | + let nodes: Vec<Node> = self.index.nodes.values().cloned().collect(); |
| 35 | + let index_pack = HnswIndexPack { |
| 36 | + max_connections: self.index.max_connections, |
| 37 | + max_connections_0: self.index.max_connections_0, |
| 38 | + max_layer: self.index.max_layer, |
| 39 | + points_by_layer: self.index.points_by_layer.clone(), |
| 40 | + nodes, |
| 41 | + entry_point: self.index.entry_point, |
| 42 | + level_scale: self.index.level_generator.level_scale, |
| 43 | + }; |
| 44 | + |
| 45 | + let index_bytes = bincode::serialize(&index_pack).map_err(|e| DbError::SerializationError(e.to_string()))?; |
| 46 | + buffer.extend(index_bytes); |
| 47 | + |
| 48 | + return Ok(buffer); |
9 | 49 | } |
| 50 | + |
10 | 51 | fn serialize_metadata(&self) -> Result<Vec<u8>, DbError> { |
11 | | - return Err(DbError::SerializationError("not implemented".to_string())); |
| 52 | + let mut buffer = Vec::new(); |
| 53 | + let index_pack = HnswMetadataPack { |
| 54 | + ef_construction: self.ef_construction, |
| 55 | + data_dimension: self.data_dimension, |
| 56 | + ef: self.ef, |
| 57 | + similarity: self.similarity |
| 58 | + }; |
| 59 | + |
| 60 | + let metadata_bytes = bincode::serialize(&index_pack).map_err(|e| DbError::SerializationError(e.to_string()))?; |
| 61 | + buffer.extend(metadata_bytes); |
| 62 | + return Ok(buffer); |
12 | 63 | } |
13 | 64 |
|
14 | 65 | fn snapshot(&self) -> Result<IndexSnapshot, DbError> { |
15 | | - return Err(DbError::SerializationError("not implemented".to_string())); |
| 66 | + let topology_bytes = self.serialize_topology()?; |
| 67 | + let metadata_bytes = self.serialize_metadata()?; |
| 68 | + Ok(IndexSnapshot { |
| 69 | + index_type: crate::IndexType::HNSW, |
| 70 | + magic: HNSW_MAGIC_BYTES, |
| 71 | + topology_b: topology_bytes, |
| 72 | + metadata_b: metadata_bytes, |
| 73 | + }) |
| 74 | + } |
| 75 | + |
| 76 | + fn populate_vectors(&mut self, storage: &dyn StorageEngine) -> Result<(), DbError> { |
| 77 | + // assumes index topology is restored |
| 78 | + for id in self.index.nodes.keys() { |
| 79 | + let vec = storage.get_vector(*id)?.ok_or(DbError::SerializationError(format!("Failed to locate vector for id: {} in storage", id)))?; |
| 80 | + self.cache.insert(*id, vec); |
| 81 | + } |
| 82 | + Ok(()) |
16 | 83 | } |
| 84 | +} |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | +impl HnswIndex { |
| 89 | + pub fn deserialize( |
| 90 | + IndexSnapshot { |
| 91 | + index_type, |
| 92 | + magic, |
| 93 | + topology_b, |
| 94 | + metadata_b, |
| 95 | + }: &IndexSnapshot, |
| 96 | + ) -> Result<HnswIndex, DbError> { |
| 97 | + if index_type != &IndexType::HNSW { |
| 98 | + return Err(DbError::SerializationError( |
| 99 | + "Invalid index type".to_string(), |
| 100 | + )); |
| 101 | + } |
| 102 | + |
| 103 | + if magic != &HNSW_MAGIC_BYTES { |
| 104 | + return Err(DbError::SerializationError( |
| 105 | + "Invalid magic bytes".to_string(), |
| 106 | + )); |
| 107 | + } |
| 108 | + |
| 109 | + let metadata: HnswMetadataPack = bincode::deserialize(metadata_b).map_err(|e| { |
| 110 | + DbError::SerializationError(format!("Failed to deserialize HNSW Metadata: {}", e)) |
| 111 | + })?; |
| 112 | + |
| 113 | + let index_pack : HnswIndexPack = bincode::deserialize(topology_b).map_err(|e| { |
| 114 | + DbError::SerializationError(format!("Failed to deserialize HNSW Index: {}", e)) |
| 115 | + })?; |
| 116 | + |
| 117 | + let mut hnsw_index_restored = PointIndexation { |
| 118 | + max_connections: index_pack.max_connections, |
| 119 | + max_connections_0: index_pack.max_connections_0, |
| 120 | + max_layer: index_pack.max_layer, |
| 121 | + points_by_layer: index_pack.points_by_layer, |
| 122 | + entry_point: index_pack.entry_point, |
| 123 | + nodes: HashMap::new(), |
| 124 | + level_generator: LevelGenerator{ |
| 125 | + level_scale: index_pack.level_scale |
| 126 | + } |
| 127 | + }; |
| 128 | + |
| 129 | + // restore nodes hashmap |
| 130 | + for i in index_pack.nodes { |
| 131 | + hnsw_index_restored.nodes.insert(i.id, i); |
| 132 | + } |
| 133 | + |
| 134 | + |
| 135 | + let hnsw = HnswIndex { |
| 136 | + ef_construction : metadata.ef_construction, |
| 137 | + data_dimension: metadata.data_dimension, |
| 138 | + ef: metadata.ef, |
| 139 | + cache: HashMap::new(), |
| 140 | + similarity: metadata.similarity, |
| 141 | + index: hnsw_index_restored |
| 142 | + }; |
17 | 143 |
|
18 | | - fn populate_vectors(&mut self, _storage: &dyn StorageEngine) -> Result<(), DbError> { |
19 | | - return Err(DbError::SerializationError("not implemented".to_string())); |
| 144 | + Ok(hnsw) |
20 | 145 | } |
21 | 146 | } |
0 commit comments