github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/tables/updates/append.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 updates
    16  
    17  import (
    18  	"encoding/binary"
    19  	"fmt"
    20  	"io"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/container/types"
    23  
    24  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/txnif"
    26  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/txn/txnbase"
    27  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/wal"
    28  )
    29  
    30  type AppendNode struct {
    31  	*txnbase.TxnMVCCNode
    32  	startRow uint32
    33  	maxRow   uint32
    34  	mvcc     *MVCCHandle
    35  	id       *common.ID
    36  }
    37  
    38  func CompareAppendNode(e, o txnif.MVCCNode) int {
    39  	return e.(*AppendNode).Compare(o.(*AppendNode).TxnMVCCNode)
    40  }
    41  
    42  func MockAppendNode(ts types.TS, startRow, maxRow uint32, mvcc *MVCCHandle) *AppendNode {
    43  	return &AppendNode{
    44  		TxnMVCCNode: &txnbase.TxnMVCCNode{
    45  			Start: ts,
    46  			End:   ts,
    47  		},
    48  		maxRow: maxRow,
    49  		mvcc:   mvcc,
    50  	}
    51  }
    52  
    53  func NewCommittedAppendNode(
    54  	ts types.TS,
    55  	startRow, maxRow uint32,
    56  	mvcc *MVCCHandle) *AppendNode {
    57  	return &AppendNode{
    58  		TxnMVCCNode: &txnbase.TxnMVCCNode{
    59  			Start:   ts,
    60  			Prepare: ts,
    61  			End:     ts,
    62  		},
    63  		startRow: startRow,
    64  		maxRow:   maxRow,
    65  		mvcc:     mvcc,
    66  	}
    67  }
    68  
    69  func NewAppendNode(
    70  	txn txnif.AsyncTxn,
    71  	startRow, maxRow uint32,
    72  	mvcc *MVCCHandle) *AppendNode {
    73  	var startTs, ts types.TS
    74  	if txn != nil {
    75  		startTs = txn.GetStartTS()
    76  		ts = txn.GetPrepareTS()
    77  	}
    78  	n := &AppendNode{
    79  		TxnMVCCNode: &txnbase.TxnMVCCNode{
    80  			Start:   startTs,
    81  			Prepare: ts,
    82  			End:     txnif.UncommitTS,
    83  			Txn:     txn,
    84  		},
    85  		startRow: startRow,
    86  		maxRow:   maxRow,
    87  		mvcc:     mvcc,
    88  	}
    89  	return n
    90  }
    91  
    92  func NewEmptyAppendNode() txnif.MVCCNode {
    93  	return &AppendNode{
    94  		TxnMVCCNode: &txnbase.TxnMVCCNode{},
    95  	}
    96  }
    97  func (node *AppendNode) Close() {
    98  	node.mvcc = nil
    99  }
   100  func (node *AppendNode) String() string {
   101  	return node.GeneralDesc()
   102  }
   103  func (node *AppendNode) CloneAll() txnif.MVCCNode {
   104  	panic("todo")
   105  }
   106  func (node *AppendNode) CloneData() txnif.MVCCNode {
   107  	panic("todo")
   108  }
   109  func (node *AppendNode) Update(txnif.MVCCNode) {
   110  	panic("todo")
   111  }
   112  func (node *AppendNode) GeneralDesc() string {
   113  	return fmt.Sprintf("%s;StartRow=%d MaxRow=%d", node.TxnMVCCNode.String(), node.startRow, node.maxRow)
   114  }
   115  func (node *AppendNode) GeneralString() string {
   116  	return node.GeneralDesc()
   117  }
   118  func (node *AppendNode) GeneralVerboseString() string {
   119  	return node.GeneralDesc()
   120  }
   121  
   122  func (node *AppendNode) SetLogIndex(idx *wal.Index) {
   123  	node.TxnMVCCNode.SetLogIndex(idx)
   124  }
   125  func (node *AppendNode) GetID() *common.ID {
   126  	return node.id
   127  }
   128  func (node *AppendNode) GetCommitTS() types.TS {
   129  	return node.GetEnd()
   130  }
   131  func (node *AppendNode) GetStartRow() uint32 { return node.startRow }
   132  func (node *AppendNode) GetMaxRow() uint32 {
   133  	return node.maxRow
   134  }
   135  func (node *AppendNode) SetMaxRow(row uint32) {
   136  	node.maxRow = row
   137  }
   138  
   139  func (node *AppendNode) PrepareCommit() error {
   140  	node.mvcc.Lock()
   141  	defer node.mvcc.Unlock()
   142  	_, err := node.TxnMVCCNode.PrepareCommit()
   143  	return err
   144  }
   145  
   146  func (node *AppendNode) ApplyCommit(index *wal.Index) error {
   147  	node.mvcc.Lock()
   148  	defer node.mvcc.Unlock()
   149  	if node.IsCommitted() {
   150  		panic("AppendNode | ApplyCommit | LogicErr")
   151  	}
   152  	node.TxnMVCCNode.ApplyCommit(index)
   153  	// logutil.Infof("Apply1Index %s TS=%d", index.String(), n.commitTs)
   154  	listener := node.mvcc.GetAppendListener()
   155  	if listener == nil {
   156  		return nil
   157  	}
   158  	return listener(node)
   159  }
   160  
   161  func (node *AppendNode) ApplyRollback(index *wal.Index) (err error) {
   162  	node.mvcc.Lock()
   163  	defer node.mvcc.Unlock()
   164  	_, err = node.TxnMVCCNode.ApplyRollback(index)
   165  	return
   166  }
   167  
   168  func (node *AppendNode) WriteTo(w io.Writer) (n int64, err error) {
   169  	cn, err := w.Write(txnbase.MarshalID(node.mvcc.GetID()))
   170  	if err != nil {
   171  		return
   172  	}
   173  	n += int64(cn)
   174  	if err = binary.Write(w, binary.BigEndian, node.startRow); err != nil {
   175  		return
   176  	}
   177  	n += 4
   178  	if err = binary.Write(w, binary.BigEndian, node.maxRow); err != nil {
   179  		return
   180  	}
   181  	n += 4
   182  	var sn int64
   183  	sn, err = node.TxnMVCCNode.WriteTo(w)
   184  	if err != nil {
   185  		return
   186  	}
   187  	n += sn
   188  	return
   189  }
   190  
   191  func (node *AppendNode) ReadFrom(r io.Reader) (n int64, err error) {
   192  	var sn int
   193  	buf := make([]byte, txnbase.IDSize)
   194  	if sn, err = r.Read(buf); err != nil {
   195  		return
   196  	}
   197  	n = int64(sn)
   198  	node.id = txnbase.UnmarshalID(buf)
   199  	if err = binary.Read(r, binary.BigEndian, &node.startRow); err != nil {
   200  		return
   201  	}
   202  	n += 4
   203  	if err = binary.Read(r, binary.BigEndian, &node.maxRow); err != nil {
   204  		return
   205  	}
   206  	n += 4
   207  	var sn2 int64
   208  	sn2, err = node.TxnMVCCNode.ReadFrom(r)
   209  	if err != nil {
   210  		return
   211  	}
   212  	n += sn2
   213  	return
   214  }
   215  
   216  func (node *AppendNode) PrepareRollback() (err error) {
   217  	node.mvcc.Lock()
   218  	defer node.mvcc.Unlock()
   219  	node.mvcc.DeleteAppendNodeLocked(node)
   220  	return
   221  }
   222  func (node *AppendNode) MakeCommand(id uint32) (cmd txnif.TxnCmd, err error) {
   223  	cmd = NewAppendCmd(id, node)
   224  	return
   225  }
   226  
   227  func (node *AppendNode) Set1PC() {
   228  	node.TxnMVCCNode.Set1PC()
   229  }
   230  func (node *AppendNode) Is1PC() bool {
   231  	return node.TxnMVCCNode.Is1PC()
   232  }