From 5c9f1dc4efa27b9f6e4238f2317336b86c1780b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philip=20Dub=C3=A9?= Date: Wed, 29 Jan 2025 13:54:39 +0000 Subject: [PATCH] cleanup init functions --- compress/zlib.go | 10 ++-------- driver/driver.go | 12 ++++++------ dump/parser.go | 12 ++++-------- mysql/util.go | 6 ++---- replication/time.go | 20 +++++++++----------- server/stmt.go | 14 ++++---------- 6 files changed, 27 insertions(+), 47 deletions(-) diff --git a/compress/zlib.go b/compress/zlib.go index 17bad746d..07e8186c7 100644 --- a/compress/zlib.go +++ b/compress/zlib.go @@ -11,17 +11,11 @@ import ( const DefaultCompressionLevel = 6 var ( - zlibReaderPool *sync.Pool - zlibWriterPool sync.Pool -) - -func init() { - zlibReaderPool = &sync.Pool{ + zlibReaderPool = sync.Pool{ New: func() interface{} { return nil }, } - zlibWriterPool = sync.Pool{ New: func() interface{} { w, err := zlib.NewWriterLevel(new(bytes.Buffer), DefaultCompressionLevel) @@ -31,7 +25,7 @@ func init() { return w }, } -} +) var _ io.WriteCloser = zlibWriter{} var _ io.ReadCloser = zlibReader{} diff --git a/driver/driver.go b/driver/driver.go index eb72f2ca3..094ac0cbd 100644 --- a/driver/driver.go +++ b/driver/driver.go @@ -27,7 +27,12 @@ var customTLSMutex sync.Mutex var ( dsnRegex = regexp.MustCompile("@[^@]+/[^@/]+") customTLSConfigMap = make(map[string]*tls.Config) - options = make(map[string]DriverOption) + options = map[string]DriverOption{ + "compress": CompressOption, + "collation": CollationOption, + "readTimeout": ReadTimeoutOption, + "writeTimeout": WriteTimeoutOption, + } // can be provided by clients to allow more control in handling Go and database // types beyond the default Value types allowed @@ -392,11 +397,6 @@ func (r *rows) Next(dest []sqldriver.Value) error { var driverName = "mysql" func init() { - options["compress"] = CompressOption - options["collation"] = CollationOption - options["readTimeout"] = ReadTimeoutOption - options["writeTimeout"] = WriteTimeoutOption - sql.Register(driverName, driver{}) } diff --git a/dump/parser.go b/dump/parser.go index 529e78bd9..f3c699b7b 100644 --- a/dump/parser.go +++ b/dump/parser.go @@ -23,20 +23,16 @@ type ParseHandler interface { Data(schema string, table string, values []string) error } -var binlogExp *regexp.Regexp -var useExp *regexp.Regexp -var valuesExp *regexp.Regexp -var gtidExp *regexp.Regexp - -func init() { +var ( binlogExp = regexp.MustCompile(`^CHANGE (MASTER|REPLICATION SOURCE) TO (MASTER_LOG_FILE|SOURCE_LOG_FILE)='(.+)', (MASTER_LOG_POS|SOURCE_LOG_POS)=(\d+);`) - useExp = regexp.MustCompile("^USE `(.+)`;") + useExp = regexp.MustCompile("^USE `(.+)`;") valuesExp = regexp.MustCompile("^INSERT INTO `(.+?)` VALUES \\((.+)\\);$") + // The pattern will only match MySQL GTID, as you know SET GLOBAL gtid_slave_pos='0-1-4' is used for MariaDB. // SET @@GLOBAL.GTID_PURGED='1638041a-0457-11e9-bb9f-00505690b730:1-429405150'; // https://dev.mysql.com/doc/refman/5.7/en/replication-gtids-concepts.html gtidExp = regexp.MustCompile(`(\w{8}(-\w{4}){3}-\w{12}(:\d+(-\d+)?)+)`) -} +) // Parse the dump data with Dumper generate. // It can not parse all the data formats with mysqldump outputs diff --git a/mysql/util.go b/mysql/util.go index c0c9f5452..5bde1fdb8 100644 --- a/mysql/util.go +++ b/mysql/util.go @@ -443,9 +443,7 @@ func init() { for i := range EncodeMap { EncodeMap[i] = DONTESCAPE } - for i := range EncodeMap { - if to, ok := encodeRef[byte(i)]; ok { - EncodeMap[byte(i)] = to - } + for k, v := range encodeRef { + EncodeMap[k] = v } } diff --git a/replication/time.go b/replication/time.go index 2adc832f4..4134f8ac0 100644 --- a/replication/time.go +++ b/replication/time.go @@ -2,12 +2,19 @@ package replication import ( "fmt" - "strings" "time" ) var ( - fracTimeFormat []string + fracTimeFormat = [7]string{ + "2006-01-02 15:04:05", + "2006-01-02 15:04:05.0", + "2006-01-02 15:04:05.00", + "2006-01-02 15:04:05.000", + "2006-01-02 15:04:05.0000", + "2006-01-02 15:04:05.00000", + "2006-01-02 15:04:05.000000", + } ) // fracTime is a help structure wrapping Golang Time. @@ -56,12 +63,3 @@ func microSecTimestampToTime(ts uint64) time.Time { } return time.Unix(int64(ts/1000000), int64(ts%1000000)*1000) } - -func init() { - fracTimeFormat = make([]string, 7) - fracTimeFormat[0] = "2006-01-02 15:04:05" - - for i := 1; i <= 6; i++ { - fracTimeFormat[i] = fmt.Sprintf("2006-01-02 15:04:05.%s", strings.Repeat("0", i)) - } -} diff --git a/server/stmt.go b/server/stmt.go index cdccb3d42..bc7902e19 100644 --- a/server/stmt.go +++ b/server/stmt.go @@ -10,16 +10,10 @@ import ( "github.com/pingcap/errors" ) -var paramFieldData []byte -var columnFieldData []byte - -func init() { - var p = &Field{Name: []byte("?")} - var c = &Field{} - - paramFieldData = p.Dump() - columnFieldData = c.Dump() -} +var ( + paramFieldData = (&Field{Name: []byte("?")}).Dump() + columnFieldData = (&Field{}).Dump() +) type Stmt struct { ID uint32