github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/logtail/table_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 logtail 16 17 import ( 18 "testing" 19 20 "github.com/matrixorigin/matrixone/pkg/container/types" 21 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common" 22 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/txn/txnbase" 23 "github.com/stretchr/testify/assert" 24 ) 25 26 func TestTxnTable1(t *testing.T) { 27 txnCnt := 22 28 blockSize := 10 29 idAlloc := common.NewTxnIDAllocator() 30 tsAlloc := types.GlobalTsAlloctor 31 table := NewTxnTable(blockSize, tsAlloc.Alloc) 32 for i := 0; i < txnCnt; i++ { 33 txn := new(txnbase.Txn) 34 txn.TxnCtx = txnbase.NewTxnCtx(idAlloc.Alloc(), tsAlloc.Alloc(), types.TS{}) 35 txn.PrepareTS = tsAlloc.Alloc() 36 assert.NoError(t, table.AddTxn(txn)) 37 } 38 t.Log(table.BlockCount()) 39 t.Log(table.String()) 40 timestamps := make([]types.TS, 0) 41 fn1 := func(block *txnBlock) bool { 42 timestamps = append(timestamps, block.bornTS) 43 return true 44 } 45 table.Scan(fn1) 46 assert.Equal(t, 3, len(timestamps)) 47 48 cnt := 0 49 50 op := func(row RowT) (goNext bool) { 51 cnt++ 52 return true 53 } 54 55 table.ForeachRowInBetween( 56 timestamps[0].Prev(), 57 types.MaxTs(), 58 nil, 59 op, 60 ) 61 assert.Equal(t, txnCnt, cnt) 62 63 cnt = 0 64 table.ForeachRowInBetween( 65 timestamps[1], 66 types.MaxTs(), 67 nil, 68 op, 69 ) 70 assert.Equal(t, txnCnt-blockSize, cnt) 71 72 cnt = 0 73 table.ForeachRowInBetween( 74 timestamps[2], 75 types.MaxTs(), 76 nil, 77 op, 78 ) 79 assert.Equal(t, txnCnt-2*blockSize, cnt) 80 81 ckp := timestamps[0].Prev() 82 cnt = table.TruncateByTimeStamp(ckp) 83 assert.Equal(t, 0, cnt) 84 85 // these two are in first block, do not delete 86 ckp = timestamps[0].Next() 87 cnt = table.TruncateByTimeStamp(ckp) 88 assert.Equal(t, 0, cnt) 89 90 ckp = timestamps[1].Prev() 91 cnt = table.TruncateByTimeStamp(ckp) 92 assert.Equal(t, 0, cnt) 93 94 // do not delete if truncate all 95 assert.Equal(t, 0, table.TruncateByTimeStamp(types.MaxTs())) 96 assert.Equal(t, 0, table.TruncateByTimeStamp(types.MaxTs())) 97 98 ckp = timestamps[1].Next() 99 cnt = table.TruncateByTimeStamp(ckp) 100 assert.Equal(t, 1, cnt) 101 102 ckp = timestamps[2] 103 cnt = table.TruncateByTimeStamp(ckp) 104 // 2 blocks left and skip deleting only one block 105 assert.Equal(t, 0, cnt) 106 } 107 108 func Less(a int, b int) bool { 109 return a < b 110 }