Skip to content

Commit

Permalink
Fix generics gen and bit calc.
Browse files Browse the repository at this point in the history
  • Loading branch information
notbdu committed Dec 21, 2018
1 parent 148b6ae commit add79c4
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 20 deletions.
93 changes: 92 additions & 1 deletion encoding/encoding_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion encoding/int_encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (enc *IntEnc) Encode(
// Default to delta encoding.
enc.metaProto.Encoding = encodingpb.EncodingType_DELTA
enc.metaProto.DeltaStart = firstVal
enc.metaProto.BitsPerEncodedValue = int64(bits.Len(uint((max - min) + 1))) // Add 1 for the sign bit.
enc.metaProto.BitsPerEncodedValue = int64(bits.Len(uint(max-min)) + 1) // Add 1 for the sign bit.
}

if err := proto.EncodeIntMeta(&enc.metaProto, &enc.buf, writer); err != nil {
Expand Down
12 changes: 6 additions & 6 deletions encoding/template/run_length_decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ var (
errNonPositiveNumberOfRepetitions = errors.New("non positive number of repetitions")
)

// readValueFn reads a GenericValue from an `io.Reader`.
type readValueFn func(reader io.Reader) (GenericValue, error)
// readValueFn reads a GenericRLValue from an `io.Reader`.
type readValueFn func(reader io.Reader) (GenericRLValue, error)

// runLengthDecodeValue run length decodes a stream of GenericValues.
// runLengthDecodeValue run length decodes a stream of GenericRLValues.
func runLengthDecodeValue(
reader io.Reader,
readValueFn readValueFn,
) *RunLengthValueIterator {
return newValueIterator(reader, readValueFn)
}

// RunLengthValueIterator iterates over a run length encoded stream of GenericValue data.
// RunLengthValueIterator iterates over a run length encoded stream of GenericRLValue data.
type RunLengthValueIterator struct {
reader io.Reader
readValueFn readValueFn
curr GenericValue
curr GenericRLValue
repetitions int64
closed bool
err error
Expand Down Expand Up @@ -61,7 +61,7 @@ func (rl *RunLengthValueIterator) Next() bool {
}

// Current returns the current string.
func (rl *RunLengthValueIterator) Current() GenericValue { return rl.curr }
func (rl *RunLengthValueIterator) Current() GenericRLValue { return rl.curr }

// Err returns any error recorded while iterating.
func (rl *RunLengthValueIterator) Err() error { return rl.err }
Expand Down
22 changes: 11 additions & 11 deletions encoding/template/run_length_encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,35 @@ import (
"github.com/xichen2020/eventdb/x/bytes"
)

// GenericValue is a generic type.
type GenericValue generic.Type
// GenericRLValue is a generic type.
type GenericRLValue generic.Type

// writeValueFn reads a GenericValue from an `io.Reader`.
type writeValueFn func(writer io.Writer, value GenericValue) error
// writeValueFn reads a GenericRLValue from an `io.Reader`.
type writeValueFn func(writer io.Writer, value GenericRLValue) error

// ForwardValueIterator allows iterating over a stream of GenericValue.
type ForwardValueIterator interface {
// ForwardRLValueIterator allows iterating over a stream of GenericRLValue.
type ForwardRLValueIterator interface {
generic.Type

io.Closer
Next() bool
Err() error
Current() GenericValue
Current() GenericRLValue
}

// runLengthEncodeValue run length encodes a stream of GenericValue.
// runLengthEncodeValue run length encodes a stream of GenericRLValue.
func runLengthEncodeValue(
writer io.Writer,
extBuf *[]byte, // extBuf is an external byte buffer for memory re-use.
writeValueFn writeValueFn,
valuesIt ForwardValueIterator,
valuesIt ForwardRLValueIterator,
) error {
// Ensure that our buffer size is large enough to handle varint ops.
*extBuf = bytes.EnsureBufferSize(*extBuf, binary.MaxVarintLen64, bytes.DontCopyData)

var (
firstTime = true
last GenericValue
last GenericRLValue
repetitions = 1
)
for valuesIt.Next() {
Expand Down Expand Up @@ -73,7 +73,7 @@ func writeRLE(
writer io.Writer,
extBuf []byte, // extBuf is an external byte buffer for memory re-use.
writeValueFn writeValueFn,
value GenericValue,
value GenericRLValue,
repetitions int,
) error {
// Encode the final value.
Expand Down
2 changes: 1 addition & 1 deletion encoding/time_encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (enc *TimeEnc) Encode(
enc.metaProto.Encoding = encodingpb.EncodingType_DELTA
enc.metaProto.Resolution = resType
enc.metaProto.DeltaStart = firstVal
enc.metaProto.BitsPerEncodedValue = int64(bits.Len(uint(max - min)))
enc.metaProto.BitsPerEncodedValue = int64(bits.Len(uint(max-min)) + 1) // Add 1 for the sign bit.

if err := proto.EncodeTimeMeta(&enc.metaProto, &enc.buf, writer); err != nil {
return err
Expand Down

0 comments on commit add79c4

Please sign in to comment.