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

@@ -5,14 +5,14 @@ const eps = 0.000001
const (
flagLiteral = 128
hSize = 1
//hSize = 1
// append scripts
addBaseValue = 0 // cumulative and instant only
addUnixtime = 0 // time only
incrementRun = 1
incrementLiteral = 2
endSeries = 3
startRun = 4
add1stDelta = 5 // time only
// addBaseValue = 0 // cumulative and instant only
// addUnixtime = 0 // time only
// incrementRun = 1
// incrementLiteral = 2
// endSeries = 3
// startRun = 4
// add1stDelta = 5 // time only
)

View File

@@ -28,7 +28,6 @@ type TimeDeltaCompressor struct {
pos int
lastUnixtime uint32
lastDelta uint32
state *TimeDeltaCapturedState
}
// Початкове створення, коли даних немає
@@ -61,11 +60,6 @@ func NewTimeDeltaCompressor(buf []byte, payloadSize int) *TimeDeltaCompressor {
return s
}
// FIX - restire lastUnixtime
// func (s *TimeDeltaCompressor) RestoreState() {
// }
func (s *TimeDeltaCompressor) Evaluate(tmp []byte, timestamp uint32) qb.TimeEvaluationReport {
var (
delta = timestamp - s.lastUnixtime
@@ -160,20 +154,10 @@ func (s *TimeDeltaCompressor) Append(offset int, change []byte, timestamp uint32
// }
type TimeDeltaCapturedState struct {
H byte
LastUnixtime uint32
LastDelta uint32
Payload []byte
}
// delta h
func (s *TimeDeltaCompressor) CaptureState() {
if s.state != nil {
qb.Abort(qb.RepeatableLock, nil)
}
func (s *TimeDeltaCompressor) CaptureState() qb.TimeDeltaCapturedState {
// позиція посувається вліво, отже може перескочити на попередній chunk
s.state = &TimeDeltaCapturedState{
return qb.TimeDeltaCapturedState{
H: s.buf[s.pos],
LastUnixtime: s.lastUnixtime,
LastDelta: s.lastDelta,
@@ -181,11 +165,6 @@ func (s *TimeDeltaCompressor) CaptureState() {
}
}
// fix - повернути в Pool буфери
func (s *TimeDeltaCompressor) ForgetCapturedState() {
s.state = nil
}
// коли сторінка заповнена і відправляється в txlog, since замінюю на until
func (s *TimeDeltaCompressor) ReplaceSinceWithUntil() uint32 {
pos := len(s.buf) - 4
@@ -218,34 +197,26 @@ func (s *TimeDeltaCompressor) Size() int {
// }
// }
func (s *TimeDeltaCompressor) WritePayloadTo(w io.Writer) (err error) {
if s.state == nil {
func (s *TimeDeltaCompressor) WritePayloadTo(w io.Writer, state *qb.TimeDeltaCapturedState) (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, uint64(s.state.LastDelta))
_, err = bin.WriteVarUint64(w, uint64(state.LastDelta))
if err != nil {
return
}
_, err = w.Write([]byte{
s.state.H,
state.H,
})
return
}
}
// func (s *TimeDeltaCompressor) encodeTail(lastUnixtime, lastDelta uint32, h byte) []byte {
// tail := make([]byte, 9)
// bin.PutUint32(tail, lastUnixtime)
// tail[4] = h
// n, _ := bin.PutVarUint64(tail[5:], uint64(lastDelta))
// return tail[:n+5]
// }
func (s *TimeDeltaCompressor) Rotate(newbuf []byte) {
// УВАГА!
// state не чіпаємо
@@ -259,23 +230,11 @@ func (s *TimeDeltaCompressor) CreateDecompressor() qb.TimestampDecompressor {
if s.pos == len(s.buf) {
return nil
}
var (
h = s.buf[s.pos]
lastUnixtime = s.lastUnixtime
lastDelta = s.lastDelta
payload = s.buf[s.pos:]
)
if s.state != nil {
h = s.state.H
lastUnixtime = s.state.LastUnixtime
lastDelta = s.state.LastDelta
payload = s.state.Payload
}
return NewTimeDeltaDecompressorFromState(TimeDeltaDecompressorFromStateOptions{
H: h,
LastUnixtime: lastUnixtime,
LastDelta: lastDelta,
Payload: payload,
H: s.buf[s.pos],
LastUnixtime: s.lastUnixtime,
LastDelta: s.lastDelta,
Payload: s.buf[s.pos:],
})
}
@@ -306,7 +265,6 @@ func NewTimeDeltaDecompressor(buf []byte) *TimeDeltaDecompressor {
log.Fatalf("bug: get last unixtime: %s", err)
}
s.pos += 4
//fmt.Println("restored last unixtime", s.lastUnixtime)
} else {
s.done = true
}
@@ -329,15 +287,10 @@ func NewTimeDeltaDecompressorFromState(opt TimeDeltaDecompressorFromStateOptions
s.lastDelta = opt.LastDelta
s.decodeHeaderByte(opt.H)
}
//fmt.Println("-------------------")
//fmt.Printf("restore from bound: isRun=%t, pending=%d, lastUnix=%d, lastDelta=%d\n",
//s.isRun, s.pending, s.lastUnixtime, s.lastDelta)
//fmt.Printf("buf: %d\n", s.buf)
return s
}
func (s *TimeDeltaDecompressor) NextValue() (value uint32, done bool) {
//fmt.Printf("NextValue(): pos: %d, isRegular: %t, pending: %d\n", s.pos, s.isRegular, s.pending)
if s.done {
return 0, true
}
@@ -346,7 +299,6 @@ func (s *TimeDeltaDecompressor) NextValue() (value uint32, done bool) {
if s.lastDelta > 0 {
s.lastUnixtime -= s.lastDelta
s.pending--
//fmt.Printf("lastUnix: %d, s.pending: %d\n", s.lastUnixtime, s.pending)
if s.pending > 0 {
// якщо в серії залишаються елементи
if !s.isRun {
@@ -358,7 +310,6 @@ func (s *TimeDeltaDecompressor) NextValue() (value uint32, done bool) {
s.readHeader()
s.readDelta()
} else {
//s.done = true
s.lastDelta = 0
}
} else {
@@ -372,12 +323,6 @@ func (s *TimeDeltaDecompressor) readHeader() {
h := s.buf[s.pos]
s.pos++
s.decodeHeaderByte(h)
// fmt.Println()
// fmt.Println("read from pos:", s.pos)
// fmt.Println("h:", h)
// fmt.Println("isRun:", s.isRun)
// fmt.Println("pending:", s.pending)
// fmt.Println()
}
func (s *TimeDeltaDecompressor) decodeHeaderByte(h byte) {
@@ -394,11 +339,6 @@ func (s *TimeDeltaDecompressor) readDelta() {
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
s.lastDelta = uint32(u64)
}

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) {