github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/tables/txnentries/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 txnentries
    16  
    17  import (
    18  	"bytes"
    19  	"testing"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common"
    22  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/testutils"
    23  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/txn/txnbase"
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  func TestCompactBlockCmd(t *testing.T) {
    28  	defer testutils.AfterTest(t)()
    29  	testutils.EnsureNoLeak(t)
    30  	from := &common.ID{TableID: 1, SegmentID: 2, BlockID: 3}
    31  	to := &common.ID{TableID: 1, SegmentID: 3, BlockID: 1}
    32  	cmd := newCompactBlockCmd(from, to, nil, 0)
    33  
    34  	var w bytes.Buffer
    35  	_, err := cmd.WriteTo(&w)
    36  	assert.Nil(t, err)
    37  
    38  	buf := w.Bytes()
    39  	r := bytes.NewBuffer(buf)
    40  
    41  	cmd2, _, err := txnbase.BuildCommandFrom(r)
    42  	assert.Nil(t, err)
    43  	checkCompactBlockCmdIsEqual(t, cmd, cmd2.(*compactBlockCmd))
    44  }
    45  
    46  func checkCompactBlockCmdIsEqual(t *testing.T, cmd1, cmd2 *compactBlockCmd) {
    47  	checkIDIsEqual(t, cmd1.from, cmd2.from)
    48  	checkIDIsEqual(t, cmd1.to, cmd2.to)
    49  }
    50  
    51  func checkIDIsEqual(t *testing.T, id1, id2 *common.ID) {
    52  	assert.Equal(t, id1.TableID, id2.TableID)
    53  	assert.Equal(t, id1.SegmentID, id2.SegmentID)
    54  	assert.Equal(t, id1.BlockID, id2.BlockID)
    55  }
    56  
    57  func TestMergeBlocksCmd(t *testing.T) {
    58  	defer testutils.AfterTest(t)()
    59  	testutils.EnsureNoLeak(t)
    60  	droppedSegs := []*common.ID{{TableID: 1, SegmentID: 2}, {TableID: 1, SegmentID: 2}}
    61  	createdSegs := []*common.ID{{TableID: 1, SegmentID: 3}}
    62  	droppedBlks := []*common.ID{{TableID: 1, SegmentID: 2, BlockID: 3}, {TableID: 1, SegmentID: 2, BlockID: 4}}
    63  	createdBlks := []*common.ID{{TableID: 1, SegmentID: 3, BlockID: 1}}
    64  	mapping := []uint32{3445, 4253, 425, 45, 123, 34, 42, 42, 2, 5, 0}
    65  	fromAddr := []uint32{40000, 40000, 40000, 42}
    66  	toAddr := []uint32{40000, 40000, 242}
    67  	cmd := newMergeBlocksCmd(
    68  		0,
    69  		droppedSegs,
    70  		createdSegs,
    71  		droppedBlks,
    72  		createdBlks,
    73  		mapping,
    74  		fromAddr,
    75  		toAddr,
    76  		nil,
    77  		0)
    78  
    79  	var w bytes.Buffer
    80  	_, err := cmd.WriteTo(&w)
    81  	assert.Nil(t, err)
    82  
    83  	buf := w.Bytes()
    84  	r := bytes.NewBuffer(buf)
    85  
    86  	cmd2, _, err := txnbase.BuildCommandFrom(r)
    87  	assert.Nil(t, err)
    88  	checkMergeBlocksCmdIsEqual(t, cmd, cmd2.(*mergeBlocksCmd))
    89  }
    90  
    91  func checkMergeBlocksCmdIsEqual(t *testing.T, cmd1, cmd2 *mergeBlocksCmd) {
    92  	assert.Equal(t, len(cmd1.createdSegs), len(cmd2.createdSegs))
    93  	for i, seg1 := range cmd1.createdSegs {
    94  		checkIDIsEqual(t, seg1, cmd2.createdSegs[i])
    95  	}
    96  	assert.Equal(t, len(cmd1.createdBlks), len(cmd2.createdBlks))
    97  	for i, blk1 := range cmd1.createdBlks {
    98  		checkIDIsEqual(t, blk1, cmd2.createdBlks[i])
    99  	}
   100  	assert.Equal(t, len(cmd1.droppedSegs), len(cmd2.droppedSegs))
   101  	for i, seg1 := range cmd1.droppedSegs {
   102  		checkIDIsEqual(t, seg1, cmd2.droppedSegs[i])
   103  	}
   104  	assert.Equal(t, len(cmd1.droppedBlks), len(cmd2.droppedBlks))
   105  	for i, blk1 := range cmd1.droppedBlks {
   106  		checkIDIsEqual(t, blk1, cmd2.droppedBlks[i])
   107  	}
   108  	assert.Equal(t, len(cmd1.fromAddr), len(cmd2.fromAddr))
   109  	assert.Equal(t, len(cmd1.toAddr), len(cmd2.toAddr))
   110  }