56 lines
1.5 KiB
Go
56 lines
1.5 KiB
Go
package storage
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
bin "gordenko.dev/dima/bin/little"
|
|
"gordenko.dev/dima/qb"
|
|
"gordenko.dev/dima/qb/enc"
|
|
"gordenko.dev/dima/qb/util"
|
|
)
|
|
|
|
func CreateTimestampDecompressor(page []byte) (qb.TimestampDecompressor, error) {
|
|
size, _ := bin.GetUint16(page[timestampsSizeIdx:])
|
|
if size > DataPageFooterSize {
|
|
return nil, fmt.Errorf("bug: invalid timestamps size %d", size)
|
|
}
|
|
pos := DataPagePayloadSize - int(size)
|
|
d := enc.NewTimeDeltaDecompressor()
|
|
d.RestoreFromEnd(page[pos:DataPagePayloadSize])
|
|
return d, nil
|
|
}
|
|
|
|
func CreateValueDecompressor(page []byte, metricType qb.MetricType, fracDigits byte) (qb.ValueDecompressor, error) {
|
|
size, _ := bin.GetUint16(page[valuesSizeIdx:])
|
|
if size > DataPageFooterSize {
|
|
return nil, fmt.Errorf("bug: invalid timestamps size %d", size)
|
|
}
|
|
d := enc.NewValueDeltaDecompressor(metricType, fracDigits)
|
|
d.RestoreFromEnd(page[:size])
|
|
return d, nil
|
|
}
|
|
|
|
func VerifyDataPageCRC32(data []byte) error {
|
|
var (
|
|
calculatedCRC = util.CalculateCRC32(data[:dataCRC32Idx])
|
|
writtenCRC, _ = bin.GetUint32(data[dataCRC32Idx:])
|
|
)
|
|
if calculatedCRC != writtenCRC {
|
|
return fmt.Errorf("calculated CRC32 %d are not equal written CRC32 %d",
|
|
calculatedCRC, writtenCRC)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func VerifyIndexPageCRC32(data []byte) error {
|
|
var (
|
|
calculatedCRC = util.CalculateCRC32(data[:indexCRC32Idx])
|
|
writtenCRC, _ = bin.GetUint32(data[indexCRC32Idx:])
|
|
)
|
|
if calculatedCRC != writtenCRC {
|
|
return fmt.Errorf("calculated CRC32 %d are not equal written CRC32 %d",
|
|
calculatedCRC, writtenCRC)
|
|
}
|
|
return nil
|
|
}
|