Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encoding buffer resizing #58

Merged
merged 5 commits into from
Jul 28, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Refactor custom scanner into its own object
camdencheek committed Jul 27, 2020
commit 4e0b3530a78a8234e67a3e81d1072ef967db71ef
33 changes: 33 additions & 0 deletions operator/builtin/input/file/positional_scanner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package file

import (
"bufio"
"io"
)

type PositionalScanner struct {
pos int64
*bufio.Scanner
}

func NewPositionalScanner(r io.Reader, maxLogSize int, startOffset int64, splitFunc bufio.SplitFunc) *PositionalScanner {
ps := &PositionalScanner{
pos: startOffset,
Scanner: bufio.NewScanner(r),
}

buf := make([]byte, 0, 16384)
ps.Scanner.Buffer(buf, maxLogSize)

scanFunc := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
advance, token, err = splitFunc(data, atEOF)
ps.pos += int64(advance)
return
}
ps.Scanner.Split(scanFunc)
return ps
}

func (ps *PositionalScanner) Pos() int64 {
return ps.pos
}
21 changes: 6 additions & 15 deletions operator/builtin/input/file/read_to_end.go
Original file line number Diff line number Diff line change
@@ -58,16 +58,7 @@ func ReadToEnd(
return fmt.Errorf("seek file: %s", err)
}

scanner := bufio.NewScanner(file)
buf := make([]byte, 0, 16384)
scanner.Buffer(buf, maxLogSize)
pos := startOffset
scanFunc := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
advance, token, err = splitFunc(data, atEOF)
pos += int64(advance)
return
}
scanner.Split(scanFunc)
scanner := NewPositionalScanner(file, maxLogSize, startOffset, splitFunc)

// Make a large, reusable buffer for transforming
decoder := encoding.NewDecoder()
@@ -113,24 +104,24 @@ func ReadToEnd(
}

emit(scanner.Bytes())
messenger.SetOffset(pos)
messenger.SetOffset(scanner.Pos())
}

// If we're not at the end of the file, and we haven't
// advanced since last cycle, read the rest of the file as an entry
if pos < stat.Size() && pos == startOffset && lastSeenFileSize == stat.Size() {
_, err := file.Seek(pos, 0)
if scanner.Pos() < stat.Size() && scanner.Pos() == startOffset && lastSeenFileSize == stat.Size() {
_, err := file.Seek(scanner.Pos(), 0)
if err != nil {
return errors.Wrap(err, "seeking for trailing entry")
}

msgBuf := make([]byte, stat.Size()-pos)
msgBuf := make([]byte, stat.Size()-scanner.Pos())
n, err := file.Read(msgBuf)
if err != nil {
return errors.Wrap(err, "reading trailing entry")
}
emit(msgBuf[:n])
messenger.SetOffset(pos + int64(n))
messenger.SetOffset(scanner.Pos() + int64(n))
}

return nil