Skip to content

Commit

Permalink
util/codec: remove stringFlag
Browse files Browse the repository at this point in the history
  • Loading branch information
disksing committed Dec 11, 2015
1 parent 6595442 commit a66bc59
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 69 deletions.
6 changes: 3 additions & 3 deletions ddl/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,15 @@ 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)
cols, err = tb.Row(ctx, rid3)
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.
Expand All @@ -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.
Expand Down
11 changes: 6 additions & 5 deletions plan/plans/from_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
8 changes: 4 additions & 4 deletions plan/plans/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
23 changes: 13 additions & 10 deletions privilege/privileges/privileges.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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;")
Expand Down Expand Up @@ -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;")
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion table/tables/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
52 changes: 14 additions & 38 deletions util/codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ const (
nilFlag byte = iota
bytesFlag
compactBytesFlag
stringFlag
compactStringFlag
intFlag
uintFlag
floatFlag
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions util/codec/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")},
},

{
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit a66bc59

Please sign in to comment.