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

Code before:
                    operation(segment)
                }
                // All segments to snapshot should be proxy, warn if this is not the case
                LockedSegment::Original(segment) => {
                    debug_assert!(
                        false,
                        "Reached non-proxy segment while applying function to proxies, this should not happen, ignoring",
                    );
                    // Call provided function on segment
                    operation(segment.as_ref())
                }
            };

            if let Err(err) = op_result {
                result = Err(OperationError::service_error(format!(
                    "Applying function to a proxied shard segment {segment_id} failed: {err}"
                )));
                break;
            }

            // Try to unproxy/release this segment since we don't use it anymore
            // Unproxying now lets us release the segment earlier, prevent unnecessary writes to the temporary segment.
            // Make sure to keep at least one proxy segment to maintain access to the points in the shared write segment.
            // The last proxy and the shared write segment will be promoted into the segment_holder atomically
            // by `Self::unproxy_all_segments` afterwards to maintain the read consistency.
            if unproxied_segment_ids.len() < proxies.len() - 1 {
                match Self::try_unproxy_segment(segments_lock, *segment_id, proxy_segment.clone()) {
                    Ok(lock) => {
                        segments_lock = lock;
                        unproxied_segment_ids.push(*segment_id);
                    }
                    Err(lock) => segments_lock = lock,
                }
            }
        }
        proxies.retain(|(id, _)| !unproxied_segment_ids.contains(id));

        // Unproxy all segments
        // Always do this to prevent leaving proxy segments behind
        log::trace!("Unproxying all shard segments after function is applied");
        Self::unproxy_all_segments(segments_lock, proxies, tmp_segment)?;

        result
    }

    /// Create a new appendable segment and add it to the segment holder.
    ///
    /// The segment configuration is sourced from the given collection parameters.
    pub fn create_appendable_segment(
        &mut self,
        segments_path: &Path,

Review comment (suggestion):
Nit, might clarify a bit more to readers:

```suggestion
            let is_last_proxy = unproxied_segment_ids.len() >= proxies.len() - 1;
            if !is_last_proxy {
```

Code after:
                    operation(segment)
                }
                // All segments to snapshot should be proxy, warn if this is not the case
                LockedSegment::Original(segment) => {
                    debug_assert!(
                        false,
                        "Reached non-proxy segment while applying function to proxies, this should not happen, ignoring",
                    );
                    // Call provided function on segment
                    operation(segment.as_ref())
                }
            };

            if let Err(err) = op_result {
                result = Err(OperationError::service_error(format!(
                    "Applying function to a proxied shard segment {segment_id} failed: {err}"
                )));
                break;
            }

            // Try to unproxy/release this segment since we don't use it anymore
            // Unproxying now lets us release the segment earlier, prevent unnecessary writes to the temporary segment.
            // Make sure to keep at least one proxy segment to maintain access to the points in the shared write segment.
            // The last proxy and the shared write segment will be promoted into the segment_holder atomically
            // by `Self::unproxy_all_segments` afterwards to maintain the read consistency.
            let remaining = proxies.len() - unproxied_segment_ids.len();
            if remaining > 1 {
                match Self::try_unproxy_segment(segments_lock, *segment_id, proxy_segment.clone()) {
                    Ok(lock) => {
                        segments_lock = lock;
                        unproxied_segment_ids.push(*segment_id);
                    }
                    Err(lock) => segments_lock = lock,
                }
            }
        }
        proxies.retain(|(id, _)| !unproxied_segment_ids.contains(id));

        // Unproxy all segments
        // Always do this to prevent leaving proxy segments behind
        log::trace!("Unproxying all shard segments after function is applied");
        Self::unproxy_all_segments(segments_lock, proxies, tmp_segment)?;

        result
    }

    /// Create a new appendable segment and add it to the segment holder.
    ///
    /// The segment configuration is sourced from the given collection parameters.
    pub fn create_appendable_segment(
        &mut self,