github.com/matrixorigin/matrixone@v1.2.0/pkg/txn/storage/mem/mvcc_kv_test.go (about)

     1  // Copyright 2021 - 2022 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 mem
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/pb/timestamp"
    21  	"github.com/stretchr/testify/assert"
    22  )
    23  
    24  func TestMVCCSetAndGetAndDelete(t *testing.T) {
    25  	mkv := NewMVCCKV()
    26  	k := []byte("k")
    27  
    28  	versions := []timestamp.Timestamp{
    29  		{PhysicalTime: 1},
    30  		{PhysicalTime: 1, LogicalTime: 1},
    31  		{PhysicalTime: 1, LogicalTime: 2},
    32  		{PhysicalTime: 1, LogicalTime: 3},
    33  	}
    34  	values := [][]byte{[]byte("1-0"), []byte("1-1"), []byte("1-2"), []byte("1-3")}
    35  
    36  	for i := 0; i < len(versions)-1; i++ {
    37  		mkv.Set(k, versions[i], values[i])
    38  	}
    39  
    40  	for i := 0; i < len(versions); i++ {
    41  		v, ok := mkv.Get(k, versions[i])
    42  		if i < len(versions)-1 {
    43  			assert.True(t, ok)
    44  			assert.Equal(t, values[i], v)
    45  		} else {
    46  			assert.False(t, ok)
    47  			assert.Empty(t, v)
    48  		}
    49  	}
    50  
    51  	mkv.Delete(k, versions[0])
    52  	v, ok := mkv.Get(k, versions[0])
    53  	assert.False(t, ok)
    54  	assert.Empty(t, v)
    55  }
    56  
    57  func TestMVCCAscendRange(t *testing.T) {
    58  	mkv := NewMVCCKV()
    59  	k := []byte("k")
    60  
    61  	versions := []timestamp.Timestamp{
    62  		{PhysicalTime: 1},
    63  		{PhysicalTime: 1, LogicalTime: 1},
    64  		{PhysicalTime: 1, LogicalTime: 2},
    65  		{PhysicalTime: 1, LogicalTime: 3},
    66  	}
    67  	values := [][]byte{[]byte("1-0"), []byte("1-1"), []byte("1-2"), []byte("1-3")}
    68  
    69  	for i := 0; i < len(versions); i++ {
    70  		mkv.Set(k, versions[i], values[i])
    71  	}
    72  
    73  	var scanVersions []timestamp.Timestamp
    74  	var scanValues [][]byte
    75  	fn := func(v []byte, ts timestamp.Timestamp) {
    76  		scanValues = append(scanValues, v)
    77  		scanVersions = append(scanVersions, ts)
    78  	}
    79  
    80  	mkv.AscendRange(k, versions[0], versions[3], fn)
    81  	assert.Equal(t, versions[:3], scanVersions)
    82  	assert.Equal(t, values[:3], scanValues)
    83  
    84  	scanVersions = scanVersions[:0]
    85  	scanValues = scanValues[:0]
    86  	mkv.AscendRange(k, versions[0], versions[0], fn)
    87  	assert.Empty(t, scanVersions)
    88  	assert.Empty(t, scanValues)
    89  }