Repository: qdrant/qdrant
File: lib/segment/src/vector_storage/dense/mmap_dense_vectors.rs (Rust)

Code before:
        } else {
            &self.mmap
        };
        let byte_slice = &mmap[offset..(offset + self.raw_size())];
        let arr: &[T] = unsafe { transmute(byte_slice) };
        &arr[0..self.dim]
    }

    /// Returns reference to vector data by key
    fn get_vector<P: AccessPattern>(&self, key: PointOffsetType) -> &[T] {
        self.get_vector_opt::<P>(key).expect("vector not found")
    }

    /// Returns an optional reference to vector data by key
    pub fn get_vector_opt<P: AccessPattern>(&self, key: PointOffsetType) -> Option<&[T]> {
        self.data_offset(key)
            .map(|offset| self.raw_vector_offset::<P>(offset))
    }

    pub fn for_each_in_batch<F: FnMut(usize, &[T])>(&self, keys: &[PointOffsetType], mut f: F) {
        // The `f` is most likely a scorer function.
        // Fetching all vectors first then scoring them is more cache friendly
        // then fetching and scoring in a single loop.
        let mut vectors_buffer = [MaybeUninit::uninit(); VECTOR_READ_BATCH_SIZE];
        let vectors = if is_read_with_prefetch_efficient(keys) {
            let iter = keys.iter().map(|key| self.get_vector::<Sequential>(*key));
            maybe_uninit_fill_from(&mut vectors_buffer, iter).0
        } else {
            let iter = keys.iter().map(|key| self.get_vector::<Random>(*key));
            maybe_uninit_fill_from(&mut vectors_buffer, iter).0
        };

        for (i, vec) in vectors.iter().enumerate() {
            f(i, vec);
        }
    }

    /// Marks the key as deleted.
    ///
    /// Returns true if the key was not deleted before, and it is now deleted.
    pub fn delete(&mut self, key: PointOffsetType) -> bool {
        let is_deleted = !self.deleted.replace(key as usize, true);
        if is_deleted {
            self.deleted_count += 1;
        }
        is_deleted
    }

    pub fn is_deleted_vector(&self, key: PointOffsetType) -> bool {
        self.deleted.get_bit(key as usize).unwrap_or(false)
    }

Review comment (bug):
If the buffer size is fixed, we should still at least `debug_assert!` that we don't have more keys than the buffer size. 

Alternatively, the size could be part of the function's const generics, but it would need more drill-down of the same constant everywhere.

Code after:
        } else {
            &self.mmap
        };
        let byte_slice = &mmap[offset..(offset + self.raw_size())];
        let arr: &[T] = unsafe { transmute(byte_slice) };
        &arr[0..self.dim]
    }

    /// Returns reference to vector data by key
    fn get_vector<P: AccessPattern>(&self, key: PointOffsetType) -> &[T] {
        self.get_vector_opt::<P>(key).expect("vector not found")
    }

    /// Returns an optional reference to vector data by key
    pub fn get_vector_opt<P: AccessPattern>(&self, key: PointOffsetType) -> Option<&[T]> {
        self.data_offset(key)
            .map(|offset| self.raw_vector_offset::<P>(offset))
    }

    pub fn for_each_in_batch<F: FnMut(usize, &[T])>(&self, keys: &[PointOffsetType], mut f: F) {
        debug_assert!(keys.len() <= VECTOR_READ_BATCH_SIZE);

        // The `f` is most likely a scorer function.
        // Fetching all vectors first then scoring them is more cache friendly
        // then fetching and scoring in a single loop.
        let mut vectors_buffer = [MaybeUninit::uninit(); VECTOR_READ_BATCH_SIZE];
        let vectors = if is_read_with_prefetch_efficient(keys) {
            let iter = keys.iter().map(|key| self.get_vector::<Sequential>(*key));
            maybe_uninit_fill_from(&mut vectors_buffer, iter).0
        } else {
            let iter = keys.iter().map(|key| self.get_vector::<Random>(*key));
            maybe_uninit_fill_from(&mut vectors_buffer, iter).0
        };

        for (i, vec) in vectors.iter().enumerate() {
            f(i, vec);
        }
    }

    /// Marks the key as deleted.
    ///
    /// Returns true if the key was not deleted before, and it is now deleted.
    pub fn delete(&mut self, key: PointOffsetType) -> bool {
        let is_deleted = !self.deleted.replace(key as usize, true);
        if is_deleted {
            self.deleted_count += 1;
        }
        is_deleted
    }

    pub fn is_deleted_vector(&self, key: PointOffsetType) -> bool {