github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/catalog/metamvccnode.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 MetadataMVCCNode struct {
    30  	*EntryMVCCNode
    31  	*txnbase.TxnMVCCNode
    32  	MetaLoc  string
    33  	DeltaLoc string
    34  }
    35  
    36  func NewEmptyMetadataMVCCNode() txnif.MVCCNode {
    37  	return &MetadataMVCCNode{
    38  		EntryMVCCNode: &EntryMVCCNode{},
    39  		TxnMVCCNode:   &txnbase.TxnMVCCNode{},
    40  	}
    41  }
    42  
    43  func CompareMetaBaseNode(e, o txnif.MVCCNode) int {
    44  	return e.(*MetadataMVCCNode).Compare(o.(*MetadataMVCCNode).TxnMVCCNode)
    45  }
    46  
    47  func (e *MetadataMVCCNode) CloneAll() txnif.MVCCNode {
    48  	node := &MetadataMVCCNode{
    49  		EntryMVCCNode: e.EntryMVCCNode.Clone(),
    50  		TxnMVCCNode:   e.TxnMVCCNode.CloneAll(),
    51  		MetaLoc:       e.MetaLoc,
    52  		DeltaLoc:      e.DeltaLoc,
    53  	}
    54  	return node
    55  }
    56  
    57  func (e *MetadataMVCCNode) CloneData() txnif.MVCCNode {
    58  	return &MetadataMVCCNode{
    59  		EntryMVCCNode: e.EntryMVCCNode.CloneData(),
    60  		TxnMVCCNode:   &txnbase.TxnMVCCNode{},
    61  		MetaLoc:       e.MetaLoc,
    62  		DeltaLoc:      e.DeltaLoc,
    63  	}
    64  }
    65  
    66  func (e *MetadataMVCCNode) String() string {
    67  
    68  	return fmt.Sprintf("%s%s[MetaLoc=\"%s\",DeltaLoc=\"%s\"]",
    69  		e.TxnMVCCNode.String(),
    70  		e.EntryMVCCNode.String(),
    71  		e.MetaLoc,
    72  		e.DeltaLoc)
    73  }
    74  func (e *MetadataMVCCNode) UpdateMetaLoc(metaLoc string) {
    75  	e.MetaLoc = metaLoc
    76  }
    77  func (e *MetadataMVCCNode) UpdateDeltaLoc(deltaLoc string) {
    78  	e.DeltaLoc = deltaLoc
    79  }
    80  
    81  // for create drop in one txn
    82  func (e *MetadataMVCCNode) Update(vun txnif.MVCCNode) {
    83  	un := vun.(*MetadataMVCCNode)
    84  	e.CreatedAt = un.CreatedAt
    85  	e.DeletedAt = un.DeletedAt
    86  	e.MetaLoc = un.MetaLoc
    87  	e.DeltaLoc = un.DeltaLoc
    88  }
    89  
    90  func (e *MetadataMVCCNode) ApplyCommit(index *wal.Index) (err error) {
    91  	var commitTS types.TS
    92  	commitTS, err = e.TxnMVCCNode.ApplyCommit(index)
    93  	if err != nil {
    94  		return
    95  	}
    96  	err = e.EntryMVCCNode.ApplyCommit(commitTS)
    97  	return err
    98  }
    99  func (e *MetadataMVCCNode) PrepareRollback() (err error) {
   100  	return e.TxnMVCCNode.PrepareRollback()
   101  }
   102  func (e *MetadataMVCCNode) ApplyRollback(index *wal.Index) (err error) {
   103  	var commitTS types.TS
   104  	commitTS, err = e.TxnMVCCNode.ApplyRollback(index)
   105  	if err != nil {
   106  		return
   107  	}
   108  	err = e.EntryMVCCNode.ApplyCommit(commitTS)
   109  	return
   110  }
   111  
   112  func (e *MetadataMVCCNode) PrepareCommit() (err error) {
   113  	_, err = e.TxnMVCCNode.PrepareCommit()
   114  	if err != nil {
   115  		return
   116  	}
   117  	err = e.EntryMVCCNode.PrepareCommit()
   118  	return
   119  }
   120  
   121  func (e *MetadataMVCCNode) WriteTo(w io.Writer) (n int64, err error) {
   122  	var sn int64
   123  	sn, err = e.EntryMVCCNode.WriteTo(w)
   124  	if err != nil {
   125  		return
   126  	}
   127  	n += sn
   128  	sn, err = e.TxnMVCCNode.WriteTo(w)
   129  	if err != nil {
   130  		return
   131  	}
   132  	n += sn
   133  
   134  	length := uint32(len([]byte(e.MetaLoc)))
   135  	if err = binary.Write(w, binary.BigEndian, length); err != nil {
   136  		return
   137  	}
   138  	n += 4
   139  	var n2 int
   140  	n2, err = w.Write([]byte(e.MetaLoc))
   141  	if err != nil {
   142  		return
   143  	}
   144  	if n2 != int(length) {
   145  		panic(moerr.NewInternalErrorNoCtx("logic err %d!=%d, %v", n2, length, err))
   146  	}
   147  	n += int64(n2)
   148  	length = uint32(len([]byte(e.DeltaLoc)))
   149  	if err = binary.Write(w, binary.BigEndian, length); err != nil {
   150  		return
   151  	}
   152  	n += 4
   153  	n2, err = w.Write([]byte(e.DeltaLoc))
   154  	if err != nil {
   155  		return
   156  	}
   157  	if n2 != int(length) {
   158  		panic(moerr.NewInternalErrorNoCtx("logic err %d!=%d, %v", n2, length, err))
   159  	}
   160  	n += int64(n2)
   161  	return
   162  }
   163  
   164  func (e *MetadataMVCCNode) ReadFrom(r io.Reader) (n int64, err error) {
   165  	var sn int64
   166  	sn, err = e.EntryMVCCNode.ReadFrom(r)
   167  	if err != nil {
   168  		return
   169  	}
   170  	n += sn
   171  	sn, err = e.TxnMVCCNode.ReadFrom(r)
   172  	if err != nil {
   173  		return
   174  	}
   175  	n += sn
   176  
   177  	length := uint32(0)
   178  	if err = binary.Read(r, binary.BigEndian, &length); err != nil {
   179  		return
   180  	}
   181  	n += 4
   182  	buf := make([]byte, length)
   183  	var n2 int
   184  	n2, err = r.Read(buf)
   185  	if err != nil {
   186  		return
   187  	}
   188  	if n2 != int(length) {
   189  		panic(moerr.NewInternalErrorNoCtx("logic err %d!=%d, %v", n2, length, err))
   190  	}
   191  	e.MetaLoc = string(buf)
   192  	if err = binary.Read(r, binary.BigEndian, &length); err != nil {
   193  		return
   194  	}
   195  	buf = make([]byte, length)
   196  	n2, err = r.Read(buf)
   197  	if err != nil {
   198  		return
   199  	}
   200  	if n2 != int(length) {
   201  		panic(moerr.NewInternalErrorNoCtx("logic err %d!=%d, %v", n2, length, err))
   202  	}
   203  	e.DeltaLoc = string(buf)
   204  	return
   205  }