This commit is contained in:
2026-05-10 00:59:47 +00:00
parent 271046231c
commit 9b38586ed8
30 changed files with 3560 additions and 1823 deletions

View File

@@ -2,22 +2,20 @@ package chunkenc
import (
"fmt"
"log"
"math"
"gordenko.dev/dima/qb/bin"
"gordenko.dev/dima/qb/conbuf"
)
// REVERSE
type ReverseInstantDeltaCompressor struct {
buf *conbuf.ContinuousBuffer
coef float64
pos int
firstValue float64
lastDelta int64
length uint16
numIdx int
buf *conbuf.ContinuousBuffer
coef float64
pos int
baseValue float64
lastDelta int64
lastDeltaSize int
h byte
}
func NewReverseInstantDeltaCompressor(buf *conbuf.ContinuousBuffer, size int, fracDigits byte) *ReverseInstantDeltaCompressor {
@@ -31,246 +29,146 @@ func NewReverseInstantDeltaCompressor(buf *conbuf.ContinuousBuffer, size int, fr
coef: coef,
}
if size > 0 {
s.restoreState()
i64, _, err := s.buf.GetVarInt64(0)
if err != nil {
log.Fatalf("bug: get base value: %s", err)
}
s.baseValue = float64(i64) / s.coef
s.h = s.buf.GetByte(s.pos - 1)
s.lastDelta, s.lastDeltaSize, err = s.buf.ReverseGetVarInt64(s.pos - 2)
if err != nil {
log.Fatalf("bug: get last delta: %s", err)
}
}
return s
}
func (s *ReverseInstantDeltaCompressor) restoreState() {
i64, n, err := s.buf.GetVarInt64(0)
if err != nil {
panic(fmt.Sprintf("bug: get first value: %s", err))
}
s.firstValue = float64(i64) / s.coef
if s.pos > n {
pos := s.pos - 1
idxOf8 := uint(8 - s.buf.GetByte(pos))
pos--
s8 := s.buf.GetByte(pos)
pos--
var n int
s.lastDelta, n, err = s.buf.ReverseGetVarInt64(pos)
if err != nil {
panic(fmt.Sprintf("bug: get last delta: %s", err))
}
pos -= n
s.numIdx = pos + 1
var flag byte = 1 << idxOf8
if (s8 & flag) == flag {
s.length, _ = s.buf.DecodeRunLength(pos)
}
}
}
func (s *ReverseInstantDeltaCompressor) Size() int {
return s.pos
}
func (s *ReverseInstantDeltaCompressor) CalcRequiredSpace(value float64) int {
if s.pos == 0 {
n := bin.CalcVarInt64Length(int64(value * s.coef))
return n + 3
}
tmp := (value - s.firstValue) * s.coef
if tmp > 0 {
tmp += eps
} else {
tmp -= eps
}
delta := int64(tmp)
if delta == s.lastDelta {
if s.length == 0 {
return 1
} else {
newLength := s.length + 1
if newLength < 130 {
return 0
} else if newLength == 130 {
return 1
} else {
if newLength < 32769 {
return 0
} else {
n := bin.CalcVarInt64Length(delta)
n += 2
s8q := s.buf.GetByte(s.pos - 1)
if s8q == 8 {
n -= 1
} else {
n -= 2
}
return n
}
}
}
} else {
n := bin.CalcVarInt64Length(delta)
n += 2
s8q := s.buf.GetByte(s.pos - 1)
if s8q == 8 {
n -= 1
} else {
n -= 2
}
return n
}
}
// В начале буфера кодирую базовое значение.
func (s *ReverseInstantDeltaCompressor) Append(value float64) {
if s.pos == 0 {
// base value
n := s.buf.PutVarInt64(s.pos, int64(value*s.coef))
s.pos += n
s.firstValue = value
s.encodeNewDelta(0, 0, 1)
s.baseValue = value
s.appendNewLiteral(0)
} else {
tmp := (value - s.firstValue) * s.coef
tmp := (value - s.baseValue) * s.coef
if tmp > 0 {
tmp += eps
} else {
tmp -= eps
}
delta := int64(tmp)
if delta == s.lastDelta {
if s.length == 0 {
s.length = 2
s.shiftOnePosToRight()
s.buf.SetByte(s.numIdx-1, 0)
s8q := s.buf.GetByte(s.pos - 1)
s.buf.SetFlag(s.pos-2, 1<<(8-s8q))
} else {
s.length++
if s.length < 130 {
s.buf.SetByte(s.numIdx-1, byte(s.length-2))
} else if s.length == 130 {
s.shiftOnePosToRight()
s.encode2bLength()
if s.h < 128 {
// run блок - отже треба збільшити лічильник
if s.h < 127 {
// increase counter
s.h++
s.buf.SetByte(s.pos-1, s.h)
} else {
if s.length < 32769 {
s.encode2bLength()
} else {
s.appendNewDelta(delta)
}
// не можу збільшити - буде переповнення. Додаю новий literal блок
// counter overflow
// fix encode delta
s.appendNewLiteral(delta)
}
} else {
// literal блок.
// Якщо в ньому лише одне значення - перетворюю його на run блок.
// Інакше забираю останнє значення із literal блока і додаю новий run блок.
q := s.h & 127
if q == 0 { // 1 кодується як 0
s.convertLiteralToRun()
} else {
s.convertLastFromLiteralToRun()
}
}
} else {
s.appendNewDelta(delta)
if s.h < 127 {
// end of run
s.appendNewLiteral(delta)
} else {
if s.h < 255 {
// encode value from pos - 1, then append h byte
s.appendDeltaToLiteral(delta)
} else {
// overflowed - encode new
s.appendNewLiteral(delta)
}
}
}
}
}
func (s *ReverseInstantDeltaCompressor) appendNewDelta(delta int64) {
s.length = 0
s8 := s.buf.GetByte(s.pos - 2)
s8q := s.buf.GetByte(s.pos - 1)
if s8q == 8 {
s.pos -= 1
s8 = 0
s8q = 1
} else {
s.pos -= 2
s8q++
}
s.encodeNewDelta(delta, s8, s8q)
func (s *ReverseInstantDeltaCompressor) convertLastFromLiteralToRun() {
// Зменшую кількість елементів в literal блоці
s.h--
s.pos -= 1 + s.lastDeltaSize
s.buf.SetByte(s.pos, s.h) // закриваю literal блок
s.pos++
s.lastDeltaSize = s.buf.ReversePutVarInt64(s.pos, s.lastDelta)
s.pos += s.lastDeltaSize
s.h = 0 // run блок, довжини 2
s.buf.SetByte(s.pos, s.h)
s.pos++
}
func (s *ReverseInstantDeltaCompressor) encodeNewDelta(delta int64, s8 byte, s8q byte) {
func (s *ReverseInstantDeltaCompressor) convertLiteralToRun() {
// Знімаю flagLiteral, а лічильник 0 дорівнює 2 елементам в серії.
s.h = 0
s.buf.SetByte(s.pos-1, s.h)
}
func (s *ReverseInstantDeltaCompressor) appendDeltaToLiteral(delta int64) {
s.h++ // збільшую к-сть дельт
s.lastDelta = delta
s.numIdx = s.pos
n := s.buf.ReversePutVarInt64(s.pos, s.lastDelta)
s.pos += n
s.buf.SetByte(s.pos, s8)
s.pos++
s.buf.SetByte(s.pos, s8q)
s.pos--
s.lastDeltaSize = s.buf.ReversePutVarInt64(s.pos, delta)
s.pos += s.lastDeltaSize
s.buf.SetByte(s.pos, s.h)
s.pos++
}
func (s *ReverseInstantDeltaCompressor) shiftOnePosToRight() {
s.buf.ShiftOnePosToRight(s.numIdx, s.pos)
func (s *ReverseInstantDeltaCompressor) appendNewLiteral(delta int64) {
s.h = flagLiteral
s.lastDelta = delta
s.lastDeltaSize = s.buf.ReversePutVarInt64(s.pos, delta)
s.pos += s.lastDeltaSize
s.buf.SetByte(s.pos, flagLiteral) // literal, length = 1
s.pos++
s.numIdx++
}
func (s *ReverseInstantDeltaCompressor) encode2bLength() {
num := s.length - 2
s.buf.SetByte(s.numIdx-1, byte(num&127)|128)
s.buf.SetByte(s.numIdx-2, byte(num>>7))
func (s *ReverseInstantDeltaCompressor) DeleteLast() {}
type InstantDeltaBound struct {
Pos int
H byte
LastDelta int64
}
func (s *ReverseInstantDeltaCompressor) DeleteLast() {
var (
s8q = s.buf.GetByte(s.pos - 1)
s8 = s.buf.GetByte(s.pos - 2)
flag byte = 1 << uint(8-s8q)
)
if s.length > 0 {
if s.length == 2 {
s.length = 0
s.buf.UnsetFlag(s.pos-2, flag)
s.buf.ShiftOnePosToLeft(s.numIdx, s.pos)
s.numIdx--
s.pos--
} else if s.length < 130 {
s.length--
s.buf.SetByte(s.numIdx-1, byte(s.length)-2)
} else if s.length == 130 {
s.length--
s.buf.ShiftOnePosToLeft(s.numIdx, s.pos)
s.numIdx--
s.pos--
s.buf.SetByte(s.numIdx-1, byte(s.length)-2)
} else {
s.length--
s.encode2bLength()
}
} else {
if s8q > 1 {
s8q--
flag = 1 << uint(8-s8q)
s.pos = s.numIdx + 2
s.buf.SetByte(s.pos-2, s8)
s.buf.SetByte(s.pos-1, s8q)
} else {
s.pos = s.numIdx + 1
s.buf.SetByte(s.pos-1, 8)
s8 = s.buf.GetByte(s.pos - 2)
flag = 1
}
var (
pos = s.pos - 3
n int
err error
)
s.lastDelta, n, err = s.buf.ReverseGetVarInt64(pos)
if err != nil {
panic(err)
}
s.numIdx = pos - n
if (s8 & flag) == flag {
s.length, _ = s.buf.DecodeRunLength(s.numIdx - 1)
}
func (s *ReverseInstantDeltaCompressor) GetState() InstantDeltaBound {
return InstantDeltaBound{
Pos: s.pos - 1 - s.lastDeltaSize,
H: s.h,
LastDelta: s.lastDelta,
}
}
// DECOMPRESSOR
type ReverseInstantDeltaDecompressor struct {
step byte
buf *conbuf.ContinuousBuffer
pos int
bound int
firstValue float64
lastValue float64
length uint16
coef float64
idxOf8 uint
s8 byte
buf *conbuf.ContinuousBuffer
coef float64
pos int
bound int
baseValue float64
lastValue float64
isRun bool
pending int
done bool
}
func NewReverseInstantDeltaDecompressor(buf *conbuf.ContinuousBuffer, size int, fracDigits byte) *ReverseInstantDeltaDecompressor {
@@ -278,68 +176,169 @@ func NewReverseInstantDeltaDecompressor(buf *conbuf.ContinuousBuffer, size int,
if fracDigits > 0 {
coef = math.Pow(10, float64(fracDigits))
}
return &ReverseInstantDeltaDecompressor{
buf: buf,
coef: coef,
pos: size,
i64, n, err := buf.GetVarInt64(0)
if err != nil {
log.Fatalf("bug: get base value: %s", err)
}
//fmt.Printf("baseValue: %.2f, bound: %d, pos: %d\n", float64(u64)/coef, n, size)
return &ReverseInstantDeltaDecompressor{
buf: buf,
coef: coef,
pos: size - 1, // last elem
baseValue: float64(i64) / coef,
bound: n,
}
}
func (s *ReverseInstantDeltaDecompressor) RestoreFromEnd() {
if s.pos > s.bound {
// читаю заголовок наступної серії
s.readHeader()
s.readValue()
} else {
s.done = true
}
}
func (s *ReverseInstantDeltaDecompressor) RestoreFromBound(bound InstantDeltaBound) {
s.pos = bound.Pos - 1
s.lastValue = s.baseValue + float64(bound.LastDelta)
s.decodeHeaderByte(bound.H)
fmt.Printf("restore from bound: isRun=%t, pending=%d\n", s.isRun, s.pending)
}
func (s *ReverseInstantDeltaDecompressor) NextValue() (value float64, done bool) {
if s.step > 0 {
if s.length > 0 {
s.length--
return s.lastValue, false
}
if s.pos < s.bound {
return 0, true
}
if s.idxOf8 == 0 {
s.s8 = s.buf.GetByte(s.pos)
s.pos--
}
s.readVar()
if s.length > 0 {
s.length--
}
return s.lastValue, false
//fmt.Printf("NextValue(): bound: %d, pos: %d, pending: %d\n", s.bound, s.pos, s.pending)
if s.done {
return 0, true
}
i64, n, err := s.buf.GetVarInt64(0)
if err != nil {
panic(err)
// повертаю значення, що було прочитано в методі RestoreFromBound/RestoreFromEnd
value = s.lastValue
s.pending--
if s.pending > 0 {
// якщо в серії залишаються елементи
if !s.isRun {
s.readValue()
}
} else if s.pos > s.bound {
// в серії більше немає елементів, отже перевіряє чи є ще дані в буфері.
// дані є - читаю заголовок наступної серії
s.readHeader()
s.readValue()
} else {
s.done = true
}
s.firstValue = float64(i64) / s.coef
s.bound = n
s.pos--
s.idxOf8 = uint(8 - s.buf.GetByte(s.pos))
s.pos--
s.s8 = s.buf.GetByte(s.pos)
s.pos--
s.readVar()
if s.length > 0 {
s.length--
}
s.step = 1
return s.lastValue, false
// серія завершена - перевіряю чи є ще серії
return value, false
}
func (s *ReverseInstantDeltaDecompressor) readVar() {
func (s *ReverseInstantDeltaDecompressor) readHeader() {
h := s.buf.GetByte(s.pos)
s.pos--
s.decodeHeaderByte(h)
// fmt.Println("h:", h)
// fmt.Println("isRun:", s.isRun)
// fmt.Println("pending:", s.pending)
}
func (s *ReverseInstantDeltaDecompressor) readValue() {
i64, n, err := s.buf.ReverseGetVarInt64(s.pos)
if err != nil {
panic(err)
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.lastValue = s.firstValue + float64(i64)/s.coef
s.lastValue = s.baseValue + float64(i64)/s.coef
}
var flag byte = 1 << s.idxOf8
if (s.s8 & flag) == flag {
s.length, n = s.buf.DecodeRunLength(s.pos)
s.pos -= n
}
if s.idxOf8 == 7 {
s.idxOf8 = 0
func (s *ReverseInstantDeltaDecompressor) decodeHeaderByte(h byte) {
s.isRun = h < 128
if s.isRun {
s.pending = int(h&127) + 2
} else {
s.idxOf8++
s.pending = int(h&127) + 1
}
}
// DECOMPRESSOR
// type ReverseInstantDeltaDecompressor struct {
// buf *conbuf.ContinuousBuffer
// pos int
// bound int
// baseValue float64
// lastValue float64
// coef float64
// isRun bool
// pending int
// }
// func NewReverseInstantDeltaDecompressor(buf *conbuf.ContinuousBuffer, size int, fracDigits byte) *ReverseInstantDeltaDecompressor {
// var coef float64 = 1
// if fracDigits > 0 {
// coef = math.Pow(10, float64(fracDigits))
// }
// u64, n, err := buf.GetVarInt64(0)
// if err != nil {
// log.Fatalf("bug: get base value: %s", err)
// }
// //fmt.Printf("baseValue: %.2f, bound: %d, pos: %d\n", float64(u64)/coef, n, size)
// return &ReverseInstantDeltaDecompressor{
// buf: buf,
// coef: coef,
// pos: size - 1, // last elem
// baseValue: float64(u64) / coef,
// bound: n,
// }
// }
// func (s *ReverseInstantDeltaDecompressor) NextValue() (value float64, done bool) {
// //fmt.Printf("NextValue(): bound: %d, pos: %d, pending: %d\n", s.bound, s.pos, s.pending)
// if s.pending > 0 {
// // якщо в серії залишаються елементи
// if !s.isRun {
// s.readValue()
// }
// s.pending--
// return s.lastValue, false
// }
// if s.pos > s.bound {
// // читаю заголовок наступної серії
// s.readHeader()
// s.readValue()
// s.pending--
// return s.lastValue, false
// }
// // серія завершена - перевіряю чи є ще серії
// return 0, true
// }
// func (s *ReverseInstantDeltaDecompressor) readHeader() {
// h := s.buf.GetByte(s.pos)
// s.pos--
// s.isRun = h < 128
// if s.isRun {
// s.pending = int(h&127) + 2
// } else {
// s.pending = int(h&127) + 1
// }
// // fmt.Println("h:", h)
// // fmt.Println("isRun:", s.isRun)
// // fmt.Println("pending:", s.pending)
// }
// func (s *ReverseInstantDeltaDecompressor) readValue() {
// i64, n, err := s.buf.ReverseGetVarInt64(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
// s.lastValue = s.baseValue + float64(i64)/s.coef
// }