2026-02-10 14:02:11 +00:00
|
|
|
package atree
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
|
2026-05-31 20:01:28 +00:00
|
|
|
"gordenko.dev/dima/qb"
|
2026-02-10 14:02:11 +00:00
|
|
|
"gordenko.dev/dima/qb/bin"
|
|
|
|
|
"gordenko.dev/dima/qb/enc"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type BackwardCursor struct {
|
2026-05-31 20:01:28 +00:00
|
|
|
metricType qb.MetricType
|
2026-02-10 14:02:11 +00:00
|
|
|
fracDigits byte
|
|
|
|
|
atree *Atree
|
|
|
|
|
pageNo uint32
|
|
|
|
|
pageData []byte
|
2026-05-31 20:01:28 +00:00
|
|
|
timestampDecompressor qb.TimestampDecompressor
|
|
|
|
|
valueDecompressor qb.ValueDecompressor
|
2026-02-10 14:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type BackwardCursorOptions struct {
|
2026-05-31 20:01:28 +00:00
|
|
|
MetricType qb.MetricType
|
2026-02-10 14:02:11 +00:00
|
|
|
FracDigits byte
|
|
|
|
|
PageNo uint32
|
|
|
|
|
PageData []byte
|
|
|
|
|
Atree *Atree
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewBackwardCursor(opt BackwardCursorOptions) (*BackwardCursor, error) {
|
|
|
|
|
switch opt.MetricType {
|
2026-05-31 20:01:28 +00:00
|
|
|
case qb.Instant, qb.Cumulative:
|
2026-02-10 14:02:11 +00:00
|
|
|
// ok
|
|
|
|
|
default:
|
|
|
|
|
return nil, fmt.Errorf("MetricType option has wrong value: %d", opt.MetricType)
|
|
|
|
|
}
|
2026-05-31 20:01:28 +00:00
|
|
|
if opt.FracDigits > qb.MaxFracDigits {
|
2026-02-10 14:02:11 +00:00
|
|
|
return nil, errors.New("FracDigits option is required")
|
|
|
|
|
}
|
|
|
|
|
if opt.Atree == nil {
|
|
|
|
|
return nil, errors.New("Atree option is required")
|
|
|
|
|
}
|
|
|
|
|
if opt.PageNo == 0 {
|
|
|
|
|
return nil, errors.New("PageNo option is required")
|
|
|
|
|
}
|
|
|
|
|
if len(opt.PageData) == 0 {
|
|
|
|
|
return nil, errors.New("PageData option is required")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s := &BackwardCursor{
|
|
|
|
|
metricType: opt.MetricType,
|
|
|
|
|
fracDigits: opt.FracDigits,
|
|
|
|
|
atree: opt.Atree,
|
|
|
|
|
pageNo: opt.PageNo,
|
|
|
|
|
pageData: opt.PageData,
|
|
|
|
|
}
|
|
|
|
|
err := s.makeDecompressors()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return s, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// timestamp, value, done, error
|
|
|
|
|
func (s *BackwardCursor) Prev() (uint32, float64, bool, error) {
|
|
|
|
|
var (
|
|
|
|
|
timestamp uint32
|
|
|
|
|
value float64
|
|
|
|
|
done bool
|
|
|
|
|
err error
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
timestamp, done = s.timestampDecompressor.NextValue()
|
|
|
|
|
if !done {
|
|
|
|
|
value, done = s.valueDecompressor.NextValue()
|
|
|
|
|
if done {
|
|
|
|
|
return 0, 0, false,
|
|
|
|
|
fmt.Errorf("corrupted data page %d: has timestamp, no value",
|
|
|
|
|
s.pageNo)
|
|
|
|
|
}
|
|
|
|
|
return timestamp, value, false, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
prevPageNo := bin.GetUint32(s.pageData[prevPageIdx:])
|
|
|
|
|
if prevPageNo == 0 {
|
|
|
|
|
return 0, 0, true, nil
|
|
|
|
|
}
|
2026-02-23 19:37:05 +00:00
|
|
|
s.atree.releasePage(s.pageNo)
|
2026-02-10 14:02:11 +00:00
|
|
|
|
|
|
|
|
s.pageNo = prevPageNo
|
|
|
|
|
s.pageData, err = s.atree.fetchDataPage(s.pageNo)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, 0, false, fmt.Errorf("atree.fetchDataPage(%d): %s", s.pageNo, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = s.makeDecompressors()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, 0, false, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
timestamp, done = s.timestampDecompressor.NextValue()
|
|
|
|
|
if done {
|
|
|
|
|
return 0, 0, false,
|
|
|
|
|
fmt.Errorf("corrupted data page %d: no timestamps",
|
|
|
|
|
s.pageNo)
|
|
|
|
|
}
|
|
|
|
|
value, done = s.valueDecompressor.NextValue()
|
|
|
|
|
if done {
|
|
|
|
|
return 0, 0, false,
|
|
|
|
|
fmt.Errorf("corrupted data page %d: no values",
|
|
|
|
|
s.pageNo)
|
|
|
|
|
}
|
|
|
|
|
return timestamp, value, false, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *BackwardCursor) Close() {
|
2026-02-23 19:37:05 +00:00
|
|
|
s.atree.releasePage(s.pageNo)
|
2026-02-10 14:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// HELPER
|
|
|
|
|
|
|
|
|
|
func (s *BackwardCursor) makeDecompressors() error {
|
|
|
|
|
timestampsPayloadSize := bin.GetUint16(s.pageData[timestampsSizeIdx:])
|
|
|
|
|
valuesPayloadSize := bin.GetUint16(s.pageData[valuesSizeIdx:])
|
|
|
|
|
|
|
|
|
|
payloadSize := timestampsPayloadSize + valuesPayloadSize
|
|
|
|
|
|
|
|
|
|
if payloadSize > dataFooterIdx {
|
|
|
|
|
return fmt.Errorf("corrupted data page %d: timestamps + values size %d gt payload size",
|
|
|
|
|
s.pageNo, payloadSize)
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-07 21:27:18 +03:00
|
|
|
s.timestampDecompressor = enc.NewTimeDeltaDecompressor(
|
2026-02-10 14:02:11 +00:00
|
|
|
s.pageData[:timestampsPayloadSize],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
vbuf := s.pageData[timestampsPayloadSize : timestampsPayloadSize+valuesPayloadSize]
|
|
|
|
|
|
|
|
|
|
switch s.metricType {
|
2026-05-31 20:01:28 +00:00
|
|
|
case qb.Instant:
|
2026-06-07 21:27:18 +03:00
|
|
|
s.valueDecompressor = enc.NewInstantDeltaDecompressor(
|
2026-02-10 14:02:11 +00:00
|
|
|
vbuf, s.fracDigits)
|
|
|
|
|
|
2026-05-31 20:01:28 +00:00
|
|
|
case qb.Cumulative:
|
2026-06-07 21:27:18 +03:00
|
|
|
s.valueDecompressor = enc.NewCumulativeDeltaDecompressor(
|
2026-02-10 14:02:11 +00:00
|
|
|
vbuf, s.fracDigits)
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return fmt.Errorf("bug: wrong metricType %d", s.metricType)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-31 20:01:28 +00:00
|
|
|
func makeDecompressors(pageData []byte, metricType qb.MetricType, fracDigits byte) (
|
|
|
|
|
qb.TimestampDecompressor, qb.ValueDecompressor, error,
|
2026-02-10 14:02:11 +00:00
|
|
|
) {
|
|
|
|
|
timestampsPayloadSize := bin.GetUint16(pageData[timestampsSizeIdx:])
|
|
|
|
|
valuesPayloadSize := bin.GetUint16(pageData[valuesSizeIdx:])
|
|
|
|
|
|
|
|
|
|
payloadSize := timestampsPayloadSize + valuesPayloadSize
|
|
|
|
|
|
|
|
|
|
if payloadSize > dataFooterIdx {
|
|
|
|
|
return nil, nil, fmt.Errorf("corrupted: timestamps + values size %d > payload size",
|
|
|
|
|
payloadSize)
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-07 21:27:18 +03:00
|
|
|
timestampDecompressor := enc.NewTimeDeltaDecompressor(
|
2026-02-10 14:02:11 +00:00
|
|
|
pageData[:timestampsPayloadSize],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
vbuf := pageData[timestampsPayloadSize : timestampsPayloadSize+valuesPayloadSize]
|
|
|
|
|
|
2026-05-31 20:01:28 +00:00
|
|
|
var valueDecompressor qb.ValueDecompressor
|
2026-02-10 14:02:11 +00:00
|
|
|
switch metricType {
|
2026-05-31 20:01:28 +00:00
|
|
|
case qb.Instant:
|
2026-06-07 21:27:18 +03:00
|
|
|
valueDecompressor = enc.NewInstantDeltaDecompressor(
|
2026-02-10 14:02:11 +00:00
|
|
|
vbuf, fracDigits)
|
|
|
|
|
|
2026-05-31 20:01:28 +00:00
|
|
|
case qb.Cumulative:
|
2026-06-07 21:27:18 +03:00
|
|
|
valueDecompressor = enc.NewCumulativeDeltaDecompressor(
|
2026-02-10 14:02:11 +00:00
|
|
|
vbuf, fracDigits)
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return nil, nil, fmt.Errorf("bug: wrong metricType %d", metricType)
|
|
|
|
|
}
|
|
|
|
|
return timestampDecompressor, valueDecompressor, nil
|
|
|
|
|
}
|