refactor: reduce command wiring boilerplate#344
Merged
Conversation
introduce a generic route_to_shard() helper that handles the repetitive send-to-shard → match WrongType/OOM/Err/channel-error boilerplate that every single-key command handler duplicated. paired with ~12 response mapper functions (resp_string_value, resp_integer, resp_len, etc.) that cover the common ShardResponse → Frame conversions. rewrites ~70 handlers across strings.rs, hashes.rs, sets.rs, sorted_sets.rs, lists.rs, and keyspace.rs from 8-15 lines to 3-5 lines each. complex multi-shard handlers (mget, mset, scan, smove, sintercard, bitop, lmpop, zmpop, etc.) are left unchanged. net reduction: ~310 lines. adding a new single-key command handler now takes 3 lines instead of 10-15.
…macro the macro generates both the enum and its is_write() method from read/write variant groupings. adding a new command forces you to place it in the correct group — the compiler enforces it. also fixes a bug where SetRange was missing from is_write(), meaning SETRANGE mutations would not be rejected when the AOF disk was full.
replaces 153 verbose CommandEntry structs (7 lines each) with one-line cmd!() invocations. the table is now scannable at a glance and each new command is a single line to add.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
summary
three targeted refactors that eliminate mechanical repetition in the command wiring layer without changing architecture or behavior:
route_to_shard()+ response mappers — a generic helper that handles send + common error mapping (WrongType, OutOfMemory, channel errors), plus ~12 standard response mapper functions (resp_integer,resp_len,resp_ok, etc.). rewrites ~70 single-key exec handlers from 10-20 lines to 3-5 lines each. complex multi-shard handlers (mget, mset, scan, blocking ops) are unchanged.shard_request!macro — generates both theShardRequestenum and itsis_write()method from read/write variant groupings. adding a new command forces you to declare read vs write upfront — the compiler enforces it. also fixes a latent bug whereSetRangewas missing fromis_write().cmd!macro for command table — replaces 153 verboseCommandEntrystructs (7 lines each) with one-linecmd!()invocations, making the table scannable at a glance.net result: ~1,800 lines removed across 9 files. adding a new single-key command handler goes from 10-20 lines to 3-5 lines.
what was tested
cargo build— full workspace compiles cleancargo test -p emberkv-core -p ember-server -p ember-protocol— all 740+ tests passcargo clippy— no warningscargo fmt— formatted