github.com/matrixorigin/matrixone@v1.2.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  	"context"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    21  	apipb "github.com/matrixorigin/matrixone/pkg/pb/api"
    22  	"github.com/matrixorigin/matrixone/pkg/pb/txn"
    23  )
    24  
    25  // Write implements storage.TxnTAEStorage
    26  func (s *taeStorage) Write(
    27  	ctx context.Context,
    28  	txnMeta txn.TxnMeta,
    29  	op uint32,
    30  	payload []byte) (result []byte, err error) {
    31  	switch op {
    32  	case uint32(apipb.OpCode_OpPreCommit):
    33  		return handleWrite(ctx, txnMeta, payload, s.taeHandler.HandlePreCommitWrite)
    34  	default:
    35  		return nil, moerr.NewNotSupported(ctx, "unknown write op: %v", op)
    36  	}
    37  }
    38  
    39  func handleWrite[PReq unmashaler[Req], PResp mashaler[Resp], Req, Resp any](
    40  	ctx context.Context,
    41  	meta txn.TxnMeta,
    42  	payload []byte,
    43  	fn func(context.Context, txn.TxnMeta, PReq, PResp) error,
    44  ) (res []byte, err error) {
    45  
    46  	var preq PReq = new(Req)
    47  	if err := preq.UnmarshalBinary(payload); err != nil {
    48  		return nil, err
    49  	}
    50  
    51  	var presp PResp = new(Resp)
    52  	defer logReq("write", preq, meta, presp, &err)()
    53  
    54  	err = fn(ctx, meta, preq, presp)
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  
    59  	data, err := presp.MarshalBinary()
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  	res = data
    64  
    65  	return
    66  }