Skip to content

Commit

Permalink
Use Marshal and Unmarshal functionality
Browse files Browse the repository at this point in the history
Change up the structure to use Marshal and Unmarshal functions
instead of the Read Write functions. Small difference, the
Unmarshal function returns a BitArray, instead of taking a pointer
to a BitArray and populating it, like the encoding/json library.
  • Loading branch information
evanh committed Aug 4, 2015
1 parent ef1ae27 commit 054d629
Show file tree
Hide file tree
Showing 6 changed files with 353 additions and 189 deletions.
69 changes: 0 additions & 69 deletions bitarray/bitarray.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ efficient way. This is *NOT* a threadsafe package.
*/
package bitarray

import (
"encoding/binary"
"io"
)

// bitArray is a struct that maintains state of a bit array.
type bitArray struct {
blocks []block
Expand Down Expand Up @@ -270,70 +265,6 @@ func (ba *bitArray) copy() BitArray {
}
}

// Write serializes the bitArray and its data and sends it to the writer.
func Write(w io.Writer, ba *bitArray) error {
err := binary.Write(w, binary.LittleEndian, ba.lowest)
if err != nil {
return err
}
err = binary.Write(w, binary.LittleEndian, ba.highest)
if err != nil {
return err
}

var encodedanyset uint8
if ba.anyset {
encodedanyset = 1
} else {
encodedanyset = 0
}
err = binary.Write(w, binary.LittleEndian, encodedanyset)
if err != nil {
return err
}

err = binary.Write(w, binary.LittleEndian, ba.blocks)
return err
}

// Read takes a reader of a serialized bitArray created by the Write function,
// and returns a bitArray object.
func Read(r io.Reader) (*bitArray, error) {
ret := &bitArray{}

err := binary.Read(r, binary.LittleEndian, &ret.lowest)
if err != nil {
return nil, err
}

err = binary.Read(r, binary.LittleEndian, &ret.highest)
if err != nil {
return nil, err
}

var encodedanyset uint8
err = binary.Read(r, binary.LittleEndian, &encodedanyset)
if err != nil {
return nil, err
}

// anyset defaults to false so we don't need an else statement
if encodedanyset == 1 {
ret.anyset = true
}

var nextblock block
err = binary.Read(r, binary.LittleEndian, &nextblock)
for err == nil {
ret.blocks = append(ret.blocks, nextblock)
err = binary.Read(r, binary.LittleEndian, &nextblock)
}
if err != io.EOF {
return nil, err
}
return ret, nil
}

// newBitArray returns a new dense BitArray at the specified size. This is a
// separate private constructor so unit tests don't have to constantly cast the
// BitArray interface to the concrete type.
Expand Down
28 changes: 0 additions & 28 deletions bitarray/bitarray_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package bitarray

import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -473,30 +472,3 @@ func BenchmarkBitArrayToNums(b *testing.B) {
ba.ToNums()
}
}

