Skip to content

Commit

Permalink
types: rename CopyRow and CopyDatum to Clone* (#10333)
Browse files Browse the repository at this point in the history
  • Loading branch information
TennyZhuang authored and jackysp committed May 9, 2019
1 parent 990e256 commit f3ab907
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion executor/insert_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func (e *InsertValues) insertRowsFromSelect(ctx context.Context, exec func(ctx c
}

for innerChunkRow := iter.Begin(); innerChunkRow != iter.End(); innerChunkRow = iter.Next() {
innerRow := types.CopyRow(innerChunkRow.GetDatumRow(fields))
innerRow := types.CloneRow(innerChunkRow.GetDatumRow(fields))
e.rowCount++
row, err := e.getRow(innerRow)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion executor/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func (e *UpdateExec) handleErr(colName model.CIStr, rowIdx int, err error) error
}

func (e *UpdateExec) composeNewRow(rowIdx int, oldRow []types.Datum, cols []*table.Column) ([]types.Datum, error) {
newRowData := types.CopyRow(oldRow)
newRowData := types.CloneRow(oldRow)
e.evalBuffer.SetDatums(newRowData...)
for _, assign := range e.OrderedList {
handleIdx, handleFound := e.columns2Handle.findHandle(int32(assign.Col.Index))
Expand Down
2 changes: 1 addition & 1 deletion expression/aggregation/first_row.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (ff *firstRowFunction) Update(evalCtx *AggEvaluateContext, sc *stmtctx.Stat
if err != nil {
return err
}
evalCtx.Value = types.CopyDatum(value)
evalCtx.Value = types.CloneDatum(value)
evalCtx.GotFirstRow = true
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion expression/aggregation/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func calculateSum(sc *stmtctx.StatementContext, sum, v types.Datum) (data types.
data = types.NewDecimalDatum(d)
}
case types.KindMysqlDecimal:
data = types.CopyDatum(v)
data = types.CloneDatum(v)
default:
var f float64
f, err = v.ToFloat64(sc)
Expand Down
6 changes: 3 additions & 3 deletions statistics/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,17 @@ func (c *SampleCollector) collect(sc *stmtctx.StatementContext, d types.Datum) e
c.TotalSize += int64(len(d.GetBytes()) - 1)
}
c.seenValues++
// The following code use types.CopyDatum(d) because d may have a deep reference
// The following code use types.CloneDatum(d) because d may have a deep reference
// to the underlying slice, GC can't free them which lead to memory leak eventually.
// TODO: Refactor the proto to avoid copying here.
if len(c.Samples) < int(c.MaxSampleSize) {
newItem := &SampleItem{Value: types.CopyDatum(d)}
newItem := &SampleItem{Value: types.CloneDatum(d)}
c.Samples = append(c.Samples, newItem)
} else {
shouldAdd := rand.Int63n(c.seenValues) < c.MaxSampleSize
if shouldAdd {
idx := rand.Intn(int(c.MaxSampleSize))
newItem := &SampleItem{Value: types.CopyDatum(d)}
newItem := &SampleItem{Value: types.CloneDatum(d)}
// To keep the order of the elements, we use delete and append, not direct replacement.
c.Samples = append(c.Samples[:idx], c.Samples[idx+1:]...)
c.Samples = append(c.Samples, newItem)
Expand Down
4 changes: 2 additions & 2 deletions store/mockstore/mocktikv/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func (e *streamAggExec) getPartialResult() ([][]byte, error) {
}
e.currGroupByValues = append(e.currGroupByValues, buf)
}
e.currGroupByRow = types.CopyRow(e.nextGroupByRow)
e.currGroupByRow = types.CloneRow(e.nextGroupByRow)
return append(value, e.currGroupByValues...), nil
}

Expand Down Expand Up @@ -296,7 +296,7 @@ func (e *streamAggExec) meetNewGroup(row [][]byte) (bool, error) {
e.tmpGroupByRow = append(e.tmpGroupByRow, d)
}
if firstGroup {
e.currGroupByRow = types.CopyRow(e.tmpGroupByRow)
e.currGroupByRow = types.CloneRow(e.tmpGroupByRow)
}
if matched {
return false, nil
Expand Down
8 changes: 4 additions & 4 deletions types/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -1835,14 +1835,14 @@ func DatumsToStrNoErr(datums []Datum) string {
return str
}

// CopyDatum returns a new copy of the datum.
// CloneDatum returns a new copy of the datum.
// TODO: Abandon this function.
func CopyDatum(datum Datum) Datum {
func CloneDatum(datum Datum) Datum {
return *datum.Copy()
}

// CopyRow deep copies a Datum slice.
func CopyRow(dr []Datum) []Datum {
// CloneRow deep copies a Datum slice.
func CloneRow(dr []Datum) []Datum {
c := make([]Datum, len(dr))
for i, d := range dr {
c[i] = *d.Copy()
Expand Down
4 changes: 2 additions & 2 deletions types/datum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ func (ts *testDatumSuite) TestComputePlusAndMinus(c *C) {
}
}

func (ts *testDatumSuite) TestCopyDatum(c *C) {
func (ts *testDatumSuite) TestCloneDatum(c *C) {
var raw Datum
raw.b = []byte("raw")
raw.k = KindRaw
Expand All @@ -366,7 +366,7 @@ func (ts *testDatumSuite) TestCopyDatum(c *C) {
sc := new(stmtctx.StatementContext)
sc.IgnoreTruncate = true
for _, tt := range tests {
tt1 := CopyDatum(tt)
tt1 := CloneDatum(tt)
res, err := tt.CompareDatum(sc, &tt1)
c.Assert(err, IsNil)
c.Assert(res, Equals, 0)
Expand Down

0 comments on commit f3ab907

Please sign in to comment.