github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/txn/txnimpl/command_test.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  	"testing"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/container/types"
    21  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/catalog"
    22  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/handle"
    23  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/tables/updates"
    24  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/testutils"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/txn/txnbase"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func TestComposedCmd(t *testing.T) {
    30  	defer testutils.AfterTest(t)()
    31  	testutils.EnsureNoLeak(t)
    32  	composed := txnbase.NewComposedCmd(0)
    33  	defer composed.Close()
    34  
    35  	schema := catalog.MockSchema(1, 0)
    36  	c := catalog.MockCatalog()
    37  	defer c.Close()
    38  
    39  	db, _ := c.CreateDBEntry("db", "", "", nil)
    40  	dbCmd, err := db.MakeCommand(1)
    41  	assert.Nil(t, err)
    42  	composed.AddCmd(dbCmd)
    43  
    44  	table, _ := db.CreateTableEntry(schema, nil, nil)
    45  	tblCmd, err := table.MakeCommand(1)
    46  	assert.Nil(t, err)
    47  	composed.AddCmd(tblCmd)
    48  
    49  	obj, _ := table.CreateObject(nil, catalog.ES_Appendable, nil, nil)
    50  	objCmd, err := obj.MakeCommand(1)
    51  	assert.Nil(t, err)
    52  	composed.AddCmd(objCmd)
    53  
    54  	objMvcc := updates.NewObjectMVCCHandle(obj)
    55  
    56  	controller := updates.NewMVCCHandle(objMvcc, 0)
    57  	ts := types.NextGlobalTsForTest()
    58  
    59  	appenderMvcc := updates.NewAppendMVCCHandle(obj)
    60  
    61  	node := updates.MockAppendNode(ts, 0, 2515, appenderMvcc)
    62  	cmd := updates.NewAppendCmd(1, node)
    63  
    64  	composed.AddCmd(cmd)
    65  
    66  	del := updates.NewDeleteNode(nil, handle.DT_Normal,
    67  		updates.IOET_WALTxnCommand_DeleteNode_V2)
    68  	del.AttachTo(controller.GetDeleteChain())
    69  	cmd2, err := del.MakeCommand(1)
    70  	assert.Nil(t, err)
    71  	composed.AddCmd(cmd2)
    72  
    73  	buf, err := composed.MarshalBinary()
    74  	assert.Nil(t, err)
    75  	_, err = txnbase.BuildCommandFrom(buf)
    76  	assert.Nil(t, err)
    77  }
    78  
    79  func TestComposedCmdMaxSize(t *testing.T) {
    80  	defer testutils.AfterTest(t)()
    81  	testutils.EnsureNoLeak(t)
    82  	composed := txnbase.NewComposedCmd(1280 + txnbase.CmdBufReserved)
    83  	defer composed.Close()
    84  
    85  	schema := catalog.MockSchema(1, 0)
    86  	c := catalog.MockCatalog()
    87  	defer c.Close()
    88  
    89  	db, _ := c.CreateDBEntry("db", "", "", nil)
    90  	dbCmd, err := db.MakeCommand(1)
    91  	assert.Nil(t, err)
    92  	composed.AddCmd(dbCmd)
    93  
    94  	table, _ := db.CreateTableEntry(schema, nil, nil)
    95  	for i := 2; i <= 10; i++ {
    96  		cmd, err := table.MakeCommand(uint32(i))
    97  		assert.Nil(t, err)
    98  		composed.AddCmd(cmd)
    99  	}
   100  
   101  	buf, err := composed.MarshalBinary()
   102  	assert.Nil(t, err)
   103  	assert.Equal(t, true, composed.MoreCmds())
   104  	cmd, err := txnbase.BuildCommandFrom(buf)
   105  	assert.Nil(t, err)
   106  	c1, ok := cmd.(*txnbase.ComposedCmd)
   107  	assert.True(t, ok)
   108  	assert.NotNil(t, c1)
   109  	assert.Equal(t, composed.LastPos, len(c1.Cmds))
   110  
   111  	buf, err = composed.MarshalBinary()
   112  	assert.Nil(t, err)
   113  	assert.Equal(t, true, composed.MoreCmds())
   114  	cmd, err = txnbase.BuildCommandFrom(buf)
   115  	assert.Nil(t, err)
   116  	c2, ok := cmd.(*txnbase.ComposedCmd)
   117  	assert.True(t, ok)
   118  	assert.NotNil(t, c2)
   119  	assert.Equal(t, composed.LastPos, len(c1.Cmds)+len(c2.Cmds))
   120  
   121  	// Remove the left commands, because it has different result in different platforms.
   122  }