Skip to content
This repository has been archived by the owner on Jan 20, 2023. It is now read-only.

Commit

Permalink
Drop 'compatibility' with reference implementation's serialization
Browse files Browse the repository at this point in the history
The reference implementation stores values we don't use and doesn't
have a documented spec. It uses two different encodings, but we only
need one. It also uses float weights, while we use integers.

Compatibility was a bit of a sham before anyway, so it's best to drop
the charade and use our own scheme.
  • Loading branch information
spenczar committed Oct 5, 2017
1 parent 8c4da54 commit 40d829c
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions serde.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,16 @@ import (
"io"
)

const encodingVersion = int32(1)
const (
magic = int16(0xc80)
encodingVersion = int32(1)
)

func marshalBinary(d *TDigest) ([]byte, error) {
buf := bytes.NewBuffer(nil)
w := &binaryBufferWriter{buf: buf}
w.writeValue(magic)
w.writeValue(encodingVersion)

var min, max float64
if len(d.centroids) > 0 {
min = d.centroids[0].mean
max = d.centroids[len(d.centroids)-1].mean
}

w.writeValue(min)
w.writeValue(max)
w.writeValue(d.compression)
w.writeValue(int32(len(d.centroids)))
for _, c := range d.centroids {
Expand All @@ -37,17 +32,19 @@ func marshalBinary(d *TDigest) ([]byte, error) {

func unmarshalBinary(d *TDigest, p []byte) error {
var (
ev int32
min, max float64
n int32
mv int16
ev int32
n int32
)
r := &binaryReader{r: bytes.NewReader(p)}
r.readValue(&mv)
if mv != magic {
return fmt.Errorf("invalid header magic value, data might be corrupted: %d", mv)
}
r.readValue(&ev)
if ev != encodingVersion {
return fmt.Errorf("invalid encoding version: %d", ev)
}
r.readValue(&min)
r.readValue(&max)
r.readValue(&d.compression)
r.readValue(&n)
if r.err != nil {
Expand Down

0 comments on commit 40d829c

Please sign in to comment.