Skip to content

refactor(map_index): extract inner state; add ReadOnlyAppendableMapIndex skeleton - #9020

Merged
generall merged 2 commits into
devfrom
refactor/map-index-read-only-appendable-mmap
May 12, 2026
Merged

generall merged 2 commits into
devfrom
refactor/map-index-read-only-appendable-mmap

Conversation

@generall

Copy link
Copy Markdown
Member

Summary

  • Extract MutableMapIndex<N>'s four in-memory fields (map, point_to_values, indexed_points, values_count) into a shared inner struct MutableMapIndexInner<N> in mutable_map_index/inner.rs. The MapIndexRead<N> impl moves wholesale onto the inner.
  • MutableMapIndex<N> becomes a thin wrapper { inner, storage: Gridstore<...> }. Its MapIndexRead<N> impl forwards to the inner. The Gridstore load loop in open_gridstore collapses to MutableMapIndexInner::empty() + inner.ingest(idx, value) per row.
  • Add mutable_map_index/read_only/ containing ReadOnlyAppendableMapIndex<N> — same inner + GridstoreReader<Vec<...>, MmapFile> instead of Gridstore. Its MapIndexRead<N> impl forwards to the same inner, so the read-method bodies are shared across the two storage variants.
  • No constructor / loader yet — the read-only variant's lifecycle (open, files, populate, …) will land in a follow-up. The storage field carries an #[allow(dead_code)] until then.

Pure refactor — no behavioural change.

Test plan

  • cargo check -p segment --tests — clean
  • cargo test -p segment --lib index::field_index::map_index:: — all 17 existing tests pass unchanged
  • Follow-up PR will add the ReadOnlyAppendableMapIndex::open(path) constructor and a cross-load test that writes via MutableMapIndex, reopens via ReadOnlyAppendableMapIndex, and asserts read parity through the shared inner.

…ableMapIndex

Extract the four in-memory fields (`map`, `point_to_values`, `indexed_points`,
`values_count`) of `MutableMapIndex<N>` into a shared inner struct
`MutableMapIndexInner<N>` under `mutable_map_index/inner.rs`. The
`MapIndexRead<N>` impl moves wholesale onto the inner. `MutableMapIndex<N>`
becomes a thin wrapper `{ inner, storage: Gridstore<...> }`; its
`MapIndexRead<N>` impl forwards to the inner. The Gridstore load loop in
`open_gridstore` collapses to `MutableMapIndexInner::empty()` +
`inner.ingest(idx, value)` per row, sharing the per-value ingestion path with
the future read-only loader.

Add a new sibling `mutable_map_index/read_only/` module containing
`ReadOnlyAppendableMapIndex<N>` (inner + `GridstoreReader<Vec<...>, MmapFile>`),
with its `MapIndexRead<N>` impl forwarding to the same inner. No constructor
yet — loading and lifecycle for the read-only variant will land in a
follow-up. Field `storage` is held to pin the on-disk type and tagged
`#[allow(dead_code)]` until then.

No behavioural change. The 17 existing map_index tests pass unchanged.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
lib/segment/src/index/field_index/map_index/mutable_map_index/lifecycle.rs (1)

103-109: ⚡ Quick win

Use explicit From conversions in add_many_to_map.

Please replace implicit .into() conversions here with explicit <Owned>::from(...) to match the Rust guideline used in this repo.

