This commit is contained in:
2025-06-03 05:04:18 +03:00
parent 0f50873f0f
commit fbb30f31e8
54 changed files with 13234 additions and 0 deletions

102
enc/cumdelta.go Normal file
View File

@@ -0,0 +1,102 @@
package enc
import (
"math"
"gordenko.dev/dima/diploma/bin"
)
type ReverseCumulativeDeltaDecompressor struct {
buf []byte
pos int
bound int
firstValue float64
lastValue float64
length uint16
coef float64
idxOf8 uint
s8 byte
}
func NewReverseCumulativeDeltaDecompressor(buf []byte, fracDigits byte) *ReverseCumulativeDeltaDecompressor {
var coef float64 = 1
if fracDigits > 0 {
coef = math.Pow(10, float64(fracDigits))
}
return &ReverseCumulativeDeltaDecompressor{
buf: buf,
coef: coef,
pos: len(buf),
}
}
func (s *ReverseCumulativeDeltaDecompressor) NextValue() (value float64, done bool) {
if s.length > 0 {
s.length--
return s.lastValue, false
}
if s.pos < s.bound {
return 0, true
}
if s.pos == len(s.buf) {
u64, n, err := bin.GetVarUint64(s.buf)
if err != nil {
panic(err)
}
s.firstValue = float64(u64) / s.coef
s.bound = n
s.pos--
s.idxOf8 = uint(8 - s.buf[s.pos])
s.pos--
s.s8 = s.buf[s.pos]
s.pos--
s.readVar()
if s.length > 0 {
s.length--
}
return s.lastValue, false
}
if s.idxOf8 == 0 {
s.s8 = s.buf[s.pos]
s.pos--
}
s.readVar()
if s.length > 0 {
s.length--
}
return s.lastValue, false
}
func (s *ReverseCumulativeDeltaDecompressor) readVar() {
u64, n, err := bin.ReverseGetVarUint64(s.buf[:s.pos+1])
if err != nil {
panic(err)
}
s.pos -= n
s.lastValue = s.firstValue + float64(u64)/s.coef
var flag byte = 1 << s.idxOf8
if (s.s8 & flag) == flag {
s.decodeLength()
}
if s.idxOf8 == 7 {
s.idxOf8 = 0
} else {
s.idxOf8++
}
}
func (s *ReverseCumulativeDeltaDecompressor) decodeLength() {
b1 := s.buf[s.pos]
s.pos--
if b1 < 128 {
s.length = uint16(b1)
} else {
b2 := s.buf[s.pos]
s.pos--
s.length = uint16(b1&127) | (uint16(b2) << 7)
}
s.length += 2
}

3
enc/enc.go Normal file
View File

@@ -0,0 +1,3 @@
package enc
const eps = 0.000001

130
enc/insdelta.go Normal file
View File

@@ -0,0 +1,130 @@
package enc
import (
"fmt"
"math"
octopus "gordenko.dev/dima/diploma"
"gordenko.dev/dima/diploma/bin"
)
type ReverseInstantDeltaDecompressor struct {
buf []byte
pos int
bound int
firstValue float64
lastValue float64
length uint16
coef float64
idxOf8 uint
s8 byte
}
func NewReverseInstantDeltaDecompressor(buf []byte, fracDigits byte) *ReverseInstantDeltaDecompressor {
var coef float64 = 1
if fracDigits > 0 {
coef = math.Pow(10, float64(fracDigits))
}
return &ReverseInstantDeltaDecompressor{
buf: buf,
coef: coef,
pos: len(buf),
}
}
func (s *ReverseInstantDeltaDecompressor) NextValue() (value float64, done bool) {
if s.length > 0 {
s.length--
return s.lastValue, false
}
if s.pos < s.bound {
return 0, true
}
if s.pos == len(s.buf) {
u64, n, err := bin.GetVarInt64(s.buf)
if err != nil {
panic(err)
}
s.firstValue = float64(u64) / s.coef
s.bound = n
s.pos--
s.idxOf8 = uint(8 - s.buf[s.pos])
s.pos--
s.s8 = s.buf[s.pos]
s.pos--
s.readVar()
if s.length > 0 {
s.length--
}
return s.lastValue, false
}
if s.idxOf8 == 0 {
s.s8 = s.buf[s.pos]
s.pos--
}
s.readVar()
if s.length > 0 {
s.length--
}
return s.lastValue, false
}
func (s *ReverseInstantDeltaDecompressor) readVar() {
i64, n, err := bin.ReverseGetVarInt64(s.buf[:s.pos+1])
if err != nil {
panic(err)
}
s.pos -= n
s.lastValue = s.firstValue + float64(i64)/s.coef
var flag byte = 1 << s.idxOf8
if (s.s8 & flag) == flag {
s.decodeLength()
}
if s.idxOf8 == 7 {
s.idxOf8 = 0
} else {
s.idxOf8++
}
}
func (s *ReverseInstantDeltaDecompressor) decodeLength() {
b1 := s.buf[s.pos]
s.pos--
if b1 < 128 {
s.length = uint16(b1)
} else {
b2 := s.buf[s.pos]
s.pos--
s.length = uint16(b1&127) | (uint16(b2) << 7)
}
s.length += 2
}
func GetValueBounds(valuesBuf []byte, metricType octopus.MetricType, fracDigits byte) (sinceValue, untilValue float64) {
var decompressor octopus.ValueDecompressor
switch metricType {
case octopus.Instant:
decompressor = NewReverseInstantDeltaDecompressor(valuesBuf, fracDigits)
case octopus.Cumulative:
decompressor = NewReverseCumulativeDeltaDecompressor(valuesBuf, fracDigits)
default:
panic(fmt.Sprintf("unknown metricType %d", metricType))
}
value, done := decompressor.NextValue()
if done {
return
}
sinceValue = value
untilValue = value
for {
value, done = decompressor.NextValue()
if done {
return
}
sinceValue = value
}
}

