Skip to content

Commit

Permalink
fix returned value for roaring64 read from unsafe bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
lemire committed Sep 17, 2024
1 parent 80b4cf2 commit 07dc72f
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 13 deletions.
10 changes: 4 additions & 6 deletions roaring64/roaring64.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,10 @@ func (rb *Bitmap) WriteTo(stream io.Writer) (int64, error) {
func (rb *Bitmap) FromUnsafeBytes(data []byte) (p int64, err error) {
stream := internal.NewByteBuffer(data)
sizeBuf := make([]byte, 8)
n, err := stream.Read(sizeBuf)
_, err = stream.Read(sizeBuf)
if err != nil {
return 0, err
}
p += int64(n)
size := binary.LittleEndian.Uint64(sizeBuf)

rb.highlowcontainer.resize(0)
Expand All @@ -115,17 +114,16 @@ func (rb *Bitmap) FromUnsafeBytes(data []byte) (p int64, err error) {
if err != nil {
return 0, fmt.Errorf("error in bitmap.UnsafeFromBytes: could not read key #%d: %w", i, err)
}
p += 4
rb.highlowcontainer.keys[i] = binary.LittleEndian.Uint32(keyBuf)
rb.highlowcontainer.containers[i] = roaring.NewBitmap()
n, err := rb.highlowcontainer.containers[i].ReadFrom(stream)

if n == 0 || err != nil {
return int64(n), fmt.Errorf("Could not deserialize bitmap for key #%d: %s", i, err)
}
p += int64(n)
}

return p, nil
return stream.GetReadBytes(), nil
}

// ReadFrom reads a serialized version of this bitmap from stream.
Expand Down Expand Up @@ -167,12 +165,12 @@ func (rb *Bitmap) ReadFrom(stream io.Reader) (p int64, err error) {
rb.highlowcontainer.keys[i] = binary.LittleEndian.Uint32(keyBuf)
rb.highlowcontainer.containers[i] = roaring.NewBitmap()
n, err := rb.highlowcontainer.containers[i].ReadFrom(stream)

if n == 0 || err != nil {
return int64(n), fmt.Errorf("Could not deserialize bitmap for key #%d: %s", i, err)
}
p += int64(n)
}

return p, nil
}

Expand Down
1 change: 0 additions & 1 deletion roaring64/roaring64cow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1481,7 +1481,6 @@ func TestBigRandomCOW(t *testing.T) {
}

func rTestCOW(t *testing.T, N int) {
log.Println("rtest N=", N)
for gap := 1; gap <= 65536; gap *= 2 {
bs1 := bitset.New(0)
rb1 := NewBitmap()
Expand Down
4 changes: 2 additions & 2 deletions roaring64/serialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ import (
func fromUnsafeBytesChecked(t *testing.T, unsafeBm *Bitmap, b []byte) {
var r bytes.Reader
r.Reset(b)
var safeBm Bitmap
safeBm := NewBitmap()
safeCount, err := safeBm.ReadFrom(&r)
unsafeCount, err := unsafeBm.FromUnsafeBytes(b)
require.NoError(t, err)
require.NoError(t, err)
assert.EqualValues(t, safeCount, unsafeCount)
assert.True(t, safeBm.Equals(unsafeBm))
assert.EqualValues(t, safeCount, unsafeCount)
}

func TestSerializationOfEmptyBitmap(t *testing.T) {
Expand Down
3 changes: 0 additions & 3 deletions roaring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ func checkValidity(t *testing.T, rb *Bitmap) {
}

func hashTest(t *testing.T, N uint64) {
t.Log("rtest N=", N)

hashes := map[uint64]struct{}{}
count := 0

Expand Down Expand Up @@ -1944,7 +1942,6 @@ func TestHash(t *testing.T) {
}

func rTest(t *testing.T, N int) {
t.Log("rtest N=", N)
for gap := 1; gap <= 65536; gap *= 2 {
bs1 := bitset.New(0)
rb1 := NewBitmap()
Expand Down
1 change: 0 additions & 1 deletion roaringcow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1533,7 +1533,6 @@ func TestBigRandomCOW(t *testing.T) {
}

func rTestCOW(t *testing.T, N int) {
t.Log("rtest N=", N)
for gap := 1; gap <= 65536; gap *= 2 {
bs1 := bitset.New(0)
rb1 := NewBitmap()
Expand Down

0 comments on commit 07dc72f

Please sign in to comment.