Skip to content

refactor: extract collection-write helpers in ember-core - #127

Merged
kacy merged 2 commits into
mainfrom
refactor/core-collection-helpers
Feb 14, 2026
Merged

refactor: extract collection-write helpers in ember-core#127
kacy merged 2 commits into
mainfrom
refactor/core-collection-helpers

Conversation

@kacy

@kacy kacy commented Feb 14, 2026

Copy link
Copy Markdown
Owner

summary

  • extracted four helpers in keyspace.rs that capture the shared pattern across list_push, zadd, hset, and sadd: type guard, memory reservation, empty entry creation, and size tracking
  • added AddResult::UNCHANGED constant in sorted_set.rs and collapsed the nx/gt/lt/eq flag checks into a single guard
  • simplified zrem with let-else pattern
  • added comment on lrange's empty-range check

what was tested

  • cargo test -p emberkv-core — all 329 tests pass
  • cargo clippy -p emberkv-core -- -D warnings — clean

design 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_size uses 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.

kacy added 2 commits February 14, 2026 17:01
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
kacy merged commit e115eab into main Feb 14, 2026
6 of 7 checks passed
@kacy
kacy deleted the refactor/core-collection-helpers branch February 14, 2026 22:21
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.
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