github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/tables/updates/append.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 updates 16 17 import ( 18 "fmt" 19 "io" 20 21 "github.com/matrixorigin/matrixone/pkg/container/types" 22 23 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common" 24 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/txnif" 25 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/txn/txnbase" 26 ) 27 28 type AppendNode struct { 29 *txnbase.TxnMVCCNode 30 startRow uint32 31 maxRow uint32 32 mvcc *AppendMVCCHandle 33 id *common.ID 34 } 35 36 func CompareAppendNode(e, o *AppendNode) int { 37 return e.Compare(o.TxnMVCCNode) 38 } 39 40 func MockAppendNode(ts types.TS, startRow, maxRow uint32, mvcc *AppendMVCCHandle) *AppendNode { 41 return &AppendNode{ 42 TxnMVCCNode: &txnbase.TxnMVCCNode{ 43 Start: ts, 44 End: ts, 45 }, 46 maxRow: maxRow, 47 mvcc: mvcc, 48 } 49 } 50 51 func NewCommittedAppendNode( 52 ts types.TS, 53 startRow, maxRow uint32, 54 mvcc *AppendMVCCHandle) *AppendNode { 55 return &AppendNode{ 56 TxnMVCCNode: &txnbase.TxnMVCCNode{ 57 Start: ts, 58 Prepare: ts, 59 End: ts, 60 }, 61 startRow: startRow, 62 maxRow: maxRow, 63 mvcc: mvcc, 64 } 65 } 66 67 func NewAppendNode( 68 txn txnif.AsyncTxn, 69 startRow, maxRow uint32, 70 mvcc *AppendMVCCHandle) *AppendNode { 71 var startTs, ts types.TS 72 if txn != nil { 73 startTs = txn.GetStartTS() 74 ts = txn.GetPrepareTS() 75 } 76 n := &AppendNode{ 77 TxnMVCCNode: &txnbase.TxnMVCCNode{ 78 Start: startTs, 79 Prepare: ts, 80 End: txnif.UncommitTS, 81 Txn: txn, 82 }, 83 startRow: startRow, 84 maxRow: maxRow, 85 mvcc: mvcc, 86 } 87 return n 88 } 89 90 func NewEmptyAppendNode() *AppendNode { 91 return &AppendNode{ 92 TxnMVCCNode: &txnbase.TxnMVCCNode{}, 93 id: &common.ID{}, 94 } 95 } 96 func (node *AppendNode) String() string { 97 return node.GeneralDesc() 98 } 99 func (node *AppendNode) CloneAll() *AppendNode { 100 panic("todo") 101 } 102 func (node *AppendNode) CloneData() *AppendNode { 103 panic("todo") 104 } 105 func (node *AppendNode) Update(*AppendNode) { 106 panic("todo") 107 } 108 func (node *AppendNode) IsNil() bool { return node == nil } 109 func (node *AppendNode) GeneralDesc() string { 110 return fmt.Sprintf("%s;StartRow=%d MaxRow=%d", node.TxnMVCCNode.String(), node.startRow, node.maxRow) 111 } 112 func (node *AppendNode) GeneralString() string { 113 return node.GeneralDesc() 114 } 115 func (node *AppendNode) GeneralVerboseString() string { 116 return node.GeneralDesc() 117 } 118 119 func (node *AppendNode) GetID() *common.ID { 120 return node.id 121 } 122 func (node *AppendNode) GetCommitTS() types.TS { 123 return node.GetEnd() 124 } 125 func (node *AppendNode) GetStartRow() uint32 { return node.startRow } 126 func (node *AppendNode) GetMaxRow() uint32 { 127 return node.maxRow 128 } 129 func (node *AppendNode) SetMaxRow(row uint32) { 130 node.maxRow = row 131 } 132 133 func (node *AppendNode) PrepareCommit() error { 134 node.mvcc.Lock() 135 defer node.mvcc.Unlock() 136 _, err := node.TxnMVCCNode.PrepareCommit() 137 return err 138 } 139 140 func (node *AppendNode) ApplyCommit() error { 141 node.mvcc.Lock() 142 defer node.mvcc.Unlock() 143 if node.IsCommitted() { 144 panic("AppendNode | ApplyCommit | LogicErr") 145 } 146 node.TxnMVCCNode.ApplyCommit() 147 listener := node.mvcc.GetAppendListener() 148 if listener == nil { 149 return nil 150 } 151 return listener(node) 152 } 153 154 func (node *AppendNode) ApplyRollback() (err error) { 155 node.mvcc.Lock() 156 defer node.mvcc.Unlock() 157 _, err = node.TxnMVCCNode.ApplyRollback() 158 return 159 } 160 161 func (node *AppendNode) WriteTo(w io.Writer) (n int64, err error) { 162 cn, err := w.Write(common.EncodeID(node.mvcc.GetID())) 163 if err != nil { 164 return 165 } 166 n += int64(cn) 167 var sn1 int 168 if sn1, err = w.Write(types.EncodeUint32(&node.startRow)); err != nil { 169 return 170 } 171 n += int64(sn1) 172 if sn1, err = w.Write(types.EncodeUint32(&node.maxRow)); err != nil { 173 return 174 } 175 n += int64(sn1) 176 var sn int64 177 sn, err = node.TxnMVCCNode.WriteTo(w) 178 if err != nil { 179 return 180 } 181 n += sn 182 return 183 } 184 185 func (node *AppendNode) ReadFrom(r io.Reader) (n int64, err error) { 186 var sn int 187 if node.id == nil { 188 node.id = &common.ID{} 189 } 190 if _, err = r.Read(common.EncodeID(node.id)); err != nil { 191 return 192 } 193 n += common.IDSize 194 if sn, err = r.Read(types.EncodeUint32(&node.startRow)); err != nil { 195 return 196 } 197 n += int64(sn) 198 if sn, err = r.Read(types.EncodeUint32(&node.maxRow)); err != nil { 199 return 200 } 201 n += int64(sn) 202 var sn2 int64 203 sn2, err = node.TxnMVCCNode.ReadFrom(r) 204 if err != nil { 205 return 206 } 207 n += sn2 208 return 209 } 210 211 func (node *AppendNode) PrepareRollback() (err error) { 212 node.mvcc.Lock() 213 defer node.mvcc.Unlock() 214 node.mvcc.DeleteAppendNodeLocked(node) 215 return 216 } 217 func (node *AppendNode) MakeCommand(id uint32) (cmd txnif.TxnCmd, err error) { 218 cmd = NewAppendCmd(id, node) 219 return 220 } 221 222 func (node *AppendNode) Set1PC() { 223 node.TxnMVCCNode.Set1PC() 224 } 225 func (node *AppendNode) Is1PC() bool { 226 return node.TxnMVCCNode.Is1PC() 227 }