Repository: qdrant/qdrant
File: lib/shard/src/segment_holder/locked.rs (Rust)

Code before:
use std::sync::Arc;
use std::time::Duration;

use parking_lot::{Mutex, RwLock, RwLockReadGuard, RwLockUpgradableReadGuard, RwLockWriteGuard};

use crate::segment_holder::SegmentHolder;

/// A guard that guarantees no update operations are happening.
///
/// This is a newtype wrapper around `parking_lot::MutexGuard<'_, ()>` that provides
/// semantic meaning: while this guard is held, no concurrent update operations can proceed.
/// This is used during critical sections like segment optimization finalization and snapshot
/// operations to ensure consistency.
pub struct UpdatesGuard<'a>(parking_lot::MutexGuard<'a, ()>);

impl<'a> UpdatesGuard<'a> {
    /// Create a new UpdatesGuard from a MutexGuard.
    fn new(guard: parking_lot::MutexGuard<'a, ()>) -> Self {
        Self(guard)
    }
}

#[derive(Clone, Debug)]
pub struct LockedSegmentHolder {
    holder: Arc<RwLock<SegmentHolder>>,
    /// Lock that prevents update operations during segment maintenance.
    /// This lock should be external to the `holder` to prevent deadlocks.
    /// This lock doesn't wrap the whole `SegmentHolder` to allow read access to segments
    ///
    ///
    ///
    /// Currently used for:
    ///
    /// - Preventing update operations during finalization of segment optimizations
    ///
    updates_mutex: Arc<Mutex<()>>,
}

impl LockedSegmentHolder {
    pub fn new(segment_holder: SegmentHolder) -> Self {
        Self {
            holder: Arc::new(RwLock::new(segment_holder)),
            updates_mutex: Arc::new(Mutex::new(())),

Review comment (suggestion):
I don't think `new` is needed.  Tuple struct constructor is enough the single usage, and `new` is not public (and shouldn't be).

Code after:
use std::sync::Arc;
use std::time::Duration;

use parking_lot::{Mutex, RwLock, RwLockReadGuard, RwLockUpgradableReadGuard, RwLockWriteGuard};

use crate::segment_holder::SegmentHolder;

/// A guard that guarantees no update operations are happening.
///
/// This is a newtype wrapper around `parking_lot::MutexGuard<'_, ()>` that provides
/// semantic meaning: while this guard is held, no concurrent update operations can proceed.
/// This is used during critical sections like segment optimization finalization and snapshot
/// operations to ensure consistency.
#[allow(dead_code)] // Field is held for its RAII Drop behavior, not for reading
pub struct UpdatesGuard<'a>(parking_lot::MutexGuard<'a, ()>);

#[derive(Clone, Debug)]
pub struct LockedSegmentHolder {
    holder: Arc<RwLock<SegmentHolder>>,
    /// Lock that prevents update operations during segment maintenance.
    /// This lock should be external to the `holder` to prevent deadlocks.
    /// This lock doesn't wrap the whole `SegmentHolder` to allow read access to segments
    ///
    ///
    ///
    /// Currently used for:
    ///
    /// - Preventing update operations during finalization of segment optimizations
    ///
    updates_mutex: Arc<Mutex<()>>,
}

impl LockedSegmentHolder {
    pub fn new(segment_holder: SegmentHolder) -> Self {
        Self {
            holder: Arc::new(RwLock::new(segment_holder)),
            updates_mutex: Arc::new(Mutex::new(())),
        }
    }

    pub fn read(&self) -> RwLockReadGuard<'_, SegmentHolder> {
        self.holder.read()
    }