diff --git a/inspectkv/inspectkv.go b/inspectkv/inspectkv.go index dc81d76e45..58242bcfce 100644 --- a/inspectkv/inspectkv.go +++ b/inspectkv/inspectkv.go @@ -63,52 +63,32 @@ func GetDDLInfo(txn kv.Transaction) (*DDLInfo, error) { return info, nil } -// GetIndexVals returns index values. -func GetIndexVals(table table.Table, txn kv.Transaction, indexInfo model.IndexInfo, handle int64) ([]interface{}, error) { - vals := make([]interface{}, len(indexInfo.Columns)) - cols := table.Cols() - - for i, col := range indexInfo.Columns { - key := table.RecordKey(handle, cols[col.Offset]) - data, err := txn.Get(key) - if err != nil { - return nil, errors.Trace(err) - } - - val, err := table.DecodeValue(data, cols[col.Offset]) - if err != nil { - return nil, errors.Trace(err) - } - vals[i] = val - } - - return vals, nil -} - -// GetIndexHandles returns index handles. -func GetIndexHandles(table table.Table, txn kv.Transaction, idx *column.IndexedCol) ([]int64, error) { +// GetIndexData returns index handles and index values. +func GetIndexData(table table.Table, txn kv.Transaction, idx *column.IndexedCol) ([]int64, []interface{}, error) { var handles []int64 + var vals []interface{} kvIndex := kv.NewKVIndex(table.IndexPrefix(), idx.Name.L, idx.ID, idx.Unique) it, err := kvIndex.SeekFirst(txn) if err != nil { - return nil, errors.Trace(err) + return nil, nil, errors.Trace(err) } for { - _, h, err := it.Next() + val, h, err := it.Next() if terror.ErrorEqual(err, io.EOF) { break } else if err != nil { - return nil, errors.Trace(err) + return nil, nil, errors.Trace(err) } + handles = append(handles, h) + vals = append(vals, val) } - return handles, nil + return handles, vals, nil } // GetTableData gets table row handles and column values. -// If there is no special iterator, it will be nil. func GetTableData(table table.Table, retriever kv.Retriever) ([]int64, []interface{}, error) { var handles []int64 var data []interface{} diff --git a/inspectkv/inspectkv_test.go b/inspectkv/inspectkv_test.go index 227c126667..783ad15d21 100644 --- a/inspectkv/inspectkv_test.go +++ b/inspectkv/inspectkv_test.go @@ -137,14 +137,18 @@ func (s *testInspectSuite) TestInspect(c *C) { c.Assert(d.(int64), Equals, int64(20)) } - handles, err = GetIndexHandles(tb, txn, indices[0]) + handles, vals, err := GetIndexData(tb, txn, indices[0]) c.Assert(err, IsNil) c.Assert(handles, HasLen, 2) - - vals, err := GetIndexVals(tb, txn, indices[0].IndexInfo, handles[0]) - c.Assert(err, IsNil) - c.Assert(vals, HasLen, 1) - c.Assert(vals[0], Equals, int64(10)) + c.Assert(handles[0], Equals, int64(1)) + c.Assert(handles[1], Equals, int64(2)) + c.Assert(vals, HasLen, 2) + for _, d := range data[0].([]interface{}) { + c.Assert(d.(int64), Equals, int64(10)) + } + for _, d := range data[1].([]interface{}) { + c.Assert(d.(int64), Equals, int64(20)) + } } // mockContext represents mocked context.Context.