Skip to content

Commit 3606141

Browse files
authored
feat:add code comments & refactor mysql config constant (#826)
* feat:add code comments & refactor mysql config constant * feat: modify code comments
1 parent 93df998 commit 3606141

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

canal/config.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ import (
88
"time"
99

1010
"github.com/BurntSushi/toml"
11-
"github.com/go-mysql-org/go-mysql/client"
12-
"github.com/go-mysql-org/go-mysql/mysql"
1311
"github.com/pingcap/errors"
1412
"github.com/siddontang/go-log/log"
1513
"github.com/siddontang/go-log/loggers"
14+
15+
"github.com/go-mysql-org/go-mysql/client"
16+
"github.com/go-mysql-org/go-mysql/mysql"
1617
)
1718

1819
type DumpConfig struct {
@@ -91,13 +92,13 @@ type Config struct {
9192
// Set TLS config
9293
TLSConfig *tls.Config
9394

94-
//Set Logger
95+
// Set Logger
9596
Logger loggers.Advanced
9697

97-
//Set Dialer
98+
// Set Dialer
9899
Dialer client.Dialer
99100

100-
//Set Localhost
101+
// Set Localhost
101102
Localhost string
102103
}
103104

@@ -121,19 +122,18 @@ func NewConfig(data string) (*Config, error) {
121122
return &c, nil
122123
}
123124

125+
// NewDefaultConfig initiates some default config for Canal
124126
func NewDefaultConfig() *Config {
125127
c := new(Config)
126128

127-
c.Addr = "127.0.0.1:3306"
128-
c.User = "root"
129-
c.Password = ""
130-
129+
c.Addr = mysql.DEFAULT_ADDR
130+
c.User = mysql.DEFAULT_USER
131+
c.Password = mysql.DEFAULT_PASSWORD
131132
c.Charset = mysql.DEFAULT_CHARSET
132133
c.ServerID = uint32(rand.New(rand.NewSource(time.Now().Unix())).Intn(1000)) + 1001
134+
c.Flavor = mysql.DEFAULT_FLAVOR
133135

134-
c.Flavor = "mysql"
135-
136-
c.Dump.ExecutionPath = "mysqldump"
136+
c.Dump.ExecutionPath = mysql.DEFAULT_DUMP_EXECUTION_PATH
137137
c.Dump.DiscardErr = true
138138
c.Dump.SkipMasterData = false
139139

mysql/const.go

+8
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,19 @@ const (
170170
)
171171

172172
const (
173+
DEFAULT_ADDR = "127.0.0.1:3306"
174+
DEFAULT_USER = "root"
175+
DEFAULT_PASSWORD = ""
176+
DEFAULT_FLAVOR = "mysql"
173177
DEFAULT_CHARSET = "utf8"
174178
DEFAULT_COLLATION_ID uint8 = 33
175179
DEFAULT_COLLATION_NAME string = "utf8_general_ci"
176180
)
177181

182+
const (
183+
DEFAULT_DUMP_EXECUTION_PATH = "mysqldump"
184+
)
185+
178186
// Like vitess, use flavor for different MySQL versions,
179187
const (
180188
MySQLFlavor = "mysql"

mysql/position.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import (
66
"strings"
77
)
88

9-
// For binlog filename + position based replication
9+
// Position for binlog filename + position based replication
1010
type Position struct {
1111
Name string
1212
Pos uint32
1313
}
1414

15+
// Compare the position information between the p and o,
16+
// if p > o return 1 means the position of p is further back than o.
1517
func (p Position) Compare(o Position) int {
1618
// First compare binlog name
1719
nameCmp := CompareBinlogFileName(p.Name, o.Name)
@@ -32,6 +34,9 @@ func (p Position) String() string {
3234
return fmt.Sprintf("(%s, %d)", p.Name, p.Pos)
3335
}
3436

37+
// CompareBinlogFileName compares the binlog filename of a and b.
38+
// if a>b will return 1.
39+
// if b>a will return -1.
3540
func CompareBinlogFileName(a, b string) int {
3641
// sometimes it's convenient to construct a `Position` literal with no `Name`
3742
if a == "" && b == "" {
@@ -61,9 +66,11 @@ func CompareBinlogFileName(a, b string) int {
6166
return n[:i], seq
6267
}
6368

69+
// get the basename(aBase) and the serial number(aSeq)
6470
aBase, aSeq := splitBinlogName(a)
6571
bBase, bSeq := splitBinlogName(b)
6672

73+
// aBase and bBase generally will be equal if they are both from the same database configuration.
6774
if aBase > bBase {
6875
return 1
6976
} else if aBase < bBase {

0 commit comments

Comments
 (0)