Skip to content

Commit

Permalink
refactor(varenc): 🎨 Simplify bit length manipulation logic in varint …
Browse files Browse the repository at this point in the history
…encoding and decoding for better readability
  • Loading branch information
amourao committed Dec 3, 2024
1 parent a1a6d44 commit c6c0041
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 20 deletions.
4 changes: 2 additions & 2 deletions adapters/repos/db/lsmkv/compaction_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ func TestCompaction(t *testing.T) {
{
name: "compactionInvertedStrategy",
f: func(ctx context.Context, t *testing.T, opts []BucketOption) {
compactionInvertedStrategy(ctx, t, opts, 8627, 8627)
compactionInvertedStrategy(ctx, t, opts, 8629, 8629)
},
opts: []BucketOption{
WithStrategy(StrategyInverted),
Expand All @@ -404,7 +404,7 @@ func TestCompaction(t *testing.T) {
{
name: "compactionInvertedStrategy_KeepTombstones",
f: func(ctx context.Context, t *testing.T, opts []BucketOption) {
compactionInvertedStrategy(ctx, t, opts, 8931, 8931)
compactionInvertedStrategy(ctx, t, opts, 8933, 8933)
},
opts: []BucketOption{
WithStrategy(StrategyInverted),
Expand Down
27 changes: 9 additions & 18 deletions adapters/repos/db/lsmkv/varenc/varint.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ func decodeReusable(deltas []uint64, packed []byte, deltaDiff bool) {
deltas[0] = binary.BigEndian.Uint64(packed[0:8])

// Read bitsNeeded (6 bits starting from bit 2 of packed[8])
bitsNeeded := int((packed[8] >> 2) & 0x3F)
bitsNeeded := int((packed[8]))
if bitsNeeded == 0 || bitsNeeded > 64 {
// Handle invalid bitsNeeded
return
}

// Starting bit position after reading bitsNeeded
bitPos := 6
bytePos := 8 // Start from packed[8]
// Set up the buffer to the beginning of next byte
bytePos := 9

// Initialize the bit buffer with the remaining bits in packed[8], if any
bitsLeft := 8 - bitPos
bitBuffer := uint64(packed[bytePos] & ((1 << bitsLeft) - 1))

bitBuffer := uint64(packed[bytePos])
bitsLeft := 8

bytePos++

Expand Down Expand Up @@ -89,18 +89,9 @@ func encodeReusable(deltas []uint64, packed []byte, deltaDiff bool) int {

bitsToStore := uint64(bitsNeeded)

currentByte |= byte((bitsToStore>>(5-bitPos))&1) << (7 - bitPos)
bitPos++
currentByte |= byte((bitsToStore>>(5-bitPos))&1) << (7 - bitPos)
bitPos++
currentByte |= byte((bitsToStore>>(5-bitPos))&1) << (7 - bitPos)
bitPos++
currentByte |= byte((bitsToStore>>(5-bitPos))&1) << (7 - bitPos)
bitPos++
currentByte |= byte((bitsToStore>>(5-bitPos))&1) << (7 - bitPos)
bitPos++
currentByte |= byte((bitsToStore>>(5-bitPos))&1) << (7 - bitPos)
bitPos++
packed[currentByteIndex] = byte(bitsToStore)
currentByteIndex++
bitPos = 0

for i, delta := range deltas[1:] {
if deltaDiff {
Expand Down

0 comments on commit c6c0041

Please sign in to comment.