Skip to content

feat: bitmap commands (GETBIT, SETBIT, BITCOUNT, BITPOS, BITOP) - #316

Merged
kacy merged 1 commit into
mainfrom
feat/bitmap-commands
Feb 26, 2026
Merged

feat: bitmap commands (GETBIT, SETBIT, BITCOUNT, BITPOS, BITOP)#316
kacy merged 1 commit into
mainfrom
feat/bitmap-commands

Conversation

@kacy

@kacy kacy commented Feb 26, 2026

Copy link
Copy Markdown
Owner

summary

adds the five redis-compatible bitmap commands backed by Value::String storage with big-endian bit ordering. bitmaps don't introduce a new value type — they reuse the existing string variant, just like redis.

GETBIT — returns bit at offset, 0 for missing keys
SETBIT — sets a bit, auto-extends the string with zero bytes, returns old value
BITCOUNT — counts set bits; supports both byte-range and bit-range (BIT unit, redis 7.0 compatible)
BITPOS — finds the first set or clear bit; returns -1 when not found
BITOP — applies AND/OR/XOR/NOT across multiple source keys, result length equals the longest source; missing keys are treated as zero-filled

bit ordering is big-endian (bit 0 = MSB of byte 0), matching redis semantics.

persistence uses two new AOF record tags (33=SETBIT, 34=BITOP). because ember-persistence does not depend on ember-protocol, BitOpKind is encoded as a u8 in AofRecord (0=AND, 1=OR, 2=XOR, 3=NOT) and decoded without importing the enum.

also fixes a pre-existing arity mismatch in shard unit tests — the expired_tx parameter was added in #313 but test call sites weren't updated.

what was tested

  • 20 unit tests in crates/ember-core/src/keyspace/bitmap.rs covering all five commands, wrong-type errors, boundary conditions, and bit-range unit semantics
  • 15 integration tests in tests/integration/src/bitmap.rs covering the full round-trip through the server: setbit/getbit, auto-extension, big-endian byte check, bitcount with byte and bit ranges, bitpos on missing/set/clear, bitop AND/OR/XOR/NOT, result-length semantics, and missing-source zero-fill
  • cargo test -p emberkv-core — 560 passed, 0 failed
  • cargo clippy -p ember-protocol -p emberkv-core -p ember-server -- -D warnings — clean
  • cargo fmt --all — applied

design considerations

storing bitmaps as strings keeps the value type count flat and avoids any migration concerns. the bit-range BITCOUNT operates bit-by-bit rather than rounding to byte boundaries, which is correct per the redis spec but slightly slower for large ranges. for the sizes typically used (bitmaps rarely exceed a few MB), this is not a concern.

implements all five redis-compatible bitmap commands backed by
Value::String storage with big-endian bit ordering (bit 0 = MSB of
byte 0). no new value type — bitmaps reuse the existing string type.

- GETBIT / SETBIT with auto-extension and old-value return
- BITCOUNT with byte-range and bit-range (BIT unit) support
- BITPOS finds first set/clear bit; returns -1 when not found
- BITOP (AND, OR, XOR, NOT) operates across arbitrary source keys,
  result length equals the longest source
- AOF persistence for SETBIT and BITOP (tags 33 and 34)
- recovery replay in ember-persistence without cross-crate dep
  (BitOpKind encoded as u8 in AofRecord)
- fix pre-existing spawn_shard arity mismatch in shard unit tests
  (expired_tx param added in #313 but tests not updated)
- 15 integration tests + 20 unit tests covering edge cases
@kacy
kacy merged commit aa23762 into main Feb 26, 2026
5 of 7 checks passed
@kacy
kacy deleted the feat/bitmap-commands branch February 26, 2026 20:33
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