Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tiny clean up util package. #308

Merged
merged 3 commits into from
Oct 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions util/arena/arena.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@

package arena

// Allocator pre-allocates memory to
// reduce memory allocation cost.
// Allocator pre-allocates memory to reduce memory allocation cost.
// It is not thread-safe.
type Allocator interface {
// Alloc allocates memory with 0 len and n cap.
// Alloc allocates memory with 0 len and capacity cap.
Alloc(capacity int) []byte

// AllocWithLen allocates memory with length and capacity.
AllocWithLen(length int, capacity int) []byte

// Reset resets arena offset, make sure all the allocated memory are not used any more.
// Reset resets arena offset.
// Make sure all the allocated memory are not used any more.
Reset()
}

// SimpleAllocator is a simple implementation of ArenaAllocator
// SimpleAllocator is a simple implementation of ArenaAllocator.
type SimpleAllocator struct {
arena []byte
off int
Expand All @@ -36,8 +36,8 @@ type SimpleAllocator struct {
type stdAllocator struct {
}

func (a *stdAllocator) Alloc(n int) []byte {
return make([]byte, 0, n)
func (a *stdAllocator) Alloc(capacity int) []byte {
return make([]byte, 0, capacity)
}

func (a *stdAllocator) AllocWithLen(length int, capacity int) []byte {
Expand All @@ -58,14 +58,14 @@ func NewAllocator(capacity int) *SimpleAllocator {
}

// Alloc implements Allocator.AllocBytes interface.
func (s *SimpleAllocator) Alloc(capcity int) []byte {
if s.off+capcity < cap(s.arena) {
slice := s.arena[s.off:s.off : s.off+capcity]
s.off += capcity
func (s *SimpleAllocator) Alloc(capacity int) []byte {
if s.off+capacity < cap(s.arena) {
slice := s.arena[s.off:s.off : s.off+capacity]
s.off += capacity
return slice
}

return make([]byte, 0, capcity)
return make([]byte, 0, capacity)
}

// AllocWithLen implements Allocator.AllocWithLen interface.
Expand Down
2 changes: 1 addition & 1 deletion util/charset/charset.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Collation struct {

var charsets = make(map[string]*Charset)

// All the supported charsets should in the following table.
// All the supported charsets should be in the following table.
var charsetInfos = []*Charset{
{"utf8", nil, make(map[string]*Collation), "UTF-8 Unicode", 3},
{"latin1", nil, make(map[string]*Collation), "cp1252 West European", 1},
Expand Down
4 changes: 2 additions & 2 deletions util/mock/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

var _ context.Context = (*Context)(nil)

// Context is a mock context.Context for test.
// Context represents mocked context.Context.
type Context struct {
values map[fmt.Stringer]interface{}
}
Expand Down Expand Up @@ -54,7 +54,7 @@ func (c *Context) FinishTxn(rollback bool) error {
return nil
}

// NewContext creates a mock context.Context.
// NewContext creates a new mocked context.Context.
func NewContext() context.Context {
return &Context{
values: make(map[fmt.Stringer]interface{}),
Expand Down
6 changes: 3 additions & 3 deletions util/mock/mocks/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ import (
"github.com/pingcap/tidb/util/format"
)

// Plan mocks a plan.Plan instance
// Plan represents mocked plan.Plan.
type Plan struct {
rset *Recordset
rows [][]interface{}
cursor int
}

// NewPlan creates a new MockPlan
// NewPlan creates a new mocked plan.Plan.
func NewPlan(rset *Recordset) *Plan {
return &Plan{rset: rset}
}
Expand Down Expand Up @@ -60,7 +60,7 @@ func (p *Plan) Next(ctx context.Context) (row *plan.Row, err error) {
return
}

// Close implements plan.Plan Close interface{}
// Close implements plan.Plan Close interface.
func (p *Plan) Close() error {
p.cursor = 0
return nil
Expand Down
12 changes: 6 additions & 6 deletions util/mock/mocks/recordset.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ import (
"github.com/pingcap/tidb/plan"
)

// Recordset represents mock Recordsets
// Recordset represents mocked rset.Recordset.
type Recordset struct {
rows [][]interface{}
fields []string
offset int
cursor int
}

// NewRecordset creates a new mocked Recordset.
// NewRecordset creates a new mocked rset.Recordset.
func NewRecordset(rows [][]interface{}, fields []string, offset int) *Recordset {
return &Recordset{
rows: rows,
Expand Down Expand Up @@ -58,12 +58,12 @@ func (r *Recordset) Fields() ([]*field.ResultField, error) {
return ret[:r.offset], nil
}

// FirstRow implements rset.Recordset.
// FirstRow implements rset.Recordset FirstRow interface.
func (r *Recordset) FirstRow() ([]interface{}, error) {
return r.rows[0], nil
}

// Rows implements rset.Recordset.
// Rows implements rset.Recordset Rows interface.
func (r *Recordset) Rows(limit, offset int) ([][]interface{}, error) {
var ret [][]interface{}
for _, row := range r.rows {
Expand All @@ -78,7 +78,7 @@ func (r *Recordset) SetFieldOffset(offset int) {
r.offset = offset
}

// Next implements rset.Recordset.
// Next implements rset.Recordset Next interface.
func (r *Recordset) Next() (row *plan.Row, err error) {
if r.cursor == len(r.rows) {
return
Expand All @@ -88,7 +88,7 @@ func (r *Recordset) Next() (row *plan.Row, err error) {
return
}

// Close implements rset.Recordset.
// Close implements rset.Recordset Close interface.
func (r *Recordset) Close() error {
r.cursor = 0
return nil
Expand Down
2 changes: 1 addition & 1 deletion util/mock/mocks/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/pingcap/tidb/util/format"
)

// Statement represents a mocked Statement.
// Statement represents mocked stmt.Statement.
type Statement struct {
text string
Rset *Recordset
Expand Down
15 changes: 8 additions & 7 deletions util/prefix_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ func hasPrefix(prefix []byte) kv.FnKeyCmp {
func ScanMetaWithPrefix(txn kv.Transaction, prefix string, filter func([]byte, []byte) bool) error {
iter, err := txn.Seek([]byte(prefix), hasPrefix([]byte(prefix)))
if err != nil {
return err
return errors.Trace(err)
}
defer iter.Close()

for {
if err != nil {
return err
return errors.Trace(err)
}

if iter.Valid() && strings.HasPrefix(iter.Key(), prefix) {
Expand All @@ -61,21 +62,21 @@ func ScanMetaWithPrefix(txn kv.Transaction, prefix string, filter func([]byte, [

// DelKeyWithPrefix deletes keys with prefix.
func DelKeyWithPrefix(ctx context.Context, prefix string) error {
log.Debug("delKeyWithPrefix", prefix)
txn, err := ctx.GetTxn(false)
if err != nil {
return err
return errors.Trace(err)
}

var keys []string
iter, err := txn.Seek([]byte(prefix), hasPrefix([]byte(prefix)))
if err != nil {
return err
return errors.Trace(err)
}

defer iter.Close()
for {
if err != nil {
return err
return errors.Trace(err)
}

if iter.Valid() && strings.HasPrefix(iter.Key(), prefix) {
Expand All @@ -89,7 +90,7 @@ func DelKeyWithPrefix(ctx context.Context, prefix string) error {
for _, key := range keys {
err := txn.Delete([]byte(key))
if err != nil {
return err
return errors.Trace(err)
}
}

Expand Down
31 changes: 15 additions & 16 deletions util/types/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func CompareFloat64(x, y float64) int {
return 1
}

// CompareInteger returns an integer comparing the int64 x to the uint64 y.
// CompareInteger returns an integer comparing the int64 x to the uint64 y.
func CompareInteger(x int64, y uint64) int {
if x < 0 {
return -1
Expand All @@ -74,24 +74,24 @@ func CompareString(x, y string) int {
return 1
}

// compareFloatString compares float a to float-formated string s.
// compareFloatString compares float a with string s.
// compareFloatString first parses s to a float value, if failed, returns error.
func compareFloatString(a float64, s string) (int, error) {
// MySQL will convert string to a float point value
// MySQL use a very loose conversation, e.g, 123.abc -> 123
// we should do a trade off whether supporting this feature or using a strict mode
// now we use a strict mode
// MySQL will convert string to a float point value.
// MySQL uses a very loose conversation, e.g, 123.abc -> 123
// We should do a trade off whether supporting this feature or using a strict mode.
// Now we use a strict mode.
b, err := StrToFloat(s)
if err != nil {
return 0, err
return 0, errors.Trace(err)
}
return CompareFloat64(a, b), nil
}

// compareStringFloat compares float-formated string s to float a.
// compareStringFloat compares string s with float a.
func compareStringFloat(s string, a float64) (int, error) {
n, err := compareFloatString(a, s)
return -n, err
return -n, errors.Trace(err)
}

func coerceCompare(a, b interface{}) (x interface{}, y interface{}, err error) {
Expand All @@ -117,7 +117,7 @@ func coerceCompare(a, b interface{}) (x interface{}, y interface{}, err error) {
err = errors.Errorf("invalid comapre type %T cmp %T", a, b)
}

return x, y, err
return x, y, errors.Trace(err)
}

func compareRow(a, b []interface{}) (int, error) {
Expand All @@ -136,7 +136,7 @@ func compareRow(a, b []interface{}) (int, error) {
return 0, nil
}

// Compare returns an integer comparing the interface a to b.
// Compare returns an integer comparing the interface a with b.
// a > b -> 1
// a = b -> 0
// a < b -> -1
Expand Down Expand Up @@ -166,7 +166,6 @@ func Compare(a, b interface{}) (int, error) {
}

// TODO: support compare time type with other int, float, decimal types.
// TODO: support hexadecimal type
switch x := a.(type) {
case float64:
switch y := b.(type) {
Expand Down Expand Up @@ -216,7 +215,7 @@ func Compare(a, b interface{}) (int, error) {
case string:
f, err := mysql.ConvertToDecimal(y)
if err != nil {
return 0, err
return 0, errors.Trace(err)
}
return x.Cmp(f), nil
}
Expand All @@ -233,15 +232,15 @@ func Compare(a, b interface{}) (int, error) {
case mysql.Decimal:
f, err := mysql.ConvertToDecimal(x)
if err != nil {
return 0, err
return 0, errors.Trace(err)
}
return f.Cmp(y), nil
case mysql.Time:
n, err := y.CompareString(x)
return -n, err
return -n, errors.Trace(err)
case mysql.Duration:
n, err := y.CompareString(x)
return -n, err
return -n, errors.Trace(err)
case mysql.Hex:
return CompareString(x, y.ToString()), nil
case mysql.Bit:
Expand Down
Loading