Suggested diff
-        for value in values.clone() {
-            let entry = self.inner.map.entry(value.into());
+        for value in values.clone() {
+            let entry = self
+                .inner
+                .map
+                .entry(<N as MapIndexKey>::Owned::from(value));
             self.inner.point_to_values[idx as usize].push(entry.key().clone());
             entry.or_default().insert(idx);
         }

-        let values = values.into_iter().map(Into::into).collect::<Vec<_>>();
+        let values = values
+            .into_iter()
+            .map(<N as MapIndexKey>::Owned::from)
+            .collect::<Vec<_>>();

As per coding guidelines **/*.rs: "Prefer explicit SomeType::from(x) over implicit x.into() in Rust".

🤖 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/mutable_map_index/lifecycle.rs`
around lines 103 - 109, In add_many_to_map, replace implicit .into() calls with
explicit From conversions: use the concrete owned key type's from (e.g.,
<Owned>::from(value)) when calling self.inner.map.entry(...) and when pushing to
self.inner.point_to_values, and replace values.into_iter().map(Into::into) with
values.into_iter().map(<Owned>::from). Update all occurrences in this function
(entry(...) and the values mapping) to use the explicit <Owned>::from(...) form
so it matches the repo's Rust guideline.
🤖 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.

Nitpick comments:
In `@lib/segment/src/index/field_index/map_index/mutable_map_index/lifecycle.rs`:
- Around line 103-109: In add_many_to_map, replace implicit .into() calls with
explicit From conversions: use the concrete owned key type's from (e.g.,
<Owned>::from(value)) when calling self.inner.map.entry(...) and when pushing to
self.inner.point_to_values, and replace values.into_iter().map(Into::into) with
values.into_iter().map(<Owned>::from). Update all occurrences in this function
(entry(...) and the values mapping) to use the explicit <Owned>::from(...) form
so it matches the repo's Rust guideline.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 380bf8f2-7598-4076-b71f-09a52da3c673

📥 Commits

Reviewing files that changed from the base of the PR and between 7fd671c and 41652f4.

📒 Files selected for processing (6)
  • lib/segment/src/index/field_index/map_index/mutable_map_index/inner.rs
  • lib/segment/src/index/field_index/map_index/mutable_map_index/lifecycle.rs
  • lib/segment/src/index/field_index/map_index/mutable_map_index/mod.rs
  • lib/segment/src/index/field_index/map_index/mutable_map_index/read_only/mod.rs
  • lib/segment/src/index/field_index/map_index/mutable_map_index/read_only/read_ops.rs
  • lib/segment/src/index/field_index/map_index/mutable_map_index/read_ops.rs

@coderabbitai

coderabbitai Bot commented May 12, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack
No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 8476bf20-4832-409e-ba66-e593ace9eaa0

📥 Commits

Reviewing files that changed from the base of the PR and between 41652f4 and 955bc96.

📒 Files selected for processing (3)
  • lib/segment/src/index/field_index/map_index/mmap_map_index/mod.rs
  • lib/segment/src/index/field_index/map_index/mutable_map_index/read_only/mod.rs
  • lib/segment/src/index/field_index/map_index/mutable_map_index/read_only/read_ops.rs
🚧 Files skipped from review as they are similar to previous changes (2)
  • lib/segment/src/index/field_index/map_index/mutable_map_index/read_only/mod.rs
  • lib/segment/src/index/field_index/map_index/mutable_map_index/read_only/read_ops.rs

📝 Walkthrough

Walkthrough

This PR refactors MutableMapIndex to extract its in-memory indexing state into a reusable MutableMapIndexInner<N> struct. The mutable index now delegates to this inner state for all queries and mutations. A new read-only variant, ReadOnlyAppendableMapIndex<N, S>, shares the same in-memory backing while adding a GridstoreReader for on-disk access. Additionally, MmapMapIndex and its internal Storage struct are parameterized over a generic UniversalRead source, enabling flexible storage backends beyond mmap-only file access.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • qdrant/qdrant#8964: Depends on changes that make GridstoreReader and related storage types generic over S: UniversalRead, required for the read-only appendable map index parameterization in this PR.

Suggested labels

release:1.18.0

Suggested reviewers

  • xzfc
  • ffuugoo
  • timvisee
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately captures the main refactoring work: extracting inner state into MutableMapIndexInner and introducing the ReadOnlyAppendableMapIndex skeleton.
Description check ✅ Passed The description is comprehensive and directly related to the changeset, clearly explaining the refactoring objectives, structural changes, test results, and future work.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/map-index-read-only-appendable-mmap

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@generall
generall merged commit 6500879 into dev May 12, 2026
15 checks passed
@generall
generall deleted the refactor/map-index-read-only-appendable-mmap branch May 12, 2026 17:02
@coderabbitai coderabbitai Bot mentioned this pull request May 12, 2026
generall added a commit that referenced this pull request May 22, 2026
…dex skeleton (#9020)

* refactor(map_index): extract MutableMapIndexInner; add ReadOnlyAppendableMapIndex

Extract the four in-memory fields (`map`, `point_to_values`, `indexed_points`,
`values_count`) of `MutableMapIndex<N>` into a shared inner struct
`MutableMapIndexInner<N>` under `mutable_map_index/inner.rs`. The
`MapIndexRead<N>` impl moves wholesale onto the inner. `MutableMapIndex<N>`
becomes a thin wrapper `{ inner, storage: Gridstore<...> }`; its
`MapIndexRead<N>` impl forwards to the inner. The Gridstore load loop in
`open_gridstore` collapses to `MutableMapIndexInner::empty()` +
`inner.ingest(idx, value)` per row, sharing the per-value ingestion path with
the future read-only loader.

Add a new sibling `mutable_map_index/read_only/` module containing
`ReadOnlyAppendableMapIndex<N>` (inner + `GridstoreReader<Vec<...>, MmapFile>`),
with its `MapIndexRead<N>` impl forwarding to the same inner. No constructor
yet — loading and lifecycle for the read-only variant will land in a
follow-up. Field `storage` is held to pin the on-disk type and tagged
`#[allow(dead_code)]` until then.

No behavioural change. The 17 existing map_index tests pass unchanged.

* expose storage generic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant