github.com/matrixorigin/matrixone@v1.2.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 "context" 19 "encoding" 20 "io" 21 22 "github.com/matrixorigin/matrixone/pkg/common/moerr" 23 apipb "github.com/matrixorigin/matrixone/pkg/pb/api" 24 "github.com/matrixorigin/matrixone/pkg/pb/txn" 25 "github.com/matrixorigin/matrixone/pkg/txn/storage" 26 ) 27 28 // Read implements storage.TxnTAEStorage 29 func (s *taeStorage) Read( 30 ctx context.Context, 31 txnMeta txn.TxnMeta, 32 op uint32, 33 payload []byte) (res storage.ReadResult, err error) { 34 switch op { 35 case uint32(apipb.OpCode_OpGetLogTail): 36 return handleRead(ctx, txnMeta, payload, s.taeHandler.HandleGetLogTail) 37 default: 38 return nil, moerr.NewNotSupported(ctx, "known read op: %v", op) 39 } 40 } 41 42 type unmashaler[T any] interface { 43 *T 44 encoding.BinaryUnmarshaler 45 } 46 47 type mashaler[T any] interface { 48 *T 49 encoding.BinaryMarshaler 50 } 51 52 func handleRead[PReq unmashaler[Req], PResp mashaler[Resp], Req, Resp any]( 53 ctx context.Context, 54 txnMeta txn.TxnMeta, 55 payload []byte, 56 fn func(context.Context, txn.TxnMeta, PReq, PResp) (func(), error), 57 ) (res storage.ReadResult, err error) { 58 59 var preq PReq = new(Req) 60 if len(payload) != 0 { 61 if err := preq.UnmarshalBinary(payload); err != nil { 62 return nil, err 63 } 64 } 65 66 var presp PResp = new(Resp) 67 defer logReq("read", preq, txnMeta, presp, &err)() 68 defer func() { 69 if closer, ok := (any)(presp).(io.Closer); ok { 70 _ = closer.Close() 71 } 72 }() 73 74 cb, err := fn(ctx, txnMeta, preq, presp) 75 if cb != nil { 76 defer cb() 77 } 78 if err != nil { 79 return nil, err 80 } 81 82 data, err := presp.MarshalBinary() 83 if err != nil { 84 return nil, err 85 } 86 res = &readResult{ 87 payload: data, 88 } 89 90 return res, nil 91 }