github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/dataio/blockio/writer_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 blockio
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    21  	"github.com/matrixorigin/matrixone/pkg/container/batch"
    22  	"github.com/matrixorigin/matrixone/pkg/container/types"
    23  	"github.com/matrixorigin/matrixone/pkg/defines"
    24  	"github.com/matrixorigin/matrixone/pkg/fileservice"
    25  	"github.com/matrixorigin/matrixone/pkg/objectio"
    26  	"github.com/matrixorigin/matrixone/pkg/testutil"
    27  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/index"
    28  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/testutils"
    29  	"github.com/stretchr/testify/assert"
    30  	"github.com/stretchr/testify/require"
    31  	"path"
    32  	"testing"
    33  )
    34  
    35  const (
    36  	ModuleName = "BlockIO"
    37  )
    38  
    39  func newBatch() *batch.Batch {
    40  	mp := mpool.MustNewZero()
    41  	types := []types.Type{
    42  		{Oid: types.T_int32},
    43  		{Oid: types.T_int16},
    44  		{Oid: types.T_int32},
    45  		{Oid: types.T_int64},
    46  		{Oid: types.T_uint16},
    47  		{Oid: types.T_uint32},
    48  		{Oid: types.T_uint8},
    49  		{Oid: types.T_uint64},
    50  	}
    51  	return testutil.NewBatch(types, false, int(40000*2), mp)
    52  }
    53  
    54  func TestWriter_WriteBlockAndZoneMap(t *testing.T) {
    55  	defer testutils.AfterTest(t)()
    56  	dir := testutils.InitTestEnv(ModuleName, t)
    57  	dir = path.Join(dir, "/local")
    58  	id := 1
    59  	name := fmt.Sprintf("%d.blk", id)
    60  	bat := newBatch()
    61  	c := fileservice.Config{
    62  		Name:    defines.LocalFileServiceName,
    63  		Backend: "DISK",
    64  		DataDir: dir,
    65  	}
    66  	service, err := fileservice.NewFileService(c)
    67  	assert.Nil(t, err)
    68  	fs := objectio.NewObjectFS(service, "local")
    69  	writer := NewWriter(context.Background(), fs, name)
    70  	idxs := make([]uint16, 3)
    71  	idxs[0] = 0
    72  	idxs[1] = 2
    73  	idxs[2] = 4
    74  	_, err = writer.WriteBlockAndZoneMap(bat, idxs)
    75  	assert.Nil(t, err)
    76  	blocks, err := writer.Sync()
    77  	assert.Nil(t, err)
    78  	assert.Equal(t, 1, len(blocks))
    79  	fd := blocks[0]
    80  	col, err := fd.GetColumn(0)
    81  	assert.Nil(t, err)
    82  	zm := index.NewZoneMap(bat.Vecs[0].Typ)
    83  	colZoneMap := col.GetMeta().GetZoneMap()
    84  
    85  	err = zm.Unmarshal(colZoneMap.GetData())
    86  	require.NoError(t, err)
    87  	res := zm.Contains(int32(500))
    88  	require.True(t, res)
    89  	res = zm.Contains(int32(79999))
    90  	require.True(t, res)
    91  	res = zm.Contains(int32(100000))
    92  	require.False(t, res)
    93  }