github.com/pingcap/br@v5.3.0-alpha.0.20220125034240-ec59c7b6ce30+incompatible/pkg/lightning/backend/local/key_adapter_test.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 local 15 16 import ( 17 "bytes" 18 "crypto/rand" 19 "math" 20 "sort" 21 22 . "github.com/pingcap/check" 23 ) 24 25 func randBytes(n int) []byte { 26 b := make([]byte, n) 27 rand.Read(b) 28 return b 29 } 30 31 type noopKeyAdapterSuite struct { 32 keyAdapter KeyAdapter 33 } 34 35 var _ = Suite(&noopKeyAdapterSuite{}) 36 37 func (s *noopKeyAdapterSuite) SetUpSuite(c *C) { 38 s.keyAdapter = noopKeyAdapter{} 39 } 40 41 func (s *noopKeyAdapterSuite) TestBasic(c *C) { 42 key := randBytes(32) 43 c.Assert(s.keyAdapter.EncodedLen(key), Equals, len(key)) 44 encodedKey := s.keyAdapter.Encode(nil, key, 0, 0) 45 c.Assert(encodedKey, BytesEquals, key) 46 47 decodedKey, _, _, err := s.keyAdapter.Decode(nil, encodedKey) 48 c.Assert(err, IsNil) 49 c.Assert(decodedKey, BytesEquals, key) 50 } 51 52 type duplicateKeyAdapterSuite struct { 53 keyAdapter KeyAdapter 54 } 55 56 var _ = Suite(&duplicateKeyAdapterSuite{}) 57 58 func (s *duplicateKeyAdapterSuite) SetUpSuite(c *C) { 59 s.keyAdapter = duplicateKeyAdapter{} 60 } 61 62 func (s *duplicateKeyAdapterSuite) TestBasic(c *C) { 63 inputs := []struct { 64 key []byte 65 rowID int64 66 offset int64 67 }{ 68 { 69 []byte{0x0}, 70 0, 71 0, 72 }, 73 { 74 randBytes(32), 75 1, 76 -2034, 77 }, 78 { 79 randBytes(32), 80 1, 81 math.MaxInt64, 82 }, 83 { 84 randBytes(32), 85 math.MaxInt32, 86 math.MinInt64, 87 }, 88 { 89 randBytes(32), 90 math.MinInt32, 91 2345678, 92 }, 93 } 94 95 for _, input := range inputs { 96 result := s.keyAdapter.Encode(nil, input.key, input.rowID, input.offset) 97 98 // Decode the result. 99 key, rowID, offset, err := s.keyAdapter.Decode(nil, result) 100 c.Assert(err, IsNil) 101 c.Assert(key, BytesEquals, input.key) 102 c.Assert(rowID, Equals, input.rowID) 103 c.Assert(offset, Equals, input.offset) 104 } 105 } 106 107 func (s *duplicateKeyAdapterSuite) TestKeyOrder(c *C) { 108 keys := [][]byte{ 109 {0x0, 0x1, 0x2}, 110 {0x0, 0x1, 0x3}, 111 {0x0, 0x1, 0x3, 0x4}, 112 {0x0, 0x1, 0x3, 0x4, 0x0}, 113 {0x0, 0x1, 0x3, 0x4, 0x0, 0x0, 0x0}, 114 } 115 keyAdapter := duplicateKeyAdapter{} 116 var encodedKeys [][]byte 117 for i, key := range keys { 118 encodedKeys = append(encodedKeys, keyAdapter.Encode(nil, key, 1, int64(i*1234))) 119 } 120 sorted := sort.SliceIsSorted(encodedKeys, func(i, j int) bool { 121 return bytes.Compare(encodedKeys[i], encodedKeys[j]) < 0 122 }) 123 c.Assert(sorted, IsTrue) 124 } 125 126 func (s *duplicateKeyAdapterSuite) TestEncodeKeyWithBuf(c *C) { 127 key := randBytes(32) 128 buf := make([]byte, 256) 129 buf2 := s.keyAdapter.Encode(buf, key, 1, 1234) 130 // Verify the encode result first. 131 key2, _, _, err := s.keyAdapter.Decode(nil, buf2) 132 c.Assert(err, IsNil) 133 c.Assert(key2, BytesEquals, key) 134 // There should be no new slice allocated. 135 // If we change a byte in `buf`, `buf2` can read the new byte. 136 c.Assert(buf[:len(buf2)], BytesEquals, buf2) 137 buf[0]++ 138 c.Assert(buf[0], Equals, buf2[0]) 139 } 140 141 func (s *duplicateKeyAdapterSuite) TestDecodeKeyWithBuf(c *C) { 142 data := []byte{ 143 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0xff, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf7, 144 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 145 } 146 buf := make([]byte, len(data)) 147 key, _, _, err := s.keyAdapter.Decode(buf, data) 148 c.Assert(err, IsNil) 149 // There should be no new slice allocated. 150 // If we change a byte in `buf`, `buf2` can read the new byte. 151 c.Assert(buf, BytesEquals, key[:len(buf)]) 152 buf[0]++ 153 c.Assert(buf[0], Equals, key[0]) 154 }