github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/storage/batch_getter.go (about) 1 // Copyright 2022 zGraph Authors. All rights reserved. 2 // 3 // Copyright 2016 PingCAP, Inc. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 package storage 18 19 import ( 20 "context" 21 22 "github.com/vescale/zgraph/storage/kv" 23 ) 24 25 // BatchBufferGetter is the interface for BatchGet. 26 type BatchBufferGetter interface { 27 kv.Getter 28 Len() int 29 } 30 31 // BufferBatchGetter is the type for BatchGet with MemBuffer. 32 type BufferBatchGetter struct { 33 buffer BatchBufferGetter 34 snapshot kv.BatchGetter 35 } 36 37 // NewBufferBatchGetter creates a new BufferBatchGetter. 38 func NewBufferBatchGetter(buffer BatchBufferGetter, snapshot kv.BatchGetter) *BufferBatchGetter { 39 return &BufferBatchGetter{buffer: buffer, snapshot: snapshot} 40 } 41 42 // BatchGet gets a batch of values. 43 func (b *BufferBatchGetter) BatchGet(ctx context.Context, keys []kv.Key) (map[string][]byte, error) { 44 if b.buffer.Len() == 0 { 45 return b.snapshot.BatchGet(ctx, keys) 46 } 47 bufferValues := make([][]byte, len(keys)) 48 shrinkKeys := make([]kv.Key, 0, len(keys)) 49 for i, key := range keys { 50 val, err := b.buffer.Get(ctx, key) 51 if err == nil { 52 bufferValues[i] = val 53 continue 54 } 55 if !kv.IsErrNotFound(err) { 56 return nil, err 57 } 58 shrinkKeys = append(shrinkKeys, key) 59 } 60 storageValues, err := b.snapshot.BatchGet(ctx, shrinkKeys) 61 if err != nil { 62 return nil, err 63 } 64 for i, key := range keys { 65 if len(bufferValues[i]) == 0 { 66 continue 67 } 68 storageValues[string(key)] = bufferValues[i] 69 } 70 return storageValues, nil 71 }