This commit is contained in:
2026-06-05 19:43:01 +00:00
parent db5ecd3dfd
commit 05eea3b279
16 changed files with 1187 additions and 724 deletions

View File

@@ -6,11 +6,12 @@ import (
"math"
"gordenko.dev/dima/qb"
"gordenko.dev/dima/qb/bin"
"gordenko.dev/dima/qb/conbuf"
)
type ReverseInstantDeltaCompressor struct {
buf *conbuf.ContinuousBuffer
buf []byte
coef float64
pos int
baseValue float64
@@ -20,7 +21,7 @@ type ReverseInstantDeltaCompressor struct {
state *InstantDeltaBound
}
func NewReverseInstantDeltaCompressor(buf *conbuf.ContinuousBuffer, size int, fracDigits byte) *ReverseInstantDeltaCompressor {
func NewReverseInstantDeltaCompressor(buf []byte, size int, fracDigits byte) *ReverseInstantDeltaCompressor {
var coef float64 = 1
if fracDigits > 0 {
coef = math.Pow(10, float64(fracDigits))
@@ -31,13 +32,13 @@ func NewReverseInstantDeltaCompressor(buf *conbuf.ContinuousBuffer, size int, fr
coef: coef,
}
if size > 0 {
i64, _, err := s.buf.GetVarInt64(0)
i64, _, err := bin.GetVarInt64(s.buf)
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)
s.h = s.buf[s.pos-1]
s.lastDelta, s.lastDeltaSize, err = bin.ReverseGetVarInt64(s.buf[:s.pos-2])
if err != nil {
log.Fatalf("bug: get last delta: %s", err)
}
@@ -52,7 +53,7 @@ func (s *ReverseInstantDeltaCompressor) Size() int {
func (s *ReverseInstantDeltaCompressor) Append(value float64) {
if s.pos == 0 {
// base value
n := s.buf.PutVarInt64(s.pos, int64(value*s.coef))
n, _ := bin.PutVarInt64(s.buf[s.pos:], int64(value*s.coef))
s.pos += n
s.baseValue = value
s.appendNewLiteral(0)
@@ -70,7 +71,7 @@ func (s *ReverseInstantDeltaCompressor) Append(value float64) {
if s.h < 127 {
// increase counter
s.h++
s.buf.SetByte(s.pos-1, s.h)
s.buf[s.pos-1] = s.h
} else {
// не можу збільшити - буде переповнення. Додаю новий literal блок
// counter overflow
@@ -109,37 +110,37 @@ func (s *ReverseInstantDeltaCompressor) convertLastFromLiteralToRun() {
// Зменшую кількість елементів в literal блоці
s.h--
s.pos -= 1 + s.lastDeltaSize
s.buf.SetByte(s.pos, s.h) // закриваю literal блок
s.buf[s.pos] = s.h // закриваю literal блок
s.pos++
s.lastDeltaSize = s.buf.ReversePutVarInt64(s.pos, s.lastDelta)
s.lastDeltaSize, _ = bin.ReversePutVarInt64(s.buf[:s.pos], s.lastDelta)
s.pos += s.lastDeltaSize
s.h = 0 // run блок, довжини 2
s.buf.SetByte(s.pos, s.h)
s.buf[s.pos] = s.h
s.pos++
}
func (s *ReverseInstantDeltaCompressor) convertLiteralToRun() {
// Знімаю flagLiteral, а лічильник 0 дорівнює 2 елементам в серії.
s.h = 0
s.buf.SetByte(s.pos-1, s.h)
s.buf[s.pos-1] = s.h
}
func (s *ReverseInstantDeltaCompressor) appendDeltaToLiteral(delta int64) {
s.h++ // збільшую к-сть дельт
s.lastDelta = delta
s.pos--
s.lastDeltaSize = s.buf.ReversePutVarInt64(s.pos, delta)
s.lastDeltaSize, _ = bin.ReversePutVarInt64(s.buf[s.pos:], delta)
s.pos += s.lastDeltaSize
s.buf.SetByte(s.pos, s.h)
s.buf[s.pos] = s.h
s.pos++
}
func (s *ReverseInstantDeltaCompressor) appendNewLiteral(delta int64) {
s.h = flagLiteral
s.lastDelta = delta
s.lastDeltaSize = s.buf.ReversePutVarInt64(s.pos, delta)
s.lastDeltaSize, _ = bin.ReversePutVarInt64(s.buf[s.pos:], delta)
s.pos += s.lastDeltaSize
s.buf.SetByte(s.pos, flagLiteral) // literal, length = 1
s.buf[s.pos] = flagLiteral // literal, length = 1
s.pos++
}
@@ -149,7 +150,7 @@ type InstantDeltaBound struct {
Pos int
H byte
LastDelta int64
Chunks [][]byte
Chunks []byte
}
// delta h
@@ -159,15 +160,11 @@ func (s *ReverseInstantDeltaCompressor) Lock() {
}
// позиція посувається вліво, отже може перескочити на попередній chunk
pos := s.pos - 1 - s.lastDeltaSize
chunksQty := pos / conbuf.ChunkSize
if (pos % conbuf.ChunkSize) > 0 {
chunksQty++
}
s.state = &InstantDeltaBound{
Pos: pos,
H: s.h,
LastDelta: s.lastDelta,
Chunks: s.buf.Chunks()[:chunksQty],
Chunks: s.buf[:s.pos], // fix check ?
}
}
@@ -183,9 +180,9 @@ func (s *ReverseInstantDeltaCompressor) Offset() int {
return 0
}
func (s *ReverseInstantDeltaCompressor) Snapshot() ([][]byte, int) {
func (s *ReverseInstantDeltaCompressor) Snapshot() ([]byte, int) {
if s.state == nil {
return s.buf.Chunks(), s.Size()
return s.buf, s.Size()
}
// ВАЖЛИВО!
// Треба відтворити стан останнього чанка
@@ -219,7 +216,7 @@ func (s *ReverseInstantDeltaCompressor) CreateDecompressor(fracDigits byte) qb.V
func (s *ReverseInstantDeltaCompressor) Renew() {
// УВАГА!
// state не чіпаємо
s.buf = conbuf.New(nil)
s.buf = make([]byte, minBufferSize)
s.pos = 0
//
s.baseValue = 0
@@ -232,14 +229,14 @@ func (s *ReverseInstantDeltaCompressor) CalcRequiredSpace(value float64) int {
return 0
}
func (s *ReverseInstantDeltaCompressor) Chunks() [][]byte {
return s.buf.Chunks()
func (s *ReverseInstantDeltaCompressor) Chunks() []byte {
return s.buf
}
// DECOMPRESSOR
type ReverseInstantDeltaDecompressor struct {
buf *conbuf.ContinuousBuffer
buf []byte
coef float64
pos int
bound int
@@ -250,12 +247,12 @@ type ReverseInstantDeltaDecompressor struct {
done bool
}
func NewReverseInstantDeltaDecompressor(buf *conbuf.ContinuousBuffer, size int, fracDigits byte) *ReverseInstantDeltaDecompressor {
func NewReverseInstantDeltaDecompressor(buf []byte, size int, fracDigits byte) *ReverseInstantDeltaDecompressor {
var coef float64 = 1
if fracDigits > 0 {
coef = math.Pow(10, float64(fracDigits))
}
i64, n, err := buf.GetVarInt64(0)
i64, n, err := bin.GetVarInt64(buf)
if err != nil {
log.Fatalf("bug: get base value: %s", err)
}
@@ -312,7 +309,7 @@ func (s *ReverseInstantDeltaDecompressor) NextValue() (value float64, done bool)
}
func (s *ReverseInstantDeltaDecompressor) readHeader() {
h := s.buf.GetByte(s.pos)
h := s.buf[s.pos]
s.pos--
s.decodeHeaderByte(h)
// fmt.Println("h:", h)
@@ -320,7 +317,7 @@ func (s *ReverseInstantDeltaDecompressor) readHeader() {
// fmt.Println("pending:", s.pending)
}
func (s *ReverseInstantDeltaDecompressor) readValue() {
i64, n, err := s.buf.ReverseGetVarInt64(s.pos)
i64, n, err := bin.ReverseGetVarInt64(s.buf[:s.pos])
if err != nil {
log.Fatalln(err)
}