github.com/matrixorigin/matrixone@v1.2.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  	"fmt"
    20  	"io"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/container/types"
    23  	"github.com/matrixorigin/matrixone/pkg/objectio"
    24  
    25  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/containers"
    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  	IOET_WALTxnCommand_Append uint16 = 3008
    32  
    33  	IOET_WALTxnCommand_Append_V1 uint16 = 1
    34  	IOET_WALTxnCommand_Append_V2 uint16 = 2
    35  
    36  	IOET_WALTxnCommand_Append_CurrVer = IOET_WALTxnCommand_Append_V2
    37  )
    38  
    39  func init() {
    40  	objectio.RegisterIOEnrtyCodec(objectio.IOEntryHeader{
    41  		Type:    IOET_WALTxnCommand_Append,
    42  		Version: IOET_WALTxnCommand_Append_V1,
    43  	}, nil,
    44  		func(b []byte) (any, error) {
    45  			cmd := NewEmptyAppendCmd()
    46  			err := cmd.UnmarshalBinaryV1(b)
    47  			return cmd, err
    48  		})
    49  	objectio.RegisterIOEnrtyCodec(objectio.IOEntryHeader{
    50  		Type:    IOET_WALTxnCommand_Append,
    51  		Version: IOET_WALTxnCommand_Append_V2,
    52  	}, nil,
    53  		func(b []byte) (any, error) {
    54  			cmd := NewEmptyAppendCmd()
    55  			err := cmd.UnmarshalBinary(b)
    56  			return cmd, err
    57  		})
    58  }
    59  
    60  type AppendCmd struct {
    61  	*txnbase.BaseCustomizedCmd
    62  	Data  *containers.Batch
    63  	Infos []*appendInfo
    64  	Ts    types.TS
    65  	Node  InsertNode
    66  }
    67  
    68  func NewEmptyAppendCmd() *AppendCmd {
    69  	cmd := &AppendCmd{}
    70  	cmd.BaseCustomizedCmd = txnbase.NewBaseCustomizedCmd(0, cmd)
    71  	return cmd
    72  }
    73  
    74  func NewAppendCmd(id uint32, node InsertNode, data *containers.Batch) *AppendCmd {
    75  	impl := &AppendCmd{
    76  		Node:  node,
    77  		Infos: node.GetAppends(),
    78  		Data:  data,
    79  	}
    80  	impl.BaseCustomizedCmd = txnbase.NewBaseCustomizedCmd(id, impl)
    81  	return impl
    82  }
    83  func (c *AppendCmd) Desc() string {
    84  	s := fmt.Sprintf("CmdName=InsertNode;ID=%d;TS=%d;Dests=[", c.ID, c.Ts)
    85  	for _, info := range c.Infos {
    86  		s = fmt.Sprintf("%s %s", s, info.Desc())
    87  	}
    88  	s = fmt.Sprintf("%s;R]ows=%d", s, c.Data.Length())
    89  	if c.Data.HasDelete() {
    90  		s = fmt.Sprintf("%s;DelCnt=%d", s, c.Data.DeleteCnt())
    91  	}
    92  	return s
    93  }
    94  func (c *AppendCmd) String() string {
    95  	s := fmt.Sprintf("CmdName=InsertNode;ID=%d;TS=%d;Dests=[", c.ID, c.Ts)
    96  	for _, info := range c.Infos {
    97  		s = fmt.Sprintf("%s%s", s, info.String())
    98  	}
    99  	s = fmt.Sprintf("%s];Rows=%d", s, c.Data.Length())
   100  	if c.Data.HasDelete() {
   101  		s = fmt.Sprintf("%s;DelCnt=%d", s, c.Data.DeleteCnt())
   102  	}
   103  	return s
   104  }
   105  func (c *AppendCmd) VerboseString() string {
   106  	s := fmt.Sprintf("CmdName=InsertNode;ID=%d;TS=%d;Dests=", c.ID, c.Ts)
   107  	for _, info := range c.Infos {
   108  		s = fmt.Sprintf("%s%s", s, info.String())
   109  	}
   110  	s = fmt.Sprintf("%s];Rows=%d", s, c.Data.Length())
   111  	if c.Data.HasDelete() {
   112  		s = fmt.Sprintf("%s;DelCnt=%d", s, c.Data.DeleteCnt())
   113  	}
   114  	return s
   115  }
   116  func (c *AppendCmd) Close() {
   117  	c.Data.Close()
   118  	c.Data = nil
   119  }
   120  func (c *AppendCmd) GetType() uint16 { return IOET_WALTxnCommand_Append }
   121  func (c *AppendCmd) WriteTo(w io.Writer) (n int64, err error) {
   122  	t := c.GetType()
   123  	if _, err = w.Write(types.EncodeUint16(&t)); err != nil {
   124  		return
   125  	}
   126  	ver := IOET_WALTxnCommand_Append_CurrVer
   127  	if _, err = w.Write(types.EncodeUint16(&ver)); err != nil {
   128  		return
   129  	}
   130  	if _, err = w.Write(types.EncodeUint32(&c.ID)); err != nil {
   131  		return
   132  	}
   133  	length := uint32(len(c.Infos))
   134  	if _, err = w.Write(types.EncodeUint32(&length)); err != nil {
   135  		return
   136  	}
   137  	var sn int64
   138  	n = 10
   139  	for _, info := range c.Infos {
   140  		if sn, err = info.WriteTo(w); err != nil {
   141  			return
   142  		}
   143  		n += sn
   144  	}
   145  	sn, err = c.Data.WriteTo(w)
   146  	n += sn
   147  	if err != nil {
   148  		return n, err
   149  	}
   150  	ts := c.Node.GetTxn().GetPrepareTS()
   151  	if _, err = w.Write(ts[:]); err != nil {
   152  		return
   153  	}
   154  	n += 16
   155  	return
   156  }
   157  
   158  func (c *AppendCmd) ReadFrom(r io.Reader) (n int64, err error) {
   159  	if _, err = r.Read(types.EncodeUint32(&c.ID)); err != nil {
   160  		return
   161  	}
   162  	length := uint32(0)
   163  	if _, err = r.Read(types.EncodeUint32(&length)); err != nil {
   164  		return
   165  	}
   166  	var sn int64
   167  	n = 8
   168  	c.Infos = make([]*appendInfo, length)
   169  	for i := 0; i < int(length); i++ {
   170  		c.Infos[i] = &appendInfo{}
   171  		if sn, err = c.Infos[i].ReadFrom(r); err != nil {
   172  			return
   173  		}
   174  		n += sn
   175  	}
   176  	c.Data = containers.NewBatch()
   177  	c.Data.ReadFrom(r)
   178  	n += sn
   179  	if err != nil {
   180  		return n, err
   181  	}
   182  	if _, err = r.Read(c.Ts[:]); err != nil {
   183  		return
   184  	}
   185  	n += 16
   186  	return
   187  }
   188  
   189  func (c *AppendCmd) ReadFromV1(r io.Reader) (n int64, err error) {
   190  	if _, err = r.Read(types.EncodeUint32(&c.ID)); err != nil {
   191  		return
   192  	}
   193  	length := uint32(0)
   194  	if _, err = r.Read(types.EncodeUint32(&length)); err != nil {
   195  		return
   196  	}
   197  	var sn int64
   198  	n = 8
   199  	c.Infos = make([]*appendInfo, length)
   200  	for i := 0; i < int(length); i++ {
   201  		c.Infos[i] = &appendInfo{}
   202  		if sn, err = c.Infos[i].ReadFrom(r); err != nil {
   203  			return
   204  		}
   205  		n += sn
   206  	}
   207  	c.Data = containers.NewBatch()
   208  	c.Data.ReadFromV1(r)
   209  	n += sn
   210  	if err != nil {
   211  		return n, err
   212  	}
   213  	if _, err = r.Read(c.Ts[:]); err != nil {
   214  		return
   215  	}
   216  	n += 16
   217  	return
   218  }
   219  
   220  func (c *AppendCmd) MarshalBinary() (buf []byte, err error) {
   221  	var bbuf bytes.Buffer
   222  	if _, err = c.WriteTo(&bbuf); err != nil {
   223  		return
   224  	}
   225  	buf = bbuf.Bytes()
   226  	return
   227  }
   228  
   229  func (c *AppendCmd) UnmarshalBinary(buf []byte) error {
   230  	bbuf := bytes.NewBuffer(buf)
   231  	_, err := c.ReadFrom(bbuf)
   232  	return err
   233  }
   234  
   235  func (c *AppendCmd) UnmarshalBinaryV1(buf []byte) error {
   236  	bbuf := bytes.NewBuffer(buf)
   237  	_, err := c.ReadFromV1(bbuf)
   238  	return err
   239  }
   240  
   241  func (c *AppendCmd) ApplyCommit()                  {}
   242  func (c *AppendCmd) ApplyRollback()                {}
   243  func (c *AppendCmd) SetReplayTxn(_ txnif.AsyncTxn) {}