github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/iface/txnif/memo.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 txnif
    16  
    17  import (
    18  	"io"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/container/types"
    21  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/model"
    22  )
    23  
    24  type TxnMemo struct {
    25  	*model.Tree
    26  	isCatalogChanged bool
    27  }
    28  
    29  func NewTxnMemo() *TxnMemo {
    30  	return &TxnMemo{
    31  		Tree: model.NewTree(),
    32  	}
    33  }
    34  
    35  func (memo *TxnMemo) AddCatalogChange() {
    36  	memo.isCatalogChanged = true
    37  }
    38  
    39  func (memo *TxnMemo) HasAnyTableDataChanges() bool {
    40  	return memo.TableCount() > 0
    41  }
    42  
    43  func (memo *TxnMemo) HasTableDataChanges(id uint64) bool {
    44  	return memo.HasTable(id)
    45  }
    46  
    47  func (memo *TxnMemo) HasCatalogChanges() bool {
    48  	return memo.isCatalogChanged
    49  }
    50  
    51  func (memo *TxnMemo) GetDirtyTableByID(id uint64) *model.TableTree {
    52  	return memo.GetTable(id)
    53  }
    54  
    55  func (memo *TxnMemo) GetDirty() *model.Tree {
    56  	return memo.Tree
    57  }
    58  
    59  func (memo *TxnMemo) WriteTo(w io.Writer) (n int64, err error) {
    60  	var tmpn int64
    61  	if tmpn, err = memo.Tree.WriteTo(w); err != nil {
    62  		return
    63  	}
    64  	n += tmpn
    65  	isCatalogChanged := int8(0)
    66  	if memo.isCatalogChanged {
    67  		isCatalogChanged = 1
    68  	}
    69  	if _, err = w.Write(types.EncodeInt8(&isCatalogChanged)); err != nil {
    70  		return
    71  	}
    72  	n += 1
    73  	return
    74  }
    75  
    76  func (memo *TxnMemo) ReadFromWithVersion(r io.Reader, ver uint16) (n int64, err error) {
    77  	var tmpn int64
    78  	if tmpn, err = memo.Tree.ReadFromWithVersion(r, ver); err != nil {
    79  		return
    80  	}
    81  	n += tmpn
    82  	isCatalogChanged := int8(0)
    83  	if _, err = r.Read(types.EncodeInt8(&isCatalogChanged)); err != nil {
    84  		return
    85  	}
    86  	if isCatalogChanged == 1 {
    87  		memo.isCatalogChanged = true
    88  	}
    89  	n += 1
    90  	return
    91  }