145
enc/time_delta_delta.go Normal file
View File

@@ -0,0 +1,145 @@
package enc
import (
"gordenko.dev/dima/diploma/bin"
)
// REVERSE
const (
lastUnixtimeIdx = 0
baseDeltaIdx = 4
)
type ReverseTimeDeltaOfDeltaDecompressor struct {
step byte
buf []byte
pos int
bound int
lastUnixtime uint32
baseDelta uint32
lastDeltaOfDelta int64
length uint16
idxOf8 uint
s8 byte
}
func NewReverseTimeDeltaOfDeltaDecompressor(buf []byte) *ReverseTimeDeltaOfDeltaDecompressor {
return &ReverseTimeDeltaOfDeltaDecompressor{
buf: buf,
pos: len(buf),
}
}
func (s *ReverseTimeDeltaOfDeltaDecompressor) NextValue() (value uint32, done bool) {
if s.step == 0 {
if s.pos == 0 {
return 0, true
}
s.lastUnixtime = bin.GetUint32(s.buf[lastUnixtimeIdx:])
s.step = 1
return s.lastUnixtime, false
}
if s.step == 1 {
if s.pos == baseDeltaIdx {
return 0, true
}
u64, n, err := bin.GetVarUint64(s.buf[baseDeltaIdx:])
if err != nil {
panic("EOF")
}
s.bound = baseDeltaIdx + n
s.baseDelta = uint32(u64)
s.pos--
s.idxOf8 = uint(8 - s.buf[s.pos])
s.pos--
s.s8 = s.buf[s.pos]
s.pos--
s.readVar()
if s.length > 0 {
s.length--
}
s.step = 2
return s.lastUnixtime, false
}
if s.length > 0 {
s.length--
delta := int64(s.baseDelta) + s.lastDeltaOfDelta
s.lastUnixtime = uint32(int64(s.lastUnixtime) - delta)
return s.lastUnixtime, false
}
if s.pos < s.bound {
return 0, true
}
if s.idxOf8 == 0 {
s.s8 = s.buf[s.pos]
s.pos--
}
s.readVar()
if s.length > 0 {
s.length--
}
return s.lastUnixtime, false
}
func GetTimeRange(timestampsBuf []byte) (since, until uint32) {
decompressor := NewReverseTimeDeltaOfDeltaDecompressor(timestampsBuf)
value, done := decompressor.NextValue()
if done {
return
}
since = value
until = value
for {
value, done = decompressor.NextValue()
if done {
return
}
since = value
}
}
func (s *ReverseTimeDeltaOfDeltaDecompressor) readVar() {
var (
n int
err error
)
s.lastDeltaOfDelta, n, err = bin.ReverseGetVarInt64(s.buf[:s.pos+1])
if err != nil {
panic(err)
}
s.pos -= n
delta := int64(s.baseDelta) + s.lastDeltaOfDelta
s.lastUnixtime = uint32(int64(s.lastUnixtime) - delta)
var flag byte = 1 << s.idxOf8
if (s.s8 & flag) == flag {
s.decodeLength()
}
if s.idxOf8 == 7 {
s.idxOf8 = 0
} else {
s.idxOf8++
}
}
func (s *ReverseTimeDeltaOfDeltaDecompressor) decodeLength() {
b1 := s.buf[s.pos]
s.pos--
if b1 < 128 {
s.length = uint16(b1)
} else {
b2 := s.buf[s.pos]
s.pos--
s.length = uint16(b1&127) | (uint16(b2) << 7)
}
s.length += 2
}