github.com/cockroachdb/pebble@v1.1.1-0.20240513155919-3622ade60459/internal/keyspan/seek_test.go (about)

     1  // Copyright 2018 The LevelDB-Go and Pebble Authors. All rights reserved. Use
     2  // of this source code is governed by a BSD-style license that can be found in
     3  // the LICENSE file.
     4  
     5  package keyspan
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"strconv"
    11  	"strings"
    12  	"testing"
    13  
    14  	"github.com/cockroachdb/datadriven"
    15  	"github.com/cockroachdb/pebble/internal/base"
    16  )
    17  
    18  func TestSeek(t *testing.T) {
    19  	cmp := base.DefaultComparer.Compare
    20  	fmtKey := base.DefaultComparer.FormatKey
    21  	var iter FragmentIterator
    22  	var buf bytes.Buffer
    23  
    24  	datadriven.RunTest(t, "testdata/seek", func(t *testing.T, d *datadriven.TestData) string {
    25  		buf.Reset()
    26  		switch d.Cmd {
    27  		case "build":
    28  			spans := buildSpans(t, cmp, fmtKey, d.Input, base.InternalKeyKindRangeDelete)
    29  			for _, s := range spans {
    30  				fmt.Fprintln(&buf, s)
    31  			}
    32  			iter = NewIter(cmp, spans)
    33  			return buf.String()
    34  		case "seek-ge", "seek-le":
    35  			seek := SeekLE
    36  			if d.Cmd == "seek-ge" {
    37  				seek = func(_ base.Compare, iter FragmentIterator, key []byte) *Span {
    38  					return iter.SeekGE(key)
    39  				}
    40  			}
    41  
    42  			for _, line := range strings.Split(d.Input, "\n") {
    43  				parts := strings.Fields(line)
    44  				if len(parts) != 2 {
    45  					return fmt.Sprintf("malformed input: %s", line)
    46  				}
    47  				seq, err := strconv.ParseUint(parts[1], 10, 64)
    48  				if err != nil {
    49  					return err.Error()
    50  				}
    51  				span := seek(cmp, iter, []byte(parts[0]))
    52  				if span != nil {
    53  					visible := span.Visible(seq)
    54  					span = &visible
    55  				}
    56  				fmt.Fprintln(&buf, span)
    57  			}
    58  			return buf.String()
    59  		default:
    60  			return fmt.Sprintf("unknown command: %s", d.Cmd)
    61  		}
    62  	})
    63  }