minor refactoring; fixed license comments

This commit is contained in:
Shlomi Noach 2016-03-24 15:11:56 +01:00
parent fb04eb232f
commit 96a8fd50c3
9 changed files with 43 additions and 40 deletions

View File

@ -1,4 +1,6 @@
#!/bin/bash #!/bin/bash
#
#
buildpath=/tmp/gh-osc buildpath=/tmp/gh-osc
target=gh-osc target=gh-osc

View File

@ -1,5 +1,5 @@
/* /*
Copyright 2015 Shlomi Noach, courtesy Booking.com Copyright 2015 Shlomi Noach
*/ */
package binlog package binlog
@ -11,8 +11,10 @@ import (
"strings" "strings"
) )
// BinlogType identifies the type of the log: relay or binary log
type BinlogType int type BinlogType int
// BinaryLog, RelayLog are binlog types
const ( const (
BinaryLog BinlogType = iota BinaryLog BinlogType = iota
RelayLog RelayLog

14
go/binlog/binlog_entry.go Normal file
View File

@ -0,0 +1,14 @@
/*
Copyright 2016 GitHub Inc.
See https://github.com/github/gh-osc/blob/master/LICENSE
*/
package binlog
type BinlogEntry struct {
LogPos uint64
EndLogPos uint64
StatementType string // INSERT, UPDATE, DELETE
DatabaseName string
TableName string
}

View File

@ -1,17 +1,12 @@
/* /*
Copyright 2016 GitHub Inc. Copyright 2016 GitHub Inc.
See https://github.com/github/gh-osc/blob/master/LICENSE
*/ */
package binlog package binlog
type BinlogEntry struct { // BinlogReader is a general interface whose implementations can choose their methods of reading
LogPos uint64 // a binary log file and parsing it into binlog entries
EndLogPos uint64
StatementType string // INSERT, UPDATE, DELETE
DatabaseName string
TableName string
}
type BinlogReader interface { type BinlogReader interface {
ReadEntries(logFile string, startPos uint64, stopPos uint64) (entries [](*BinlogEntry), err error) ReadEntries(logFile string, startPos uint64, stopPos uint64) (entries [](*BinlogEntry), err error)
} }

View File

@ -1,17 +1,6 @@
/* /*
Copyright 2014 Outbrain Inc. Copyright 2016 GitHub Inc.
See https://github.com/github/gh-osc/blob/master/LICENSE
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/ */
package binlog package binlog

View File

@ -1,5 +1,6 @@
/* /*
Copyright 2016 GitHub Inc. Copyright 2016 GitHub Inc.
See https://github.com/github/gh-osc/blob/master/LICENSE
*/ */
package binlog package binlog
@ -26,8 +27,10 @@ var (
tokenRegxp = regexp.MustCompile("### (WHERE|SET)$") tokenRegxp = regexp.MustCompile("### (WHERE|SET)$")
) )
// BinlogEntryState is a state in the binlog parser automaton / state machine
type BinlogEntryState string type BinlogEntryState string
// States of the state machine
const ( const (
InvalidState BinlogEntryState = "InvalidState" InvalidState BinlogEntryState = "InvalidState"
SearchForStartPosOrStatementState = "SearchForStartPosOrStatementState" SearchForStartPosOrStatementState = "SearchForStartPosOrStatementState"
@ -44,6 +47,7 @@ type MySQLBinlogReader struct {
MySQLBinlogBinary string MySQLBinlogBinary string
} }
// NewMySQLBinlogReader creates a new reader that directly parses binlog files from the filesystem
func NewMySQLBinlogReader(basedir string, datadir string) (mySQLBinlogReader *MySQLBinlogReader) { func NewMySQLBinlogReader(basedir string, datadir string) (mySQLBinlogReader *MySQLBinlogReader) {
mySQLBinlogReader = &MySQLBinlogReader{ mySQLBinlogReader = &MySQLBinlogReader{
Basedir: basedir, Basedir: basedir,
@ -89,8 +93,8 @@ func (this *MySQLBinlogReader) ReadEntries(logFile string, startPos uint64, stop
return entries, err return entries, err
} }
// automaton step: accept wither beginning of new entry, or beginning of new statement
func searchForStartPosOrStatement(scanner *bufio.Scanner, binlogEntry *BinlogEntry, previousEndLogPos uint64) (nextState BinlogEntryState, nextBinlogEntry *BinlogEntry, err error) { func searchForStartPosOrStatement(scanner *bufio.Scanner, binlogEntry *BinlogEntry, previousEndLogPos uint64) (nextState BinlogEntryState, nextBinlogEntry *BinlogEntry, err error) {
onStartEntry := func(submatch []string) (BinlogEntryState, *BinlogEntry, error) { onStartEntry := func(submatch []string) (BinlogEntryState, *BinlogEntry, error) {
startLogPos, _ := strconv.ParseUint(submatch[1], 10, 64) startLogPos, _ := strconv.ParseUint(submatch[1], 10, 64)
@ -135,6 +139,7 @@ func searchForStartPosOrStatement(scanner *bufio.Scanner, binlogEntry *BinlogEnt
return SearchForStartPosOrStatementState, binlogEntry, nil return SearchForStartPosOrStatementState, binlogEntry, nil
} }
// automaton step: expect an end_log_pos line`
func expectEndLogPos(scanner *bufio.Scanner, binlogEntry *BinlogEntry) (nextState BinlogEntryState, err error) { func expectEndLogPos(scanner *bufio.Scanner, binlogEntry *BinlogEntry) (nextState BinlogEntryState, err error) {
line := scanner.Text() line := scanner.Text()
@ -146,6 +151,8 @@ func expectEndLogPos(scanner *bufio.Scanner, binlogEntry *BinlogEntry) (nextStat
return InvalidState, fmt.Errorf("Expected to find end_log_pos following pos %+v", binlogEntry.LogPos) return InvalidState, fmt.Errorf("Expected to find end_log_pos following pos %+v", binlogEntry.LogPos)
} }
// automaton step: a not-strictly-required but good-to-have-around validation that
// we see an expected token following a statement
func expectToken(scanner *bufio.Scanner, binlogEntry *BinlogEntry) (nextState BinlogEntryState, err error) { func expectToken(scanner *bufio.Scanner, binlogEntry *BinlogEntry) (nextState BinlogEntryState, err error) {
line := scanner.Text() line := scanner.Text()
if submatch := tokenRegxp.FindStringSubmatch(line); len(submatch) > 1 { if submatch := tokenRegxp.FindStringSubmatch(line); len(submatch) > 1 {
@ -154,6 +161,8 @@ func expectToken(scanner *bufio.Scanner, binlogEntry *BinlogEntry) (nextState Bi
return InvalidState, fmt.Errorf("Expected to find token following pos %+v", binlogEntry.LogPos) return InvalidState, fmt.Errorf("Expected to find token following pos %+v", binlogEntry.LogPos)
} }
// parseEntries will parse output of `mysqlbinlog --verbose --base64-output=DECODE-ROWS`
// It issues an automaton / state machine to do its thang.
func parseEntries(scanner *bufio.Scanner) (entries [](*BinlogEntry), err error) { func parseEntries(scanner *bufio.Scanner) (entries [](*BinlogEntry), err error) {
binlogEntry := &BinlogEntry{} binlogEntry := &BinlogEntry{}
var state BinlogEntryState = SearchForStartPosOrStatementState var state BinlogEntryState = SearchForStartPosOrStatementState

View File

@ -1,17 +1,6 @@
/* /*
Copyright 2014 Outbrain Inc. Copyright 2016 GitHub Inc.
See https://github.com/github/gh-osc/blob/master/LICENSE
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/ */
package binlog package binlog

View File

@ -1,3 +1,8 @@
/*
Copyright 2016 GitHub Inc.
See https://github.com/github/gh-osc/blob/master/LICENSE
*/
package main package main
import ( import (

View File

@ -35,8 +35,6 @@ func execCmd(commandText string, arguments ...string) (*exec.Cmd, string, error)
shellArguments = append(shellArguments, arguments...) shellArguments = append(shellArguments, arguments...)
log.Debugf("%+v", shellArguments) log.Debugf("%+v", shellArguments)
return exec.Command("bash", shellArguments...), tmpFile.Name(), nil return exec.Command("bash", shellArguments...), tmpFile.Name(), nil
//return exec.Command(commandText, arguments...) , "", nil
} }
// CommandRun executes a command // CommandRun executes a command