github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/cdc/entry/codec_test.go (about) 1 // Copyright 2020 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 entry 15 16 import ( 17 "testing" 18 19 "github.com/pingcap/check" 20 "github.com/pingcap/ticdc/pkg/util/testleak" 21 "github.com/pingcap/tidb/kv" 22 "github.com/pingcap/tidb/tablecodec" 23 "github.com/pingcap/tidb/util/codec" 24 ) 25 26 func Test(t *testing.T) { check.TestingT(t) } 27 28 type codecSuite struct { 29 } 30 31 var _ = check.Suite(&codecSuite{}) 32 33 func (s *codecSuite) TestDecodeRecordKey(c *check.C) { 34 defer testleak.AfterTest(c)() 35 recordPrefix := tablecodec.GenTableRecordPrefix(12345) 36 key := tablecodec.EncodeRecordKey(recordPrefix, kv.IntHandle(67890)) 37 key, tableID, err := decodeTableID(key) 38 c.Assert(err, check.IsNil) 39 c.Assert(tableID, check.Equals, int64(12345)) 40 key, recordID, err := decodeRecordID(key) 41 c.Assert(err, check.IsNil) 42 c.Assert(recordID, check.Equals, int64(67890)) 43 c.Assert(len(key), check.Equals, 0) 44 } 45 46 type decodeMetaKeySuite struct { 47 } 48 49 var _ = check.Suite(&decodeMetaKeySuite{}) 50 51 func (s *decodeMetaKeySuite) TestDecodeListData(c *check.C) { 52 defer testleak.AfterTest(c)() 53 key := []byte("hello") 54 var index int64 = 3 55 56 meta, err := decodeMetaKey(buildMetaKey(key, index)) 57 c.Assert(err, check.IsNil) 58 c.Assert(meta.getType(), check.Equals, ListData) 59 list := meta.(metaListData) 60 c.Assert(list.key, check.Equals, string(key)) 61 c.Assert(list.index, check.Equals, index) 62 } 63 64 func buildMetaKey(key []byte, index int64) []byte { 65 ek := make([]byte, 0, len(metaPrefix)+len(key)+36) 66 ek = append(ek, metaPrefix...) 67 ek = codec.EncodeBytes(ek, key) 68 ek = codec.EncodeUint(ek, uint64(ListData)) 69 return codec.EncodeInt(ek, index) 70 }