github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/catalog/basemvccnode.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  	"unsafe"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/container/types"
    23  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    24  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/containers"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/txnif"
    26  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/txn/txnbase"
    27  )
    28  
    29  const (
    30  	EntryMVCCNodeSize int = int(unsafe.Sizeof(EntryMVCCNode{}))
    31  )
    32  
    33  type EntryMVCCNode struct {
    34  	CreatedAt, DeletedAt types.TS
    35  }
    36  
    37  func EncodeEntryMVCCNode(node *EntryMVCCNode) []byte {
    38  	return unsafe.Slice((*byte)(unsafe.Pointer(node)), EntryMVCCNodeSize)
    39  }
    40  
    41  func DecodeEntryMVCCNode(v []byte) *EntryMVCCNode {
    42  	return (*EntryMVCCNode)(unsafe.Pointer(&v[0]))
    43  }
    44  
    45  // Dropped committed
    46  func (un *EntryMVCCNode) HasDropCommitted() bool {
    47  	return !un.DeletedAt.IsEmpty() && un.DeletedAt != txnif.UncommitTS
    48  }
    49  
    50  // Dropped committed or uncommitted
    51  func (un *EntryMVCCNode) HasDropIntent() bool {
    52  	return !un.DeletedAt.IsEmpty()
    53  }
    54  
    55  func (un *EntryMVCCNode) GetCreatedAt() types.TS {
    56  	return un.CreatedAt
    57  }
    58  
    59  func (un *EntryMVCCNode) GetDeletedAt() types.TS {
    60  	return un.DeletedAt
    61  }
    62  
    63  func (un *EntryMVCCNode) IsCreating() bool {
    64  	return un.CreatedAt.Equal(&txnif.UncommitTS)
    65  }
    66  
    67  func (un *EntryMVCCNode) Clone() *EntryMVCCNode {
    68  	return &EntryMVCCNode{
    69  		CreatedAt: un.CreatedAt,
    70  		DeletedAt: un.DeletedAt,
    71  	}
    72  }
    73  
    74  func (un *EntryMVCCNode) CloneData() *EntryMVCCNode {
    75  	return &EntryMVCCNode{
    76  		CreatedAt: un.CreatedAt,
    77  		DeletedAt: un.DeletedAt,
    78  	}
    79  }
    80  
    81  func (un *EntryMVCCNode) Delete() {
    82  	un.DeletedAt = txnif.UncommitTS
    83  }
    84  
    85  func (un *EntryMVCCNode) ReadFrom(r io.Reader) (n int64, err error) {
    86  	var sn int
    87  	if sn, err = r.Read(EncodeEntryMVCCNode(un)); err != nil {
    88  		return
    89  	}
    90  	n += int64(sn)
    91  	return
    92  }
    93  func (un *EntryMVCCNode) WriteTo(w io.Writer) (n int64, err error) {
    94  	var sn int
    95  	if sn, err = w.Write(EncodeEntryMVCCNode(un)); err != nil {
    96  		return
    97  	}
    98  	n += int64(sn)
    99  	return
   100  }
   101  func (un *EntryMVCCNode) PrepareCommit() (err error) {
   102  	return nil
   103  }
   104  func (un *EntryMVCCNode) String() string {
   105  	return fmt.Sprintf("[C@%s,D@%s]", un.CreatedAt.ToString(), un.DeletedAt.ToString())
   106  }
   107  func (un *EntryMVCCNode) ApplyCommit(ts types.TS) (err error) {
   108  	if un.CreatedAt == txnif.UncommitTS {
   109  		un.CreatedAt = ts
   110  	}
   111  	if un.DeletedAt == txnif.UncommitTS {
   112  		un.DeletedAt = ts
   113  	}
   114  	return nil
   115  }
   116  
   117  func (un *EntryMVCCNode) AppendTuple(bat *containers.Batch) {
   118  	startTSVec := bat.GetVectorByName(EntryNode_CreateAt)
   119  	vector.AppendFixed(
   120  		startTSVec.GetDownstreamVector(),
   121  		un.CreatedAt,
   122  		false,
   123  		startTSVec.GetAllocator(),
   124  	)
   125  	vector.AppendFixed(
   126  		bat.GetVectorByName(EntryNode_DeleteAt).GetDownstreamVector(),
   127  		un.DeletedAt,
   128  		false,
   129  		startTSVec.GetAllocator(),
   130  	)
   131  }
   132  
   133  func (un *EntryMVCCNode) AppendTupleWithCommitTS(bat *containers.Batch, ts types.TS) {
   134  	startTSVec := bat.GetVectorByName(EntryNode_CreateAt)
   135  	createTS := un.CreatedAt
   136  	if createTS.Equal(&txnif.UncommitTS) {
   137  		createTS = ts
   138  	}
   139  	vector.AppendFixed(
   140  		startTSVec.GetDownstreamVector(),
   141  		createTS,
   142  		false,
   143  		startTSVec.GetAllocator(),
   144  	)
   145  	deleteTS := un.DeletedAt
   146  	if deleteTS.Equal(&txnif.UncommitTS) {
   147  		deleteTS = ts
   148  	}
   149  	vector.AppendFixed(
   150  		bat.GetVectorByName(EntryNode_DeleteAt).GetDownstreamVector(),
   151  		deleteTS,
   152  		false,
   153  		startTSVec.GetAllocator(),
   154  	)
   155  }
   156  
   157  func ReadEntryNodeTuple(bat *containers.Batch, row int) (un *EntryMVCCNode) {
   158  	un = &EntryMVCCNode{
   159  		CreatedAt: bat.GetVectorByName(EntryNode_CreateAt).Get(row).(types.TS),
   160  		DeletedAt: bat.GetVectorByName(EntryNode_DeleteAt).Get(row).(types.TS),
   161  	}
   162  	return
   163  }
   164  
   165  type BaseNode[T any] interface {
   166  	CloneAll() T
   167  	CloneData() T
   168  	String() string
   169  	Update(vun T)
   170  	IdempotentUpdate(vun T)
   171  	WriteTo(w io.Writer) (n int64, err error)
   172  	ReadFromWithVersion(r io.Reader, ver uint16) (n int64, err error)
   173  }
   174  
   175  type MVCCNode[T BaseNode[T]] struct {
   176  	*EntryMVCCNode
   177  	*txnbase.TxnMVCCNode
   178  	BaseNode T
   179  }
   180  
   181  func NewEmptyMVCCNodeFactory[T BaseNode[T]](factory func() T) func() *MVCCNode[T] {
   182  	return func() *MVCCNode[T] {
   183  		return &MVCCNode[T]{
   184  			EntryMVCCNode: &EntryMVCCNode{},
   185  			TxnMVCCNode:   &txnbase.TxnMVCCNode{},
   186  			BaseNode:      factory(),
   187  		}
   188  	}
   189  }
   190  
   191  func CompareBaseNode[T BaseNode[T]](e, o *MVCCNode[T]) int {
   192  	return e.Compare(o.TxnMVCCNode)
   193  }
   194  
   195  func (e *MVCCNode[T]) CloneAll() *MVCCNode[T] {
   196  	node := &MVCCNode[T]{
   197  		EntryMVCCNode: e.EntryMVCCNode.Clone(),
   198  		TxnMVCCNode:   e.TxnMVCCNode.CloneAll(),
   199  		BaseNode:      e.BaseNode.CloneAll(),
   200  	}
   201  	return node
   202  }
   203  
   204  func (e *MVCCNode[T]) CloneData() *MVCCNode[T] {
   205  	return &MVCCNode[T]{
   206  		EntryMVCCNode: e.EntryMVCCNode.CloneData(),
   207  		TxnMVCCNode:   &txnbase.TxnMVCCNode{},
   208  		BaseNode:      e.BaseNode.CloneData(),
   209  	}
   210  }
   211  func (e *MVCCNode[T]) IsNil() bool {
   212  	return e == nil
   213  }
   214  func (e *MVCCNode[T]) String() string {
   215  
   216  	return fmt.Sprintf("%s%s%s",
   217  		e.TxnMVCCNode.String(),
   218  		e.EntryMVCCNode.String(),
   219  		e.BaseNode.String())
   220  }
   221  
   222  // for create drop in one txn
   223  func (e *MVCCNode[T]) Update(un *MVCCNode[T]) {
   224  	e.CreatedAt = un.CreatedAt
   225  	e.DeletedAt = un.DeletedAt
   226  	e.BaseNode.Update(un.BaseNode)
   227  }
   228  
   229  func (e *MVCCNode[T]) IdempotentUpdate(un *MVCCNode[T]) {
   230  	e.CreatedAt = un.CreatedAt
   231  	e.DeletedAt = un.DeletedAt
   232  	e.BaseNode.Update(un.BaseNode)
   233  }
   234  
   235  func (e *MVCCNode[T]) ApplyCommit() (err error) {
   236  	var commitTS types.TS
   237  	commitTS, err = e.TxnMVCCNode.ApplyCommit()
   238  	if err != nil {
   239  		return
   240  	}
   241  	err = e.EntryMVCCNode.ApplyCommit(commitTS)
   242  	return err
   243  }
   244  func (e *MVCCNode[T]) PrepareRollback() (err error) {
   245  	return e.TxnMVCCNode.PrepareRollback()
   246  }
   247  func (e *MVCCNode[T]) ApplyRollback() (err error) {
   248  	var commitTS types.TS
   249  	commitTS, err = e.TxnMVCCNode.ApplyRollback()
   250  	if err != nil {
   251  		return
   252  	}
   253  	err = e.EntryMVCCNode.ApplyCommit(commitTS)
   254  	return
   255  }
   256  
   257  func (e *MVCCNode[T]) PrepareCommit() (err error) {
   258  	_, err = e.TxnMVCCNode.PrepareCommit()
   259  	if err != nil {
   260  		return
   261  	}
   262  	err = e.EntryMVCCNode.PrepareCommit()
   263  	return
   264  }
   265  
   266  func (e *MVCCNode[T]) WriteTo(w io.Writer) (n int64, err error) {
   267  	var sn int64
   268  	sn, err = e.EntryMVCCNode.WriteTo(w)
   269  	if err != nil {
   270  		return
   271  	}
   272  	n += sn
   273  	sn, err = e.TxnMVCCNode.WriteTo(w)
   274  	if err != nil {
   275  		return
   276  	}
   277  	n += sn
   278  
   279  	sn, err = e.BaseNode.WriteTo(w)
   280  	if err != nil {
   281  		return
   282  	}
   283  	n += sn
   284  	return
   285  }
   286  
   287  func (e *MVCCNode[T]) ReadFromWithVersion(r io.Reader, ver uint16) (n int64, err error) {
   288  	var sn int64
   289  	sn, err = e.EntryMVCCNode.ReadFrom(r)
   290  	if err != nil {
   291  		return
   292  	}
   293  	n += sn
   294  	sn, err = e.TxnMVCCNode.ReadFrom(r)
   295  	if err != nil {
   296  		return
   297  	}
   298  	n += sn
   299  	sn, err = e.BaseNode.ReadFromWithVersion(r, ver)
   300  	if err != nil {
   301  		return
   302  	}
   303  	n += sn
   304  	return
   305  }