util/codec: remove stringFlag

This commit is contained in:
disksing
2015-12-11 12:20:47 +08:00
parent 65954424d1
commit a66bc590bc
8 changed files with 49 additions and 69 deletions

View File

@ -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.

View File

@ -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{

View File

@ -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{

View File

@ -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

View File

@ -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) {

View File

@ -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:

View File

@ -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:

View File

@ -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 {