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

     1  // Copyright 2019 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  	"fmt"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/cockroachdb/datadriven"
    13  	"github.com/cockroachdb/pebble/internal/base"
    14  )
    15  
    16  func TestTruncate(t *testing.T) {
    17  	cmp := base.DefaultComparer.Compare
    18  	fmtKey := base.DefaultComparer.FormatKey
    19  	var iter FragmentIterator
    20  	var savedIter FragmentIterator
    21  	defer func() {
    22  		if savedIter != nil {
    23  			savedIter.Close()
    24  			savedIter = nil
    25  		}
    26  	}()
    27  
    28  	datadriven.RunTest(t, "testdata/truncate", func(t *testing.T, d *datadriven.TestData) string {
    29  		doTruncate := func() FragmentIterator {
    30  			if len(d.Input) > 0 {
    31  				t.Fatalf("unexpected input: %s", d.Input)
    32  			}
    33  			if len(d.CmdArgs) < 1 || len(d.CmdArgs) > 3 {
    34  				t.Fatalf("expected 1-3 arguments: %s", d.CmdArgs)
    35  			}
    36  			parts := strings.Split(d.CmdArgs[0].String(), "-")
    37  			var startKey, endKey *base.InternalKey
    38  			if len(d.CmdArgs) > 1 {
    39  				for _, arg := range d.CmdArgs[1:] {
    40  					switch arg.Key {
    41  					case "startKey":
    42  						startKey = &base.InternalKey{}
    43  						*startKey = base.ParseInternalKey(arg.Vals[0])
    44  					case "endKey":
    45  						endKey = &base.InternalKey{}
    46  						*endKey = base.ParseInternalKey(arg.Vals[0])
    47  					}
    48  				}
    49  			}
    50  			if len(parts) != 2 {
    51  				t.Fatalf("malformed arg: %s", d.CmdArgs[0])
    52  			}
    53  			lower := []byte(parts[0])
    54  			upper := []byte(parts[1])
    55  
    56  			tIter := Truncate(
    57  				cmp, iter, lower, upper, startKey, endKey, false,
    58  			)
    59  			return tIter
    60  		}
    61  
    62  		switch d.Cmd {
    63  		case "build":
    64  			tombstones := buildSpans(t, cmp, fmtKey, d.Input, base.InternalKeyKindRangeDelete)
    65  			iter = NewIter(cmp, tombstones)
    66  			return formatAlphabeticSpans(tombstones)
    67  
    68  		case "truncate":
    69  			tIter := doTruncate()
    70  			defer tIter.Close()
    71  			var truncated []Span
    72  			for s := tIter.First(); s != nil; s = tIter.Next() {
    73  				truncated = append(truncated, s.ShallowClone())
    74  			}
    75  			return formatAlphabeticSpans(truncated)
    76  
    77  		case "truncate-and-save-iter":
    78  			if savedIter != nil {
    79  				savedIter.Close()
    80  			}
    81  			savedIter = doTruncate()
    82  			return "ok"
    83  
    84  		case "saved-iter":
    85  			return runIterCmd(t, d, savedIter)
    86  
    87  		default:
    88  			return fmt.Sprintf("unknown command: %s", d.Cmd)
    89  		}
    90  	})
    91  }