2024-05-12 14:33:29 +00:00
|
|
|
package logstorage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync/atomic"
|
|
|
|
)
|
|
|
|
|
|
|
|
// pipeOffset implements '| offset ...' pipe.
|
|
|
|
//
|
|
|
|
// See https://docs.victoriametrics.com/victorialogs/logsql/#offset-pipe
|
|
|
|
type pipeOffset struct {
|
2024-05-20 02:08:30 +00:00
|
|
|
offset uint64
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (po *pipeOffset) String() string {
|
2024-05-20 02:08:30 +00:00
|
|
|
return fmt.Sprintf("offset %d", po.offset)
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (po *pipeOffset) updateNeededFields(_, _ fieldsSet) {
|
|
|
|
}
|
|
|
|
|
2024-05-13 13:35:11 +00:00
|
|
|
func (po *pipeOffset) newPipeProcessor(_ int, _ <-chan struct{}, _ func(), ppBase pipeProcessor) pipeProcessor {
|
2024-05-12 14:33:29 +00:00
|
|
|
return &pipeOffsetProcessor{
|
|
|
|
po: po,
|
|
|
|
ppBase: ppBase,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type pipeOffsetProcessor struct {
|
|
|
|
po *pipeOffset
|
|
|
|
ppBase pipeProcessor
|
|
|
|
|
|
|
|
rowsProcessed atomic.Uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pop *pipeOffsetProcessor) writeBlock(workerID uint, br *blockResult) {
|
|
|
|
if len(br.timestamps) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rowsProcessed := pop.rowsProcessed.Add(uint64(len(br.timestamps)))
|
2024-05-20 02:08:30 +00:00
|
|
|
if rowsProcessed <= pop.po.offset {
|
2024-05-12 14:33:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rowsProcessed -= uint64(len(br.timestamps))
|
2024-05-20 02:08:30 +00:00
|
|
|
if rowsProcessed >= pop.po.offset {
|
2024-05-12 14:33:29 +00:00
|
|
|
pop.ppBase.writeBlock(workerID, br)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-20 02:08:30 +00:00
|
|
|
rowsSkip := pop.po.offset - rowsProcessed
|
2024-05-12 14:33:29 +00:00
|
|
|
br.skipRows(int(rowsSkip))
|
|
|
|
pop.ppBase.writeBlock(workerID, br)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pop *pipeOffsetProcessor) flush() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parsePipeOffset(lex *lexer) (*pipeOffset, error) {
|
|
|
|
if !lex.isKeyword("offset", "skip") {
|
|
|
|
return nil, fmt.Errorf("expecting 'offset' or 'skip'; got %q", lex.token)
|
|
|
|
}
|
|
|
|
|
|
|
|
lex.nextToken()
|
|
|
|
n, err := parseUint(lex.token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot parse the number of rows to skip from %q: %w", lex.token, err)
|
|
|
|
}
|
|
|
|
lex.nextToken()
|
|
|
|
po := &pipeOffset{
|
2024-05-20 02:08:30 +00:00
|
|
|
offset: n,
|
2024-05-12 14:33:29 +00:00
|
|
|
}
|
|
|
|
return po, nil
|
|
|
|
}
|