This commit is contained in:
2026-06-07 21:27:18 +03:00
parent 776a341c57
commit 964fe4b4a8
14 changed files with 2415 additions and 2283 deletions

View File

@@ -8,6 +8,8 @@ import (
// METRIC
const minBufferSize = 1024
type IndexLevelTail struct {
Payload []byte
RecordsCount int
@@ -21,38 +23,39 @@ type _metric struct {
Since uint32
UntilValue float64
Until uint32
Buffer []byte
Timestamps qb.TimestampCompressor
Values qb.ValueCompressor
XLock bool
RLocks int
WaitQueue []any
IndexLevelTails []IndexLevelTail // root - last element
IndexLevelTails []txlog.IndexLevelTail // root - last element
}
//IndexLevels [][]IndexRec // root - last element
func (s *_metric) ReinitBy(timestamp uint32, value float64) {
s.Timestamps.Renew()
s.Values.Renew()
// func (s *_metric) ReinitBy(timestamp uint32, value float64) {
// s.Timestamps.Renew()
// s.Values.Renew()
s.Timestamps.Append(timestamp)
s.Values.Append(value)
// s.Timestamps.Append(timestamp)
// s.Values.Append(value)
s.Since = timestamp
s.SinceValue = value
s.Until = timestamp
s.UntilValue = value
}
// s.Since = timestamp
// s.SinceValue = value
// s.Until = timestamp
// s.UntilValue = value
// }
func (s *_metric) DeleteMeasures() {
s.Timestamps.Renew()
s.Values.Renew()
// s.Timestamps.Renew()
// s.Values.Renew()
s.LastPageNo = 0
s.Since = 0
s.SinceValue = 0
s.Until = 0
s.UntilValue = 0
// s.LastPageNo = 0
// s.Since = 0
// s.SinceValue = 0
// s.Until = 0
// s.UntilValue = 0
}
// func (s *_metric) startAppendMeasures(req tryAppendMeasuresReq, sendToStorage func(txlog.AppendedMeasures)) {
@@ -73,15 +76,15 @@ func (s *_metric) StartAppendMeasures(req tryAppendMeasuresReq, sendToStorage fu
timestamps = s.Timestamps
values = s.Values
indexLevels []atree.IndexLevelTail
dataPages []atree.DataPayload
indexLevels []txlog.IndexLevelTail
dataPages []txlog.DataPayload
//written int
//resultCode byte
)
s.Values.Lock()
s.Timestamps.Lock()
s.Values.CaptureState()
s.Timestamps.CaptureState()
for idx, measure := range req.Measures {
if s.Since == 0 {
@@ -91,8 +94,8 @@ func (s *_metric) StartAppendMeasures(req tryAppendMeasuresReq, sendToStorage fu
// якщо idx == 0 - одразу знімаю блокування і нічого не відправляю в txlog
if measure.Timestamp <= s.Until {
if idx == 0 {
s.Values.Unlock()
s.Timestamps.Unlock()
s.Values.ForgetCapturedState()
s.Timestamps.ForgetCapturedState()
req.ResultCh <- tryAppendMeasuresResult{
ResultCode: ExpiredMeasure,
@@ -107,8 +110,8 @@ func (s *_metric) StartAppendMeasures(req tryAppendMeasuresReq, sendToStorage fu
if s.MetricType == qb.Cumulative && measure.Value < s.UntilValue {
if idx == 0 {
s.Values.Unlock()
s.Timestamps.Unlock()
s.Values.ForgetCapturedState()
s.Timestamps.ForgetCapturedState()
req.ResultCh <- tryAppendMeasuresResult{
ResultCode: NonMonotonicValue,
@@ -122,31 +125,38 @@ func (s *_metric) StartAppendMeasures(req tryAppendMeasuresReq, sendToStorage fu
}
// fix - 1 + 8 bytes
extraSpace := timestamps.CalcRequiredSpace(measure.Timestamp) +
values.CalcRequiredSpace(measure.Value)
totalSpace := timestamps.Size() + values.Size() + extraSpace
timestampCompressionWay, timestampRequiredSpace := timestamps.Evaluate(measure.Timestamp)
valueCompressionWay, valueRequiredSpace := values.Evaluate(measure.Value)
totalSpace := timestampRequiredSpace + valueRequiredSpace
if totalSpace <= atree.DataPagePayloadSize {
// накопичую
timestamps.Append(measure.Timestamp)
values.Append(measure.Value)
timestamps.Compress(timestampCompressionWay, measure.Timestamp)
values.Compress(valueCompressionWay, measure.Value)
} else {
// сторінка заповнена
buffer := make([]byte, minBufferSize)
timestampsSize := timestamps.Rotate(buffer)
valuesSize := values.Rotate(buffer)
// prevPageNo - виставляю в txlog, коли забираю номер сторінки із freeList або генерую новий
dataPages = append(dataPages, atree.DataPayload{
dataPages = append(dataPages, txlog.DataPayload{
Since: s.Since,
Content: nil,
TimestampsSize: timestamps.Size(),
ValuesSize: values.Size(),
Content: s.Buffer,
TimestampsSize: timestampsSize,
ValuesSize: valuesSize,
})
// обнуляються буфери, але state незмінний
timestamps.Renew()
values.Renew()
// renew
s.Buffer = buffer
timestamps.Append(measure.Timestamp)
values.Append(measure.Value)
timestampCompressionWay, _ = timestamps.Evaluate(measure.Timestamp)
valueCompressionWay, _ = values.Evaluate(measure.Value)
timestamps.Compress(timestampCompressionWay, measure.Timestamp)
values.Compress(valueCompressionWay, measure.Value)
s.Since = measure.Timestamp
}
@@ -161,8 +171,8 @@ func (s *_metric) StartAppendMeasures(req tryAppendMeasuresReq, sendToStorage fu
if len(dataPages) > 0 {
// пишу в txlog довгим шляхом через redo файл і запис в data файл
for _, tail := range s.IndexLevelTails {
indexLevels = append(indexLevels, atree.IndexLevelTail{
Payload: tail.Payload,
indexLevels = append(indexLevels, txlog.IndexLevelTail{
Records: tail.Records,
RecordsCount: tail.RecordsCount,
})
}
@@ -189,8 +199,8 @@ func (s *_metric) StartAppendMeasures(req tryAppendMeasuresReq, sendToStorage fu
func (s *_metric) FinAppendMeasures(rec txlog.AppendMeasuresSummary) {
// Видаляю state. Оригінальні Timestamps і Values вже мають останню версію
s.Values.Unlock()
s.Timestamps.Unlock()
s.Values.ForgetCapturedState()
s.Timestamps.ForgetCapturedState()
// fix write index levels
// update prev pageNo
// if len(rec.DataPages) > 0 {
@@ -201,80 +211,80 @@ func (s *_metric) FinAppendMeasures(rec txlog.AppendMeasuresSummary) {
}
// В txlog я передав повний індекс. У нього додали елементи (можливо нові рівні).
// Тому проста заміна
s.IndexLevels = rec.Index
s.IndexLevelTails = rec.Index
}
// READ
func (s *_metric) StartRangeScan(req tryRangeScanReq) {
if s.Since == 0 {
req.ResultCh <- rangeScanResult{
ResultCode: QueryDone,
}
return
}
// if s.Since == 0 {
// req.ResultCh <- rangeScanResult{
// ResultCode: QueryDone,
// }
// return
// }
if req.Since > s.Until {
req.ResultCh <- rangeScanResult{
ResultCode: QueryDone,
}
return
}
// if req.Since > s.Until {
// req.ResultCh <- rangeScanResult{
// ResultCode: QueryDone,
// }
// return
// }
if req.Until < s.Since {
if s.RootPageNo > 0 {
req.ResultCh <- rangeScanResult{
ResultCode: UntilNotFound,
RootPageNo: s.RootPageNo,
FracDigits: s.FracDigits,
}
s.RLocks++
return
} else {
req.ResultCh <- rangeScanResult{
ResultCode: QueryDone,
}
return
}
}
// if req.Until < s.Since {
// if s.RootPageNo > 0 {
// req.ResultCh <- rangeScanResult{
// ResultCode: UntilNotFound,
// RootPageNo: s.RootPageNo,
// FracDigits: s.FracDigits,
// }
// s.RLocks++
// return
// } else {
// req.ResultCh <- rangeScanResult{
// ResultCode: QueryDone,
// }
// return
// }
// }
timestampDecompressor := s.Timestamps.CreateDecompressor()
valueDecompressor := s.Values.CreateDecompressor(s.FracDigits)
// timestampDecompressor := s.Timestamps.CreateDecompressor()
// valueDecompressor := s.Values.CreateDecompressor(s.FracDigits)
for {
timestamp, done := timestampDecompressor.NextValue()
if done {
break
}
// for {
// timestamp, done := timestampDecompressor.NextValue()
// if done {
// break
// }
value, done := valueDecompressor.NextValue()
if done {
qb.Abort(qb.HasTimestampNoValueBug, ErrNoValueBug)
}
// value, done := valueDecompressor.NextValue()
// if done {
// qb.Abort(qb.HasTimestampNoValueBug, ErrNoValueBug)
// }
if timestamp <= req.Until {
req.ResponseWriter.FeedNoSend(timestamp, value)
if timestamp < req.Since {
req.ResultCh <- rangeScanResult{
ResultCode: QueryDone,
}
return
}
}
}
// if timestamp <= req.Until {
// req.ResponseWriter.FeedNoSend(timestamp, value)
// if timestamp < req.Since {
// req.ResultCh <- rangeScanResult{
// ResultCode: QueryDone,
// }
// return
// }
// }
// }
if s.LastPageNo > 0 {
req.ResultCh <- rangeScanResult{
ResultCode: UntilFound,
LastPageNo: s.LastPageNo,
FracDigits: s.FracDigits,
}
s.RLocks++
} else {
req.ResultCh <- rangeScanResult{
ResultCode: QueryDone,
}
}
// if s.LastPageNo > 0 {
// req.ResultCh <- rangeScanResult{
// ResultCode: UntilFound,
// LastPageNo: s.LastPageNo,
// FracDigits: s.FracDigits,
// }
// s.RLocks++
// } else {
// req.ResultCh <- rangeScanResult{
// ResultCode: QueryDone,
// }
// }
}
func (s *_metric) StartFullScan(req tryFullScanReq) {