refactor: extract collection-write helpers in ember-core - #127
Merged
Conversation
the add_with_flags method had 5 identical `AddResult { added: false,
updated: false }` returns scattered across separate if-blocks. added a
const UNCHANGED and collapsed nx/gt/lt/eq checks into a single guard.
list_push, zadd, hset, and sadd all followed the same 10-step pattern: expire check, type guard, memory estimation, limit enforcement, empty entry creation, old size measurement, mutation, new size measurement, memory adjustment, touch. extracted four helpers that capture the shared steps: - ensure_collection_type(): is-new check + type guard - reserve_memory(): estimate increase + enforce limit - insert_empty(): create entry with empty collection - track_size(): measure before/after + adjust tracker also simplified zrem with let-else and added a comment on lrange's empty-range check. each collection-write function is now ~15 lines instead of ~50.
kacy
added a commit
that referenced
this pull request
Feb 19, 2026
* refactor: add AddResult::UNCHANGED, collapse sorted_set flag checks
the add_with_flags method had 5 identical `AddResult { added: false,
updated: false }` returns scattered across separate if-blocks. added a
const UNCHANGED and collapsed nx/gt/lt/eq checks into a single guard.
* refactor: extract collection-write helpers in keyspace
list_push, zadd, hset, and sadd all followed the same 10-step pattern:
expire check, type guard, memory estimation, limit enforcement, empty
entry creation, old size measurement, mutation, new size measurement,
memory adjustment, touch.
extracted four helpers that capture the shared steps:
- ensure_collection_type(): is-new check + type guard
- reserve_memory(): estimate increase + enforce limit
- insert_empty(): create entry with empty collection
- track_size(): measure before/after + adjust tracker
also simplified zrem with let-else and added a comment on lrange's
empty-range check. each collection-write function is now ~15 lines
instead of ~50.
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
keyspace.rsthat capture the shared pattern acrosslist_push,zadd,hset, andsadd: type guard, memory reservation, empty entry creation, and size trackingAddResult::UNCHANGEDconstant insorted_set.rsand collapsed the nx/gt/lt/eq flag checks into a single guardzremwith let-else patternwhat was tested
cargo test -p emberkv-core— all 329 tests passcargo clippy -p emberkv-core -- -D warnings— cleandesign considerations
the four helpers (
ensure_collection_type,reserve_memory,insert_empty,track_size) map directly to the steps that were duplicated across all collection-write functions.track_sizeuses a closure to measure before/after sizes, which keeps the mutation logic inline while centralizing the bookkeeping. this avoids the alternative of splitting each function into prepare/mutate/finalize phases which would obscure the flow.