From 6c12fc4b33049111ad6ddc3f62bf979f843fad73 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sat, 4 Dec 2021 11:44:46 +0000 Subject: refactor --- internal/io/fs/readfile.go | 136 --------------------------------------------- 1 file changed, 136 deletions(-) (limited to 'internal/io') diff --git a/internal/io/fs/readfile.go b/internal/io/fs/readfile.go index 9fbbad5..008111d 100644 --- a/internal/io/fs/readfile.go +++ b/internal/io/fs/readfile.go @@ -218,142 +218,6 @@ func (f *readFile) filter(ctx context.Context, ltx lcontext.LContext, f.filterWithoutLContext(ctx, rawLines, lines, re) } -func (f *readFile) filterWithoutLContext(ctx context.Context, rawLines <-chan *bytes.Buffer, - lines chan<- line.Line, re regex.Regex) { - - for { - select { - case line, ok := <-rawLines: - f.updatePosition() - if !ok { - return - } - if filteredLine, ok := f.transmittable(line, len(lines), cap(lines), re); ok { - select { - case lines <- filteredLine: - case <-ctx.Done(): - return - } - } - } - } -} - -// Filter log lines matching a given regular expression, however with local grep context. -func (f *readFile) filterWithLContext(ctx context.Context, ltx lcontext.LContext, - rawLines <-chan *bytes.Buffer, lines chan<- line.Line, re regex.Regex) { - - // Scenario 1: Finish once maxCount hits found - maxCount := ltx.MaxCount - processMaxCount := maxCount > 0 - maxReached := false - - // Scenario 2: Print prev. N lines when current line matches. - before := ltx.BeforeContext - processBefore := before > 0 - var beforeBuf chan *bytes.Buffer - if processBefore { - beforeBuf = make(chan *bytes.Buffer, before) - } - - // Screnario 3: Print next N lines when current line matches. - after := 0 - processAfter := ltx.AfterContext > 0 - - for lineBytesBuffer := range rawLines { - f.updatePosition() - - if !re.Match(lineBytesBuffer.Bytes()) { - f.updateLineNotMatched() - - if processAfter && after > 0 { - after-- - myLine := line.Line{ - Content: lineBytesBuffer, - SourceID: f.globID, - Count: f.totalLineCount(), - TransmittedPerc: 100, - } - - select { - case lines <- myLine: - case <-ctx.Done(): - return - } - - } else if processBefore { - // Keep last num BeforeContext raw messages. - select { - case beforeBuf <- lineBytesBuffer: - default: - pool.RecycleBytesBuffer(<-beforeBuf) - beforeBuf <- lineBytesBuffer - } - } - continue - } - - f.updateLineMatched() - - if processAfter { - if maxReached { - return - } - after = ltx.AfterContext - } - - if processBefore { - i := uint64(len(beforeBuf)) - for { - select { - case lineBytesBuffer := <-beforeBuf: - myLine := line.Line{ - Content: lineBytesBuffer, - SourceID: f.globID, - Count: f.totalLineCount() - i, - TransmittedPerc: 100, - } - i-- - - select { - case lines <- myLine: - case <-ctx.Done(): - return - } - default: - // beforeBuf is now empty. - } - if len(beforeBuf) == 0 { - break - } - } - } - - line := line.Line{ - Content: lineBytesBuffer, - SourceID: f.globID, - Count: f.totalLineCount(), - TransmittedPerc: 100, - } - - select { - case lines <- line: - if processMaxCount { - maxCount-- - if maxCount == 0 { - if !processAfter || after == 0 { - return - } - // Unfortunatley we have to continue filter, as there might be more lines to print - maxReached = true - } - } - case <-ctx.Done(): - return - } - } -} - func (f *readFile) transmittable(lineBytesBuffer *bytes.Buffer, length, capacity int, re regex.Regex) (line.Line, bool) { -- cgit v1.2.3