func (d *ddl) buildIndex(ctx context.Context, t table.Table, idxInfo *model.IndexInfo, unique bool) error { firstKey := t.FirstKey() prefix := t.KeyPrefix() txn, err := ctx.GetTxn(false) if err != nil { return errors.Trace(err) } it, err := txn.Seek([]byte(firstKey)) if err != nil { return errors.Trace(err) } defer it.Close() for it.Valid() && strings.HasPrefix(it.Key(), prefix) { var err error handle, err := util.DecodeHandleFromRowKey(it.Key()) log.Info("building index...", handle) if err != nil { return errors.Trace(err) } // TODO: v is timestamp ? // fetch datas cols := t.Cols() var vals []interface{} for _, v := range idxInfo.Columns { var ( data []byte val interface{} ) col := cols[v.Offset] k := t.RecordKey(handle, col) data, err = txn.Get([]byte(k)) if err != nil { return errors.Trace(err) } val, err = t.DecodeValue(data, col) if err != nil { return errors.Trace(err) } vals = append(vals, val) } // build index kvX := kv.NewKVIndex(t.IndexPrefix(), idxInfo.Name.L, unique) err = kvX.Create(txn, vals, handle) if err != nil { return errors.Trace(err) } rk := []byte(t.RecordKey(handle, nil)) it, err = kv.NextUntil(it, util.RowKeyPrefixFilter(rk)) if err != nil { return errors.Trace(err) } } return nil }
func (s *testColumnSuite) checkColumnKVExist(c *C, ctx context.Context, t table.Table, handle int64, col *column.Col, columnValue interface{}, isExist bool) { txn, err := ctx.GetTxn(true) c.Assert(err, IsNil) key := t.RecordKey(handle, col) data, err := txn.Get(key) if isExist { c.Assert(err, IsNil) v, err1 := t.DecodeValue(data, col) c.Assert(err1, IsNil) value, err1 := types.Convert(v, &col.FieldType) c.Assert(err1, IsNil) c.Assert(value, Equals, columnValue) } else { c.Assert(err, NotNil) } err = ctx.FinishTxn(false) c.Assert(err, IsNil) }
func fetchRowColVals(txn kv.Transaction, t table.Table, handle int64, indexInfo *model.IndexInfo) ([]interface{}, error) { // fetch datas cols := t.Cols() var vals []interface{} for _, v := range indexInfo.Columns { var val interface{} col := cols[v.Offset] k := t.RecordKey(handle, col) data, err := txn.Get(k) if err != nil { return nil, errors.Trace(err) } val, err = t.DecodeValue(data, col) if err != nil { return nil, errors.Trace(err) } vals = append(vals, val) } return vals, nil }