Repository: qdrant/qdrant
File: lib/gridstore/src/tracker.rs (Rust)

Code before:
        let end_offset = start_offset + size_of::<Option<ValuePointer>>();
        if end_offset > self.mmap.len() {
            return None;
        }
        let page_pointer = transmute_from_u8(&self.mmap[start_offset..end_offset]);
        Some(page_pointer)
    }

    /// Get the page pointer at the given point offset
    pub fn get(&self, point_offset: PointOffset) -> Option<ValuePointer> {
        self.pending_updates
            .get(&point_offset)
            .map(PointerUpdates::latest)
            // if the value is not in the pending updates, check the mmap
            .or_else(|| self.get_raw(point_offset).copied())
            .flatten()
    }

    /// Increment the header count if the given point offset is larger than the current count
    fn persist_pointer_count(&mut self) {
        self.header.next_pointer_offset = self.next_pointer_offset;
        self.write_header();
    }

    pub fn has_pointer(&self, point_offset: PointOffset) -> bool {
        self.pending_updates.contains_key(&point_offset) || self.get(point_offset).is_some()
    }

    pub fn set(&mut self, point_offset: PointOffset, value_pointer: ValuePointer) {
        self.pending_updates
            .entry(point_offset)
            .or_default()
            .set(value_pointer);
        self.next_pointer_offset = self.next_pointer_offset.max(point_offset + 1);
    }

    /// Unset the value at the given point offset and return its previous value
    pub fn unset(&mut self, point_offset: PointOffset) -> Option<ValuePointer> {
        let pointer_opt = self.get(point_offset);

        if let Some(pointer) = pointer_opt {
            self.pending_updates
                .entry(point_offset)
                .or_default()
                .unset(pointer);
        }

        pointer_opt
    }

    pub fn populate(&self) {

Review comment (suggestion):
`pending_updates.contains_key` is not correct, the pending operation might be a delete

the `get` method implements this check properly internally

Code after:
        let end_offset = start_offset + size_of::<Option<ValuePointer>>();
        if end_offset > self.mmap.len() {
            return None;
        }
        let page_pointer = transmute_from_u8(&self.mmap[start_offset..end_offset]);
        Some(page_pointer)
    }

    /// Get the page pointer at the given point offset
    pub fn get(&self, point_offset: PointOffset) -> Option<ValuePointer> {
        self.pending_updates
            .get(&point_offset)
            .map(PointerUpdates::latest)
            // if the value is not in the pending updates, check the mmap
            .or_else(|| self.get_raw(point_offset).copied())
            .flatten()
    }

    /// Increment the header count if the given point offset is larger than the current count
    fn persist_pointer_count(&mut self) {
        self.header.next_pointer_offset = self.next_pointer_offset;
        self.write_header();
    }

    pub fn has_pointer(&self, point_offset: PointOffset) -> bool {
        self.get(point_offset).is_some()
    }

    pub fn set(&mut self, point_offset: PointOffset, value_pointer: ValuePointer) {
        self.pending_updates
            .entry(point_offset)
            .or_default()
            .set(value_pointer);
        self.next_pointer_offset = self.next_pointer_offset.max(point_offset + 1);
    }

    /// Unset the value at the given point offset and return its previous value
    pub fn unset(&mut self, point_offset: PointOffset) -> Option<ValuePointer> {
        let pointer_opt = self.get(point_offset);

        if let Some(pointer) = pointer_opt {
            self.pending_updates
                .entry(point_offset)
                .or_default()
                .unset(pointer);
        }

        pointer_opt
    }

    pub fn populate(&self) {