From a66bc590bc9f4520461c6c34026cbba9012217e6 Mon Sep 17 00:00:00 2001 From: disksing Date: Fri, 11 Dec 2015 12:20:47 +0800 Subject: [PATCH] util/codec: remove stringFlag --- ddl/ddl_test.go | 6 ++-- plan/plans/from_test.go | 11 ++++--- plan/plans/index_test.go | 8 ++--- privilege/privileges/privileges.go | 23 +++++++------ session_test.go | 12 +++---- table/tables/tables.go | 2 +- util/codec/codec.go | 52 ++++++++---------------------- util/codec/codec_test.go | 4 +-- 8 files changed, 49 insertions(+), 69 deletions(-) diff --git a/ddl/ddl_test.go b/ddl/ddl_test.go index 409d1cea65..6b491d7db2 100644 --- a/ddl/ddl_test.go +++ b/ddl/ddl_test.go @@ -146,7 +146,7 @@ func (ts *testSuite) TestDDL(c *C) { c.Assert(err, IsNil) c.Assert(len(cols), Equals, 3) c.Assert(cols[0], Equals, nil) - c.Assert(cols[1], Equals, "abc") + c.Assert(cols[1], BytesEquals, []byte("abc")) c.Assert(cols[2], Equals, int64(2)) rid3, err := tb.AddRecord(ctx, []interface{}{mysql.Enum{Name: "bb", Value: 1}, "c", 3}, 0) c.Assert(err, IsNil) @@ -154,7 +154,7 @@ func (ts *testSuite) TestDDL(c *C) { c.Assert(err, IsNil) c.Assert(len(cols), Equals, 3) c.Assert(cols[0], Equals, mysql.Enum{Name: "bb", Value: 1}) - c.Assert(cols[1], Equals, "c") + c.Assert(cols[1], BytesEquals, []byte("c")) c.Assert(cols[2], Equals, int64(3)) // Test add column after a not exist column, get an error. @@ -180,7 +180,7 @@ func (ts *testSuite) TestDDL(c *C) { cols, err = tb.Row(ctx, rid0) c.Assert(err, IsNil) c.Assert(len(cols), Equals, 2) - c.Assert(cols[0], Equals, "abc") + c.Assert(cols[0], BytesEquals, []byte("abc")) c.Assert(cols[1], Equals, int64(1)) // Test drop a not exist column from table, get an error. diff --git a/plan/plans/from_test.go b/plan/plans/from_test.go index 182cf9c8d1..3318e4b494 100644 --- a/plan/plans/from_test.go +++ b/plan/plans/from_test.go @@ -136,17 +136,18 @@ func (p *testFromSuit) TestTableDefaultPlan(c *C) { }, } - ret := map[int64]string{} + ret := map[int64][]byte{} rset := rsets.Recordset{Ctx: p, Plan: pln} rset.Do(func(data []interface{}) (bool, error) { - ret[data[0].(int64)] = data[1].(string) + ret[data[0].(int64)] = data[1].([]byte) return true, nil }) - excepted := map[int64]string{} + excepted := map[int64][]byte{} for i := 0; i < 10; i++ { - excepted[int64(i*10)] = "hello" + excepted[int64(i*10)] = []byte("hello") } - c.Assert(reflect.DeepEqual(ret, excepted), Equals, true) + //c.Assert(reflect.DeepEqual(ret, excepted), Equals, true) + c.Assert(ret, DeepEquals, excepted) // expr: id > 0 expr := &expression.BinaryOperation{ diff --git a/plan/plans/index_test.go b/plan/plans/index_test.go index d6b31d1344..3da7d59656 100644 --- a/plan/plans/index_test.go +++ b/plan/plans/index_test.go @@ -189,18 +189,18 @@ func (p *testIndexSuit) TestIndexPlan(c *C) { c.Assert(err, IsNil) c.Assert(np, NotNil) - ret := map[int64]string{} + ret := map[int64][]byte{} rset := rsets.Recordset{ Plan: np, Ctx: p.ctx, } rset.Do(func(data []interface{}) (bool, error) { - ret[data[0].(int64)] = data[1].(string) + ret[data[0].(int64)] = data[1].([]byte) return true, nil }) - excepted := map[int64]string{} + excepted := map[int64][]byte{} for i := 6; i < 10; i++ { - excepted[int64(i*10)] = "hello" + excepted[int64(i*10)] = []byte("hello") } expr6 := &expression.UnaryOperation{ diff --git a/privilege/privileges/privileges.go b/privilege/privileges/privileges.go index e387e9ac70..c790e0de5a 100644 --- a/privilege/privileges/privileges.go +++ b/privilege/privileges/privileges.go @@ -301,11 +301,12 @@ func (p *UserPrivileges) loadDBScopePrivileges(ctx context.Context) error { break } // DB - db, ok := row.Data[1].(string) + db, ok := row.Data[1].([]byte) if !ok { - errors.New("This should be never happened!") + return errors.New("This should be never happened!") } - ps[db] = &privileges{Level: coldef.GrantLevelDB} + dbStr := string(db) + ps[dbStr] = &privileges{Level: coldef.GrantLevelDB} for i := dbTablePrivColumnStartIndex; i < len(fs); i++ { d := row.Data[i] ed, ok := d.(mysql.Enum) @@ -320,7 +321,7 @@ func (p *UserPrivileges) loadDBScopePrivileges(ctx context.Context) error { if !ok { return errors.New("Unknown Privilege Type!") } - ps[db].add(p) + ps[dbStr].add(p) } } p.privs.DBPrivs = ps @@ -345,20 +346,22 @@ func (p *UserPrivileges) loadTableScopePrivileges(ctx context.Context) error { break } // DB - db, ok := row.Data[1].(string) + db, ok := row.Data[1].([]byte) if !ok { return errors.New("This should be never happened!") } + dbStr := string(db) // Table_name - tbl, ok := row.Data[3].(string) + tbl, ok := row.Data[3].([]byte) if !ok { return errors.New("This should be never happened!") } - _, ok = ps[db] + tblStr := string(tbl) + _, ok = ps[dbStr] if !ok { - ps[db] = make(map[string]*privileges) + ps[dbStr] = make(map[string]*privileges) } - ps[db][tbl] = &privileges{Level: coldef.GrantLevelTable} + ps[dbStr][tblStr] = &privileges{Level: coldef.GrantLevelTable} // Table_priv tblPrivs, ok := row.Data[6].(mysql.Set) if !ok { @@ -370,7 +373,7 @@ func (p *UserPrivileges) loadTableScopePrivileges(ctx context.Context) error { if !ok { return errors.New("Unknown Privilege Type!") } - ps[db][tbl].add(p) + ps[dbStr][tblStr].add(p) } } p.privs.TablePrivs = ps diff --git a/session_test.go b/session_test.go index d03bca8a35..d80d2537c5 100644 --- a/session_test.go +++ b/session_test.go @@ -170,14 +170,14 @@ func (s *testSessionSuite) TestPrimaryKeyAutoincrement(c *C) { c.Assert(rs, NotNil) row, err := rs.FirstRow() c.Assert(err, IsNil) - match(c, row, id, "abc", nil) + match(c, row, id, []byte("abc"), nil) mustExecSQL(c, se, "update t set name = 'abc', status = 1 where id = ?", id) rs = mustExecSQL(c, se2, "select * from t") c.Assert(rs, NotNil) row, err = rs.FirstRow() c.Assert(err, IsNil) - match(c, row, id, "abc", 1) + match(c, row, id, []byte("abc"), 1) // Check for pass bool param to tidb prepared statement mustExecSQL(c, se, "drop table if exists t") mustExecSQL(c, se, "create table t (id tiny)") @@ -549,7 +549,7 @@ func (s *testSessionSuite) TestSelect(c *C) { r = mustExecSQL(c, se, `select * from t where c like 'pingcap ''-->'' tidb'`) row, err = r.FirstRow() c.Assert(err, IsNil) - match(c, row, `pingcap '-->' tidb`) + match(c, row, []byte(`pingcap '-->' tidb`)) mustExecSQL(c, se, "drop table if exists t1") mustExecSQL(c, se, "drop table if exists t2") @@ -723,7 +723,7 @@ func (s *testSessionSuite) TestBootstrap(c *C) { row, err := r.Next() c.Assert(err, IsNil) c.Assert(row, NotNil) - match(c, row.Data, "%", "root", "", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y") + match(c, row.Data, []byte("%"), []byte("root"), []byte(""), "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y") c.Assert(se.Auth("root@anyhost", []byte(""), []byte("")), IsTrue) mustExecSQL(c, se, "USE test;") @@ -792,7 +792,7 @@ func (s *testSessionSuite) TestBootstrapWithError(c *C) { row, err := r.Next() c.Assert(err, IsNil) c.Assert(row, NotNil) - match(c, row.Data, "%", "root", "", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y") + match(c, row.Data, []byte("%"), []byte("root"), []byte(""), "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y") mustExecSQL(c, se, "USE test;") // Check privilege tables. mustExecSQL(c, se, "SELECT * from mysql.db;") @@ -809,7 +809,7 @@ func (s *testSessionSuite) TestBootstrapWithError(c *C) { c.Assert(err, IsNil) c.Assert(row, NotNil) c.Assert(row.Data, HasLen, 1) - c.Assert(row.Data[0], Equals, "True") + c.Assert(row.Data[0], BytesEquals, []byte("True")) } func (s *testSessionSuite) TestEnum(c *C) { diff --git a/table/tables/tables.go b/table/tables/tables.go index d6755b7c1c..0d553b0391 100644 --- a/table/tables/tables.go +++ b/table/tables/tables.go @@ -208,7 +208,7 @@ func (t *Table) unflatten(rec interface{}, col *column.Col) (interface{}, error) case mysql.TypeDuration: return mysql.Duration{Duration: time.Duration(rec.(int64)), Fsp: col.Decimal}, nil case mysql.TypeNewDecimal, mysql.TypeDecimal: - return mysql.ParseDecimal(rec.(string)) + return mysql.ParseDecimal(string(rec.([]byte))) case mysql.TypeEnum: return mysql.ParseEnumValue(col.Elems, rec.(uint64)) case mysql.TypeSet: diff --git a/util/codec/codec.go b/util/codec/codec.go index f3aa8d87ed..e49002e481 100644 --- a/util/codec/codec.go +++ b/util/codec/codec.go @@ -24,8 +24,6 @@ const ( nilFlag byte = iota bytesFlag compactBytesFlag - stringFlag - compactStringFlag intFlag uintFlag floatFlag @@ -80,32 +78,11 @@ func encode(b []byte, vals []interface{}, comparable bool) ([]byte, error) { b = append(b, floatFlag) b = EncodeFloat(b, float64(v)) case string: - if comparable { - b = append(b, stringFlag) - b = EncodeBytes(b, []byte(v)) - } else { - b = append(b, compactStringFlag) - b = EncodeCompactBytes(b, []byte(v)) - } - + b = encodeBytes(b, []byte(v), comparable) case []byte: - if comparable { - b = append(b, bytesFlag) - b = EncodeBytes(b, v) - } else { - b = append(b, compactBytesFlag) - b = EncodeCompactBytes(b, v) - } - + b = encodeBytes(b, v, comparable) case mysql.Time: - if comparable { - b = append(b, stringFlag) - b = EncodeBytes(b, []byte(v.String())) - } else { - b = append(b, compactStringFlag) - b = EncodeCompactBytes(b, []byte(v.String())) - } - + b = encodeBytes(b, []byte(v.String()), comparable) case mysql.Duration: // duration may have negative value, so we cannot use String to encode directly. b = append(b, durationFlag) @@ -135,6 +112,17 @@ func encode(b []byte, vals []interface{}, comparable bool) ([]byte, error) { return b, nil } +func encodeBytes(b []byte, v []byte, comparable bool) []byte { + if comparable { + b = append(b, bytesFlag) + b = EncodeBytes(b, v) + } else { + b = append(b, compactBytesFlag) + b = EncodeCompactBytes(b, v) + } + return b +} + // EncodeKey appends the encoded values to byte slice b, returns the appended // slice. It guarantees the encoded value is in ascending order for comparison. func EncodeKey(b []byte, v ...interface{}) ([]byte, error) { @@ -175,18 +163,6 @@ func Decode(b []byte) ([]interface{}, error) { b, v, err = DecodeBytes(b) case compactBytesFlag: b, v, err = DecodeCompactBytes(b) - case stringFlag: - var r []byte - b, r, err = DecodeBytes(b) - if err == nil { - v = string(r) - } - case compactStringFlag: - var r []byte - b, r, err = DecodeCompactBytes(b) - if err == nil { - v = string(r) - } case decimalFlag: b, v, err = DecodeDecimal(b) case durationFlag: diff --git a/util/codec/codec_test.go b/util/codec/codec_test.go index d90e9ec6e3..ff385e4bda 100644 --- a/util/codec/codec_test.go +++ b/util/codec/codec_test.go @@ -43,7 +43,7 @@ func (s *testCodecSuite) TestCodecKey(c *C) { { []interface{}{float32(1), float64(3.15), []byte("123"), "123"}, - []interface{}{float64(1), float64(3.15), []byte("123"), "123"}, + []interface{}{float64(1), float64(3.15), []byte("123"), []byte("123")}, }, { @@ -495,7 +495,7 @@ func (s *testCodecSuite) TestTime(c *C) { c.Assert(err, IsNil) v, err := Decode(b) c.Assert(err, IsNil) - c.Assert(v, DeepEquals, []interface{}{t}) + c.Assert(v, DeepEquals, []interface{}{[]byte(t)}) } tblCmp := []struct {