github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/txn/txnimpl/appendcmd.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 txnimpl 16 17 import ( 18 "bytes" 19 "encoding/binary" 20 "fmt" 21 "io" 22 23 "github.com/matrixorigin/matrixone/pkg/container/types" 24 25 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common" 26 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/txnif" 27 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/txn/txnbase" 28 ) 29 30 const ( 31 CmdAppend int16 = txnbase.CmdCustomized + iota 32 ) 33 34 func init() { 35 txnif.RegisterCmdFactory(CmdAppend, func(int16) txnif.TxnCmd { 36 return NewEmptyAppendCmd() 37 }) 38 } 39 40 type AppendCmd struct { 41 *txnbase.BaseCustomizedCmd 42 *txnbase.ComposedCmd 43 Infos []*appendInfo 44 Ts types.TS 45 Node InsertNode 46 } 47 48 func NewEmptyAppendCmd() *AppendCmd { 49 cmd := &AppendCmd{ 50 ComposedCmd: txnbase.NewComposedCmd(), 51 } 52 cmd.BaseCustomizedCmd = txnbase.NewBaseCustomizedCmd(0, cmd) 53 return cmd 54 } 55 56 func NewAppendCmd(id uint32, node InsertNode) *AppendCmd { 57 impl := &AppendCmd{ 58 ComposedCmd: txnbase.NewComposedCmd(), 59 Node: node, 60 Infos: node.GetAppends(), 61 } 62 impl.BaseCustomizedCmd = txnbase.NewBaseCustomizedCmd(id, impl) 63 return impl 64 } 65 func (c *AppendCmd) Desc() string { 66 s := fmt.Sprintf("CmdName=InsertNode;ID=%d;TS=%d;Dests=[", c.ID, c.Ts) 67 for _, info := range c.Infos { 68 s = fmt.Sprintf("%s %s", s, info.Desc()) 69 } 70 s = fmt.Sprintf("%s];\n%s", s, c.ComposedCmd.ToDesc("\t\t")) 71 return s 72 } 73 func (c *AppendCmd) String() string { 74 s := fmt.Sprintf("CmdName=InsertNode;ID=%d;TS=%d;Dests=[", c.ID, c.Ts) 75 for _, info := range c.Infos { 76 s = fmt.Sprintf("%s%s", s, info.String()) 77 } 78 s = fmt.Sprintf("%s];\n%s", s, c.ComposedCmd.ToString("\t\t")) 79 return s 80 } 81 func (c *AppendCmd) VerboseString() string { 82 s := fmt.Sprintf("CmdName=InsertNode;ID=%d;TS=%d;Dests=", c.ID, c.Ts) 83 for _, info := range c.Infos { 84 s = fmt.Sprintf("%s%s", s, info.String()) 85 } 86 s = fmt.Sprintf("%s];\n%s", s, c.ComposedCmd.ToVerboseString("\t\t")) 87 return s 88 } 89 func (c *AppendCmd) Close() { c.ComposedCmd.Close() } 90 func (c *AppendCmd) GetType() int16 { return CmdAppend } 91 func (c *AppendCmd) WriteTo(w io.Writer) (n int64, err error) { 92 if err = binary.Write(w, binary.BigEndian, c.GetType()); err != nil { 93 return 94 } 95 if err = binary.Write(w, binary.BigEndian, c.ID); err != nil { 96 return 97 } 98 if err = binary.Write(w, binary.BigEndian, uint32(len(c.Infos))); err != nil { 99 return 100 } 101 var sn int64 102 n = 10 103 for _, info := range c.Infos { 104 if sn, err = info.WriteTo(w); err != nil { 105 return 106 } 107 n += sn 108 } 109 sn, err = c.ComposedCmd.WriteTo(w) 110 n += sn 111 if err != nil { 112 return n, err 113 } 114 if err = binary.Write(w, binary.BigEndian, c.Node.GetTxn().GetPrepareTS()); err != nil { 115 return 116 } 117 n += 16 118 return 119 } 120 121 func (c *AppendCmd) ReadFrom(r io.Reader) (n int64, err error) { 122 if err = binary.Read(r, binary.BigEndian, &c.ID); err != nil { 123 return 124 } 125 length := uint32(0) 126 if err = binary.Read(r, binary.BigEndian, &length); err != nil { 127 return 128 } 129 var sn int64 130 n = 8 131 c.Infos = make([]*appendInfo, length) 132 for i := 0; i < int(length); i++ { 133 c.Infos[i] = &appendInfo{dest: &common.ID{}} 134 if sn, err = c.Infos[i].ReadFrom(r); err != nil { 135 return 136 } 137 n += sn 138 } 139 cc, sn, err := txnbase.BuildCommandFrom(r) 140 c.ComposedCmd = cc.(*txnbase.ComposedCmd) 141 n += sn 142 if err != nil { 143 return n, err 144 } 145 if err = binary.Read(r, binary.BigEndian, &c.Ts); err != nil { 146 return 147 } 148 n += 16 149 return 150 } 151 152 func (c *AppendCmd) Marshal() (buf []byte, err error) { 153 var bbuf bytes.Buffer 154 if _, err = c.WriteTo(&bbuf); err != nil { 155 return 156 } 157 buf = bbuf.Bytes() 158 return 159 } 160 161 func (c *AppendCmd) Unmarshal(buf []byte) error { 162 bbuf := bytes.NewBuffer(buf) 163 _, err := c.ReadFrom(bbuf) 164 return err 165 }