This commit is contained in:
2026-06-07 01:06:16 +00:00
parent 0e019118c2
commit 776a341c57
9 changed files with 738 additions and 747 deletions

View File

@@ -9,7 +9,7 @@ import (
"gordenko.dev/dima/qb/bin"
)
type ReverseInstantDeltaCompressor struct {
type InstantDeltaCompressor struct {
buf []byte
coef float64
pos int
@@ -20,12 +20,12 @@ type ReverseInstantDeltaCompressor struct {
state *InstantDeltaBound
}
func NewReverseInstantDeltaCompressor(buf []byte, size int, fracDigits byte) *ReverseInstantDeltaCompressor {
func NewInstantDeltaCompressor(buf []byte, size int, fracDigits byte) *InstantDeltaCompressor {
var coef float64 = 1
if fracDigits > 0 {
coef = math.Pow(10, float64(fracDigits))
}
s := &ReverseInstantDeltaCompressor{
s := &InstantDeltaCompressor{
buf: buf,
pos: size,
coef: coef,
@@ -45,11 +45,11 @@ func NewReverseInstantDeltaCompressor(buf []byte, size int, fracDigits byte) *Re
return s
}
func (s *ReverseInstantDeltaCompressor) Size() int {
func (s *InstantDeltaCompressor) Size() int {
return s.pos
}
func (s *ReverseInstantDeltaCompressor) Append(value float64) {
func (s *InstantDeltaCompressor) Append(value float64) {
if s.pos == 0 {
// base value
n, _ := bin.PutVarInt64(s.buf[s.pos:], int64(value*s.coef))
@@ -105,7 +105,7 @@ func (s *ReverseInstantDeltaCompressor) Append(value float64) {
}
}
func (s *ReverseInstantDeltaCompressor) convertLastFromLiteralToRun() {
func (s *InstantDeltaCompressor) convertLastFromLiteralToRun() {
// Зменшую кількість елементів в literal блоці
s.h--
s.pos -= 1 + s.lastDeltaSize
@@ -118,13 +118,13 @@ func (s *ReverseInstantDeltaCompressor) convertLastFromLiteralToRun() {
s.pos++
}
func (s *ReverseInstantDeltaCompressor) convertLiteralToRun() {
func (s *InstantDeltaCompressor) convertLiteralToRun() {
// Знімаю flagLiteral, а лічильник 0 дорівнює 2 елементам в серії.
s.h = 0
s.buf[s.pos-1] = s.h
}
func (s *ReverseInstantDeltaCompressor) appendDeltaToLiteral(delta int64) {
func (s *InstantDeltaCompressor) appendDeltaToLiteral(delta int64) {
s.h++ // збільшую к-сть дельт
s.lastDelta = delta
s.pos--
@@ -134,7 +134,7 @@ func (s *ReverseInstantDeltaCompressor) appendDeltaToLiteral(delta int64) {
s.pos++
}
func (s *ReverseInstantDeltaCompressor) appendNewLiteral(delta int64) {
func (s *InstantDeltaCompressor) appendNewLiteral(delta int64) {
s.h = flagLiteral
s.lastDelta = delta
s.lastDeltaSize, _ = bin.ReversePutVarInt64(s.buf[s.pos:], delta)
@@ -143,7 +143,7 @@ func (s *ReverseInstantDeltaCompressor) appendNewLiteral(delta int64) {
s.pos++
}
func (s *ReverseInstantDeltaCompressor) DeleteLast() {}
func (s *InstantDeltaCompressor) DeleteLast() {}
type InstantDeltaBound struct {
Pos int
@@ -153,7 +153,7 @@ type InstantDeltaBound struct {
}
// delta h
func (s *ReverseInstantDeltaCompressor) Lock() {
func (s *InstantDeltaCompressor) Lock() {
if s.state != nil {
qb.Abort(qb.RepeatableLock, nil)
}
@@ -168,18 +168,18 @@ func (s *ReverseInstantDeltaCompressor) Lock() {
}
// fix - повернути в Pool буфери
func (s *ReverseInstantDeltaCompressor) Unlock() {
func (s *InstantDeltaCompressor) Unlock() {
s.state = nil
}
func (s *ReverseInstantDeltaCompressor) Offset() int {
func (s *InstantDeltaCompressor) Offset() int {
if s.state != nil {
return s.state.Pos
}
return 0
}
func (s *ReverseInstantDeltaCompressor) Snapshot() ([]byte, int) {
func (s *InstantDeltaCompressor) Snapshot() ([]byte, int) {
// if s.state == nil {
// return s.buf, s.Size()
// }
@@ -202,18 +202,18 @@ func (s *ReverseInstantDeltaCompressor) Snapshot() ([]byte, int) {
return nil, 0
}
func (s *ReverseInstantDeltaCompressor) CreateDecompressor(fracDigits byte) qb.ValueDecompressor {
func (s *InstantDeltaCompressor) CreateDecompressor(fracDigits byte) qb.ValueDecompressor {
if s.state == nil {
d := NewReverseInstantDeltaDecompressor(s.buf, s.Size(), fracDigits)
d := NewInstantDeltaDecompressor(s.buf, s.Size(), fracDigits)
d.RestoreFromEnd()
return d
}
d := NewReverseInstantDeltaDecompressor(s.buf, s.Size(), fracDigits)
d := NewInstantDeltaDecompressor(s.buf, s.Size(), fracDigits)
d.RestoreFromBound(*s.state)
return d
}
func (s *ReverseInstantDeltaCompressor) Renew() {
func (s *InstantDeltaCompressor) Renew() {
// УВАГА!
// state не чіпаємо
s.buf = make([]byte, minBufferSize)
@@ -225,17 +225,17 @@ func (s *ReverseInstantDeltaCompressor) Renew() {
s.h = 0
}
func (s *ReverseInstantDeltaCompressor) CalcRequiredSpace(value float64) int {
func (s *InstantDeltaCompressor) CalcRequiredSpace(value float64) int {
return 0
}
func (s *ReverseInstantDeltaCompressor) Chunks() []byte {
func (s *InstantDeltaCompressor) Chunks() []byte {
return s.buf
}
// DECOMPRESSOR
type ReverseInstantDeltaDecompressor struct {
type InstantDeltaDecompressor struct {
buf []byte
coef float64
pos int
@@ -247,7 +247,7 @@ type ReverseInstantDeltaDecompressor struct {
done bool
}
func NewReverseInstantDeltaDecompressor(buf []byte, size int, fracDigits byte) *ReverseInstantDeltaDecompressor {
func NewInstantDeltaDecompressor(buf []byte, size int, fracDigits byte) *InstantDeltaDecompressor {
var coef float64 = 1
if fracDigits > 0 {
coef = math.Pow(10, float64(fracDigits))
@@ -257,7 +257,7 @@ func NewReverseInstantDeltaDecompressor(buf []byte, size int, fracDigits byte) *
log.Fatalf("bug: get base value: %s", err)
}
//fmt.Printf("baseValue: %.2f, bound: %d, pos: %d\n", float64(u64)/coef, n, size)
return &ReverseInstantDeltaDecompressor{
return &InstantDeltaDecompressor{
buf: buf,
coef: coef,
pos: size - 1, // last elem
@@ -266,7 +266,7 @@ func NewReverseInstantDeltaDecompressor(buf []byte, size int, fracDigits byte) *
}
}
func (s *ReverseInstantDeltaDecompressor) RestoreFromEnd() {
func (s *InstantDeltaDecompressor) RestoreFromEnd() {
if s.pos > s.bound {
// читаю заголовок наступної серії
s.readHeader()
@@ -276,14 +276,14 @@ func (s *ReverseInstantDeltaDecompressor) RestoreFromEnd() {
}
}
func (s *ReverseInstantDeltaDecompressor) RestoreFromBound(bound InstantDeltaBound) {
func (s *InstantDeltaDecompressor) 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) {
func (s *InstantDeltaDecompressor) NextValue() (value float64, done bool) {
//fmt.Printf("NextValue(): bound: %d, pos: %d, pending: %d\n", s.bound, s.pos, s.pending)
if s.done {
return 0, true
@@ -308,7 +308,7 @@ func (s *ReverseInstantDeltaDecompressor) NextValue() (value float64, done bool)
return value, false
}
func (s *ReverseInstantDeltaDecompressor) readHeader() {
func (s *InstantDeltaDecompressor) readHeader() {
h := s.buf[s.pos]
s.pos--
s.decodeHeaderByte(h)
@@ -316,7 +316,7 @@ func (s *ReverseInstantDeltaDecompressor) readHeader() {
// fmt.Println("isRun:", s.isRun)
// fmt.Println("pending:", s.pending)
}
func (s *ReverseInstantDeltaDecompressor) readValue() {
func (s *InstantDeltaDecompressor) readValue() {
i64, n, err := bin.ReverseGetVarInt64(s.buf[:s.pos])
if err != nil {
log.Fatalln(err)
@@ -330,7 +330,7 @@ func (s *ReverseInstantDeltaDecompressor) readValue() {
s.lastValue = s.baseValue + float64(i64)/s.coef
}
func (s *ReverseInstantDeltaDecompressor) decodeHeaderByte(h byte) {
func (s *InstantDeltaDecompressor) decodeHeaderByte(h byte) {
s.isRun = h < 128
if s.isRun {
s.pending = int(h&127) + 2