github.com/matrixorigin/matrixone@v0.7.0/pkg/txn/storage/tae/write.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 ) 25 26 // Write implements storage.TxnTAEStorage 27 28 func (s *taeStorage) Write( 29 ctx context.Context, 30 txnMeta txn.TxnMeta, 31 op uint32, 32 payload []byte) (result []byte, err error) { 33 34 switch op { 35 36 case uint32(apipb.OpCode_OpPreCommit): 37 38 return handleWrite( 39 ctx, s, 40 txnMeta, payload, 41 s.taeHandler.HandlePreCommitWrite, 42 ) 43 44 default: 45 panic(moerr.NewInfoNoCtx("OpCode is not supported")) 46 47 } 48 49 } 50 51 func handleWrite[ 52 Req any, 53 Resp any, 54 ]( 55 ctx context.Context, 56 s *taeStorage, 57 meta txn.TxnMeta, 58 payload []byte, 59 fn func( 60 ctx context.Context, 61 meta txn.TxnMeta, 62 req Req, 63 resp *Resp, 64 ) ( 65 err error, 66 ), 67 ) ( 68 res []byte, 69 err error, 70 ) { 71 72 var req Req 73 if err := gob.NewDecoder(bytes.NewReader(payload)).Decode(&req); err != nil { 74 return nil, err 75 } 76 77 var resp Resp 78 defer logReq("write", req, meta, &resp, &err)() 79 80 err = fn(ctx, meta, req, &resp) 81 if err != nil { 82 return nil, err 83 } 84 85 buf := new(bytes.Buffer) 86 if err := gob.NewEncoder(buf).Encode(resp); err != nil { 87 return nil, err 88 } 89 res = buf.Bytes() 90 91 return 92 }