github.com/pingcap/br@v5.3.0-alpha.0.20220125034240-ec59c7b6ce30+incompatible/pkg/lightning/backend/kv/kv2sql.go (about)

     1  // Copyright 2021 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package kv
    15  
    16  import (
    17  	"github.com/pingcap/parser/model"
    18  	"github.com/pingcap/tidb/kv"
    19  	"github.com/pingcap/tidb/table"
    20  	"github.com/pingcap/tidb/table/tables"
    21  	"github.com/pingcap/tidb/tablecodec"
    22  	"github.com/pingcap/tidb/types"
    23  
    24  	"github.com/pingcap/br/pkg/lightning/metric"
    25  )
    26  
    27  type TableKVDecoder struct {
    28  	tbl table.Table
    29  	se  *session
    30  }
    31  
    32  func (t *TableKVDecoder) DecodeHandleFromTable(key []byte) (kv.Handle, error) {
    33  	return tablecodec.DecodeRowKey(key)
    34  }
    35  
    36  func (t *TableKVDecoder) EncodeHandleKey(h kv.Handle) kv.Key {
    37  	return tablecodec.EncodeRowKeyWithHandle(t.tbl.Meta().ID, h)
    38  }
    39  
    40  func (t *TableKVDecoder) DecodeHandleFromIndex(indexInfo *model.IndexInfo, key []byte, value []byte) (kv.Handle, error) {
    41  	cols := tables.BuildRowcodecColInfoForIndexColumns(indexInfo, t.tbl.Meta())
    42  	return tablecodec.DecodeIndexHandle(key, value, len(cols))
    43  }
    44  
    45  // DecodeRawRowData decodes raw row data into a datum slice and a (columnID:columnValue) map.
    46  func (t *TableKVDecoder) DecodeRawRowData(h kv.Handle, value []byte) ([]types.Datum, map[int64]types.Datum, error) {
    47  	return tables.DecodeRawRowData(t.se, t.tbl.Meta(), h, t.tbl.Cols(), value)
    48  }
    49  
    50  func NewTableKVDecoder(tbl table.Table, options *SessionOptions) (*TableKVDecoder, error) {
    51  	metric.KvEncoderCounter.WithLabelValues("open").Inc()
    52  	se := newSession(options)
    53  	cols := tbl.Cols()
    54  	// Set CommonAddRecordCtx to session to reuse the slices and BufStore in AddRecord
    55  	recordCtx := tables.NewCommonAddRecordCtx(len(cols))
    56  	tables.SetAddRecordCtx(se, recordCtx)
    57  
    58  	return &TableKVDecoder{
    59  		tbl: tbl,
    60  		se:  se,
    61  	}, nil
    62  }