diff options
| author | Paul Buetow <pbuetow@mimecast.com> | 2021-12-06 09:39:33 +0000 |
|---|---|---|
| committer | Paul Buetow <pbuetow@mimecast.com> | 2021-12-06 09:39:33 +0000 |
| commit | 6031ce78df6cda9c8b924b231554bb4e0c077358 (patch) | |
| tree | 6427bcab0e0207f40d03ccbe7b424de728116172 | |
| parent | 7ec5c5b144866c392e3676778041a2ae6aa9d360 (diff) | |
Refactor local context filter method to reduce its cognitive complexity
| -rw-r--r-- | internal/io/fs/readfile.go | 5 | ||||
| -rw-r--r-- | internal/io/fs/readfilelcontext.go | 144 |
2 files changed, 96 insertions, 53 deletions
diff --git a/internal/io/fs/readfile.go b/internal/io/fs/readfile.go index f37b07d..90c5966 100644 --- a/internal/io/fs/readfile.go +++ b/internal/io/fs/readfile.go @@ -26,6 +26,7 @@ import ( type readStatus int const ( + nothing readStatus = iota abortReading readStatus = iota continueReading readStatus = iota ) @@ -300,7 +301,7 @@ func (f *readFile) handleReadError(ctx context.Context, err error, fd *os.File, return abortReading, nil } - return continueReading, nil + return nothing, nil } // Now process the byte we just read from the fd. @@ -333,5 +334,5 @@ func (f *readFile) handleReadByte(ctx context.Context, b byte, } } - return continueReading, message + return nothing, message } diff --git a/internal/io/fs/readfilelcontext.go b/internal/io/fs/readfilelcontext.go index 0d41a07..44ce17d 100644 --- a/internal/io/fs/readfilelcontext.go +++ b/internal/io/fs/readfilelcontext.go @@ -73,10 +73,14 @@ func (f *readFile) filterWithLContext(ctx context.Context, ltx lcontext.LContext ls.after = 0 ls.processAfter = ltx.AfterContext > 0 + // No go through all raw lines read to determine with they satisfy the local + // context or not. "Matching" lines will be sent to the lines channel. for rawLine := range rawLines { status := f.filterLineWithLContext(ctx, <x, &ls, rawLines, lines, &re, rawLine) - if status == abortReading { + switch status { + case abortReading: return + default: } } } @@ -87,59 +91,36 @@ func (f *readFile) filterLineWithLContext(ctx context.Context, ltx *lcontext.LCo rawLine *bytes.Buffer) readStatus { f.updatePosition() + if !re.Match(rawLine.Bytes()) { f.updateLineNotMatched() - - if ls.processAfter && ls.after > 0 { - ls.after-- - myLine := line.New(rawLine, f.totalLineCount(), 100, f.globID) - - select { - case lines <- myLine: - case <-ctx.Done(): - return abortReading - } - - } else if ls.processBefore { - // Keep last num BeforeContext raw messages. - select { - case ls.beforeBuf <- rawLine: - default: - pool.RecycleBytesBuffer(<-ls.beforeBuf) - ls.beforeBuf <- rawLine - } + status := f.lContextNotMatched(ctx, ls, lines, rawLine) + switch status { + case nothing: + default: + return status } - return continueReading } f.updateLineMatched() + // If we have an "after" context to worry about... if ls.processAfter { if ls.maxReached { + // We have reached the "max" hits. Stop/abort reading. return abortReading } + // Reset the "after" context. ls.after = ltx.AfterContext } + // If we have a "before" context to worry about... if ls.processBefore { - i := uint64(len(ls.beforeBuf)) - for { - select { - case rawLine := <-ls.beforeBuf: - myLine := line.New(rawLine, f.totalLineCount()-i, 100, f.globID) - i-- - - select { - case lines <- myLine: - case <-ctx.Done(): - return abortReading - } - default: - // beforeBuf is now empty. - } - if len(ls.beforeBuf) == 0 { - break - } + status := f.lContextProcessBefore(ctx, ls, lines, rawLine) + switch status { + case nothing: + default: + return status } } @@ -147,26 +128,87 @@ func (f *readFile) filterLineWithLContext(ctx context.Context, ltx *lcontext.LCo select { case lines <- line: + // If we have a "max" context to worry about... if ls.processMaxCount { - ls.maxCount-- - if ls.maxCount == 0 { - if !ls.processAfter || ls.after == 0 { - return abortReading - } - // Unfortunatley we have to continue filter, as there might be more lines to print - ls.maxReached = true + status := f.lContextProcessMaxCount(ctx, ls) + switch status { + case nothing: + default: + return status } } case <-ctx.Done(): return abortReading } + return nothing +} + +// Do some post-processing for the "after" and the "before" contexts in case the +// line didn't match the regex. +func (f *readFile) lContextNotMatched(ctx context.Context, ls *ltxState, + lines chan<- *line.Line, rawLine *bytes.Buffer) readStatus { + + if ls.processAfter && ls.after > 0 { + ls.after-- + myLine := line.New(rawLine, f.totalLineCount(), 100, f.globID) + + select { + case lines <- myLine: + case <-ctx.Done(): + return abortReading + } + + } else if ls.processBefore { + // Keep last num BeforeContext raw messages. + select { + case ls.beforeBuf <- rawLine: + default: + pool.RecycleBytesBuffer(<-ls.beforeBuf) + ls.beforeBuf <- rawLine + } + } + return continueReading } -/* -func (f *readFile) filterLineWithLContextNoMatch(ctx context.Context, ltx *lcontext.LContext, - ls *ltxState, rawLines <-chan *bytes.Buffer, lines chan<- line.Line, re *regex.Regex, - rawLine *bytes.Buffer) readStatus { +// Do some processing for the "before" context. +func (f *readFile) lContextProcessBefore(ctx context.Context, + ls *ltxState, lines chan<- *line.Line, rawLine *bytes.Buffer) readStatus { + + i := uint64(len(ls.beforeBuf)) + for { + select { + case rawLine := <-ls.beforeBuf: + myLine := line.New(rawLine, f.totalLineCount()-i, 100, f.globID) + i-- + + select { + case lines <- myLine: + case <-ctx.Done(): + return abortReading + } + default: + // beforeBuf is now empty. + } + if len(ls.beforeBuf) == 0 { + break + } + } + + return nothing +} + +// Do some processing for the "max" context. +func (f *readFile) lContextProcessMaxCount(ctx context.Context, ls *ltxState) readStatus { + ls.maxCount-- + if ls.maxCount == 0 { + if !ls.processAfter || ls.after == 0 { + return abortReading + } + // Unfortunatley we have to continue filter, as there might be more lines to print + ls.maxReached = true + } + + return nothing } -*/ |
