github.com/KinWaiYuen/client-go/v2@v2.5.4/txnkv/transaction/batch_getter.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 40 tikverr "github.com/KinWaiYuen/client-go/v2/error" 41 "github.com/KinWaiYuen/client-go/v2/internal/unionstore" 42 "github.com/pingcap/errors" 43 ) 44 45 // BatchBufferGetter is the interface for BatchGet. 46 type BatchBufferGetter interface { 47 Len() int 48 unionstore.Getter 49 } 50 51 // BatchGetter is the interface for BatchGet. 52 type BatchGetter interface { 53 // BatchGet gets a batch of values. 54 BatchGet(ctx context.Context, keys [][]byte) (map[string][]byte, error) 55 } 56 57 // BufferBatchGetter is the type for BatchGet with MemBuffer. 58 type BufferBatchGetter struct { 59 buffer BatchBufferGetter 60 snapshot BatchGetter 61 } 62 63 // NewBufferBatchGetter creates a new BufferBatchGetter. 64 func NewBufferBatchGetter(buffer BatchBufferGetter, snapshot BatchGetter) *BufferBatchGetter { 65 return &BufferBatchGetter{buffer: buffer, snapshot: snapshot} 66 } 67 68 // BatchGet gets a batch of values. 69 func (b *BufferBatchGetter) BatchGet(ctx context.Context, keys [][]byte) (map[string][]byte, error) { 70 if b.buffer.Len() == 0 { 71 return b.snapshot.BatchGet(ctx, keys) 72 } 73 bufferValues := make([][]byte, len(keys)) 74 shrinkKeys := make([][]byte, 0, len(keys)) 75 for i, key := range keys { 76 val, err := b.buffer.Get(key) 77 if err == nil { 78 bufferValues[i] = val 79 continue 80 } 81 if !tikverr.IsErrNotFound(err) { 82 return nil, errors.Trace(err) 83 } 84 shrinkKeys = append(shrinkKeys, key) 85 } 86 storageValues, err := b.snapshot.BatchGet(ctx, shrinkKeys) 87 if err != nil { 88 return nil, errors.Trace(err) 89 } 90 for i, key := range keys { 91 if len(bufferValues[i]) == 0 { 92 continue 93 } 94 storageValues[string(key)] = bufferValues[i] 95 } 96 return storageValues, nil 97 }