github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/txn/txnimpl/cmdmgr.go (about)

     1  // Copyright 2021 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 txnimpl
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/logutil"
    19  	// "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common"
    20  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/txnif"
    21  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/logstore/entry"
    22  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/txn/txnbase"
    23  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/wal"
    24  )
    25  
    26  type commandManager struct {
    27  	cmd    *txnbase.TxnCmd
    28  	lsn    uint64
    29  	csn    uint32
    30  	driver wal.Driver
    31  }
    32  
    33  func newCommandManager(driver wal.Driver) *commandManager {
    34  	return &commandManager{
    35  		cmd:    txnbase.NewTxnCmd(),
    36  		driver: driver,
    37  	}
    38  }
    39  
    40  func (mgr *commandManager) GetCSN() uint32 {
    41  	return mgr.csn
    42  }
    43  
    44  func (mgr *commandManager) AddInternalCmd(cmd txnif.TxnCmd) {
    45  	mgr.cmd.AddCmd(cmd)
    46  }
    47  
    48  func (mgr *commandManager) AddCmd(cmd txnif.TxnCmd) {
    49  	mgr.cmd.AddCmd(cmd)
    50  	mgr.csn++
    51  }
    52  
    53  func (mgr *commandManager) MakeLogIndex(csn uint32) *wal.Index {
    54  	return &wal.Index{LSN: mgr.lsn, CSN: csn, Size: mgr.csn}
    55  }
    56  
    57  func (mgr *commandManager) ApplyTxnRecord(tid string, txn txnif.AsyncTxn) (logEntry entry.Entry, err error) {
    58  	if mgr.driver == nil {
    59  		return
    60  	}
    61  	mgr.cmd.SetCmdSize(mgr.csn)
    62  	mgr.cmd.SetTxn(txn)
    63  	var buf []byte
    64  	if buf, err = mgr.cmd.Marshal(); err != nil {
    65  		return
    66  	}
    67  	// logutil.Info("", common.OperationField("suxi-replay-cmd"),
    68  	// common.OperandField(mgr.cmd.Desc()))
    69  	logEntry = entry.GetBase()
    70  	logEntry.SetType(ETTxnRecord)
    71  	if err = logEntry.SetPayload(buf); err != nil {
    72  		return
    73  	}
    74  	info := &entry.Info{
    75  		Group: wal.GroupPrepare,
    76  		TxnId: tid,
    77  	}
    78  	logEntry.SetInfo(info)
    79  	mgr.lsn, err = mgr.driver.AppendEntry(wal.GroupPrepare, logEntry)
    80  	logutil.Debugf("ApplyTxnRecord LSN=%d, Size=%d", mgr.lsn, len(buf))
    81  	return
    82  }