2026-06-09 16:42:17 +00:00
|
|
|
|
package database
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2026-06-10 06:18:45 +03:00
|
|
|
|
"fmt"
|
2026-06-09 16:42:17 +00:00
|
|
|
|
"io"
|
|
|
|
|
|
|
|
|
|
|
|
bin "gordenko.dev/dima/bin/little"
|
2026-06-10 06:18:45 +03:00
|
|
|
|
"gordenko.dev/dima/qb"
|
|
|
|
|
|
"gordenko.dev/dima/qb/enc"
|
|
|
|
|
|
"gordenko.dev/dima/qb/storage"
|
2026-06-09 16:42:17 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type ReplayMetric struct {
|
|
|
|
|
|
metricType qb.MetricType
|
|
|
|
|
|
fracDigits byte
|
|
|
|
|
|
lastPageNo uint32
|
|
|
|
|
|
buf []byte
|
|
|
|
|
|
databuf []byte // якщо розмір buf == DataPageSize, то databuf буде менше (мінус футер)
|
|
|
|
|
|
tSize int
|
|
|
|
|
|
vSize int
|
2026-06-10 06:18:45 +03:00
|
|
|
|
indexLevelTails []storage.IndexLevelTail // root - last element
|
2026-06-09 16:42:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *ReplayMetric) ReadFrom(r io.Reader) (err error) {
|
|
|
|
|
|
metricType, err := bin.ReadByte(r)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
s.metricType = qb.MetricType(metricType)
|
|
|
|
|
|
s.fracDigits, err = bin.ReadByte(r)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
s.lastPageNo, err = bin.ReadUint32(r)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
s.tSize, err = bin.ReadUint16AsInt(r)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
s.vSize, err = bin.ReadUint16AsInt(r)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
err = bin.ReadNInto(r, s.databuf[len(s.databuf)-s.tSize:])
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
err = bin.ReadNInto(r, s.databuf[:s.vSize])
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
//
|
|
|
|
|
|
levelsCount, err := bin.ReadVarSize(r)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
for range levelsCount {
|
|
|
|
|
|
var (
|
2026-06-10 06:18:45 +03:00
|
|
|
|
buf = make([]byte, storage.IndexPageSize)
|
2026-06-09 16:42:17 +00:00
|
|
|
|
count int
|
|
|
|
|
|
)
|
|
|
|
|
|
count, err = bin.ReadVarSize(r)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
err = bin.ReadNInto(r, buf[:count*indexRecordSize])
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-06-10 06:18:45 +03:00
|
|
|
|
s.indexLevelTails = append(s.indexLevelTails, storage.IndexLevelTail{
|
2026-06-09 16:42:17 +00:00
|
|
|
|
Buffer: buf,
|
|
|
|
|
|
RecordsCount: count,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-10 06:18:45 +03:00
|
|
|
|
// func appendNewRecordsToIndexTail(level storage.IndexLevelTail, records []byte) {
|
|
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// func countReusedIndexPages(changedIndexLevels []storage.ChangedIndexLevel) (count int) {
|
|
|
|
|
|
|
|
|
|
|
|
// return
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
func (s *ReplayMetric) MeasuresAppend(rec storage.MeasuresAppendRecord) {
|
|
|
|
|
|
timestampsSize := rec.TimestampsOffset - len(rec.Timestamps)
|
|
|
|
|
|
// копіюю нові дані із WAL з урахуванням offset-ів
|
|
|
|
|
|
copy(s.databuf[rec.ValuesOffset:], rec.Values)
|
|
|
|
|
|
copy(s.databuf[len(s.databuf)-timestampsSize:], rec.Timestamps)
|
|
|
|
|
|
//
|
|
|
|
|
|
s.tSize = timestampsSize
|
|
|
|
|
|
s.vSize = rec.ValuesOffset - len(rec.Values)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-09 16:42:17 +00:00
|
|
|
|
type AppendMeasuresResult struct {
|
2026-06-10 06:18:45 +03:00
|
|
|
|
IndexPages []storage.PageToWrite
|
|
|
|
|
|
DataPages []storage.PageToWrite
|
2026-06-09 16:42:17 +00:00
|
|
|
|
ReusedIndexPagesCount int
|
|
|
|
|
|
ReusedDataPagesCount int
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-10 06:18:45 +03:00
|
|
|
|
func (s *ReplayMetric) MeasuresAppendWithGrow(rec storage.MeasuresAppendWithGrowRecord, isLastPacket bool) (_ AppendMeasuresResult) {
|
|
|
|
|
|
var (
|
|
|
|
|
|
reusedIndexPages int
|
|
|
|
|
|
reusedDataPages int
|
|
|
|
|
|
indexPages []storage.PageToWrite
|
|
|
|
|
|
dataPages []storage.PageToWrite
|
|
|
|
|
|
)
|
2026-06-09 16:42:17 +00:00
|
|
|
|
// Додати в index level tails недостаючі дані, або замінити
|
|
|
|
|
|
for levelIdx, change := range rec.ChangedIndexLevels {
|
2026-06-10 06:18:45 +03:00
|
|
|
|
var (
|
|
|
|
|
|
head = change.HeadIndexPage
|
|
|
|
|
|
newRecordsCount = len(change.TailRecords) / indexRecordSize
|
|
|
|
|
|
)
|
|
|
|
|
|
// 1. набиваю сторінки для перезапису в index файлі
|
|
|
|
|
|
if isLastPacket {
|
|
|
|
|
|
if head != nil {
|
|
|
|
|
|
indexPages = append(indexPages,
|
|
|
|
|
|
composeHeadIndexPage(levelIdx, s.indexLevelTails[levelIdx], head))
|
|
|
|
|
|
}
|
|
|
|
|
|
for _, x := range change.IndexPages {
|
|
|
|
|
|
indexPages = append(indexPages, storage.PageToWrite{
|
|
|
|
|
|
PageNo: x.PageNo,
|
|
|
|
|
|
Content: x.Content,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 2. Вношу зміни в поточні індексні хвости
|
2026-06-09 16:42:17 +00:00
|
|
|
|
if levelIdx < len(s.indexLevelTails) {
|
|
|
|
|
|
level := s.indexLevelTails[levelIdx]
|
2026-06-10 06:18:45 +03:00
|
|
|
|
if head != nil {
|
|
|
|
|
|
// попередній хвіст індексного рівня перетворився на head сторінку,
|
2026-06-09 16:42:17 +00:00
|
|
|
|
// отже TailRecords - це новий хвіст
|
|
|
|
|
|
copy(level.Buffer, change.TailRecords)
|
2026-06-10 06:18:45 +03:00
|
|
|
|
level.RecordsCount = newRecordsCount
|
2026-06-09 16:42:17 +00:00
|
|
|
|
} else {
|
|
|
|
|
|
// TailRecords - це нові дані, які треба додати
|
|
|
|
|
|
pos := level.RecordsCount * indexRecordSize
|
|
|
|
|
|
copy(level.Buffer[pos:], change.TailRecords)
|
2026-06-10 06:18:45 +03:00
|
|
|
|
level.RecordsCount += newRecordsCount
|
2026-06-09 16:42:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
s.indexLevelTails[levelIdx] = level
|
|
|
|
|
|
} else {
|
2026-06-10 06:18:45 +03:00
|
|
|
|
// додаю новий індексний рівень
|
|
|
|
|
|
buf := make([]byte, storage.IndexPageSize)
|
|
|
|
|
|
copy(buf, change.TailRecords)
|
|
|
|
|
|
//
|
|
|
|
|
|
s.indexLevelTails = append(s.indexLevelTails, storage.IndexLevelTail{
|
|
|
|
|
|
Buffer: buf,
|
|
|
|
|
|
RecordsCount: newRecordsCount,
|
2026-06-09 16:42:17 +00:00
|
|
|
|
})
|
|
|
|
|
|
}
|
2026-06-10 06:18:45 +03:00
|
|
|
|
|
|
|
|
|
|
// 3. рахую кількість reused індексних сторінок
|
|
|
|
|
|
if head != nil {
|
|
|
|
|
|
if head.Reused {
|
|
|
|
|
|
reusedIndexPages++
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
for _, x := range change.IndexPages {
|
|
|
|
|
|
if x.Reused {
|
|
|
|
|
|
reusedIndexPages++
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-06-09 16:42:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-10 06:18:45 +03:00
|
|
|
|
// 4. набиваю сторінки для перезапису в data файлі
|
|
|
|
|
|
if isLastPacket {
|
|
|
|
|
|
dataPages = append(dataPages, composeHeadDataPage(s.databuf, rec.HeadDataPage))
|
2026-06-09 16:42:17 +00:00
|
|
|
|
|
2026-06-10 06:18:45 +03:00
|
|
|
|
for _, x := range rec.DataPages {
|
|
|
|
|
|
dataPages = append(dataPages, storage.PageToWrite{
|
|
|
|
|
|
PageNo: x.PageNo,
|
|
|
|
|
|
Content: x.Content,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-06-09 16:42:17 +00:00
|
|
|
|
|
2026-06-10 06:18:45 +03:00
|
|
|
|
// рахую кількість reused дата сторінок
|
|
|
|
|
|
if rec.HeadDataPage.Reused {
|
|
|
|
|
|
reusedDataPages++
|
|
|
|
|
|
}
|
|
|
|
|
|
for _, x := range rec.DataPages {
|
|
|
|
|
|
if x.Reused {
|
|
|
|
|
|
reusedDataPages++
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// HeadDataPage є полюбому, оскільки це транзакція із мінімум однією заповненою
|
|
|
|
|
|
// data сторінкою. Отже TailTimestamps і TailValues - це нові хвости data рівня.
|
|
|
|
|
|
copy(s.databuf, rec.TailValues)
|
|
|
|
|
|
pos := len(s.databuf) - len(rec.TailTimestamps)
|
|
|
|
|
|
copy(s.databuf[pos:], rec.TailTimestamps)
|
|
|
|
|
|
|
|
|
|
|
|
s.tSize = len(rec.TailTimestamps)
|
|
|
|
|
|
s.vSize = len(rec.TailValues)
|
|
|
|
|
|
|
|
|
|
|
|
if len(rec.DataPages) == 0 {
|
|
|
|
|
|
s.lastPageNo = rec.HeadDataPage.PageNo
|
2026-06-09 16:42:17 +00:00
|
|
|
|
} else {
|
2026-06-10 06:18:45 +03:00
|
|
|
|
s.lastPageNo = rec.DataPages[len(rec.DataPages)-1].PageNo
|
|
|
|
|
|
}
|
2026-06-09 16:42:17 +00:00
|
|
|
|
|
2026-06-10 06:18:45 +03:00
|
|
|
|
return AppendMeasuresResult{
|
|
|
|
|
|
IndexPages: indexPages,
|
|
|
|
|
|
DataPages: dataPages,
|
|
|
|
|
|
ReusedIndexPagesCount: reusedIndexPages,
|
|
|
|
|
|
ReusedDataPagesCount: reusedDataPages,
|
2026-06-09 16:42:17 +00:00
|
|
|
|
}
|
2026-06-10 06:18:45 +03:00
|
|
|
|
}
|
2026-06-09 16:42:17 +00:00
|
|
|
|
|
2026-06-10 06:18:45 +03:00
|
|
|
|
// HELPERS
|
|
|
|
|
|
|
|
|
|
|
|
func composeHeadIndexPage(levelIdx int, level storage.IndexLevelTail, head *storage.HeadIndexPage) storage.PageToWrite {
|
|
|
|
|
|
// розраховую pos, з якого буду дописувати хвіст
|
|
|
|
|
|
pos := level.RecordsCount * indexRecordSize
|
|
|
|
|
|
// створюю копію сторінки
|
|
|
|
|
|
page := make([]byte, storage.IndexPageSize)
|
|
|
|
|
|
copy(page, level.Buffer[:pos]) // поточні дані
|
|
|
|
|
|
copy(page[pos:], head.Records)
|
|
|
|
|
|
// запечатати сторінку
|
|
|
|
|
|
calculatedCRC := storage.SealIndexPage(storage.SealIndexPageIn{
|
|
|
|
|
|
Content: page,
|
|
|
|
|
|
RecordsCount: level.RecordsCount + len(head.Records)/indexRecordSize,
|
|
|
|
|
|
ZeroLevel: levelIdx == 0,
|
|
|
|
|
|
})
|
|
|
|
|
|
// перевірка CRC
|
|
|
|
|
|
if calculatedCRC != head.Checksum {
|
|
|
|
|
|
qb.Abort(qb.WALReplayFailed,
|
|
|
|
|
|
fmt.Errorf("calculated CRC %d not equal expected %d of head page %d on index level %d",
|
|
|
|
|
|
calculatedCRC, head.Checksum, head.PageNo, levelIdx))
|
|
|
|
|
|
}
|
|
|
|
|
|
return storage.PageToWrite{
|
|
|
|
|
|
PageNo: head.PageNo,
|
|
|
|
|
|
Content: page,
|
|
|
|
|
|
}
|
2026-06-09 16:42:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-10 06:18:45 +03:00
|
|
|
|
func composeHeadDataPage(databuf []byte, head storage.HeadDataPage) storage.PageToWrite {
|
|
|
|
|
|
// створюю копію сторінки
|
|
|
|
|
|
var (
|
|
|
|
|
|
page = make([]byte, storage.DataPageSize)
|
|
|
|
|
|
timestampsSize = head.TimestampsOffset - len(head.Timestamps)
|
|
|
|
|
|
)
|
|
|
|
|
|
// копіюю поточні дані
|
|
|
|
|
|
copy(page, databuf)
|
|
|
|
|
|
// копіюю нові дані із WAL з урахуванням offset-ів
|
|
|
|
|
|
copy(page[head.ValuesOffset:], head.Values)
|
|
|
|
|
|
copy(page[len(page)-timestampsSize:], head.Timestamps)
|
|
|
|
|
|
// запечатати сторінку
|
|
|
|
|
|
calculatedCRC := storage.SealDataPage(storage.SealDataPageIn{
|
|
|
|
|
|
Content: page,
|
|
|
|
|
|
PrevPageNo: head.PrevPageNo,
|
|
|
|
|
|
TimestampsSize: timestampsSize,
|
|
|
|
|
|
ValuesSize: head.ValuesOffset + len(head.Values),
|
|
|
|
|
|
})
|
|
|
|
|
|
// перевірка CRC
|
|
|
|
|
|
if calculatedCRC != head.Checksum {
|
|
|
|
|
|
qb.Abort(qb.WALReplayFailed,
|
|
|
|
|
|
fmt.Errorf("calculated CRC %d not equal expected %d of head data page %d",
|
|
|
|
|
|
calculatedCRC, head.Checksum, head.PageNo))
|
2026-06-09 16:42:17 +00:00
|
|
|
|
}
|
2026-06-10 06:18:45 +03:00
|
|
|
|
return storage.PageToWrite{
|
|
|
|
|
|
PageNo: head.PageNo,
|
|
|
|
|
|
Content: page,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-06-09 16:42:17 +00:00
|
|
|
|
|
2026-06-10 06:18:45 +03:00
|
|
|
|
func (s *ReplayMetric) ToMetric() *_metric {
|
|
|
|
|
|
var (
|
|
|
|
|
|
requiredSpace = s.tSize + s.vSize
|
|
|
|
|
|
)
|
|
|
|
|
|
bufferSize := calculateDataBufferSize(requiredSpace)
|
|
|
|
|
|
if len(s.buf) > bufferSize {
|
|
|
|
|
|
s.buf = shrinkBuffer(shrinkBufferIn{
|
|
|
|
|
|
Buffer: s.buf,
|
|
|
|
|
|
TimestampsSize: s.tSize,
|
|
|
|
|
|
ValuesSize: s.vSize,
|
|
|
|
|
|
NewBufferSize: bufferSize,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2026-06-12 00:29:02 +03:00
|
|
|
|
values := enc.NewValueDeltaCompressor(s.metricType, s.fracDigits, s.buf, s.vSize)
|
2026-06-10 06:18:45 +03:00
|
|
|
|
return &_metric{
|
|
|
|
|
|
metricType: s.metricType,
|
|
|
|
|
|
fracDigits: s.fracDigits,
|
|
|
|
|
|
lastPageNo: s.lastPageNo,
|
|
|
|
|
|
lastValue: values.LastValue(),
|
|
|
|
|
|
buffer: s.buf,
|
|
|
|
|
|
timestamps: enc.NewTimeDeltaCompressor(s.buf, s.tSize),
|
|
|
|
|
|
values: values,
|
|
|
|
|
|
indexLevelTails: s.indexLevelTails,
|
|
|
|
|
|
}
|
2026-06-09 16:42:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-10 06:18:45 +03:00
|
|
|
|
// func (s *ReplayMetric) ApplyHeadDataPage(p storage.HeadDataPage) {
|
|
|
|
|
|
// // буфер має бути розміру сторінки
|
|
|
|
|
|
// if len(s.buf) != atree.DataPageSize {
|
|
|
|
|
|
// s.buf, s.databuf = growBuffers(growBuffersIn{
|
|
|
|
|
|
// databuf: s.databuf,
|
|
|
|
|
|
// tSize: s.tSize,
|
|
|
|
|
|
// vSize: s.vSize,
|
|
|
|
|
|
// requiredSpace: atree.DataPageSize,
|
|
|
|
|
|
// })
|
|
|
|
|
|
// }
|
|
|
|
|
|
// // дописую дані по зміщенню
|
|
|
|
|
|
// pos := len(s.databuf) - p.TimestampsOffset - len(p.Timestamps)
|
|
|
|
|
|
// copy(s.databuf[pos:], p.Timestamps)
|
|
|
|
|
|
// copy(s.databuf[p.ValuesOffset:], p.Values)
|
|
|
|
|
|
// //
|
|
|
|
|
|
// s.tSize = p.TimestampsOffset + len(p.Timestamps)
|
|
|
|
|
|
// s.tSize = p.ValuesOffset + len(p.Values)
|
|
|
|
|
|
|
|
|
|
|
|
// // fix - заполнить страницу
|
|
|
|
|
|
// // fix - check checksum
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// func (s *ReplayMetric) ApplyHeadIndexPages(changedIndexLevels storage.ChangedIndexLevel) {
|
2026-06-09 16:42:17 +00:00
|
|
|
|
// for levelIdx, changedLevel := range changedIndexLevels {
|
2026-06-10 06:18:45 +03:00
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
//
|
2026-06-09 16:42:17 +00:00
|
|
|
|
// }
|
|
|
|
|
|
// }
|