93 lines
2.5 KiB
Go
93 lines
2.5 KiB
Go
package storage
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
bin "gordenko.dev/dima/bin/little"
|
|
"gordenko.dev/dima/qb"
|
|
"gordenko.dev/dima/qb/enc"
|
|
)
|
|
|
|
// func (s *BackwardCursor) makeDecompressors() error {
|
|
// timestampsSize, _ := bin.GetUint16(s.pageData[timestampsSizeIdx:])
|
|
// valuesSize, _ := bin.GetUint16(s.pageData[valuesSizeIdx:])
|
|
|
|
// payloadSize := timestampsSize + valuesSize
|
|
|
|
// if payloadSize > dataFooterIdx {
|
|
// return fmt.Errorf("corrupted data page %d: timestamps + values size %d gt payload size",
|
|
// s.pageNo, payloadSize)
|
|
// }
|
|
|
|
// s.timestampDecompressor = enc.NewTimeDeltaDecompressor(
|
|
// s.pageData[:timestampsSize],
|
|
// )
|
|
|
|
// vbuf := s.pageData[timestampsSize : timestampsSize+valuesSize]
|
|
|
|
// switch s.metricType {
|
|
// case qb.Instant:
|
|
// s.valueDecompressor = enc.NewInstantDeltaDecompressor(
|
|
// vbuf, s.fracDigits)
|
|
|
|
// case qb.Cumulative:
|
|
// s.valueDecompressor = enc.NewCumulativeDeltaDecompressor(
|
|
// vbuf, s.fracDigits)
|
|
|
|
// default:
|
|
// return fmt.Errorf("bug: wrong metricType %d", s.metricType)
|
|
// }
|
|
// return nil
|
|
// }
|
|
|
|
func makeDecompressors(pageData []byte, metricType qb.MetricType, fracDigits byte) (
|
|
qb.TimestampDecompressor, qb.ValueDecompressor, error,
|
|
) {
|
|
|
|
valuesSize, _ := bin.GetUint16(pageData[valuesSizeIdx:])
|
|
|
|
payloadSize := timestampsSize + valuesSize
|
|
|
|
if payloadSize > dataFooterIdx {
|
|
return nil, nil, fmt.Errorf("corrupted: timestamps + values size %d > payload size",
|
|
payloadSize)
|
|
}
|
|
|
|
timestampDecompressor := enc.NewTimeDeltaDecompressor(
|
|
pageData[:timestampsSize],
|
|
)
|
|
|
|
vbuf := pageData[timestampsSize : timestampsSize+valuesSize]
|
|
|
|
var valueDecompressor qb.ValueDecompressor
|
|
switch metricType {
|
|
case qb.Instant:
|
|
valueDecompressor = enc.NewInstantDeltaDecompressor(
|
|
vbuf, fracDigits)
|
|
|
|
case qb.Cumulative:
|
|
valueDecompressor = enc.NewCumulativeDeltaDecompressor(
|
|
vbuf, fracDigits)
|
|
|
|
default:
|
|
return nil, nil, fmt.Errorf("bug: wrong metricType %d", metricType)
|
|
}
|
|
return timestampDecompressor, valueDecompressor, nil
|
|
return nil, nil, nil
|
|
}
|
|
|
|
func CreateTimeDeltaDecompressor(page []byte) qb.TimestampDecompressor {
|
|
size, _ := bin.GetUint16(page[timestampsSizeIdx:])
|
|
pos := DataPagePayloadSize - int(size)
|
|
d := enc.NewTimeDeltaDecompressor()
|
|
d.RestoreFromEnd(page[pos:DataPagePayloadSize])
|
|
return d
|
|
}
|
|
|
|
func CreateValueDeltaDecompressor(page []byte, metricType qb.MetricType, fracDigits byte) qb.ValueDecompressor {
|
|
size, _ := bin.GetUint16(page[valuesSizeIdx:])
|
|
d := enc.NewValueDeltaDecompressor(metricType, fracDigits)
|
|
d.RestoreFromEnd(page[:size])
|
|
return d
|
|
}
|