github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/tables/object_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 tables
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/container/types"
    22  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/catalog"
    23  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common"
    24  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/containers"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/handle"
    26  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/index/indexwrapper"
    27  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/tables/updates"
    28  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/testutils"
    29  	"github.com/stretchr/testify/assert"
    30  )
    31  
    32  func TestGetActiveRow(t *testing.T) {
    33  	defer testutils.AfterTest(t)()
    34  	ts1 := types.BuildTS(1, 0)
    35  	ts2 := types.BuildTS(2, 0)
    36  	schema := catalog.MockSchema(1, 0)
    37  	c := catalog.MockCatalog()
    38  	defer c.Close()
    39  
    40  	db, _ := c.CreateDBEntry("db", "", "", nil)
    41  	table, _ := db.CreateTableEntry(schema, nil, nil)
    42  	obj, _ := table.CreateObject(nil, catalog.ES_Appendable, nil, nil)
    43  	mvcc := updates.NewAppendMVCCHandle(obj)
    44  	// blk := &dataBlock{
    45  	// 	mvcc: mvcc,
    46  	// }
    47  	b := &baseObject{
    48  		RWMutex:    mvcc.RWMutex,
    49  		appendMVCC: mvcc,
    50  		meta:       obj,
    51  	}
    52  	mnode := &memoryNode{
    53  		object: b,
    54  	}
    55  	blk := &aobject{baseObject: b}
    56  
    57  	mnode.Ref()
    58  	n := NewNode(mnode)
    59  	blk.node.Store(n)
    60  
    61  	// appendnode1 [0,1)
    62  	an1, _ := mvcc.AddAppendNodeLocked(nil, 0, 1)
    63  	an1.Start = ts1
    64  	an1.Prepare = ts1
    65  	an1.End = ts1
    66  
    67  	// appendnode1 [1,2)
    68  	an2, _ := mvcc.AddAppendNodeLocked(nil, 1, 2)
    69  	an2.Start = ts1
    70  	an2.Prepare = ts1
    71  	an2.End = ts1
    72  
    73  	// index uint8(1)-0,1
    74  	vec := containers.MakeVector(types.T_int8.ToType(), common.DefaultAllocator)
    75  	vec.Append(int8(1), false)
    76  	vec.Append(int8(1), false)
    77  	idx := indexwrapper.NewMutIndex(types.T_int8.ToType())
    78  	err := idx.BatchUpsert(vec.GetDownstreamVector(), 0)
    79  	assert.NoError(t, err)
    80  	blk.node.Load().MustMNode().pkIndex = idx
    81  
    82  	node := blk.node.Load().MustMNode()
    83  	// row, err := blk.GetActiveRow(int8(1), ts2)
    84  	_, row, err := node.GetRowByFilter(
    85  		context.Background(), updates.MockTxnWithStartTS(ts2), handle.NewEQFilter(int8(1)), common.DefaultAllocator,
    86  	)
    87  	assert.NoError(t, err)
    88  	assert.Equal(t, uint32(1), row)
    89  
    90  	//abort appendnode2
    91  	an2.Aborted = true
    92  
    93  	_, row, err = node.GetRowByFilter(
    94  		context.Background(), updates.MockTxnWithStartTS(ts2), handle.NewEQFilter(int8(1)), common.DefaultAllocator,
    95  	)
    96  	// row, err = blk.GetActiveRow(int8(1), ts2)
    97  	assert.NoError(t, err)
    98  	assert.Equal(t, uint32(0), row)
    99  }