github.com/matrixorigin/matrixone@v1.2.0/pkg/objectio/object_stats_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 objectio
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"github.com/matrixorigin/matrixone/pkg/container/types"
    21  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/index"
    22  	"math/rand"
    23  	"testing"
    24  
    25  	"github.com/stretchr/testify/require"
    26  )
    27  
    28  func Test_ObjectStats(t *testing.T) {
    29  	// test nil object stats and input data
    30  	require.NotNil(t, SetObjectStatsObjectName(nil, []byte("nil object stats")))
    31  	require.NotNil(t, SetObjectStatsObjectName(NewObjectStats(), nil))
    32  
    33  	// test setter and getter methods
    34  	stats := NewObjectStats()
    35  	require.True(t, stats.IsZero())
    36  
    37  	objName := BuildObjectName(&types.Uuid{0x1f, 0x2f}, 0)
    38  	require.Nil(t, SetObjectStatsObjectName(stats, objName))
    39  	require.True(t, bytes.Equal(stats.ObjectName(), objName))
    40  
    41  	extent := NewExtent(0x1f, 0x2f, 0x3f, 0x4f)
    42  	require.Nil(t, SetObjectStatsExtent(stats, extent))
    43  	require.True(t, bytes.Equal(stats.Extent(), extent))
    44  
    45  	blkCnt := uint32(99)
    46  	rowCnt := uint32(98)
    47  
    48  	require.Nil(t, SetObjectStatsBlkCnt(stats, blkCnt))
    49  	require.Equal(t, stats.BlkCnt(), blkCnt)
    50  
    51  	require.Nil(t, SetObjectStatsRowCnt(stats, rowCnt))
    52  	require.Equal(t, stats.Rows(), rowCnt)
    53  
    54  	sortKeyZoneMap := index.BuildZM(types.T_uint8, []byte{0xa, 0xb, 0xc, 0xd})
    55  
    56  	require.Nil(t, SetObjectStatsSortKeyZoneMap(stats, sortKeyZoneMap))
    57  	require.True(t, bytes.Equal(stats.SortKeyZoneMap(), sortKeyZoneMap))
    58  
    59  	require.True(t, bytes.Equal(stats.ObjectLocation(), BuildLocation(objName, extent, 0, 0)))
    60  
    61  	// test set location
    62  	loc := BuildLocation([]byte{0x3f}, []byte{0x7f}, 0, 0)
    63  	require.Nil(t, SetObjectStatsLocation(stats, loc))
    64  	require.True(t, bytes.Equal(stats.ObjectLocation(), loc))
    65  
    66  	x := BuildObjectBlockid(objName, 0)
    67  	s := ShortName(x)
    68  	SetObjectStatsShortName(stats, s)
    69  	require.True(t, bytes.Equal(stats.ObjectShortName()[:], s[:]))
    70  }
    71  
    72  func TestObjectStats_Clone(t *testing.T) {
    73  	stats := NewObjectStats()
    74  	require.Nil(t, SetObjectStatsRowCnt(stats, 99))
    75  
    76  	copied := stats.Clone()
    77  	require.True(t, bytes.Equal(stats.Marshal(), copied.Marshal()))
    78  
    79  	require.Nil(t, SetObjectStatsRowCnt(copied, 199))
    80  	require.False(t, bytes.Equal(stats.Marshal(), copied.Marshal()))
    81  
    82  	fmt.Println(stats.String())
    83  	fmt.Println(copied.String())
    84  }
    85  
    86  func TestObjectStats_Marshal_UnMarshal(t *testing.T) {
    87  	rawBytes := make([]byte, ObjectStatsLen)
    88  	for idx := 0; idx < ObjectStatsLen; idx++ {
    89  		rr := rand.Uint32()
    90  		rawBytes[idx] = types.EncodeUint32(&rr)[0]
    91  	}
    92  
    93  	stats := NewObjectStats()
    94  	stats.UnMarshal(rawBytes)
    95  
    96  	require.True(t, bytes.Equal(stats.Marshal(), rawBytes))
    97  	fmt.Println(stats.String())
    98  }