2024-04-29 01:44:54 +00:00
|
|
|
package logstorage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync/atomic"
|
|
|
|
)
|
|
|
|
|
2024-04-30 23:19:22 +00:00
|
|
|
// pipeSkip implements '| skip ...' pipe.
|
|
|
|
//
|
|
|
|
// See https://docs.victoriametrics.com/victorialogs/logsql/#limiters
|
2024-04-29 01:44:54 +00:00
|
|
|
type pipeSkip struct {
|
|
|
|
n uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *pipeSkip) String() string {
|
|
|
|
return fmt.Sprintf("skip %d", ps.n)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *pipeSkip) newPipeProcessor(workersCount int, _ <-chan struct{}, _ func(), ppBase pipeProcessor) pipeProcessor {
|
|
|
|
return &pipeSkipProcessor{
|
|
|
|
ps: ps,
|
|
|
|
ppBase: ppBase,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type pipeSkipProcessor struct {
|
|
|
|
ps *pipeSkip
|
|
|
|
ppBase pipeProcessor
|
|
|
|
|
|
|
|
rowsProcessed atomic.Uint64
|
|
|
|
}
|
|
|
|
|
2024-04-30 23:19:22 +00:00
|
|
|
func (psp *pipeSkipProcessor) writeBlock(workerID uint, br *blockResult) {
|
|
|
|
rowsProcessed := psp.rowsProcessed.Add(uint64(len(br.timestamps)))
|
|
|
|
if rowsProcessed <= psp.ps.n {
|
2024-04-29 01:44:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-30 21:03:34 +00:00
|
|
|
rowsProcessed -= uint64(len(br.timestamps))
|
2024-04-30 23:19:22 +00:00
|
|
|
if rowsProcessed >= psp.ps.n {
|
|
|
|
psp.ppBase.writeBlock(workerID, br)
|
2024-04-29 01:44:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-30 23:19:22 +00:00
|
|
|
rowsSkip := psp.ps.n - rowsProcessed
|
2024-04-30 21:03:34 +00:00
|
|
|
br.skipRows(int(rowsSkip))
|
2024-04-30 23:19:22 +00:00
|
|
|
psp.ppBase.writeBlock(workerID, br)
|
2024-04-29 01:44:54 +00:00
|
|
|
}
|
|
|
|
|
2024-04-30 23:19:22 +00:00
|
|
|
func (psp *pipeSkipProcessor) flush() error {
|
2024-04-29 01:44:54 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parsePipeSkip(lex *lexer) (*pipeSkip, error) {
|
|
|
|
if !lex.mustNextToken() {
|
|
|
|
return nil, fmt.Errorf("missing the number of rows to skip")
|
|
|
|
}
|
|
|
|
n, err := parseUint(lex.token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot parse the number of rows to skip %q: %w", lex.token, err)
|
|
|
|
}
|
|
|
|
lex.nextToken()
|
|
|
|
ps := &pipeSkip{
|
|
|
|
n: n,
|
|
|
|
}
|
|
|
|
return ps, nil
|
|
|
|
}
|