MCPcopy Create free account
hub / github.com/carsonpo/haystackdb / get_contiguous

Method get_contiguous

src/structures/dense_vector_list.rs:140–175  ·  view source on GitHub ↗
(
        &self,
        index: usize,
        num_elements: usize,
    )

Source from the content-addressed store, hash-verified

138 }
139
140 pub fn get_contiguous(
141 &self,
142 index: usize,
143 num_elements: usize,
144 ) -> io::Result<&[[u8; QUANTIZED_VECTOR_SIZE]]> {
145 let start = HEADER_SIZE + index * QUANTIZED_VECTOR_SIZE;
146 let end = start + num_elements * QUANTIZED_VECTOR_SIZE;
147
148 if end > self.used_space + HEADER_SIZE {
149 println!("start: {}", start);
150 println!("End: {}", end);
151 println!("Used space: {}", self.used_space);
152 println!("Num elements: {}", num_elements);
153 println!("Index: {}", index);
154 return Err(io::Error::new(
155 io::ErrorKind::InvalidInput,
156 "Index out of bounds",
157 ));
158 }
159
160 // let mut vectors = Vec::with_capacity(num_elements);
161 // for i in 0..num_elements {
162 // let offset = HEADER_SIZE + (index + i) * QUANTIZED_VECTOR_SIZE;
163 // vectors.push(self.get(index + i)?);
164 // }
165
166 // the indices are contiguous, so we can just get a slice of the mmap
167 let vectors: &[[u8; QUANTIZED_VECTOR_SIZE]] = unsafe {
168 std::slice::from_raw_parts(
169 self.mmap.as_ptr().add(start) as *const [u8; QUANTIZED_VECTOR_SIZE],
170 num_elements,
171 )
172 };
173
174 Ok(vectors)
175 }
176
177 pub fn len(&self) -> usize {
178 self.used_space / QUANTIZED_VECTOR_SIZE

Callers 1

queryMethod · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected