Repository: qdrant/qdrant
File: lib/collection/src/shards/local_shard/clock_map.rs (Rust)

Code before:

            ClockMapSnapshot::TakeIfMissing => {
                self.snapshot.replace(self.clocks.clone());
                self.changed = true;
            }
            ClockMapSnapshot::Clear => {
                self.snapshot.take();
                self.changed = true;
            }
        }
    }

    /// Create a recovery point based on the current clock map state, so that we can recover any
    /// new operations with new clock values
    ///
    /// If `use_snapshot` is `true`, the recovery point is created from the clocks snapshot if it
    /// exists. Otherwise the current clocks are used.
    ///
    /// The recovery point contains every clock that is in this clock map. So, it represents all
    /// the clock ticks we have.
    pub fn to_recovery_point(&self, use_snapshot: bool) -> RecoveryPoint {
        let clocks = if use_snapshot {
            self.snapshot.as_ref().unwrap_or(&self.clocks)
        } else {
            &self.clocks
        };

        RecoveryPoint {
            clocks: clocks
                .iter()
                .map(|(&key, clock)| (key, (clock.current_tick, clock.token)))
                .collect(),
        }
    }

    #[cfg(test)]
    pub fn current_tick(&self, peer_id: PeerId, clock_id: u32) -> Option<u64> {
        self.clocks
            .get(&Key::new(peer_id, clock_id))
            .map(Clock::current_tick)
    }
}

#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Deserialize, Serialize)]
pub struct Key {
    peer_id: PeerId,
    clock_id: u32,
}

impl Key {
    fn new(peer_id: PeerId, clock_id: u32) -> Self {

Review comment (suggestion):
I'd remove `use_snapshot` parameter and just always do `self.snapshot.as_ref().unwrap_or(&self.clocks)`.

Code after:
            return false;
        }

        self.snapshot.replace(self.clocks.clone());
        self.changed = true;
        true
    }

    /// Clear any snapshot of clocks
    ///
    /// Returns `true` if a snapshot was cleared.
    pub fn clear_snapshot(&mut self) -> bool {
        if self.snapshot.is_none() {
            return false;
        }

        self.snapshot.take();
        self.changed = true;
        true
    }

    /// Create a recovery point based on the current clock map state, so that we can recover any
    /// new operations with new clock values
    ///
    /// The recovery point will be derived from a clocks snapshot if it exists. Otherwise the
    /// current clocks are used.
    ///
    /// The recovery point contains every clock that is in this clock map. So, it represents all
    /// the clock ticks we have.
    pub fn to_recovery_point(&self) -> RecoveryPoint {
        let clocks = self.snapshot.as_ref().unwrap_or(&self.clocks);

        RecoveryPoint {
            clocks: clocks
                .iter()
                .map(|(&key, clock)| (key, (clock.current_tick, clock.token)))
                .collect(),
        }
    }

    #[cfg(test)]
    pub fn current_tick(&self, peer_id: PeerId, clock_id: u32) -> Option<u64> {
        self.clocks
            .get(&Key::new(peer_id, clock_id))
            .map(Clock::current_tick)
    }
}

#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Deserialize, Serialize)]
pub struct Key {
    peer_id: PeerId,