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

Code before:
                .history
                .iter()
                .copied()
                .collect::<AHashSet<_>>()
                .len(),
            persisted.history.len(),
            "persisted must not have duplicate pointers in history",
        );

        // If both are the same, we have persisted the entry and can drop the pending change
        if self == persisted {
            return true;
        }

        // Pointers we consider persisted
        // If latest_is_set is different, the last pointer has new changes so we cannot consider it
        // persisted
        let mut persisted_pointers = persisted.history.as_slice();
        if self.latest_is_set != persisted.latest_is_set {
            persisted_pointers = &persisted_pointers[..persisted_pointers.len().saturating_sub(1)];
        }

        // Remove all persisted pointers from history
        self.history
            .retain(|pointer| !persisted_pointers.contains(pointer));

        // Drop entry if history is exhausted
        self.history.is_empty()
    }
}

#[derive(Debug, Default, Clone)]
struct TrackerHeader {
    next_pointer_offset: u32,
}

#[derive(Debug)]
pub struct Tracker {
    /// Path to the file
    path: PathBuf,
    /// Header of the file
    header: TrackerHeader,
    /// Mmap of the file
    mmap: MmapMut,
    /// Updates that haven't been flushed
    ///
    /// When flushing, these updates get written into the mmap and flushed at once.
    pub(super) pending_updates: AHashMap<PointOffset, PointerUpdates>,

    /// The maximum pointer offset in the tracker (updated in memory).
    next_pointer_offset: PointOffset,

Review comment (bug):
We also do the same debug assertion in the caller of this function. But I vote to keep it for now, until I more properly refactor it later. It does not affect release builds.

Code after:
                .history
                .iter()
                .copied()
                .collect::<AHashSet<_>>()
                .len(),
            persisted.history.len(),
            "persisted must not have duplicate pointers in history",
        );

        // If both are the same, we have persisted the entry and can drop the pending change
        if self == persisted {
            return true;
        }

        // Remove persisted entries to not persist them again, drop if history is exhausted
        self.update_from_persisted(persisted);
        self.history.is_empty()
    }

    /// Remove persisted entries from current so that we don't persist them again
    fn update_from_persisted(&mut self, persisted: &Self) {
        debug_assert_eq!(
            self.history.iter().copied().collect::<AHashSet<_>>().len(),
            self.history.len(),
            "self must not have duplicate pointers in history",
        );

        let (persist_set, persist_unset) = persisted.to_set_unset();

        // Update current set if necessary
        match (self.latest(), persist_set) {
            // Current and persisted set are equal, pop set from current
            (Some(last), Some(set)) if last == set => {
                self.history.pop();
                self.latest_is_set = false;
            }
            // Current and persisted set exist but are not equal, keep current set
            // Can be reached if current has a newer set
            (Some(_), Some(_)) => {}
            // If current does not have any set, don't touch it
            // Can be reached if current has newer unset
            (None, _) => {}
            // If not persisted any set, don't touch current set
            (_, None) => {}
        }

        // In current history, range of unset entries
        let unset_range = if self.latest_is_set {
            0..self.history.len().saturating_sub(1)
        } else {
            0..self.history.len()