VictoriaMetrics/lib/logstorage/stats_sum_len.go

89 lines
1.9 KiB
Go
Raw Normal View History

2024-05-15 04:55:44 +02:00
package logstorage
import (
"strconv"
"unsafe"
)
type statsSumLen struct {
2024-05-22 21:01:20 +02:00
fields []string
2024-05-15 04:55:44 +02:00
}
func (ss *statsSumLen) String() string {
2024-05-22 21:01:20 +02:00
return "sum_len(" + statsFuncFieldsToString(ss.fields) + ")"
2024-05-15 04:55:44 +02:00
}
2024-05-20 04:08:30 +02:00
func (ss *statsSumLen) updateNeededFields(neededFields fieldsSet) {
2024-05-22 21:01:20 +02:00
updateNeededFieldsForStatsFunc(neededFields, ss.fields)
2024-05-15 04:55:44 +02:00
}
func (ss *statsSumLen) newStatsProcessor() (statsProcessor, int) {
ssp := &statsSumLenProcessor{
ss: ss,
sumLen: 0,
}
return ssp, int(unsafe.Sizeof(*ssp))
}
type statsSumLenProcessor struct {
ss *statsSumLen
sumLen uint64
}
func (ssp *statsSumLenProcessor) updateStatsForAllRows(br *blockResult) int {
2024-05-22 21:01:20 +02:00
fields := ssp.ss.fields
if len(fields) == 0 {
2024-05-15 04:55:44 +02:00
// Sum all the columns
for _, c := range br.getColumns() {
ssp.sumLen += c.sumLenValues(br)
}
} else {
// Sum the requested columns
2024-05-22 21:01:20 +02:00
for _, field := range fields {
2024-05-15 04:55:44 +02:00
c := br.getColumnByName(field)
ssp.sumLen += c.sumLenValues(br)
}
}
return 0
}
func (ssp *statsSumLenProcessor) updateStatsForRow(br *blockResult, rowIdx int) int {
2024-05-22 21:01:20 +02:00
fields := ssp.ss.fields
if len(fields) == 0 {
2024-05-15 04:55:44 +02:00
// Sum all the fields for the given row
for _, c := range br.getColumns() {
v := c.getValueAtRow(br, rowIdx)
ssp.sumLen += uint64(len(v))
}
} else {
// Sum only the given fields for the given row
2024-05-22 21:01:20 +02:00
for _, field := range fields {
2024-05-15 04:55:44 +02:00
c := br.getColumnByName(field)
v := c.getValueAtRow(br, rowIdx)
ssp.sumLen += uint64(len(v))
}
}
return 0
}
func (ssp *statsSumLenProcessor) mergeState(sfp statsProcessor) {
src := sfp.(*statsSumLenProcessor)
ssp.sumLen += src.sumLen
}
func (ssp *statsSumLenProcessor) finalizeStats() string {
return strconv.FormatUint(ssp.sumLen, 10)
}
func parseStatsSumLen(lex *lexer) (*statsSumLen, error) {
2024-05-22 21:01:20 +02:00
fields, err := parseStatsFuncFields(lex, "sum_len")
2024-05-15 04:55:44 +02:00
if err != nil {
return nil, err
}
ss := &statsSumLen{
2024-05-22 21:01:20 +02:00
fields: fields,
2024-05-15 04:55:44 +02:00
}
return ss, nil
}