github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/tables/updates/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 updates
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/RoaringBitmap/roaring"
    21  	"github.com/matrixorigin/matrixone/pkg/container/types"
    22  
    23  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/catalog"
    24  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/handle"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/testutils"
    26  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/txn/txnbase"
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  func TestCompactBlockCmd(t *testing.T) {
    31  	defer testutils.AfterTest(t)()
    32  	schema := catalog.MockSchema(1, 0)
    33  	c := catalog.MockCatalog()
    34  	defer c.Close()
    35  
    36  	db, _ := c.CreateDBEntry("db", "", "", nil)
    37  	table, _ := db.CreateTableEntry(schema, nil, nil)
    38  	obj, _ := table.CreateObject(nil, catalog.ES_Appendable, nil, nil)
    39  
    40  	controller := NewAppendMVCCHandle(obj)
    41  
    42  	ts := types.NextGlobalTsForTest()
    43  	//node := MockAppendNode(341, 0, 2515, controller)
    44  	node := MockAppendNode(ts, 0, 2515, controller)
    45  	cmd := NewAppendCmd(1, node)
    46  
    47  	buf, err := cmd.MarshalBinary()
    48  	assert.Nil(t, err)
    49  
    50  	cmd2, err := txnbase.BuildCommandFrom(buf)
    51  	assert.Nil(t, err)
    52  	checkAppendCmdIsEqual(t, cmd, cmd2.(*UpdateCmd))
    53  }
    54  
    55  func checkAppendCmdIsEqual(t *testing.T, cmd1, cmd2 *UpdateCmd) {
    56  	assert.Equal(t, IOET_WALTxnCommand_AppendNode, cmd1.GetType())
    57  	assert.Equal(t, IOET_WALTxnCommand_AppendNode, cmd2.GetType())
    58  	assert.Equal(t, cmd1.append.maxRow, cmd2.append.maxRow)
    59  }
    60  
    61  func TestDeleteNodeCmd(t *testing.T) {
    62  	defer testutils.AfterTest(t)()
    63  	schema := catalog.MockSchema(1, 0)
    64  	c := catalog.MockCatalog()
    65  	defer c.Close()
    66  
    67  	db, _ := c.CreateDBEntry("db", "", "", nil)
    68  	table, _ := db.CreateTableEntry(schema, nil, nil)
    69  	obj, _ := table.CreateObject(nil, catalog.ES_Appendable, nil, nil)
    70  	objHandle := NewObjectMVCCHandle(obj)
    71  
    72  	controller := NewMVCCHandle(objHandle, 0)
    73  
    74  	node := NewDeleteNode(nil, handle.DT_Normal,
    75  		IOET_WALTxnCommand_DeleteNode_CurrVer)
    76  	node.mask = roaring.NewBitmap()
    77  	node.mask.Add(35)
    78  	node.chain.Store(controller.deletes)
    79  	cmd, err := node.MakeCommand(1)
    80  	assert.Nil(t, err)
    81  
    82  	buf, err := cmd.MarshalBinary()
    83  	assert.Nil(t, err)
    84  
    85  	cmd2, err := txnbase.BuildCommandFrom(buf)
    86  	assert.Nil(t, err)
    87  	checkDeleteCmdIsEqual(t, cmd.(*UpdateCmd), cmd2.(*UpdateCmd))
    88  }
    89  
    90  func checkDeleteCmdIsEqual(t *testing.T, cmd1, cmd2 *UpdateCmd) {
    91  	assert.Equal(t, IOET_WALTxnCommand_DeleteNode, cmd1.GetType())
    92  	assert.Equal(t, IOET_WALTxnCommand_DeleteNode, cmd2.GetType())
    93  	assert.Equal(t, cmd1.delete.GetCardinalityLocked(), cmd2.delete.GetCardinalityLocked())
    94  	mask1 := cmd1.delete.mask
    95  	if mask1 != nil && !mask1.IsEmpty() {
    96  		it := mask1.Iterator()
    97  		for it.HasNext() {
    98  			row := it.Next()
    99  			assert.True(t, cmd2.delete.mask.Contains(row))
   100  		}
   101  	}
   102  }