wp
This commit is contained in:
@@ -20,10 +20,16 @@ type ValueDeltaCompressor struct {
|
||||
baseValue float64
|
||||
lastDelta uint64
|
||||
state *ValueDeltaCapturedState
|
||||
// (baseValue, coef, value) => delta
|
||||
calcDelta func(float64, float64, float64) uint64
|
||||
// (value, coef) => uint64
|
||||
toUint64 func(float64, float64) uint64
|
||||
// (value, coef) => uint64
|
||||
toFloat64 func(uint64, float64) float64
|
||||
}
|
||||
|
||||
// Після відновлення із снапшота
|
||||
func NewValueDeltaCompressor(fracDigits byte, buf []byte, payloadSize int) *ValueDeltaCompressor {
|
||||
func NewValueDeltaCompressor(metricType qb.MetricType, fracDigits byte, buf []byte, payloadSize int) *ValueDeltaCompressor {
|
||||
var coef float64 = 1
|
||||
if fracDigits > 0 {
|
||||
coef = math.Pow(10, float64(fracDigits))
|
||||
@@ -33,6 +39,15 @@ func NewValueDeltaCompressor(fracDigits byte, buf []byte, payloadSize int) *Valu
|
||||
coef: coef,
|
||||
pos: payloadSize,
|
||||
}
|
||||
if metricType == qb.Cumulative {
|
||||
s.calcDelta = calcCumulativeDelta
|
||||
s.toUint64 = toCumulativeUint64
|
||||
s.toFloat64 = toCumulativeFloat64
|
||||
} else {
|
||||
s.calcDelta = calcInstantDelta
|
||||
s.toUint64 = toInstantUint64
|
||||
s.toFloat64 = toInstantFloat64
|
||||
}
|
||||
if payloadSize > 0 {
|
||||
// base value на початку
|
||||
u64, _, err := bin.GetVarUint64(s.buf)
|
||||
@@ -59,7 +74,19 @@ func (s *ValueDeltaCompressor) Evaluate(tmp []byte, value float64) qb.ValueEvalu
|
||||
i int
|
||||
)
|
||||
if s.pos > 0 {
|
||||
delta = uint64((value-s.baseValue)*s.coef + eps)
|
||||
delta = s.calcDelta(s.baseValue, s.coef, value)
|
||||
// if false {
|
||||
// delta = uint64((value-s.baseValue)*s.coef + eps)
|
||||
|
||||
// } else {
|
||||
// f64 := (value - s.baseValue) * s.coef
|
||||
// if f64 > 0 {
|
||||
// f64 += eps
|
||||
// } else {
|
||||
// f64 -= eps
|
||||
// }
|
||||
// delta = bin.EncodeZigZag(int64(f64))
|
||||
// }
|
||||
h := s.buf[s.pos-1]
|
||||
if h < 128 {
|
||||
// run
|
||||
@@ -110,7 +137,13 @@ func (s *ValueDeltaCompressor) Evaluate(tmp []byte, value float64) qb.ValueEvalu
|
||||
}
|
||||
}
|
||||
} else {
|
||||
n, _ := bin.PutVarUint64(tmp, uint64(value*s.coef)) // base value
|
||||
// var baseValue uint64
|
||||
// if false {
|
||||
// baseValue = uint64(value * s.coef)
|
||||
// } else {
|
||||
// baseValue = bin.EncodeZigZag(int64(value * s.coef))
|
||||
// }
|
||||
n, _ := bin.PutVarUint64(tmp, s.toUint64(value, s.coef))
|
||||
i += n
|
||||
n, _ = bin.ReversePutVarUint64(tmp[i:], 0) // delta
|
||||
i += n
|
||||
@@ -164,12 +197,13 @@ func (s *ValueDeltaCompressor) ForgetCapturedState() {
|
||||
s.state = nil
|
||||
}
|
||||
|
||||
// для зростання буфера під час вставки даних. State не цікавить
|
||||
func (s *ValueDeltaCompressor) Size() int {
|
||||
if s.state == nil {
|
||||
return s.pos
|
||||
} else {
|
||||
return bin.CountVarUint64(s.state.LastDelta) + hSize + len(s.state.Payload)
|
||||
}
|
||||
//if s.state == nil {
|
||||
return s.pos
|
||||
// } else {
|
||||
// return bin.CountVarUint64(s.state.LastDelta) + hSize + len(s.state.Payload)
|
||||
// }
|
||||
}
|
||||
|
||||
// // Snapshot - для створення снапшота.
|
||||
@@ -217,7 +251,7 @@ func (s *ValueDeltaCompressor) LastValue() float64 {
|
||||
} else {
|
||||
delta = s.state.LastDelta
|
||||
}
|
||||
return s.baseValue + float64(delta)*s.coef
|
||||
return s.baseValue + s.toFloat64(delta, s.coef)
|
||||
}
|
||||
|
||||
func (s *ValueDeltaCompressor) CreateDecompressor() qb.ValueDecompressor {
|
||||
@@ -236,6 +270,7 @@ func (s *ValueDeltaCompressor) CreateDecompressor() qb.ValueDecompressor {
|
||||
}
|
||||
return NewValueDeltaDecompressorFromState(ValueDeltaDecompressorFromStateOptions{
|
||||
Coef: s.coef,
|
||||
ToFloat64: s.toFloat64,
|
||||
H: h,
|
||||
LastDelta: lastDelta,
|
||||
Payload: payload,
|
||||
@@ -254,9 +289,11 @@ type ValueDeltaDecompressor struct {
|
||||
isRun bool
|
||||
pending int
|
||||
done bool
|
||||
// (value, coef) => uint64
|
||||
toFloat64 func(uint64, float64) float64
|
||||
}
|
||||
|
||||
func NewValueDeltaDecompressor(buf []byte, fracDigits byte) *ValueDeltaDecompressor {
|
||||
func NewValueDeltaDecompressor(fracDigits byte, buf []byte, toFloat64 func(uint64, float64) float64) *ValueDeltaDecompressor {
|
||||
var coef float64 = 1
|
||||
if fracDigits > 0 {
|
||||
coef = math.Pow(10, float64(fracDigits))
|
||||
@@ -265,14 +302,21 @@ func NewValueDeltaDecompressor(buf []byte, fracDigits byte) *ValueDeltaDecompres
|
||||
if err != nil {
|
||||
log.Fatalf("bug: get base value: %s", err)
|
||||
}
|
||||
// if false {
|
||||
// baseValue = float64(u64) / coef
|
||||
// } else {
|
||||
// baseValue = float64(bin.DecodeZigZag(u64)) / coef
|
||||
// }
|
||||
//fmt.Printf("baseValue: %.2f, bound: %d, pos: %d\n", float64(u64)/coef, n, size)
|
||||
s := &ValueDeltaDecompressor{
|
||||
buf: buf,
|
||||
coef: coef,
|
||||
pos: len(buf), // first free
|
||||
baseValue: float64(u64) / coef,
|
||||
buf: buf,
|
||||
coef: coef,
|
||||
pos: len(buf), // first free
|
||||
// baseValue: baseValue,
|
||||
bound: n,
|
||||
toFloat64: toFloat64,
|
||||
}
|
||||
s.baseValue = s.toFloat64(u64, coef)
|
||||
// читаю заголовок наступної серії
|
||||
s.readHeader()
|
||||
s.readValue()
|
||||
@@ -280,7 +324,9 @@ func NewValueDeltaDecompressor(buf []byte, fracDigits byte) *ValueDeltaDecompres
|
||||
}
|
||||
|
||||
type ValueDeltaDecompressorFromStateOptions struct {
|
||||
Coef float64
|
||||
Coef float64
|
||||
// (value, coef) => uint64
|
||||
ToFloat64 func(uint64, float64) float64
|
||||
H byte
|
||||
LastDelta uint64
|
||||
Payload []byte
|
||||
@@ -291,15 +337,28 @@ func NewValueDeltaDecompressorFromState(opt ValueDeltaDecompressorFromStateOptio
|
||||
if err != nil {
|
||||
log.Fatalf("bug: get base value: %s", err)
|
||||
}
|
||||
// var baseValue float64
|
||||
// if false {
|
||||
// baseValue = float64(u64) / opt.Coef
|
||||
// } else {
|
||||
// baseValue = float64(bin.DecodeZigZag(u64)) / opt.Coef
|
||||
// }
|
||||
//fmt.Printf("baseValue: %.2f, bound: %d, pos: %d\n", float64(u64)/opt.Coef, n, size)
|
||||
s := &ValueDeltaDecompressor{
|
||||
buf: opt.Payload,
|
||||
coef: opt.Coef,
|
||||
pos: len(opt.Payload), // first free
|
||||
baseValue: float64(u64) / opt.Coef,
|
||||
buf: opt.Payload,
|
||||
coef: opt.Coef,
|
||||
pos: len(opt.Payload), // first free
|
||||
//baseValue: baseValue,
|
||||
bound: n,
|
||||
toFloat64: opt.ToFloat64,
|
||||
}
|
||||
s.lastValue = s.baseValue + float64(opt.LastDelta)/s.coef
|
||||
s.baseValue = s.toFloat64(u64, s.coef)
|
||||
// if false {
|
||||
// s.lastValue = s.baseValue + float64(opt.LastDelta)/s.coef
|
||||
// } else {
|
||||
// s.lastValue = s.baseValue + float64(bin.DecodeZigZag(opt.LastDelta))/s.coef
|
||||
// }
|
||||
s.lastValue = s.baseValue + s.toFloat64(opt.LastDelta, s.coef)
|
||||
s.decodeHeaderByte(opt.H)
|
||||
//fmt.Printf("payload: %d\n", opt.Payload)
|
||||
//fmt.Printf("restore from state: bound=%d, isRun=%t, pending=%d, baseValue=%v, lastValue=%v\n",
|
||||
@@ -353,7 +412,12 @@ func (s *ValueDeltaDecompressor) readValue() {
|
||||
// fmt.Println("read delta n:", n)
|
||||
// fmt.Println()
|
||||
s.pos -= n
|
||||
s.lastValue = s.baseValue + float64(u64)/s.coef
|
||||
// if false {
|
||||
// s.lastValue = s.baseValue + float64(u64)/s.coef
|
||||
// } else {
|
||||
// s.lastValue = s.baseValue + float64(bin.DecodeZigZag(u64))/s.coef
|
||||
// }
|
||||
s.lastValue = s.baseValue + s.toFloat64(u64, s.coef)
|
||||
//fmt.Println(s.baseValue, float64(u64)/s.coef)
|
||||
}
|
||||
|
||||
@@ -366,6 +430,38 @@ func (s *ValueDeltaDecompressor) decodeHeaderByte(h byte) {
|
||||
}
|
||||
}
|
||||
|
||||
// HELPERS
|
||||
|
||||
func calcCumulativeDelta(baseValue float64, coef float64, value float64) uint64 {
|
||||
return uint64((value-baseValue)*coef + eps)
|
||||
}
|
||||
|
||||
func calcInstantDelta(baseValue float64, coef float64, value float64) uint64 {
|
||||
f64 := (value - baseValue) * coef
|
||||
if f64 > 0 {
|
||||
f64 += eps
|
||||
} else {
|
||||
f64 -= eps
|
||||
}
|
||||
return bin.EncodeZigZag(int64(f64))
|
||||
}
|
||||
|
||||
func toCumulativeUint64(value float64, coef float64) uint64 {
|
||||
return uint64(value * coef)
|
||||
}
|
||||
|
||||
func toInstantUint64(value float64, coef float64) uint64 {
|
||||
return bin.EncodeZigZag(int64(value * coef))
|
||||
}
|
||||
|
||||
func toCumulativeFloat64(value uint64, coef float64) float64 {
|
||||
return float64(value) / coef
|
||||
}
|
||||
|
||||
func toInstantFloat64(value uint64, coef float64) float64 {
|
||||
return float64(bin.DecodeZigZag(value)) / coef
|
||||
}
|
||||
|
||||
/*
|
||||
Формат:
|
||||
base value (var u64)
|
||||
|
||||
Reference in New Issue
Block a user