Remove zig zag encoding from timestamp encoder

Not needed since all timestamps will be sorted in ascending order.  Negatives
are not possible.
pull/4317/merge
Jason Wilder 2015-09-30 13:11:49 -06:00
parent 01b5b9268e
commit b826f8d6ac
2 changed files with 8 additions and 16 deletions

View File

@ -1,16 +1,13 @@
package pd1 package pd1
// Timestamp encoding is adapative and based on structure of the timestamps that are encoded. It // Timestamp encoding is adapative and based on structure of the timestamps that are encoded. It
// uses a combination of delta encoding, zig zag encoding, scaling and compression using simple8b, // uses a combination of delta encoding, scaling and compression using simple8b, run length encoding
// run length encoding as well as falling back to no compression if needed. // as well as falling back to no compression if needed.
// //
// Timestamp values to be encoded should be sorted before encoding. When encoded, the values are // Timestamp values to be encoded should be sorted before encoding. When encoded, the values are
// first delta-encoded. The first value is the starting timestamp, subsequent values are the difference. // first delta-encoded. The first value is the starting timestamp, subsequent values are the difference.
// from the prior value. // from the prior value.
// //
// Delta encoding can produce negative values. After delta encoding, the values are zig zag encoded
// to convert them to positive values.
//
// Timestamp resolution can also be in the nanosecond. Many timestamps are monotonically increasing // Timestamp resolution can also be in the nanosecond. Many timestamps are monotonically increasing
// and fall on even boundaries of time such as every 10s. When the timestamps have this structure, // and fall on even boundaries of time such as every 10s. When the timestamps have this structure,
// they are scaled by the largest common divisor that is also a factor of 10. This has the effect // they are scaled by the largest common divisor that is also a factor of 10. This has the effect
@ -83,10 +80,7 @@ func (e *encoder) reduce() (max, divisor uint64, rle bool, deltas []uint64) {
for i := len(deltas) - 1; i > 0; i-- { for i := len(deltas) - 1; i > 0; i-- {
// First differential encode the values // First differential encode the values
delta := int64(deltas[i] - deltas[i-1]) deltas[i] = deltas[i] - deltas[i-1]
// The delta may be negative so zigzag encode it into a postive value
deltas[i] = ZigZagEncode(delta)
// We're also need to keep track of the max value and largest common divisor // We're also need to keep track of the max value and largest common divisor
v := deltas[i] v := deltas[i]
@ -243,8 +237,8 @@ func (d *decoder) decodePacked(b []byte) {
// Compute the prefix sum and scale the deltas back up // Compute the prefix sum and scale the deltas back up
for i := 1; i < len(deltas); i++ { for i := 1; i < len(deltas); i++ {
dgap := ZigZagDecode(deltas[i] * div) dgap := deltas[i] * div
deltas[i] = uint64(int64(deltas[i-1]) + dgap) deltas[i] = deltas[i-1] + dgap
} }
d.ts = deltas d.ts = deltas
@ -264,8 +258,6 @@ func (d *decoder) decodeRLE(b []byte) {
// Next 1-10 bytes is our (scaled down by factor of 10) run length values // Next 1-10 bytes is our (scaled down by factor of 10) run length values
value, n := binary.Uvarint(b[i:]) value, n := binary.Uvarint(b[i:])
value = uint64(ZigZagDecode(value))
// Scale the value back up // Scale the value back up
value *= uint64(mod) value *= uint64(mod)
i += n i += n
@ -293,10 +285,10 @@ func (d *decoder) decodeRaw(b []byte) {
for i := range d.ts { for i := range d.ts {
d.ts[i] = binary.BigEndian.Uint64(b[i*8 : i*8+8]) d.ts[i] = binary.BigEndian.Uint64(b[i*8 : i*8+8])
delta := ZigZagDecode(d.ts[i]) delta := d.ts[i]
// Compute the prefix sum and scale the deltas back up // Compute the prefix sum and scale the deltas back up
if i > 0 { if i > 0 {
d.ts[i] = uint64(int64(d.ts[i-1]) + delta) d.ts[i] = d.ts[i-1] + delta
} }
} }
} }

View File

@ -297,7 +297,7 @@ func Test_TimeEncoder_Reverse(t *testing.T) {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
if got := b[0] >> 4; got != pd1.EncodingPackedSimple { if got := b[0] >> 4; got != pd1.EncodingUncompressed {
t.Fatalf("Wrong encoding used: expected uncompressed, got %v", got) t.Fatalf("Wrong encoding used: expected uncompressed, got %v", got)
} }