github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/store/prolly/tree/chunker_test.go (about)

     1  // Copyright 2021 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  package tree
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func TestTreeChunker(t *testing.T) {
    26  	t.Run("round trip tree items", func(t *testing.T) {
    27  		roundTripTreeItems(t)
    28  	})
    29  }
    30  
    31  func roundTripTreeItems(t *testing.T) {
    32  	root, items, ns := randomTree(t, 1000)
    33  	assert.NotNil(t, root)
    34  	assert.True(t, root.count > 0)
    35  	level := root.Level()
    36  	assert.True(t, level > 0)
    37  	//assert.Equal(t, uint64(1000), root.cumulativeCount())
    38  	assert.Equal(t, countTree(t, ns, root), 1000)
    39  	tc, err := root.TreeCount()
    40  	require.NoError(t, err)
    41  	assert.Equal(t, tc*2, 1000)
    42  	validateTreeItems(t, ns, root, items)
    43  
    44  	root, items, ns = randomTree(t, 10_000)
    45  	assert.NotNil(t, root)
    46  	assert.True(t, root.count > 0)
    47  	level = root.Level()
    48  	assert.True(t, level > 0)
    49  	//assert.Equal(t, uint64(10_000), root.cumulativeCount())
    50  	assert.Equal(t, countTree(t, ns, root), 10_000)
    51  	tc, err = root.TreeCount()
    52  	require.NoError(t, err)
    53  	assert.Equal(t, tc*2, 10_000)
    54  	validateTreeItems(t, ns, root, items)
    55  
    56  	root, items, ns = randomTree(t, 100_000)
    57  	assert.NotNil(t, root)
    58  	assert.True(t, root.count > 0)
    59  	level = root.Level()
    60  	assert.True(t, level > 0)
    61  	//assert.Equal(t, uint64(100_000), root.cumulativeCount())
    62  	assert.Equal(t, countTree(t, ns, root), 100_000)
    63  	tc, err = root.TreeCount()
    64  	require.NoError(t, err)
    65  	assert.Equal(t, tc*2, 100_000)
    66  	validateTreeItems(t, ns, root, items)
    67  }
    68  
    69  func countTree(t *testing.T, ns NodeStore, nd Node) (count int) {
    70  	ctx := context.Background()
    71  	err := iterTree(ctx, ns, nd, func(_ Item) (err error) {
    72  		count++
    73  		return
    74  	})
    75  	require.NoError(t, err)
    76  	return
    77  }
    78  
    79  func validateTreeItems(t *testing.T, ns NodeStore, nd Node, expected [][2]Item) {
    80  	i := 0
    81  	ctx := context.Background()
    82  	err := iterTree(ctx, ns, nd, func(actual Item) (err error) {
    83  		assert.Equal(t, expected[i/2][i%2], actual)
    84  		i++
    85  		return
    86  	})
    87  	require.NoError(t, err)
    88  	return
    89  }
    90  
    91  func iterTree(ctx context.Context, ns NodeStore, nd Node, cb func(item Item) error) error {
    92  	if nd.empty() {
    93  		return nil
    94  	}
    95  
    96  	cur, err := newCursorAtStart(ctx, ns, nd)
    97  	if err != nil {
    98  		return err
    99  	}
   100  
   101  	for !cur.outOfBounds() {
   102  		err = cb(cur.CurrentKey())
   103  		if err != nil {
   104  			return err
   105  		}
   106  
   107  		err = cb(cur.currentValue())
   108  		if err != nil {
   109  			return err
   110  		}
   111  
   112  		err = cur.advance(ctx)
   113  		if err != nil {
   114  			return err
   115  		}
   116  	}
   117  	return err
   118  }