github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/disttae/txn_table_test.go (about) 1 // Copyright 2023 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 disttae 16 17 import ( 18 "context" 19 "github.com/matrixorigin/matrixone/pkg/pb/timestamp" 20 "github.com/matrixorigin/matrixone/pkg/txn/client" 21 "testing" 22 23 "github.com/matrixorigin/matrixone/pkg/common/mpool" 24 "github.com/matrixorigin/matrixone/pkg/container/batch" 25 "github.com/matrixorigin/matrixone/pkg/container/types" 26 "github.com/matrixorigin/matrixone/pkg/container/vector" 27 "github.com/matrixorigin/matrixone/pkg/fileservice" 28 "github.com/stretchr/testify/assert" 29 ) 30 31 func newTxnTableForTest( 32 mp *mpool.MPool, 33 ) *txnTable { 34 engine := &Engine{ 35 packerPool: fileservice.NewPool( 36 128, 37 func() *types.Packer { 38 return types.NewPacker(mp) 39 }, 40 func(packer *types.Packer) { 41 packer.Reset() 42 }, 43 func(packer *types.Packer) { 44 packer.FreeMem() 45 }, 46 ), 47 } 48 var tnStore DNStore 49 txn := &Transaction{ 50 engine: engine, 51 tnStores: []DNStore{tnStore}, 52 } 53 c := client.NewTxnClient(nil, nil, nil) 54 op, _ := c.New(context.Background(), timestamp.Timestamp{}) 55 op.AddWorkspace(txn) 56 57 db := &txnDatabase{ 58 op: op, 59 } 60 table := &txnTable{ 61 db: db, 62 primaryIdx: 0, 63 } 64 return table 65 } 66 67 func makeBatchForTest( 68 mp *mpool.MPool, 69 ints ...int64, 70 ) *batch.Batch { 71 bat := batch.New(false, []string{"a"}) 72 vec := vector.NewVec(types.T_int64.ToType()) 73 for _, n := range ints { 74 vector.AppendFixed(vec, n, false, mp) 75 } 76 bat.SetVector(0, vec) 77 bat.SetRowCount(len(ints)) 78 return bat 79 } 80 81 // func TestPrimaryKeyCheck(t *testing.T) { 82 // ctx := context.Background() 83 // mp := mpool.MustNewZero() 84 85 // getRowIDsBatch := func(table *txnTable) *batch.Batch { 86 // bat := batch.New(false, []string{catalog.Row_ID}) 87 // vec := vector.NewVec(types.T_Rowid.ToType()) 88 // iter := table.localState.NewRowsIter( 89 // types.TimestampToTS(table.nextLocalTS()), 90 // nil, 91 // false, 92 // ) 93 // l := 0 94 // for iter.Next() { 95 // entry := iter.Entry() 96 // vector.AppendFixed(vec, entry.RowID, false, mp) 97 // l++ 98 // } 99 // iter.Close() 100 // bat.SetVector(0, vec) 101 // bat.SetZs(l, mp) 102 // return bat 103 // } 104 105 // table := newTxnTableForTest(mp) 106 107 // // insert 108 // err := table.Write( 109 // ctx, 110 // makeBatchForTest(mp, 1), 111 // ) 112 // assert.Nil(t, err) 113 114 // // // insert duplicated 115 // // we check duplicated in pipeline runing now 116 // // err = table.Write( 117 // // ctx, 118 // // makeBatchForTest(mp, 1), 119 // // ) 120 // // assert.True(t, moerr.IsMoErrCode(err, moerr.ErrDuplicateEntry)) 121 122 // // insert no duplicated 123 // err = table.Write( 124 // ctx, 125 // makeBatchForTest(mp, 2, 3), 126 // ) 127 // assert.Nil(t, err) 128 129 // // duplicated in same batch 130 // // we check duplicated in pipeline runing now 131 // // err = table.Write( 132 // // ctx, 133 // // makeBatchForTest(mp, 4, 4), 134 // // ) 135 // // assert.True(t, moerr.IsMoErrCode(err, moerr.ErrDuplicateEntry)) 136 137 // table = newTxnTableForTest(mp) 138 139 // // insert, delete then insert 140 // err = table.Write( 141 // ctx, 142 // makeBatchForTest(mp, 1), 143 // ) 144 // assert.Nil(t, err) 145 // err = table.Delete( 146 // ctx, 147 // getRowIDsBatch(table), 148 // catalog.Row_ID, 149 // ) 150 // assert.Nil(t, err) 151 // err = table.Write( 152 // ctx, 153 // makeBatchForTest(mp, 5), 154 // ) 155 // assert.Nil(t, err) 156 157 // } 158 159 func BenchmarkTxnTableInsert(b *testing.B) { 160 ctx := context.Background() 161 mp := mpool.MustNewZero() 162 table := newTxnTableForTest(mp) 163 for i, max := int64(0), int64(b.N); i < max; i++ { 164 err := table.Write( 165 ctx, 166 makeBatchForTest(mp, i), 167 ) 168 assert.Nil(b, err) 169 } 170 }