github.com/zuoyebang/bitalostable@v1.0.1-0.20240229032404-e3b99a834294/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/zuoyebang/bitalostable/internal/base" 13 "github.com/zuoyebang/bitalostable/internal/datadriven" 14 ) 15 16 func TestTruncate(t *testing.T) { 17 cmp := base.DefaultComparer.Compare 18 fmtKey := base.DefaultComparer.FormatKey 19 var iter FragmentIterator 20 21 datadriven.RunTest(t, "testdata/truncate", func(d *datadriven.TestData) string { 22 switch d.Cmd { 23 case "build": 24 tombstones := buildSpans(t, cmp, fmtKey, d.Input, base.InternalKeyKindRangeDelete) 25 iter = NewIter(cmp, tombstones) 26 return formatAlphabeticSpans(tombstones) 27 28 case "truncate": 29 if len(d.Input) > 0 { 30 t.Fatalf("unexpected input: %s", d.Input) 31 } 32 if len(d.CmdArgs) < 1 || len(d.CmdArgs) > 3 { 33 t.Fatalf("expected 1-3 arguments: %s", d.CmdArgs) 34 } 35 parts := strings.Split(d.CmdArgs[0].String(), "-") 36 var startKey, endKey *base.InternalKey 37 if len(d.CmdArgs) > 1 { 38 for _, arg := range d.CmdArgs[1:] { 39 switch arg.Key { 40 case "startKey": 41 startKey = &base.InternalKey{} 42 *startKey = base.ParseInternalKey(arg.Vals[0]) 43 case "endKey": 44 endKey = &base.InternalKey{} 45 *endKey = base.ParseInternalKey(arg.Vals[0]) 46 } 47 } 48 } 49 if len(parts) != 2 { 50 t.Fatalf("malformed arg: %s", d.CmdArgs[0]) 51 } 52 lower := []byte(parts[0]) 53 upper := []byte(parts[1]) 54 55 tIter := Truncate(cmp, iter, lower, upper, startKey, endKey) 56 defer tIter.Close() 57 var truncated []Span 58 for s := tIter.First(); s != nil; s = tIter.Next() { 59 truncated = append(truncated, s.ShallowClone()) 60 } 61 return formatAlphabeticSpans(truncated) 62 63 default: 64 return fmt.Sprintf("unknown command: %s", d.Cmd) 65 } 66 }) 67 }