Repository navigation
Immutable storage null index - #8653
Conversation
4b0ba85 to
5bf8ede
Compare
623adba to
f0f9cc3
Compare
d08c453 to
394949b
Compare
6e21729 to
d624076
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
lib/segment/src/index/struct_payload_index.rs (1)
165-206: Consolidateid_trackerborrow — currently borrowed three times.The new
id_tracker_borrowat Line 165 is only consumed once (Line 191). Line 175 still calls&self.id_tracker.borrow()inline, and Line 206 redundantly re-borrows (shadowingid_tracker_borrow). Reuse the single borrow across both branches.♻️ Suggested refactor
let mut indexes = if payload_schema.types.is_empty() { let selector = self.selector(&payload_schema.schema); let indexes = selector.new_index( field, &payload_schema.schema, create_if_missing, - &self.id_tracker.borrow(), + &id_tracker_borrow, )?; if let Some(mut indexes) = indexes { debug_assert!( !indexes .iter() .any(|index| matches!(index, FieldIndex::NullIndex(_))), "index selector is not expected to provide null index", ); // Special null index complements every index. if let Some(null_index) = selector.new_null_index( &self.path, field, create_if_missing, &id_tracker_borrow, )? { indexes.push(null_index); } // Persist exact payload index types is_dirty = true; payload_schema.types = indexes.iter().map(|i| i.get_full_index_type()).collect(); indexes } else { rebuild = true; vec![] } } else { - let id_tracker_borrow = self.id_tracker.borrow(); payload_schema .types .iter()🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@lib/segment/src/index/struct_payload_index.rs` around lines 165 - 206, The code borrows self.id_tracker three times; create one borrow named id_tracker_borrow before the if and reuse it in both branches instead of calling &self.id_tracker.borrow() inline and re-borrowing/shadowing later; update calls to selector.new_index(..., &id_tracker_borrow) and selector.new_null_index(..., &id_tracker_borrow) and remove the redundant let id_tracker_borrow = self.id_tracker.borrow() in the else branch so the single borrow lives across the entire payload_schema.types.is_empty() block and prevents multiple borrows.lib/segment/src/index/field_index/null_index/mutable_null_index.rs (1)
195-196: Nit: doc typo — "is not not changed".📝 Suggested fix
- // N.B. We do not update total_point_count because it comes from the id tracker and is not not changed - // in non-appendable segments. + // N.B. We do not update total_point_count because it comes from the id tracker and is not changed + // in non-appendable segments.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@lib/segment/src/index/field_index/null_index/mutable_null_index.rs` around lines 195 - 196, Fix the doc comment typo in mutable_null_index.rs: remove the duplicated "not" so the comment reads "...and is not changed in non-appendable segments." Locate the comment in the MutableNullIndex area (the block referencing total_point_count and id tracker) and update the text accordingly.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@lib/segment/src/index/field_index/null_index/immutable_null_index.rs`:
- Around line 403-444: The test test_remove_reopen currently clears the deleted
bit twice (once via index.remove_point(1) and again via the deleted BitVec), so
it never exercises the reopen-with-deletion path; change the test to let the
deleted BitVec be the sole remover by removing the call to
ImmutableNullIndex::remove_point (the index.remove_point(1) line) and only set
deleted.set(1, true) before drop(index), then call ImmutableNullIndex::open(...)
to verify reopen behavior (this ensures MutableNullIndex::open_immutable /
remove_point_immutable actually runs for the bit supplied via deleted).
---
Nitpick comments:
In `@lib/segment/src/index/field_index/null_index/mutable_null_index.rs`:
- Around line 195-196: Fix the doc comment typo in mutable_null_index.rs: remove
the duplicated "not" so the comment reads "...and is not changed in
non-appendable segments." Locate the comment in the MutableNullIndex area (the
block referencing total_point_count and id tracker) and update the text
accordingly.
In `@lib/segment/src/index/struct_payload_index.rs`:
- Around line 165-206: The code borrows self.id_tracker three times; create one
borrow named id_tracker_borrow before the if and reuse it in both branches
instead of calling &self.id_tracker.borrow() inline and re-borrowing/shadowing
later; update calls to selector.new_index(..., &id_tracker_borrow) and
selector.new_null_index(..., &id_tracker_borrow) and remove the redundant let
id_tracker_borrow = self.id_tracker.borrow() in the else branch so the single
borrow lives across the entire payload_schema.types.is_empty() block and
prevents multiple borrows.
🪄 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: e6569e0b-60c4-44e0-9b98-d7645e4e251b
📒 Files selected for processing (7)
lib/segment/src/index/field_index/field_index_base.rslib/segment/src/index/field_index/index_selector.rslib/segment/src/index/field_index/null_index/immutable_null_index.rslib/segment/src/index/field_index/null_index/mod.rslib/segment/src/index/field_index/null_index/mutable_null_index.rslib/segment/src/index/query_optimization/condition_converter.rslib/segment/src/index/struct_payload_index.rs
|
Note: needs the same treatment as in #8785 |
Add `ImmutableNullIndex` that uses segment-level deleted points to update in-memory indexes without modifying the storage.
d624076 to
2a9296c
Compare
| match self { | ||
| IndexSelector::Mmap(_) => Ok(ImmutableNullIndex::open( | ||
| &null_dir(dir, field), | ||
| total_point_count, | ||
| id_tracker.deleted_point_bitslice(), | ||
| )? | ||
| .map(NullIndex::Immutable) | ||
| .map(FieldIndex::NullIndex)), | ||
| IndexSelector::Gridstore(_) => Ok(MutableNullIndex::open( | ||
| &null_dir(dir, field), | ||
| total_point_count, | ||
| create_if_missing, | ||
| )? | ||
| .map(NullIndex::Mutable) | ||
| .map(FieldIndex::NullIndex)), | ||
| // `MutableNullIndex` and `ImmutableNullIndex` share the same on-disk | ||
| // format; stored mutability picks which in-memory wrapper to build. | ||
| IndexSelector::Mmap(_) => match mutability { | ||
| IndexMutability::Immutable => Ok(ImmutableNullIndex::open( | ||
| &null_dir, | ||
| total_point_count, | ||
| id_tracker.deleted_point_bitslice(), | ||
| )? | ||
| .map(NullIndex::Immutable) | ||
| .map(FieldIndex::NullIndex)), | ||
| IndexMutability::Mutable => open_mutable(), | ||
| }, | ||
| // Gridstore segments are always appendable, so the null index is | ||
| // always mutable regardless of the stored mutability marker. | ||
| IndexSelector::Gridstore(_) => open_mutable(), | ||
| } |
There was a problem hiding this comment.
If you do match (self, mutability) you can use a single
(IndexSelector::Mmap(_), IndexMutability::Mutable)
| (IndexSelector::Gridstore(_), _) => {}branch to get rid of the open_mutable closure.
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughThe PR introduces a unified Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 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 unit tests (beta)
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/struct_payload_index.rs (2)
334-340: Nit: duplicate comment line after the refactor.Line 339 (
// Special null index complements every index.) repeats the first sentence of the comment block at lines 334-337. Looks like a leftover from replacing the old call site — safe to drop.♻️ Proposed cleanup
let total_point_count = self.id_tracker.borrow().total_point_count(); - // Special null index complements every index. let null_index = selector.null_builder(&self.path, field, total_point_count)?;🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@lib/segment/src/index/struct_payload_index.rs` around lines 334 - 340, Remove the duplicated comment line by deleting the redundant "// Special null index complements every index." immediately above the call to selector.null_builder in struct_payload_index.rs; keep the initial explanatory comment block that mentions seeding the null index with total_point_count and leave the call that computes total_point_count via self.id_tracker.borrow().total_point_count() and the subsequent let null_index = selector.null_builder(&self.path, field, total_point_count)?; unchanged.
167-238: Consolidate the redundantid_trackerborrows.Line 167 introduces
id_tracker_borrow, but the function then takes two additional borrows on the same cell: line 177 (&self.id_tracker.borrow()) and line 213 (which also shadows the outer binding).AtomicRefCelltolerates concurrent shared borrows so this isn't a correctness issue, but it's inconsistent with the refactor's stated intent and leaves stale code paths behind.♻️ Proposed cleanup
let mut indexes = if payload_schema.types.is_empty() { let selector = self.selector(&payload_schema.schema); let indexes = selector.new_index( field, &payload_schema.schema, create_if_missing, - &self.id_tracker.borrow(), + &id_tracker_borrow, )?; @@ } else { - let id_tracker_borrow = self.id_tracker.borrow(); payload_schema .types🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@lib/segment/src/index/struct_payload_index.rs` around lines 167 - 238, The code creates multiple borrows of self.id_tracker (id_tracker_borrow at top, then &self.id_tracker.borrow() and a shadowed id_tracker_borrow later); consolidate to a single shared borrow by reusing the outer id_tracker_borrow when calling selector.new_index, selector.new_null_index and selector.new_index_with_type, and remove the inner shadowing declaration so no second borrow variable is created; update calls that currently pass &self.id_tracker.borrow() or the shadowed binding to use &id_tracker_borrow instead, keeping logic around payload_schema.types, selector.new_null_index, selector.new_index_with_type and the rebuild/is_dirty flow unchanged.lib/segment/src/index/field_index/index_selector.rs (1)
121-133: Drop the redundantdirparameter — the selector already owns it.Both
null_builderandnew_null_indextakedir: &Path, butIndexSelector::{Mmap,Gridstore}already carry adirfield, and every call site (this file'snew_index_with_typeas well asstruct_payload_index.rs::{load_from_db, build_field_indexes}) passes exactly that same path. Innew_index_with_typethe redundancy is particularly visible: the match at lines 122-125 extractsdirfromselfjust to feed it back intonew_null_index, which could read it offselfdirectly.Removing the parameter would simplify the API and eliminate the (currently safe, but easy to diverge) contract that callers pass the selector's own directory.
♻️ Proposed shape (signatures shown)
- pub fn null_builder( - &self, - dir: &Path, - field: &JsonPath, - total_point_count: usize, - ) -> OperationResult<FieldIndexBuilder> { - let builder = match self { - IndexSelector::Mmap(_) => FieldIndexBuilder::ImmutableNullIndex( - ImmutableNullIndex::builder(&null_dir(dir, field), total_point_count)?, - ), - IndexSelector::Gridstore(_) => FieldIndexBuilder::MutableNullIndex( - MutableNullIndex::builder(&null_dir(dir, field), total_point_count)?, - ), - }; - Ok(builder) - } + pub fn null_builder( + &self, + field: &JsonPath, + total_point_count: usize, + ) -> OperationResult<FieldIndexBuilder> { + let (dir, is_mmap) = match self { + IndexSelector::Mmap(IndexSelectorMmap { dir, is_on_disk: _ }) => (*dir, true), + IndexSelector::Gridstore(IndexSelectorGridstore { dir }) => (*dir, false), + }; + let null_dir = null_dir(dir, field); + Ok(if is_mmap { + FieldIndexBuilder::ImmutableNullIndex(ImmutableNullIndex::builder(&null_dir, total_point_count)?) + } else { + FieldIndexBuilder::MutableNullIndex(MutableNullIndex::builder(&null_dir, total_point_count)?) + }) + }
new_null_indexand theNullIndexarm ofnew_index_with_typecan follow the same pattern; the call sites then drop thedir/&self.pathargument.Also applies to: 394-445
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@lib/segment/src/index/field_index/index_selector.rs` around lines 121 - 133, The code redundantly passes a dir parameter even though IndexSelector variants (IndexSelector::Mmap / IndexSelector::Gridstore) already own the directory; change the APIs to remove the dir argument: update the signature of new_null_index (and any null_builder helpers) to read the directory from &self instead of receiving dir: &Path, update the NullIndex-related call sites in new_index_with_type (the match arm currently extracting dir) and in struct_payload_index.rs::{load_from_db, build_field_indexes} to stop passing dir/&self.path, and adjust any callers and tests accordingly so the selector-provided dir is used internally. Ensure you update all referenced symbols: new_null_index, null_builder, IndexSelector::{Mmap,Gridstore}, new_index_with_type, load_from_db, build_field_indexes.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@lib/segment/src/index/field_index/index_selector.rs`:
- Around line 121-133: The match arm destructures self with `..` which hides the
`is_on_disk` field; change the pattern to explicitly ignore that field (e.g.,
use `IndexSelector::Mmap(IndexSelectorMmap { dir, is_on_disk: _, .. })` style or
match the existing project convention used elsewhere) so both
IndexSelector::Mmap and IndexSelector::Gridstore destructurings use `is_on_disk:
_` instead of `..`; update the patterns around IndexSelector, IndexSelectorMmap,
and IndexSelectorGridstore in the PayloadIndexType::NullIndex arm that calls
self.new_null_index(field, create_if_missing, id_tracker, index_type.mutability)
accordingly.
- Around line 411-445: The ImmutableNullIndex open path doesn't honor
create_if_missing, causing missing null index dirs to be skipped; update
ImmutableNullIndex::open (and its helper open_immutable) to take a
create_if_missing: bool parameter and pass that flag through to
MutableNullIndex::open, and then update callers (notably
IndexSelector::new_null_index where ImmutableNullIndex::open(...) is invoked) to
forward the create_if_missing argument so the first-load path creates the
directory when requested.
---
Nitpick comments:
In `@lib/segment/src/index/field_index/index_selector.rs`:
- Around line 121-133: The code redundantly passes a dir parameter even though
IndexSelector variants (IndexSelector::Mmap / IndexSelector::Gridstore) already
own the directory; change the APIs to remove the dir argument: update the
signature of new_null_index (and any null_builder helpers) to read the directory
from &self instead of receiving dir: &Path, update the NullIndex-related call
sites in new_index_with_type (the match arm currently extracting dir) and in
struct_payload_index.rs::{load_from_db, build_field_indexes} to stop passing
dir/&self.path, and adjust any callers and tests accordingly so the
selector-provided dir is used internally. Ensure you update all referenced
symbols: new_null_index, null_builder, IndexSelector::{Mmap,Gridstore},
new_index_with_type, load_from_db, build_field_indexes.
In `@lib/segment/src/index/struct_payload_index.rs`:
- Around line 334-340: Remove the duplicated comment line by deleting the
redundant "// Special null index complements every index." immediately above the
call to selector.null_builder in struct_payload_index.rs; keep the initial
explanatory comment block that mentions seeding the null index with
total_point_count and leave the call that computes total_point_count via
self.id_tracker.borrow().total_point_count() and the subsequent let null_index =
selector.null_builder(&self.path, field, total_point_count)?; unchanged.
- Around line 167-238: The code creates multiple borrows of self.id_tracker
(id_tracker_borrow at top, then &self.id_tracker.borrow() and a shadowed
id_tracker_borrow later); consolidate to a single shared borrow by reusing the
outer id_tracker_borrow when calling selector.new_index, selector.new_null_index
and selector.new_index_with_type, and remove the inner shadowing declaration so
no second borrow variable is created; update calls that currently pass
&self.id_tracker.borrow() or the shadowed binding to use &id_tracker_borrow
instead, keeping logic around payload_schema.types, selector.new_null_index,
selector.new_index_with_type and the rebuild/is_dirty flow unchanged.
🪄 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: 2b047d69-4fc2-4f20-988b-0e607ca8f705
📒 Files selected for processing (3)
lib/segment/src/index/field_index/index_selector.rslib/segment/src/index/struct_payload_index.rslib/segment/tests/integration/payload_index_test.rs
There was a problem hiding this comment.
🧹 Nitpick comments (1)
lib/segment/src/index/field_index/index_selector.rs (1)
121-133: Redundantdirextraction; derive it insidenew_null_index.Now that
new_null_index(andnull_builder) take&self, thedircan be derived from the selector inside the method. The manual destructuring in lines 122–125 just to forwarddiris duplicative —null_dir(dir, field)could be computed once insidenew_null_indexfromself, and the same applies tonull_builder(Lines 394–409).♻️ Proposed refactor
(PayloadIndexType::NullIndex, _) => { - let dir = match self { - IndexSelector::Mmap(IndexSelectorMmap { dir, is_on_disk: _ }) => *dir, - IndexSelector::Gridstore(IndexSelectorGridstore { dir }) => *dir, - }; self.new_null_index( - dir, field, create_if_missing, id_tracker, index_type.mutability, )? }And in
new_null_index/null_builder, drop thedir: &Pathparameter and compute it locally:pub fn new_null_index( &self, - dir: &Path, field: &JsonPath, create_if_missing: bool, id_tracker: &IdTrackerEnum, mutability: IndexMutability, ) -> OperationResult<Option<FieldIndex>> { + let dir = match self { + IndexSelector::Mmap(IndexSelectorMmap { dir, is_on_disk: _ }) => *dir, + IndexSelector::Gridstore(IndexSelectorGridstore { dir }) => *dir, + }; let total_point_count = id_tracker.total_point_count(); let null_dir = null_dir(dir, field);🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@lib/segment/src/index/field_index/index_selector.rs` around lines 121 - 133, The code redundantly destructures IndexSelector to extract dir before calling new_null_index; since new_null_index (and null_builder) now take &self they should compute the directory from the selector internally. Remove the manual match that extracts dir in the PayloadIndexType::NullIndex arm (the pattern matching on IndexSelectorMmap/IndexSelectorGridstore) and update new_null_index and null_builder to drop the dir: &Path parameter and derive the directory from self (handling IndexSelectorMmap and IndexSelectorGridstore variants inside those methods) so callers simply pass self, field, create_if_missing, id_tracker, and index_type.mutability.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@lib/segment/src/index/field_index/index_selector.rs`:
- Around line 121-133: The code redundantly destructures IndexSelector to
extract dir before calling new_null_index; since new_null_index (and
null_builder) now take &self they should compute the directory from the selector
internally. Remove the manual match that extracts dir in the
PayloadIndexType::NullIndex arm (the pattern matching on
IndexSelectorMmap/IndexSelectorGridstore) and update new_null_index and
null_builder to drop the dir: &Path parameter and derive the directory from self
(handling IndexSelectorMmap and IndexSelectorGridstore variants inside those
methods) so callers simply pass self, field, create_if_missing, id_tracker, and
index_type.mutability.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 09449c6f-b05d-41a3-b1cd-ed172977600f
📒 Files selected for processing (2)
lib/segment/src/index/field_index/index_selector.rslib/segment/src/index/field_index/null_index/immutable_null_index.rs
✅ Files skipped from review due to trivial changes (1)
- lib/segment/src/index/field_index/null_index/immutable_null_index.rs
There was a problem hiding this comment.
🧹 Nitpick comments (1)
lib/segment/src/index/struct_payload_index.rs (1)
189-198: Optional: extract the selector→default-mutability mapping into a helper.The same mapping exists in
index_selector.rs::new_index(theBoolarm, lines 181‑184) and is implicitly encoded again insideIndexSelector::new_null_index. A smallIndexSelector::default_mutability(&self) -> IndexMutabilitymethod would dedupe the three call sites and make the intent clearer. Note also that for theGridstorecase the value passed here is effectively ignored (new_null_indexalways opensMutableNullIndexfor Gridstore), so the explicitnull_mutabilityhere only matters for theMmapbranch.♻️ Sketch
In
index_selector.rs:impl IndexSelector<'_> { pub fn default_mutability(&self) -> IndexMutability { match self { IndexSelector::Mmap(_) => IndexMutability::Immutable, IndexSelector::Gridstore(_) => IndexMutability::Mutable, } } }Then here:
- let null_mutability = match &selector { - IndexSelector::Mmap(_) => IndexMutability::Immutable, - IndexSelector::Gridstore(_) => IndexMutability::Mutable, - }; if let Some(null_index) = selector.new_null_index( field, create_if_missing, &id_tracker_borrow, - null_mutability, + selector.default_mutability(), )? {🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@lib/segment/src/index/struct_payload_index.rs` around lines 189 - 198, Add a helper method IndexSelector::default_mutability(&self) -> IndexMutability that returns the mapping (Mmap => Immutable, Gridstore => Mutable), then replace the local null_mutability computation in struct_payload_index.rs (the variable currently set from match &selector) with a call to selector.default_mutability(); also update the other call sites (index_selector.rs::new_index Bool arm and anywhere new_null_index is invoked) to use this helper so the mapping is centralized (note new_null_index still controls actual behavior for Gridstore).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@lib/segment/src/index/struct_payload_index.rs`:
- Around line 189-198: Add a helper method
IndexSelector::default_mutability(&self) -> IndexMutability that returns the
mapping (Mmap => Immutable, Gridstore => Mutable), then replace the local
null_mutability computation in struct_payload_index.rs (the variable currently
set from match &selector) with a call to selector.default_mutability(); also
update the other call sites (index_selector.rs::new_index Bool arm and anywhere
new_null_index is invoked) to use this helper so the mapping is centralized
(note new_null_index still controls actual behavior for Gridstore).
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 88c32de3-21c7-47e3-bb47-2186ad148484
📒 Files selected for processing (2)
lib/segment/src/index/field_index/index_selector.rslib/segment/src/index/struct_payload_index.rs
* Immutable null storage index Add `ImmutableNullIndex` that uses segment-level deleted points to update in-memory indexes without modifying the storage. * Integrate `ImmutableNullIndex` * Some refinements * Fix merge conflicts * Apply review suggestions * [ai] Get rid of delegate * Fix tests * Fix reopen null index * code review * nitpicks * cut duplication --------- Co-authored-by: timvisee <[email protected]> Co-authored-by: Arnaud Gourlay <[email protected]>
Depends on #8625
Add
ImmutableNullIndextype that uses immutable storage and segment's deleted bitmask.It would reduce IO on serverless Qdrant instances.
Tasks
All Submissions:
devbranch. Did you create your branch fromdev?New Feature Submissions:
cargo +nightly fmt --allcommand prior to submission?cargo clippy --workspace --all-featurescommand?Changes to Core Features: