This commit is contained in:
2026-06-12 00:29:02 +03:00
parent ed203195fa
commit dc0410c9e4
8 changed files with 161 additions and 259 deletions

View File

@@ -19,7 +19,6 @@ type ValueDeltaCompressor struct {
pos int // payload size
baseValue float64
lastDelta uint64
state *ValueDeltaCapturedState
// (baseValue, coef, value) => delta
calcDelta func(float64, float64, float64) uint64
// (value, coef) => uint64
@@ -75,18 +74,6 @@ func (s *ValueDeltaCompressor) Evaluate(tmp []byte, value float64) qb.ValueEvalu
)
if s.pos > 0 {
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
@@ -137,12 +124,6 @@ func (s *ValueDeltaCompressor) Evaluate(tmp []byte, value float64) qb.ValueEvalu
}
}
} else {
// 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
@@ -173,30 +154,19 @@ func (s *ValueDeltaCompressor) DeleteLast() {
}
type ValueDeltaCapturedState struct {
H byte
LastDelta uint64
Payload []byte
}
func (s *ValueDeltaCompressor) CaptureState() {
if s.state != nil {
qb.Abort(qb.RepeatableLock, nil)
}
func (s *ValueDeltaCompressor) CaptureState() qb.ValueDeltaCapturedState {
// if s.state != nil {
// qb.Abort(qb.RepeatableLock, nil)
// }
// позиція посувається вліво, отже може перескочити на попередній chunk
pos := s.pos - 1 - bin.CountVarUint64(s.lastDelta)
s.state = &ValueDeltaCapturedState{
return qb.ValueDeltaCapturedState{
H: s.buf[s.pos-1],
LastDelta: s.lastDelta,
Payload: s.buf[:pos],
}
}
// fix - повернути в Pool буфери
func (s *ValueDeltaCompressor) ForgetCapturedState() {
s.state = nil
}
// для зростання буфера під час вставки даних. State не цікавить
func (s *ValueDeltaCompressor) Size() int {
//if s.state == nil {
@@ -215,21 +185,21 @@ func (s *ValueDeltaCompressor) Size() int {
// }
// }
func (s *ValueDeltaCompressor) WritePayloadTo(w io.Writer) (err error) {
if s.state == nil {
func (s *ValueDeltaCompressor) WritePayloadTo(w io.Writer, state *qb.ValueDeltaCapturedState) (err error) {
if state == nil {
_, err = w.Write(s.buf[:s.pos])
return
} else {
_, err = w.Write(s.state.Payload)
_, err = w.Write(state.Payload)
if err != nil {
return
}
_, err = bin.WriteVarUint64(w, s.state.LastDelta)
_, err = bin.WriteVarUint64(w, state.LastDelta)
if err != nil {
return
}
_, err = w.Write([]byte{
s.state.H,
state.H,
})
return
}
@@ -245,35 +215,27 @@ func (s *ValueDeltaCompressor) Rotate(newbuf []byte) {
}
func (s *ValueDeltaCompressor) LastValue() float64 {
var delta uint64
if s.state == nil {
delta = s.lastDelta
} else {
delta = s.state.LastDelta
}
return s.baseValue + s.toFloat64(delta, s.coef)
// FIX
// var delta uint64
// if s.state == nil {
// delta = s.lastDelta
// } else {
// delta = s.state.LastDelta
// }
// return s.baseValue + s.toFloat64(delta, s.coef)
return 0
}
func (s *ValueDeltaCompressor) CreateDecompressor() qb.ValueDecompressor {
if s.pos == 0 {
return nil
}
var (
h = s.buf[s.pos-1]
lastDelta = s.lastDelta
payload = s.buf[:s.pos]
)
if s.state != nil {
h = s.state.H
lastDelta = s.state.LastDelta
payload = s.state.Payload
}
return NewValueDeltaDecompressorFromState(ValueDeltaDecompressorFromStateOptions{
Coef: s.coef,
ToFloat64: s.toFloat64,
H: h,
LastDelta: lastDelta,
Payload: payload,
H: s.buf[s.pos-1],
LastDelta: s.lastDelta,
Payload: s.buf[:s.pos],
})
}
@@ -302,17 +264,10 @@ func NewValueDeltaDecompressor(fracDigits byte, buf []byte, toFloat64 func(uint6
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: baseValue,
buf: buf,
coef: coef,
pos: len(buf), // first free
bound: n,
toFloat64: toFloat64,
}
@@ -337,32 +292,16 @@ 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: baseValue,
buf: opt.Payload,
coef: opt.Coef,
pos: len(opt.Payload), // first free
bound: n,
toFloat64: opt.ToFloat64,
}
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",
// n, s.isRun, s.pending, s.baseValue, s.lastValue)
return s
}
@@ -392,33 +331,17 @@ func (s *ValueDeltaDecompressor) NextValue() (value float64, done bool) {
}
func (s *ValueDeltaDecompressor) readHeader() {
//fmt.Println("read from pos:", s.pos)
s.pos--
h := s.buf[s.pos]
s.decodeHeaderByte(h)
// fmt.Println("h:", h)
// fmt.Println("isRun:", s.isRun)
// fmt.Println("pending:", s.pending)
}
func (s *ValueDeltaDecompressor) readValue() {
u64, n, err := bin.ReverseGetVarUint64(s.buf[:s.pos])
if err != nil {
log.Fatalln(err)
}
// fmt.Println()
// fmt.Println("read from pos:", s.pos)
// fmt.Println("read delta:", u64)
// fmt.Println("read delta n:", n)
// fmt.Println()
s.pos -= n
// 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)
}
func (s *ValueDeltaDecompressor) decodeHeaderByte(h byte) {