github.com/KinWaiYuen/client-go/v2@v2.5.4/txnkv/transaction/batch_getter_test.go (about) 1 // Copyright 2021 TiKV Authors 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 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // NOTE: The code in this file is based on code from the 16 // TiDB project, licensed under the Apache License v 2.0 17 // 18 // https://github.com/pingcap/tidb/tree/cc5e161ac06827589c4966674597c137cc9e809c/store/tikv/tests/snapshot_test.go 19 // 20 21 // Copyright 2016 PingCAP, Inc. 22 // 23 // Licensed under the Apache License, Version 2.0 (the "License"); 24 // you may not use this file except in compliance with the License. 25 // You may obtain a copy of the License at 26 // 27 // http://www.apache.org/licenses/LICENSE-2.0 28 // 29 // Unless required by applicable law or agreed to in writing, software 30 // distributed under the License is distributed on an "AS IS" BASIS, 31 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 32 // See the License for the specific language governing permissions and 33 // limitations under the License. 34 35 package transaction 36 37 import ( 38 "context" 39 "testing" 40 41 tikverr "github.com/KinWaiYuen/client-go/v2/error" 42 "github.com/KinWaiYuen/client-go/v2/kv" 43 "github.com/stretchr/testify/assert" 44 ) 45 46 func TestBufferBatchGetter(t *testing.T) { 47 snap := newMockStore() 48 ka := []byte("a") 49 kb := []byte("b") 50 kc := []byte("c") 51 kd := []byte("d") 52 snap.Set(ka, ka) 53 snap.Set(kb, kb) 54 snap.Set(kc, kc) 55 snap.Set(kd, kd) 56 57 buffer := newMockStore() 58 buffer.Set(ka, []byte("a1")) 59 buffer.Delete(kb) 60 61 batchGetter := NewBufferBatchGetter(buffer, snap) 62 result, err := batchGetter.BatchGet(context.Background(), [][]byte{ka, kb, kc, kd}) 63 assert.NoError(t, err) 64 assert.Equal(t, 3, len(result)) 65 assert.Equal(t, "a1", string(result[string(ka)])) 66 assert.Equal(t, "c", string(result[string(kc)])) 67 assert.Equal(t, "d", string(result[string(kd)])) 68 } 69 70 type mockBatchGetterStore struct { 71 index [][]byte 72 value [][]byte 73 } 74 75 func newMockStore() *mockBatchGetterStore { 76 return &mockBatchGetterStore{ 77 index: make([][]byte, 0), 78 value: make([][]byte, 0), 79 } 80 } 81 82 func (s *mockBatchGetterStore) Len() int { 83 return len(s.index) 84 } 85 86 func (s *mockBatchGetterStore) Get(k []byte) ([]byte, error) { 87 for i, key := range s.index { 88 if kv.CmpKey(key, k) == 0 { 89 return s.value[i], nil 90 } 91 } 92 return nil, tikverr.ErrNotExist 93 } 94 95 func (s *mockBatchGetterStore) BatchGet(ctx context.Context, keys [][]byte) (map[string][]byte, error) { 96 m := make(map[string][]byte) 97 for _, k := range keys { 98 v, err := s.Get(k) 99 if err == nil { 100 m[string(k)] = v 101 continue 102 } 103 if tikverr.IsErrNotFound(err) { 104 continue 105 } 106 return m, err 107 } 108 return m, nil 109 } 110 111 func (s *mockBatchGetterStore) Set(k []byte, v []byte) error { 112 for i, key := range s.index { 113 if kv.CmpKey(key, k) == 0 { 114 s.value[i] = v 115 return nil 116 } 117 } 118 s.index = append(s.index, k) 119 s.value = append(s.value, v) 120 return nil 121 } 122 123 func (s *mockBatchGetterStore) Delete(k []byte) error { 124 s.Set(k, []byte{}) 125 return nil 126 }