github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/catalog/dbmvccnode.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 catalog
    16  
    17  import (
    18  	"fmt"
    19  	"io"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/container/types"
    22  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/txnif"
    23  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/txn/txnbase"
    24  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/wal"
    25  )
    26  
    27  type DBMVCCNode struct {
    28  	*EntryMVCCNode
    29  	*txnbase.TxnMVCCNode
    30  }
    31  
    32  func NewEmptyDBMVCCNode() txnif.MVCCNode {
    33  	return &DBMVCCNode{
    34  		EntryMVCCNode: &EntryMVCCNode{},
    35  		TxnMVCCNode:   &txnbase.TxnMVCCNode{},
    36  	}
    37  }
    38  
    39  func CompareDBBaseNode(e, o txnif.MVCCNode) int {
    40  	return e.(*DBMVCCNode).Compare(o.(*DBMVCCNode).TxnMVCCNode)
    41  }
    42  
    43  func (e *DBMVCCNode) CloneAll() txnif.MVCCNode {
    44  	node := &DBMVCCNode{
    45  		EntryMVCCNode: e.EntryMVCCNode.Clone(),
    46  		TxnMVCCNode:   e.TxnMVCCNode.CloneAll(),
    47  	}
    48  	return node
    49  }
    50  
    51  func (e *DBMVCCNode) CloneData() txnif.MVCCNode {
    52  	return &DBMVCCNode{
    53  		EntryMVCCNode: e.EntryMVCCNode.CloneData(),
    54  		TxnMVCCNode:   &txnbase.TxnMVCCNode{},
    55  	}
    56  }
    57  
    58  func (e *DBMVCCNode) String() string {
    59  
    60  	return fmt.Sprintf("%s%s",
    61  		e.TxnMVCCNode.String(),
    62  		e.EntryMVCCNode.String())
    63  }
    64  
    65  // for create drop in one txn
    66  func (e *DBMVCCNode) Update(vun txnif.MVCCNode) {
    67  	un := vun.(*DBMVCCNode)
    68  	e.CreatedAt = un.CreatedAt
    69  	e.DeletedAt = un.DeletedAt
    70  }
    71  
    72  func (e *DBMVCCNode) ApplyCommit(index *wal.Index) (err error) {
    73  	var commitTS types.TS
    74  	commitTS, err = e.TxnMVCCNode.ApplyCommit(index)
    75  	if err != nil {
    76  		return
    77  	}
    78  	e.EntryMVCCNode.ApplyCommit(commitTS)
    79  	return nil
    80  }
    81  func (e *DBMVCCNode) ApplyRollback(index *wal.Index) (err error) {
    82  	var commitTS types.TS
    83  	commitTS, err = e.TxnMVCCNode.ApplyRollback(index)
    84  	if err != nil {
    85  		return
    86  	}
    87  	e.EntryMVCCNode.ApplyCommit(commitTS)
    88  	return nil
    89  }
    90  
    91  func (e *DBMVCCNode) PrepareCommit() (err error) {
    92  	_, err = e.TxnMVCCNode.PrepareCommit()
    93  	if err != nil {
    94  		return
    95  	}
    96  	err = e.EntryMVCCNode.PrepareCommit()
    97  	return
    98  }
    99  
   100  func (e *DBMVCCNode) WriteTo(w io.Writer) (n int64, err error) {
   101  	var sn int64
   102  	sn, err = e.EntryMVCCNode.WriteTo(w)
   103  	if err != nil {
   104  		return
   105  	}
   106  	n += sn
   107  	sn, err = e.TxnMVCCNode.WriteTo(w)
   108  	if err != nil {
   109  		return
   110  	}
   111  	n += sn
   112  	return
   113  }
   114  
   115  func (e *DBMVCCNode) ReadFrom(r io.Reader) (n int64, err error) {
   116  	var sn int64
   117  	sn, err = e.EntryMVCCNode.ReadFrom(r)
   118  	if err != nil {
   119  		return
   120  	}
   121  	n += sn
   122  	sn, err = e.TxnMVCCNode.ReadFrom(r)
   123  	if err != nil {
   124  		return
   125  	}
   126  	n += sn
   127  	return
   128  }