Skip to content

Conversation

jhernandezb
Copy link

@jhernandezb jhernandezb commented Aug 8, 2025

Description

Implements IterateRaw() missing for Multi index

Closes: #25122

Summary by CodeRabbit

  • New Features

    • Introduced a new low-level iteration method for advanced access to raw index keys in multi-index collections.
  • Tests

    • Added tests covering multi-indexes with composite primary keys and the new raw iteration functionality, ensuring correct behavior in various scenarios.

Copy link
Contributor

coderabbitai bot commented Aug 8, 2025

📝 Walkthrough

Walkthrough

A new IterateRaw method was added to the Multi index type in collections/indexes/multi.go, enabling raw iteration over the underlying key set. Corresponding tests were introduced in multi_test.go to verify the behavior of IterateRaw, including composite primary key scenarios and iteration order.

Changes

Cohort / File(s) Change Summary
Multi Index: IterateRaw Implementation
collections/indexes/multi.go
Added IterateRaw method to the Multi type, delegating to the underlying refKeys.IterateRaw for raw key iteration.
Testing: Multi Index Raw Iteration and Composite Keys
collections/indexes/multi_test.go
Added two new test functions: one for composite primary keys (TestMulti_PairPrimaryKey), and one for IterateRaw behavior in both directions (TestMulti_IterateRaw).

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant MultiIndex
    participant RefKeys

    Client->>MultiIndex: IterateRaw(ctx, start, end, order)
    MultiIndex->>RefKeys: IterateRaw(ctx, start, end, order)
    RefKeys-->>MultiIndex: Iterator
    MultiIndex-->>Client: Iterator
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Assessment against linked issues

Objective Addressed Explanation
Add IterateRaw support to Multi index (#25122)
Ensure compliance with Collection[K, V any] interface for paginated queries (#25122)

Assessment against linked issues: Out-of-scope changes

No out-of-scope changes detected.

Note

🔌 MCP (Model Context Protocol) integration is now available in Early Access!

Pro users can now connect to remote MCP servers under the Integrations page to get reviews and chat conversations that understand additional development context.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (4)
collections/indexes/multi.go (1)

122-129: Add a doc comment for IterateRaw

Briefly document delegation, caller Close() responsibility, and that semantics mirror KeySet.IterateRaw.

+// IterateRaw exposes raw iteration over the underlying index key space.
+// It delegates to the embedded KeySet. The returned iterator must be closed
+// by the caller. Range and ordering semantics follow KeySet.IterateRaw.
 func (m *Multi[ReferenceKey, PrimaryKey, Value]) IterateRaw(
 	ctx context.Context, start, end []byte, order collections.Order,
 ) (
 	iter collections.Iterator[collections.Pair[ReferenceKey, PrimaryKey], collections.NoValue], err error,
 ) {
 	return m.refKeys.IterateRaw(ctx, start, end, order)
 }
collections/indexes/multi_test.go (3)

170-174: Use ErrNotFound for new inserts in lazyOldValue

Returning a non-error from lazyOldValue implies an existing value and triggers unreference(). For fresh inserts, return ErrNotFound to avoid relying on Remove semantics.

- err := mi.Reference(ctx, ref, c, func() (company, error) { return c, nil })
+ err := mi.Reference(ctx, ref, c, func() (company, error) { return company{}, collections.ErrNotFound })

176-190: Strengthen assertions to validate ordering and content

These loops only assert non-empty keys and counts. Consider asserting the actual sequence to verify ascending vs. descending order and key correctness, catching ordering regressions.

I can propose concrete assertions comparing the collected slices from both iterations to ensure they are exact reversals and match expected key tuples.

Also applies to: 191-205


206-219: Add a raw-range test for IterateRaw

This block uses MatchExact (typed range) rather than exercising IterateRaw’s start/end range. Add a test deriving start/end for a single reference key (e.g., "milan") via the key codec so IterateRaw is validated for bounded scans too.

If you confirm the preferred helper(s) for deriving raw range bounds from the pair key codec, I can provide a drop-in test snippet.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
Knowledge Base: Disabled due to Reviews > Disable Knowledge Base setting

📥 Commits

Reviewing files that changed from the base of the PR and between bb1dcdc and c3712c1.

📒 Files selected for processing (2)
  • collections/indexes/multi.go (1 hunks)
  • collections/indexes/multi_test.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Summary
🔇 Additional comments (1)
collections/indexes/multi.go (1)

122-129: LGTM: IterateRaw passthrough is correct

Directly delegating to refKeys.IterateRaw is the right behavior here and brings Multi in line with the Collection interface for raw iteration.

Comment on lines +131 to +139
iter, err := mi.MatchExact(ctx, uint64(1))
require.NoError(t, err)
pks, err := iter.PrimaryKeys()
require.NoError(t, err)
expectedPks := []collections.Pair[uint64, uint64]{
collections.Join(uint64(1), uint64(1)),
collections.Join(uint64(2), uint64(1)),
}
require.Equal(t, expectedPks, pks)
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Ensure iterator from MatchExact is closed

The iterator created at Line 131 isn’t closed. Add a defer right after the error check.

 iter, err := mi.MatchExact(ctx, uint64(1))
 require.NoError(t, err)
+defer iter.Close()
 pks, err := iter.PrimaryKeys()
📝 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.

Suggested change
iter, err := mi.MatchExact(ctx, uint64(1))
require.NoError(t, err)
pks, err := iter.PrimaryKeys()
require.NoError(t, err)
expectedPks := []collections.Pair[uint64, uint64]{
collections.Join(uint64(1), uint64(1)),
collections.Join(uint64(2), uint64(1)),
}
require.Equal(t, expectedPks, pks)
iter, err := mi.MatchExact(ctx, uint64(1))
require.NoError(t, err)
defer iter.Close()
pks, err := iter.PrimaryKeys()
require.NoError(t, err)
expectedPks := []collections.Pair[uint64, uint64]{
collections.Join(uint64(1), uint64(1)),
collections.Join(uint64(2), uint64(1)),
}
require.Equal(t, expectedPks, pks)
🤖 Prompt for AI Agents
In collections/indexes/multi_test.go around lines 131 to 139, the iterator
returned by mi.MatchExact is not closed, which can lead to resource leaks. After
checking that err is nil, add a defer statement to close the iterator to ensure
it is properly released after use.


rawIter, err := mi.IterateRaw(ctx, nil, nil, collections.OrderAscending)
require.NoError(t, err)
defer iter.Close()
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Bug: Closing the wrong iterator (resource leak)

Line 143 defers iter.Close(), but the iterator created at Line 141 is rawIter. rawIter is never closed.

- defer iter.Close()
+ defer rawIter.Close()
📝 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.

Suggested change
defer iter.Close()
defer rawIter.Close()
🤖 Prompt for AI Agents
In collections/indexes/multi_test.go at line 143, the deferred call incorrectly
closes iter instead of rawIter, causing a resource leak. Replace defer
iter.Close() with defer rawIter.Close() to ensure the correct iterator is closed
and resources are properly released.

@aljo242 aljo242 self-assigned this Aug 11, 2025
@jhernandezb
Copy link
Author

@aljo242 any chance to review this anytime soon?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

collections: lack of support for IterateRaw in Multi Index
2 participants