2024-05-12 16:33:29 +02: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 04:08:30 +02:00
|
|
|
offset uint64
|
2024-05-12 16:33:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (po *pipeOffset) String() string {
|
2024-05-20 04:08:30 +02:00
|
|
|
return fmt.Sprintf("offset %d", po.offset)
|
2024-05-12 16:33:29 +02:00
|
|
|
}
|
|
|
|
|
2024-06-27 14:18:42 +02:00
|
|
|
func (po *pipeOffset) canLiveTail() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-05-12 16:33:29 +02:00
|
|
|
func (po *pipeOffset) updateNeededFields(_, _ fieldsSet) {
|
2024-05-25 21:36:16 +02:00
|
|
|
// nothing to do
|
2024-05-12 16:33:29 +02:00
|
|
|
}
|
|
|
|
|
2024-05-25 21:36:16 +02:00
|
|
|
func (po *pipeOffset) optimize() {
|
|
|
|
// nothing to do
|
|
|
|
}
|
|
|
|
|
|
|
|
func (po *pipeOffset) hasFilterInWithQuery() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-05-26 02:01:32 +02:00
|
|
|
func (po *pipeOffset) initFilterInValues(_ map[string][]string, _ getFieldValuesFunc) (pipe, error) {
|
2024-05-25 21:36:16 +02:00
|
|
|
return po, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (po *pipeOffset) newPipeProcessor(_ int, _ <-chan struct{}, _ func(), ppNext pipeProcessor) pipeProcessor {
|
2024-05-12 16:33:29 +02:00
|
|
|
return &pipeOffsetProcessor{
|
|
|
|
po: po,
|
2024-05-25 21:36:16 +02:00
|
|
|
ppNext: ppNext,
|
2024-05-12 16:33:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type pipeOffsetProcessor struct {
|
|
|
|
po *pipeOffset
|
2024-05-25 21:36:16 +02:00
|
|
|
ppNext pipeProcessor
|
2024-05-12 16:33:29 +02:00
|
|
|
|
|
|
|
rowsProcessed atomic.Uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pop *pipeOffsetProcessor) writeBlock(workerID uint, br *blockResult) {
|
2024-09-25 16:16:53 +02:00
|
|
|
if br.rowsLen == 0 {
|
2024-05-12 16:33:29 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-25 16:16:53 +02:00
|
|
|
rowsProcessed := pop.rowsProcessed.Add(uint64(br.rowsLen))
|
2024-05-20 04:08:30 +02:00
|
|
|
if rowsProcessed <= pop.po.offset {
|
2024-05-12 16:33:29 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-25 16:16:53 +02:00
|
|
|
rowsProcessed -= uint64(br.rowsLen)
|
2024-05-20 04:08:30 +02:00
|
|
|
if rowsProcessed >= pop.po.offset {
|
2024-05-25 21:36:16 +02:00
|
|
|
pop.ppNext.writeBlock(workerID, br)
|
2024-05-12 16:33:29 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-20 04:08:30 +02:00
|
|
|
rowsSkip := pop.po.offset - rowsProcessed
|
2024-05-12 16:33:29 +02:00
|
|
|
br.skipRows(int(rowsSkip))
|
2024-05-25 21:36:16 +02:00
|
|
|
pop.ppNext.writeBlock(workerID, br)
|
2024-05-12 16:33:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 04:08:30 +02:00
|
|
|
offset: n,
|
2024-05-12 16:33:29 +02:00
|
|
|
}
|
|
|
|
return po, nil
|
|
|
|
}
|