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

Code before:
        let create_options = StorageOptions::from(self.config);
        let base_path = self.base_path.clone();

        // Wipe
        self.pages.write().clear();
        fs::remove_dir_all(&base_path)
            .map_err(|err| format!("Failed to remove gridstore storage directory: {err}"))?;

        // Recreate
        fs::create_dir_all(&base_path)
            .map_err(|err| format!("Failed to create gridstore storage directory: {err}"))?;
        *self = Self::new(base_path, create_options)?;
        Ok(())
    }

    /// Wipe the storage, drop all pages and delete the base directory
    ///
    /// Takes ownership because this function leaves Gridstore in an inconsistent state which does
    /// not allow further usage. Use [`clear`] instead to clear and reuse the storage.
    pub fn wipe(self) -> Result<()> {
        let base_path = self.base_path.clone();

        // make sure we are not deleting files mid-flush
        {
            let _ = self.bitmask.write();
        }

        // Make sure strong references are dropped, to avoid starting another flush
        drop(self);

        // deleted base directory
        fs::remove_dir_all(base_path)
            .map_err(|err| format!("Failed to remove gridstore storage directory: {err}"))
    }

    /// Iterate over all the values in the storage
    pub fn iter<F, E>(
        &self,
        mut callback: F,
        hw_counter: HwMetricRefCounter,
    ) -> std::result::Result<(), E>
    where
        F: FnMut(PointOffset, V) -> std::result::Result<bool, E>,
    {
        for (point_offset, pointer) in
            self.tracker
                .read()
                .iter_pointers()
                .filter_map(|(point_offset, opt_pointer)| {
                    opt_pointer.map(|pointer| (point_offset, pointer))
                })

Review comment (suggestion):
Maybe a bit cleaner?

```suggestion
        // Barrier to wait for any ongoing flush to finish
        drop(self.bitmask.write());
```

Could you add a comment to the bitmask field in the struct to explain we also use it as barrier for dropping.

Code after:
    /// Completely wipes the storage, and recreates it with a single empty page.
    pub fn clear(&mut self) -> Result<()> {
        let create_options = StorageOptions::from(self.config);
        let base_path = self.base_path.clone();

        // Wipe
        self.pages.write().clear();
        fs::remove_dir_all(&base_path)
            .map_err(|err| format!("Failed to remove gridstore storage directory: {err}"))?;

        // Recreate
        fs::create_dir_all(&base_path)
            .map_err(|err| format!("Failed to create gridstore storage directory: {err}"))?;
        *self = Self::new(base_path, create_options)?;
        Ok(())
    }

    /// Wipe the storage, drop all pages and delete the base directory
    ///
    /// Takes ownership because this function leaves Gridstore in an inconsistent state which does
    /// not allow further usage. Use [`clear`](Self::clear) instead to clear and reuse the storage.
    pub fn wipe(self) -> Result<()> {
        let base_path = self.base_path.clone();

        // Barrier to wait for any ongoing flush to finish
        drop(self.bitmask.write());

        // Make sure strong references are dropped, to avoid starting another flush
        drop(self);

        // deleted base directory
        fs::remove_dir_all(base_path)
            .map_err(|err| format!("Failed to remove gridstore storage directory: {err}"))
    }

    /// Iterate over all the values in the storage
    pub fn iter<F, E>(
        &self,
        mut callback: F,
        hw_counter: HwMetricRefCounter,
    ) -> std::result::Result<(), E>
    where
        F: FnMut(PointOffset, V) -> std::result::Result<bool, E>,
    {
        for (point_offset, pointer) in
            self.tracker
                .read()
                .iter_pointers()
                .filter_map(|(point_offset, opt_pointer)| {
                    opt_pointer.map(|pointer| (point_offset, pointer))
                })