This commit is contained in:
2026-06-14 23:12:03 +03:00
parent 181031a753
commit b32279deaf
35 changed files with 1395 additions and 1062 deletions

View File

@@ -9,6 +9,10 @@ import (
"gordenko.dev/dima/qb/util"
)
type Packable interface {
Pack(io.Writer)
}
type WritePreparer struct {
pagePreparer *PageIngester
//dataPagePayloadBound int
@@ -39,15 +43,19 @@ type PreparedData struct {
Commits []any
}
// const (
// prefixSize = 9
// crc32Size = 4
// )
func (s *WritePreparer) Prepare(input []any) PreparedData {
s.w.Write([]byte{
0, 0, 0, 0, 0, 0, 0, 0, 0, // size (max 9 byte)
0, 0, 0, 0, // crc32
})
hasher := util.NewHasher()
w := io.MultiWriter(s.w, hasher)
multi := io.MultiWriter(s.w, hasher)
// 1. Пакую всі дані в WAL буфер запису
for _, untyped := range input {
@@ -58,7 +66,7 @@ func (s *WritePreparer) Prepare(input []any) PreparedData {
MetricType: x.MetricType,
FracDigits: x.FracDigits,
}
rec.Pack(w)
rec.Pack(multi)
s.commits = append(s.commits, MetricAddCommited{
MetricID: x.MetricID,
ResultCh: x.ResultCh,
@@ -76,7 +84,7 @@ func (s *WritePreparer) Prepare(input []any) PreparedData {
FreeIndexPages: x.FreeIndexPages,
FreeDataPages: x.FreeDataPages,
}
rec.Pack(w)
rec.Pack(multi)
s.commits = append(s.commits, MetricDeleteCommited{
MetricID: x.MetricID,
ResultCh: x.ResultCh,
@@ -90,7 +98,7 @@ func (s *WritePreparer) Prepare(input []any) PreparedData {
ValuesRewindOffset: x.ValuesRewindOffset,
Values: x.Values,
}
rec.Pack(w)
rec.Pack(multi)
// Дані для Worker
s.commits = append(s.commits, MeasuresAppendCommited{
MetricID: x.MetricID,
@@ -188,9 +196,9 @@ func (s *WritePreparer) Prepare(input []any) PreparedData {
ChangedIndexLevels: changedIndexPages,
}
fmt.Printf("%#v\ns", rec.ChangedIndexLevels[0].IndexPageTail)
//fmt.Printf("%#v\ns", rec.ChangedIndexLevels[0].IndexPageTail)
rec.Pack(w)
rec.Pack(multi)
// Дані для Worker
s.commits = append(s.commits, MeasuresAppendWithGrowCommited{
MetricID: x.MetricID,
@@ -213,7 +221,7 @@ func (s *WritePreparer) Prepare(input []any) PreparedData {
FreeIndexPages: x.FreeIndexPages,
FreeDataPages: x.FreeDataPages,
}
rec.Pack(w)
rec.Pack(multi)
s.commits = append(s.commits, MeasuresDeleteCommited{
MetricID: x.MetricID,
ResultCh: x.ResultCh,
@@ -222,19 +230,11 @@ func (s *WritePreparer) Prepare(input []any) PreparedData {
}
}
// 2. Додаю розмір пакету і чексуму
packet := finalizePacket(s.w, hasher.Sum32())
//s.written += int64(len(packet)) + 12
packet := s.w.Bytes()
// write size
payloadSize := len(packet) - 13
start := 9 - bin.CountVarSize(payloadSize)
bin.PutVarSize(packet[start:], payloadSize)
// CRC32
bin.PutUint32(packet[9:], hasher.Sum32())
//
return PreparedData{
Packet: packet[start:],
Packet: packet,
WriteToIndex: s.indexPages,
WriteToData: s.dataPages,
Commits: s.commits,
@@ -260,3 +260,13 @@ func getIndexRecords(buf []byte, count int) (list []IndexRecord) {
}
return
}
func finalizePacket(w *bytes.Buffer, checksum uint32) []byte {
bin.WriteUint32(w, checksum)
packet := w.Bytes()
bodySize := len(packet) - 13 // 9 bytes prefix + 4 bytes crc32
// cut empty bytes at start
start := 9 - bin.CountVarSize(bodySize)
bin.PutVarSize(packet[start:], bodySize)
return packet[start:]
}