github.com/matrixorigin/matrixone@v0.7.0/pkg/txn/storage/tae/read.go (about) 1 // Copyright 2021 - 2022 Matrix Origin 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 package taestorage 16 17 import ( 18 "bytes" 19 "context" 20 "encoding/gob" 21 "github.com/matrixorigin/matrixone/pkg/common/moerr" 22 apipb "github.com/matrixorigin/matrixone/pkg/pb/api" 23 "github.com/matrixorigin/matrixone/pkg/pb/txn" 24 "github.com/matrixorigin/matrixone/pkg/txn/storage" 25 "io" 26 ) 27 28 // Read implements storage.TxnTAEStorage 29 30 func (s *taeStorage) Read( 31 ctx context.Context, 32 txnMeta txn.TxnMeta, 33 op uint32, 34 payload []byte) (res storage.ReadResult, err error) { 35 36 switch op { 37 38 case uint32(apipb.OpCode_OpGetLogTail): 39 return handleRead( 40 ctx, s, 41 txnMeta, payload, 42 s.taeHandler.HandleGetLogTail, 43 ) 44 default: 45 panic(moerr.NewInfoNoCtx("op is not supported")) 46 } 47 48 } 49 50 func handleRead[Req any, Resp any]( 51 ctx context.Context, 52 s *taeStorage, 53 txnMeta txn.TxnMeta, 54 payload []byte, 55 fn func( 56 ctx context.Context, 57 meta txn.TxnMeta, 58 req Req, 59 resp *Resp, 60 ) ( 61 err error, 62 ), 63 ) ( 64 res storage.ReadResult, 65 err error, 66 ) { 67 68 var req Req 69 if len(payload) != 0 { 70 if err := gob.NewDecoder(bytes.NewReader(payload)).Decode(&req); err != nil { 71 return nil, err 72 } 73 } 74 75 var resp Resp 76 defer logReq("read", req, txnMeta, &resp, &err)() 77 defer func() { 78 if closer, ok := (any)(resp).(io.Closer); ok { 79 _ = closer.Close() 80 } 81 }() 82 83 err = fn(ctx, txnMeta, req, &resp) 84 if err != nil { 85 return nil, err 86 } 87 88 buf := new(bytes.Buffer) 89 if err := gob.NewEncoder(buf).Encode(resp); err != nil { 90 return nil, err 91 } 92 res = &readResult{ 93 payload: buf.Bytes(), 94 } 95 96 return res, nil 97 }