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

Replace io.ReadSeeker with io.Reader #59

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 10 additions & 4 deletions log/slow/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ var (

// SlowLogParser parses a MySQL slow log. It implements the LogParser interface.
type SlowLogParser struct {
reader io.ReadSeeker
reader io.Reader
opt log.Options
// --
stopChan chan bool
Expand All @@ -78,7 +78,7 @@ type SlowLogParser struct {
}

// NewSlowLogParser returns a new SlowLogParser that reads from the open file.
func NewSlowLogParser(r io.ReadSeeker, opt log.Options) *SlowLogParser {
func NewSlowLogParser(r io.Reader, opt log.Options) *SlowLogParser {
if opt.DefaultLocation == nil {
// Old MySQL format assumes time is taken from SYSTEM.
opt.DefaultLocation = time.Local
Expand Down Expand Up @@ -130,8 +130,14 @@ func (p *SlowLogParser) Start() error {
// Seek to the offset, if any.
// @todo error if start off > file size
if p.opt.StartOffset > 0 {
if _, err := p.reader.Seek(int64(p.opt.StartOffset), os.SEEK_SET); err != nil {
return err
if reader, ok := p.reader.(io.ReadSeeker); ok {
if _, err := reader.Seek(int64(p.opt.StartOffset), os.SEEK_SET); err != nil {
return err
}
} else {
if _, err := io.CopyN(io.Discard, p.reader, int64(p.opt.StartOffset)); err != nil {
return err
}
}
}

Expand Down
25 changes: 25 additions & 0 deletions log/slow/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ package slow_test

import (
"bytes"
"io"
l "log"
"os"
"path"
Expand Down Expand Up @@ -1986,3 +1987,27 @@ SELECT fruit FROM trees;

assert.NotEqual(t, 0, len(got))
}

func TestParseFromMultiReader(t *testing.T) {
query1 := `
# Time: 071218 11:48:27
# User@Host: [SQL_SLAVE] @ []
# Thread_id: 3 Schema: db1
# Query_time: 0.000012 Lock_time: 0.000000 Rows_sent: 0 Rows_examined: 0`
query2 := `
use db2;
SELECT fruit FROM trees;
`

buf1 := bytes.NewReader([]byte(query1))
buf2 := bytes.NewReader([]byte(query2))
p := parser.NewSlowLogParser(io.MultiReader(buf1, buf2), opt)

got := []log.Event{}
go p.Start()
for e := range p.EventChan() {
got = append(got, *e)
}

assert.NotEqual(t, 0, len(got))
}