Repository navigation
refactor(map_index): split monolithic mod.rs into focused submodules - #8999
Conversation
The ~2150-line map_index/mod.rs was difficult to navigate. Split it into focused files while preserving all logic and public API: - key.rs: MapIndexKey trait and impls for str, IntPayloadType, UuidIntType - builders.rs: MapIndexBuilder, MapIndexMmapBuilder, MapIndexGridstoreBuilder - payload_index_impl_str.rs: PayloadFieldIndex/Read for MapIndex<str> - payload_index_impl_int.rs: PayloadFieldIndex/Read for MapIndex<IntPayloadType> - payload_index_impl_uuid.rs: PayloadFieldIndex/Read for MapIndex<UuidIntType> - facet_index_impl.rs: FacetIndex impl for MapIndex<N> - value_indexer_impl.rs: ValueIndexer impls and value_retriever methods mod.rs retains the MapIndex enum, its core inherent methods, type aliases, constants, and tests. Co-authored-by: Cursor <[email protected]>
|
ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughAdds a MapIndexKey trait and implements builders (mutable, mmap, gridstore), FacetIndex, PayloadFieldIndex/PayloadFieldIndexRead for str/Int/UUID keys, and ValueIndexer/value_retriever methods. Exposes multiple MapIndex internals as crate-visible helpers and updates mmap-related tests and payload IO/hw-counter assertions. Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Tip 💬 Introducing Slack Agent: The best way for teams to turn conversations into code.Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
Built for teams:
One agent for your entire SDLC. Right inside Slack. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (3)
lib/segment/src/index/field_index/map_index/payload_index_impl_uuid.rs (1)
120-121: ⚡ Quick winReplace catch-all arms with explicit variants in match handling.
Line 120 and Line 205 use
_ => None. Please enumerate unsupported variants explicitly so newMatchvariants don’t get silently ignored.As per coding guidelines: "Prefer explicit exhaustive matches over catch-all
_arm in Rust match expressions, so we don't miss handling new enum variants when they are added later."Also applies to: 205-206
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@lib/segment/src/index/field_index/map_index/payload_index_impl_uuid.rs` around lines 120 - 121, The match expressions in payload_index_impl_uuid.rs that currently end with `_ => None` should be changed to explicitly list each unsupported enum variant and return None for them (instead of a catch-all) so additions to the enum will produce a compile error; locate the two match sites in the MapIndex/Uuid payload implementation (the matches in the payload_index_impl_uuid module) and replace `_ => None` with explicit arms for every enum variant (e.g., VariantA => None, VariantB => None, ...) or the exact variant names from the matched enum, adding brief comments like `// unsupported` if helpful; this ensures exhaustive matching and fails compilation if new variants are introduced.lib/segment/src/index/field_index/map_index/payload_index_impl_int.rs (1)
86-87: ⚡ Quick winUse explicit variant arms instead of
_ => None.Line 86 and Line 150 use catch-all fallbacks. Please list unsupported
Matchvariants explicitly to keep behavior reviewable when enum variants evolve.As per coding guidelines: "Prefer explicit exhaustive matches over catch-all
_arm in Rust match expressions, so we don't miss handling new enum variants when they are added later."Also applies to: 150-151
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@lib/segment/src/index/field_index/map_index/payload_index_impl_int.rs` around lines 86 - 87, The match expressions in payload_index_impl_int.rs use a catch-all `_ => None` for the `Match` enum; replace those with explicit arms for every `Match` variant (e.g., list each variant and return None for unsupported ones) so the compiler will require updates when new variants are added, and remove the `_` fallback; update both occurrences that currently use `_ => None` so all `Match::...` variants are handled explicitly.lib/segment/src/index/field_index/map_index/payload_index_impl_str.rs (1)
87-88: ⚡ Quick winAvoid wildcard fallback arms in production match logic.
Line 87 and Line 151 use
_ => None. Please switch to explicit unsupported variants for forward-safe behavior whenMatchgrows.As per coding guidelines: "Prefer explicit exhaustive matches over catch-all
_arm in Rust match expressions, so we don't miss handling new enum variants when they are added later."Also applies to: 151-152
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@lib/segment/src/index/field_index/map_index/builders.rs`:
- Around line 147-151: Replace the panic assert in the builder's init method
with an error return: check if self.index.is_some() and, instead of asserting,
return an Err(OperationError) via the existing OperationResult return type with
a clear message like "index already initialized"; otherwise proceed with
initialization. Update the init function (the method named init in this builder)
to produce an OperationError for the repeated-init path rather than calling
assert!, using whatever constructor/variant your OperationError type exposes so
callers can handle the misuse without aborting the process.
In `@lib/segment/src/index/field_index/map_index/payload_index_impl_str.rs`:
- Line 165: The conditional currently uses `if count > threshold` which excludes
blocks where count == threshold and causes a mismatch with int/uuid handling;
change the comparison to `if count >= threshold` in the payload block check (the
conditional that uses the `count` and `threshold` variables in
payload_index_impl_str) so string payload blocks include exact-threshold cases
and match the inclusive behavior of int/uuid code paths.
---
Nitpick comments:
In `@lib/segment/src/index/field_index/map_index/payload_index_impl_int.rs`:
- Around line 86-87: The match expressions in payload_index_impl_int.rs use a
catch-all `_ => None` for the `Match` enum; replace those with explicit arms for
every `Match` variant (e.g., list each variant and return None for unsupported
ones) so the compiler will require updates when new variants are added, and
remove the `_` fallback; update both occurrences that currently use `_ => None`
so all `Match::...` variants are handled explicitly.
In `@lib/segment/src/index/field_index/map_index/payload_index_impl_uuid.rs`:
- Around line 120-121: The match expressions in payload_index_impl_uuid.rs that
currently end with `_ => None` should be changed to explicitly list each
unsupported enum variant and return None for them (instead of a catch-all) so
additions to the enum will produce a compile error; locate the two match sites
in the MapIndex/Uuid payload implementation (the matches in the
payload_index_impl_uuid module) and replace `_ => None` with explicit arms for
every enum variant (e.g., VariantA => None, VariantB => None, ...) or the exact
variant names from the matched enum, adding brief comments like `// unsupported`
if helpful; this ensures exhaustive matching and fails compilation if new
variants are introduced.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 38f47961-34dc-4534-afe8-94a80833c6cf
📒 Files selected for processing (8)
lib/segment/src/index/field_index/map_index/builders.rslib/segment/src/index/field_index/map_index/facet_index_impl.rslib/segment/src/index/field_index/map_index/key.rslib/segment/src/index/field_index/map_index/mod.rslib/segment/src/index/field_index/map_index/payload_index_impl_int.rslib/segment/src/index/field_index/map_index/payload_index_impl_str.rslib/segment/src/index/field_index/map_index/payload_index_impl_uuid.rslib/segment/src/index/field_index/map_index/value_indexer_impl.rs
| fn init(&mut self) -> OperationResult<()> { | ||
| assert!( | ||
| self.index.is_none(), | ||
| "index must be initialized exactly once", | ||
| ); |
There was a problem hiding this comment.
Avoid panic in builder init path; return an error instead.
Line 148 currently panics on repeated init(). This should be an OperationError to avoid process abort in recoverable misuse paths.
Suggested fix
fn init(&mut self) -> OperationResult<()> {
- assert!(
- self.index.is_none(),
- "index must be initialized exactly once",
- );
+ if self.index.is_some() {
+ return Err(OperationError::service_error(
+ "MapIndexGridstoreBuilder: init called more than once",
+ ));
+ }
self.index.replace(
MapIndex::new_gridstore(self.dir.clone(), true)?.ok_or_else(|| {
OperationError::service_error("Failed to create mutable map index")🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@lib/segment/src/index/field_index/map_index/builders.rs` around lines 147 -
151, Replace the panic assert in the builder's init method with an error return:
check if self.index.is_some() and, instead of asserting, return an
Err(OperationError) via the existing OperationResult return type with a clear
message like "index already initialized"; otherwise proceed with initialization.
Update the init function (the method named init in this builder) to produce an
OperationError for the repeated-init path rather than calling assert!, using
whatever constructor/variant your OperationError type exposes so callers can
handle the misuse without aborting the process.
| let count = self | ||
| .get_count_for_value(value, &HardwareCounterCell::disposable()) // Payload_blocks only used in HNSW building, which is unmeasured. | ||
| .unwrap_or(0); | ||
| if count > threshold { |
There was a problem hiding this comment.
Use inclusive threshold comparison for payload blocks.
Line 165 uses count > threshold, which excludes exact-threshold blocks and diverges from int/uuid behavior (>=). This creates type-dependent boundary behavior.
Suggested fix
- if count > threshold {
+ if count >= threshold {📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| if count > threshold { | |
| if count >= threshold { |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@lib/segment/src/index/field_index/map_index/payload_index_impl_str.rs` at
line 165, The conditional currently uses `if count > threshold` which excludes
blocks where count == threshold and causes a mismatch with int/uuid handling;
change the comparison to `if count >= threshold` in the payload block check (the
conditional that uses the `count` and `threshold` variables in
payload_index_impl_str) so string payload blocks include exact-threshold cases
and match the inclusive behavior of int/uuid code paths.
…ex-module Co-authored-by: Cursor <[email protected]> # Conflicts: # lib/segment/src/index/field_index/map_index/mod.rs
…8999) The ~2150-line map_index/mod.rs was difficult to navigate. Split it into focused files while preserving all logic and public API: - key.rs: MapIndexKey trait and impls for str, IntPayloadType, UuidIntType - builders.rs: MapIndexBuilder, MapIndexMmapBuilder, MapIndexGridstoreBuilder - payload_index_impl_str.rs: PayloadFieldIndex/Read for MapIndex<str> - payload_index_impl_int.rs: PayloadFieldIndex/Read for MapIndex<IntPayloadType> - payload_index_impl_uuid.rs: PayloadFieldIndex/Read for MapIndex<UuidIntType> - facet_index_impl.rs: FacetIndex impl for MapIndex<N> - value_indexer_impl.rs: ValueIndexer impls and value_retriever methods mod.rs retains the MapIndex enum, its core inherent methods, type aliases, constants, and tests. Co-authored-by: Cursor Agent <[email protected]> Co-authored-by: Cursor <[email protected]>
Summary
lib/segment/src/index/field_index/map_index/mod.rsinto 7 focused submodules while preserving all logic and public APIkey.rs(MapIndexKey trait),builders.rs(index builders),payload_index_impl_str.rs/payload_index_impl_int.rs/payload_index_impl_uuid.rs(per-type PayloadFieldIndex impls),facet_index_impl.rs(FacetIndex impl),value_indexer_impl.rs(ValueIndexer impls + value retrievers)mod.rsretains theMapIndexenum, core inherent methods, type aliases, constants, and testsTest plan
cargo check -p segmentpasses with zero warningsmap_index::testspassMade with Cursor