This commit is contained in:
2026-05-31 20:01:28 +00:00
parent e7ec6083cb
commit db5ecd3dfd
28 changed files with 2090 additions and 2155 deletions

View File

@@ -5,6 +5,7 @@ import (
"log"
"gordenko.dev/dima/pretty"
"gordenko.dev/dima/qb"
"gordenko.dev/dima/qb/conbuf"
)
@@ -15,6 +16,7 @@ type ReverseTimeDeltaCompressor struct {
lastDelta uint32
lastDeltaSize int
h byte
state *TimeDeltaBound
}
func NewReverseTimeDeltaCompressor(buf *conbuf.ContinuousBuffer, size int) *ReverseTimeDeltaCompressor {
@@ -45,10 +47,6 @@ func (s *ReverseTimeDeltaCompressor) Size() int {
return s.pos
}
func (s *ReverseTimeDeltaCompressor) Chunks() [][]byte {
return s.buf.Chunks()
}
func (s *ReverseTimeDeltaCompressor) CalcRequiredSpace(value uint32) int {
return 0
}
@@ -151,42 +149,109 @@ func (s *ReverseTimeDeltaCompressor) Sync() {
s.pos += s.buf.ReversePutVarUint64(s.pos, uint64(s.lastUnixtime))
}
// FIX - check methods
type TimeDeltaBound struct {
Pos int
H byte
LastUnixtime uint32
LastDelta uint32
Chunks [][]byte
}
// delta h
func (s *ReverseTimeDeltaCompressor) GetState() TimeDeltaBound { // fix replace by Lock
bound := TimeDeltaBound{
LastUnixtime: s.lastUnixtime,
}
if s.pos > 0 {
bound.Pos = s.pos - 1 - s.lastDeltaSize
bound.H = s.h
bound.LastDelta = s.lastDelta
}
return bound
}
// func (s *ReverseTimeDeltaCompressor) GetState() TimeDeltaBound { // fix replace by Lock
// bound := TimeDeltaBound{
// LastUnixtime: s.lastUnixtime,
// }
// if s.pos > 0 {
// bound.Pos = s.pos - 1 - s.lastDeltaSize
// bound.H = s.h
// bound.LastDelta = s.lastDelta
// }
// return bound
// }
// delta h
func (s *ReverseTimeDeltaCompressor) Lock() {
// fix
if s.state != nil {
qb.Abort(qb.RepeatableLock, nil)
}
// позиція посувається вліво, отже може перескочити на попередній chunk
pos := s.pos - 1 - s.lastDeltaSize
chunksQty := pos / conbuf.ChunkSize
if (pos % conbuf.ChunkSize) > 0 {
chunksQty++
}
s.state = &TimeDeltaBound{
Pos: pos,
H: s.h,
LastUnixtime: s.lastUnixtime, // fix
LastDelta: s.lastDelta,
Chunks: s.buf.Chunks()[:chunksQty],
}
}
// fix - повернути в Pool буфери
func (s *ReverseTimeDeltaCompressor) Unlock() {
//s.state = nil // fix
s.state = nil
}
func (s *ReverseTimeDeltaCompressor) Offset() int {
if s.state != nil {
return s.state.Pos
}
return 0
}
// fix -
func (s *ReverseTimeDeltaCompressor) Snapshot() ([][]byte, int) {
if s.state == nil {
return s.buf.Chunks(), s.Size()
}
// ВАЖЛИВО!
// Треба відтворити стан останнього чанка
var (
pos = s.state.Pos
chunk = make([]byte, conbuf.ChunkSize)
lastChunkIdx = len(s.state.Chunks) - 1
qtyToCopy = pos % conbuf.ChunkSize
)
copy(chunk, s.state.Chunks[lastChunkIdx][:qtyToCopy])
chunks := append(s.state.Chunks[:lastChunkIdx], chunk)
buf := conbuf.New(chunks)
pos += buf.ReversePutVarUint64(pos, uint64(s.state.LastDelta))
buf.SetByte(pos, s.state.H)
pos++
return chunks, pos
}
func (s *ReverseTimeDeltaCompressor) CreateDecompressor() qb.TimestampDecompressor {
if s.state == nil {
d := NewReverseTimeDeltaDecompressor(s.buf, s.Size())
d.RestoreFromEnd()
return d
}
d := NewReverseTimeDeltaDecompressor(s.buf, s.Size())
d.RestoreFromBound(*s.state)
return d
}
func (s *ReverseTimeDeltaCompressor) Renew() {
// s.buf = conbuf.New(nil)
// s.pos = 0
// //
// s.baseValue = 0
// s.lastDelta = 0
// s.lastDeltaSize = 0
// s.h = 0
// УВАГА!
// state не чіпаємо
s.buf = conbuf.New(nil)
s.pos = 0
//
//s.baseValue = 0
s.lastDelta = 0
s.lastDeltaSize = 0
s.h = 0
}
func (s *ReverseTimeDeltaCompressor) Chunks() [][]byte {
return s.buf.Chunks()
}
// DECOMPRESSOR