Skip to content

fix(partition): support map keys in put_newer#21

Open
SAY-5 wants to merge 2 commits into
appcues:mainfrom
SAY-5:fix/put-newer-map-key
Open

fix(partition): support map keys in put_newer#21
SAY-5 wants to merge 2 commits into
appcues:mainfrom
SAY-5:fix/put-newer-map-key

Conversation

@SAY-5

@SAY-5 SAY-5 commented Jun 18, 2026

Copy link
Copy Markdown

Closes #18.

put_newer/5 raised ArgumentError ("not a valid match specification") whenever the key contained a map, because ETS rejects a raw map at the key position of a select_replace/2 match head. This binds the key to a match variable and compares it in the guard instead, which works for any key shape. Added a regression test alongside the existing tuple/list-key cases and updated the changelog.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed an issue where put_newer/5 and put_all_newer/3 could raise an ArgumentError when entry keys contained maps.
    • Map-containing keys (including maps nested inside list/tuple structures) are now correctly compared and updated, preserving “latest version wins” behavior.
  • Tests

    • Added coverage to verify correct stored values, versioning, and update/telemetry results for map-based keys in put_newer/5.

Signed-off-by: Sai Asish Y <say.apm35@gmail.com>
@coderabbitai

coderabbitai Bot commented Jun 18, 2026

Copy link
Copy Markdown

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 Plus

Run ID: 47f3a219-f8a4-42aa-8915-887c82265235

📥 Commits

Reviewing files that changed from the base of the PR and between f56e892 and 84891ed.

📒 Files selected for processing (2)
  • lib/partitioned_buffer/partition.ex
  • test/partitioned_buffer/map_test.exs
🚧 Files skipped from review as they are similar to previous changes (1)
  • lib/partitioned_buffer/partition.ex

📝 Walkthrough

Walkthrough

replace_match_spec/3 now selects a literal or guarded ETS match specification based on whether the key contains a map. Guarded specifications bind the stored key to a match variable and compare it with ms_literal(key). Tests cover direct and nested map keys with increasing versions, and the changelog documents the fix with a reference to issue #18.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: adding map-key support to put_newer.
Linked Issues check ✅ Passed The PR addresses issue #18 by preventing the match-spec crash for map-containing keys and adds regression coverage.
Out of Scope Changes check ✅ Passed The changes are limited to the bug fix, regression tests, and changelog update described by the issue.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 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/partitioned_buffer/partition.ex`:
- Around line 520-538: The current implementation of replace_match_spec uses a
guard-based key equality check with key: :"$3", which prevents ETS from using
its O(1) keyed lookup optimization on :set tables and forces a full table scan
instead. To fix this, implement a conditional match spec that uses the original
literal-key approach in the match head for non-map keys (which enables the fast
lookup), and only falls back to the guard-based approach with ms_literal(key)
for keys that contain maps (which would otherwise cause ETS to reject the match
spec). This way, the fast path is preserved for the common case while still
handling map-containing keys correctly.
🪄 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 Plus

Run ID: 6d511c27-ffd9-4e7b-89b2-7cca28c7c652

📥 Commits

Reviewing files that changed from the base of the PR and between fdcd7b0 and f56e892.

📒 Files selected for processing (3)
  • CHANGELOG.md
  • lib/partitioned_buffer/partition.ex
  • test/partitioned_buffer/map_test.exs

Comment thread lib/partitioned_buffer/partition.ex
Signed-off-by: Sai Asish Y <say.apm35@gmail.com>
@SAY-5

SAY-5 commented Jul 20, 2026

Copy link
Copy Markdown
Author

Split the match spec so non-map keys keep the literal-key head (single keyed lookup on the set table), and only keys that actually contain a map fall back to the $3 guard form that avoids the ETS ArgumentError. Added a regression test for a map nested inside a list key; full suite is green.

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.

put_newer crashes with "not a valid match specification" when the key contains a map

1 participant