github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/catalog/tablemvccnode.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  	"encoding/binary"
    19  	"fmt"
    20  	"io"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    23  	"github.com/matrixorigin/matrixone/pkg/container/types"
    24  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/txnif"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/txn/txnbase"
    26  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/wal"
    27  )
    28  
    29  type TableMVCCNode struct {
    30  	*EntryMVCCNode
    31  	*txnbase.TxnMVCCNode
    32  	SchemaConstraints string // store as immutable, bytes actually
    33  }
    34  
    35  func NewEmptyTableMVCCNode() txnif.MVCCNode {
    36  	return &TableMVCCNode{
    37  		EntryMVCCNode: &EntryMVCCNode{},
    38  		TxnMVCCNode:   &txnbase.TxnMVCCNode{},
    39  	}
    40  }
    41  
    42  func CompareTableBaseNode(e, o txnif.MVCCNode) int {
    43  	return e.(*TableMVCCNode).Compare(o.(*TableMVCCNode).TxnMVCCNode)
    44  }
    45  
    46  func (e *TableMVCCNode) CloneAll() txnif.MVCCNode {
    47  	node := &TableMVCCNode{}
    48  	node.EntryMVCCNode = e.EntryMVCCNode.Clone()
    49  	node.TxnMVCCNode = e.TxnMVCCNode.CloneAll()
    50  	node.SchemaConstraints = e.SchemaConstraints
    51  	return node
    52  }
    53  
    54  func (e *TableMVCCNode) CloneData() txnif.MVCCNode {
    55  	return &TableMVCCNode{
    56  		EntryMVCCNode:     e.EntryMVCCNode.CloneData(),
    57  		TxnMVCCNode:       &txnbase.TxnMVCCNode{},
    58  		SchemaConstraints: e.SchemaConstraints,
    59  	}
    60  }
    61  
    62  func (e *TableMVCCNode) String() string {
    63  
    64  	return fmt.Sprintf("%s%scstr[%d]",
    65  		e.TxnMVCCNode.String(),
    66  		e.EntryMVCCNode.String(),
    67  		len(e.SchemaConstraints))
    68  }
    69  
    70  // for create drop in one txn
    71  func (e *TableMVCCNode) Update(vun txnif.MVCCNode) {
    72  	un := vun.(*TableMVCCNode)
    73  	e.CreatedAt = un.CreatedAt
    74  	e.DeletedAt = un.DeletedAt
    75  	e.SchemaConstraints = un.SchemaConstraints
    76  }
    77  
    78  func (e *TableMVCCNode) ApplyCommit(index *wal.Index) (err error) {
    79  	var commitTS types.TS
    80  	commitTS, err = e.TxnMVCCNode.ApplyCommit(index)
    81  	if err != nil {
    82  		return
    83  	}
    84  	err = e.EntryMVCCNode.ApplyCommit(commitTS)
    85  	return err
    86  }
    87  func (e *TableMVCCNode) ApplyRollback(index *wal.Index) (err error) {
    88  	var commitTS types.TS
    89  	commitTS, err = e.TxnMVCCNode.ApplyRollback(index)
    90  	if err != nil {
    91  		return
    92  	}
    93  	err = e.EntryMVCCNode.ApplyCommit(commitTS)
    94  	return err
    95  }
    96  func (e *TableMVCCNode) PrepareCommit() (err error) {
    97  	_, err = e.TxnMVCCNode.PrepareCommit()
    98  	if err != nil {
    99  		return
   100  	}
   101  	err = e.EntryMVCCNode.PrepareCommit()
   102  	return
   103  }
   104  
   105  func (e *TableMVCCNode) WriteTo(w io.Writer) (n int64, err error) {
   106  	var sn int64
   107  	sn, err = e.EntryMVCCNode.WriteTo(w)
   108  	if err != nil {
   109  		return
   110  	}
   111  	n += sn
   112  	sn, err = e.TxnMVCCNode.WriteTo(w)
   113  	if err != nil {
   114  		return
   115  	}
   116  	n += sn
   117  	condata := []byte(e.SchemaConstraints)
   118  	l := uint32(len(condata))
   119  	if err = binary.Write(w, binary.BigEndian, l); err != nil {
   120  		return
   121  	}
   122  	n += 4
   123  	var n1 int
   124  	if n1, err = w.Write(condata); err != nil {
   125  		return
   126  	}
   127  	n += int64(n1)
   128  	return
   129  }
   130  
   131  func (e *TableMVCCNode) ReadFrom(r io.Reader) (n int64, err error) {
   132  	var sn int64
   133  	sn, err = e.EntryMVCCNode.ReadFrom(r)
   134  	if err != nil {
   135  		return
   136  	}
   137  	n += sn
   138  	sn, err = e.TxnMVCCNode.ReadFrom(r)
   139  	if err != nil {
   140  		return
   141  	}
   142  	n += sn
   143  
   144  	length := uint32(0)
   145  	if err = binary.Read(r, binary.BigEndian, &length); err != nil {
   146  		return
   147  	}
   148  	n += 4
   149  	buf := make([]byte, length)
   150  	var n2 int
   151  	n2, err = r.Read(buf)
   152  	if err != nil {
   153  		return
   154  	}
   155  	if n2 != int(length) {
   156  		panic(moerr.NewInternalErrorNoCtx("logic err %d!=%d, %v", n2, length, err))
   157  	}
   158  	e.SchemaConstraints = string(buf)
   159  	return
   160  }