Repository: qdrant/qdrant
File: lib/segment/src/common/flags/buffered_dynamic_flags.rs (Rust)

Code before:
        // take pending changes
        let (updates, required_len) = {
            let mut buffer_guard = self.buffer.write();
            let updates = std::mem::take(&mut *buffer_guard);
            let Some(required_len) = updates.keys().max().map(|&max_id| max_id as usize + 1) else {
                return Box::new(|| Ok(()));
            };
            (updates, required_len)
        };

        // Weak reference to detect when the storage has been deleted
        let flags_arc = Arc::downgrade(&self.storage);
        let is_alive_flush_lock = self.is_alive_flush_lock.handle();

        Box::new(move || {
            let Some(is_alive_flush_guard) = is_alive_flush_lock.lock_if_alive() else {
                return Err(OperationError::cancelled(
                    "Aborted flushing on a dropped BufferedDynamicFlags instance",
                ));
            };

            let Some(flags_arc) = flags_arc.upgrade() else {
                return Err(OperationError::cancelled(
                    "Aborted flushing on a dropped BufferedDynamicFlags instance",
                ));
            };

            // lock for the entire flushing process
            let mut flags_guard = flags_arc.lock();

            // resize if needed
            if required_len > flags_guard.len() {
                flags_guard.set_len(required_len)?;
            }

            for (index, value) in updates {
                flags_guard.set(index as usize, value);
            }

            flags_guard.flusher()()?;

            // Keep the guard till the end of the flush to prevent concurrent drop/flushes
            drop(is_alive_flush_guard);

            Ok(())
        })
    }
}

#[cfg(test)]
mod tests {

Review comment (refactor):
Nit: we _can_ use a single if-statement here like this to not duplicate the message:

```rust
let (Some(is_alive_flush_guard), Some(flags_arc)) =
    (is_alive_flush_lock.lock_if_alive(), flags_arc.upgrade())
else {
    return Err(OperationError::cancelled(
        "Aborted flushing on a dropped BufferedDynamicFlags instance",
    ));
};
```

Code after:
        // take pending changes
        let (updates, required_len) = {
            let mut buffer_guard = self.buffer.write();
            let updates = std::mem::take(&mut *buffer_guard);
            let Some(required_len) = updates.keys().max().map(|&max_id| max_id as usize + 1) else {
                return Box::new(|| Ok(()));
            };
            (updates, required_len)
        };

        // Weak reference to detect when the storage has been deleted
        let flags_arc = Arc::downgrade(&self.storage);
        let is_alive_flush_lock = self.is_alive_flush_lock.handle();

        Box::new(move || {
            let (Some(is_alive_flush_guard), Some(flags_arc)) =
                (is_alive_flush_lock.lock_if_alive(), flags_arc.upgrade())
            else {
                return Err(OperationError::cancelled(
                    "Aborted flushing on a dropped BufferedDynamicFlags instance",
                ));
            };

            // lock for the entire flushing process
            let mut flags_guard = flags_arc.lock();

            // resize if needed
            if required_len > flags_guard.len() {
                flags_guard.set_len(required_len)?;
            }

            for (index, value) in updates {
                flags_guard.set(index as usize, value);
            }

            flags_guard.flusher()()?;

            // Keep the guard till the end of the flush to prevent concurrent drop/flushes
            drop(is_alive_flush_guard);

            Ok(())
        })
    }
}

#[cfg(test)]
mod tests {

    use common::types::PointOffsetType;
    use rand::rngs::StdRng;
    use rand::{Rng, SeedableRng};