github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/store/nbs/table_persister_test.go (about)

     1  // Copyright 2019 Dolthub, Inc.
     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  // This file incorporates work covered by the following copyright and
    16  // permission notice:
    17  //
    18  // Copyright 2017 Attic Labs, Inc. All rights reserved.
    19  // Licensed under the Apache License, version 2.0:
    20  // http://www.apache.org/licenses/LICENSE-2.0
    21  
    22  package nbs
    23  
    24  import (
    25  	"context"
    26  	"testing"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  	"github.com/stretchr/testify/require"
    30  )
    31  
    32  func TestPlanCompaction(t *testing.T) {
    33  	ctx := context.Background()
    34  	assert := assert.New(t)
    35  	tableContents := [][][]byte{
    36  		{[]byte("hello2"), []byte("goodbye2"), []byte("badbye2")},
    37  		{[]byte("red"), []byte("blue")},
    38  		{[]byte("solo")},
    39  	}
    40  
    41  	q := &UnlimitedQuotaProvider{}
    42  
    43  	var sources chunkSources
    44  	var dataLens []uint64
    45  	var totalUnc uint64
    46  	for _, content := range tableContents {
    47  		for _, chnk := range content {
    48  			totalUnc += uint64(len(chnk))
    49  		}
    50  		data, name, err := buildTable(content)
    51  		require.NoError(t, err)
    52  		ti, err := parseTableIndexByCopy(ctx, data, q)
    53  		require.NoError(t, err)
    54  		tr, err := newTableReader(ti, tableReaderAtFromBytes(data), fileBlockSize)
    55  		require.NoError(t, err)
    56  		src := chunkSourceAdapter{tr, name}
    57  		dataLens = append(dataLens, uint64(len(data))-indexSize(mustUint32(src.count()))-footerSize)
    58  		sources = append(sources, src)
    59  	}
    60  	defer func() {
    61  		for _, s := range sources {
    62  			s.close()
    63  		}
    64  	}()
    65  
    66  	plan, err := planRangeCopyConjoin(sources, &Stats{})
    67  	require.NoError(t, err)
    68  
    69  	var totalChunks uint32
    70  	for i, src := range sources {
    71  		assert.Equal(dataLens[i], plan.sources.sws[i].dataLen)
    72  		totalChunks += mustUint32(src.count())
    73  	}
    74  
    75  	idx, err := parseTableIndexByCopy(ctx, plan.mergedIndex, q)
    76  	require.NoError(t, err)
    77  
    78  	assert.Equal(totalChunks, idx.chunkCount())
    79  	assert.Equal(totalUnc, idx.totalUncompressedData())
    80  
    81  	tr, err := newTableReader(idx, tableReaderAtFromBytes(nil), fileBlockSize)
    82  	require.NoError(t, err)
    83  	defer tr.close()
    84  	for _, content := range tableContents {
    85  		assertChunksInReader(content, tr, assert)
    86  	}
    87  }