This commit is contained in:
2026-06-14 23:12:03 +03:00
parent 181031a753
commit b32279deaf
35 changed files with 1395 additions and 1062 deletions

View File

@@ -1,6 +1,7 @@
package enc
import (
"fmt"
"io"
"log"
"math"
@@ -48,9 +49,6 @@ func NewValueDeltaCompressor(metricType qb.MetricType, fracDigits byte, buf []by
s.toFloat64 = toInstantFloat64
}
if payloadSize > 0 {
// fmt.Printf("% x\n", buf)
// fmt.Printf("% x\n", buf[:s.pos])
// fmt.Printf("pos: %d\n", s.pos)
u64, _, err := bin.GetVarUint64(s.buf)
if err != nil {
log.Fatalf("bug: get base value: %s", err)
@@ -145,13 +143,21 @@ func (s *ValueDeltaCompressor) Evaluate(tmp []byte, value float64) qb.ValueEvalu
// pos завжди вказує на h
func (s *ValueDeltaCompressor) Append(rewindOffset int, change []byte, value float64, delta uint64) {
// fmt.Printf("append value:\n")
// fmt.Printf("rewind offset: %d\n", rewindOffset)
// fmt.Printf("change: % x\n", change)
// fmt.Printf("value: %v\n", value)
// fmt.Printf("delta: %d\n", delta)
if s.pos > 0 {
s.lastDelta = delta
} else {
s.baseValue = value
}
//fmt.Printf("buf before: % x\n", s.buf[:s.pos])
copy(s.buf[s.pos-rewindOffset:], change)
s.pos += len(change) - rewindOffset
//fmt.Printf("buf after: % x\n", s.buf[:s.pos])
//fmt.Printf("buf after: % x\n", s.buf[:s.pos])
}
func (s *ValueDeltaCompressor) DeleteLast() {
@@ -159,11 +165,20 @@ func (s *ValueDeltaCompressor) DeleteLast() {
}
func (s *ValueDeltaCompressor) getState() qb.ValueDeltaCapturedState {
bound := s.pos - 1 - bin.CountVarUint64(s.lastDelta)
return qb.ValueDeltaCapturedState{
H: s.buf[s.pos-1],
LastDelta: s.lastDelta,
Payload: s.buf[:bound],
if s.pos > 0 {
// fmt.Println("getState")
// fmt.Printf("pos: %d\n", s.pos)
// fmt.Printf("buf: % x\n", s.buf[:s.pos])
// fmt.Printf("h: %d\n", s.buf[s.pos-1])
// fmt.Printf("lastDelta: %d\n", s.lastDelta)
bound := s.pos - 1 - bin.CountVarUint64(s.lastDelta)
return qb.ValueDeltaCapturedState{
H: s.buf[s.pos-1],
LastDelta: s.lastDelta,
Payload: s.buf[:bound],
}
} else {
return qb.ValueDeltaCapturedState{}
}
}
@@ -185,11 +200,15 @@ func (s *ValueDeltaCompressor) Size() int {
return s.pos
}
func (s *ValueDeltaCompressor) StoredSize() int {
func (s *ValueDeltaCompressor) CommitedSize() int {
if s.state == nil {
return len(s.buf) - s.pos
return s.pos
} else {
return bin.CountVarUint64(uint64(s.state.LastDelta)) + 1 + len(s.state.Payload)
if len(s.state.Payload) > 0 {
return bin.CountVarUint64(uint64(s.state.LastDelta)) + 1 + len(s.state.Payload)
} else {
return 0
}
}
}
@@ -202,7 +221,7 @@ func (s *ValueDeltaCompressor) StoredSize() int {
// }
// }
func (s *ValueDeltaCompressor) WriteStoredTo(w io.Writer) (err error) {
func (s *ValueDeltaCompressor) WriteCommitedTo(w io.Writer) (err error) {
if s.state == nil {
_, err = w.Write(s.buf[:s.pos])
return
@@ -289,17 +308,26 @@ func NewValueDeltaDecompressor(metricType qb.MetricType, fracDigits byte) *Value
func (s *ValueDeltaDecompressor) RestoreFromEnd(buf []byte) {
s.buf = buf
s.pos = len(buf) // first free
s.readBaseValue()
s.readHeader()
s.readValue()
if len(buf) > 0 {
s.readBaseValue()
s.readHeader()
s.readValue()
} else {
s.done = true
}
}
func (s *ValueDeltaDecompressor) RestoreFromState(state qb.ValueDeltaCapturedState) {
fmt.Printf("%#v\n", state)
s.buf = state.Payload
s.pos = len(s.buf) // first free
s.readBaseValue()
s.lastValue = s.baseValue + s.toFloat64(state.LastDelta, s.coef)
s.decodeHeaderByte(state.H)
if len(s.buf) > 0 {
s.readBaseValue()
s.lastValue = s.baseValue + s.toFloat64(state.LastDelta, s.coef)
s.decodeHeaderByte(state.H)
} else {
s.done = true
}
}
func (s *ValueDeltaDecompressor) readBaseValue() {