func TestBitArrayReadWrite(t *testing.T) {
numItems := uint64(1280)
input := newBitArray(numItems)

for i := uint64(0); i < numItems; i++ {
if i%3 == 0 {
input.SetBit(i)
}
}

writebuf := new(bytes.Buffer)
err := Write(writebuf, input)
assert.Equal(t, err, nil)

// 1280 bits = 20 blocks = 160 bytes, plus lowest and highest at
// 128 bits = 16 bytes plus 1 byte for the anyset param
assert.Equal(t, len(writebuf.Bytes()), 177)

expected := []byte{0, 0, 0, 0, 0, 0, 0, 0, 254}
assert.Equal(t, expected, writebuf.Bytes()[:9])

readbuf := bytes.NewReader(writebuf.Bytes())
output, err := Read(readbuf)
assert.Equal(t, err, nil)
assert.True(t, input.Equals(output))
}
213 changes: 213 additions & 0 deletions bitarray/encoding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
/*
Copyright 2014 Workiva, LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package bitarray

import (
"bytes"
"encoding/binary"
"errors"
"io"
)

// Marshal takes a dense or sparse bit array and serializes it to a
// byte slice.
func Marshal(ba BitArray) ([]byte, error) {
if eba, ok := ba.(*bitArray); ok {
return eba.Serialize()
} else if sba, ok := ba.(*sparseBitArray); ok {
return sba.Serialize()
} else {
return nil, errors.New("not a valid BitArray")
}
}

// Unmarshal takes a byte slice, of the same format produced by Marshal,
// and returns a BitArray.
func Unmarshal(input []byte) (BitArray, error) {
if len(input) == 0 {
return nil, errors.New("no data in input")
}
if input[0] == 'B' {
ret := newBitArray(0)
err := ret.Deserialize(input)
if err != nil {
return nil, err
}
return ret, nil
} else if input[0] == 'S' {
ret := newSparseBitArray()
err := ret.Deserialize(input)
if err != nil {
return nil, err
}
return ret, nil
} else {
return nil, errors.New("unrecognized encoding")
}
}

// Serialize converts the sparseBitArray to a byte slice
func (ba *sparseBitArray) Serialize() ([]byte, error) {
w := new(bytes.Buffer)

var identifier uint8 = 'S'
err := binary.Write(w, binary.LittleEndian, identifier)
if err != nil {
return nil, err
}

blocksLen := uint64(len(ba.blocks))
indexLen := uint64(len(ba.indices))

err = binary.Write(w, binary.LittleEndian, blocksLen)
if err != nil {
return nil, err
}

err = binary.Write(w, binary.LittleEndian, ba.blocks)
if err != nil {
return nil, err
}

err = binary.Write(w, binary.LittleEndian, indexLen)
if err != nil {
return nil, err
}

err = binary.Write(w, binary.LittleEndian, ba.indices)
if err != nil {
return nil, err
}
return w.Bytes(), nil
}

// Deserialize takes the incoming byte slice, and populates the sparseBitArray
// with data in the bytes. Note that this will overwrite any capacity
// specified when creating the sparseBitArray. Also note that if an error
// is returned, the sparseBitArray this is called on might be populated
// with partial data.
func (ret *sparseBitArray) Deserialize(incoming []byte) error {
r := bytes.NewReader(incoming[1:]) // Discard identifier

var intsToRead uint64
err := binary.Read(r, binary.LittleEndian, &intsToRead)
if err != nil {
return err
}

var nextblock block
for i := intsToRead; i > uint64(0); i-- {
err = binary.Read(r, binary.LittleEndian, &nextblock)
if err != nil {
return err
}
ret.blocks = append(ret.blocks, nextblock)
}

err = binary.Read(r, binary.LittleEndian, &intsToRead)
if err != nil {
return err
}

var nextuint uint64
for i := intsToRead; i > uint64(0); i-- {
err = binary.Read(r, binary.LittleEndian, &nextuint)
if err != nil {
return err
}
ret.indices = append(ret.indices, nextuint)
}

return nil
}

// Serialize converts the bitArray to a byte slice.
func (ba *bitArray) Serialize() ([]byte, error) {
w := new(bytes.Buffer)

var identifier uint8 = 'B'
err := binary.Write(w, binary.LittleEndian, identifier)
if err != nil {
return nil, err
}

err = binary.Write(w, binary.LittleEndian, ba.lowest)
if err != nil {
return nil, err
}
err = binary.Write(w, binary.LittleEndian, ba.highest)
if err != nil {
return nil, err
}

var encodedanyset uint8
if ba.anyset {
encodedanyset = 1
} else {
encodedanyset = 0
}
err = binary.Write(w, binary.LittleEndian, encodedanyset)
if err != nil {
return nil, err
}

err = binary.Write(w, binary.LittleEndian, ba.blocks)
if err != nil {
return nil, err
}
return w.Bytes(), nil
}

// Deserialize takes the incoming byte slice, and populates the bitArray
// with data in the bytes. Note that this will overwrite any capacity
// specified when creating the bitArray. Also note that if an error is returned,
// the bitArray this is called on might be populated with partial data.
func (ret *bitArray) Deserialize(incoming []byte) error {
r := bytes.NewReader(incoming[1:]) // Discard identifier

err := binary.Read(r, binary.LittleEndian, &ret.lowest)
if err != nil {
return err
}

err = binary.Read(r, binary.LittleEndian, &ret.highest)
if err != nil {
return err
}

var encodedanyset uint8
err = binary.Read(r, binary.LittleEndian, &encodedanyset)
if err != nil {
return err
}

// anyset defaults to false so we don't need an else statement
if encodedanyset == 1 {
ret.anyset = true
}

var nextblock block
err = binary.Read(r, binary.LittleEndian, &nextblock)
for err == nil {
ret.blocks = append(ret.blocks, nextblock)
err = binary.Read(r, binary.LittleEndian, &nextblock)
}
if err != io.EOF {
return err
}
return nil
}
Loading

0 comments on commit 054d629

Please sign in to comment.