github.com/zuoyebang/bitalostable@v1.0.1-0.20240229032404-e3b99a834294/internal/keyspan/filter_test.go (about)

     1  // Copyright 2022 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  	"github.com/zuoyebang/bitalostable/internal/testkeys"
    15  )
    16  
    17  func TestFilteringIter(t *testing.T) {
    18  	// makeFilter returns a FilterFunc that will filter out all keys in a Span
    19  	// that are not of the given kind. Empty spans are skipped.
    20  	makeFilter := func(kind base.InternalKeyKind) FilterFunc {
    21  		return func(in *Span, out *Span) (keep bool) {
    22  			out.Start, out.End = in.Start, in.End
    23  			out.Keys = out.Keys[:0]
    24  			for _, k := range in.Keys {
    25  				if k.Kind() != kind {
    26  					continue
    27  				}
    28  				out.Keys = append(out.Keys, k)
    29  			}
    30  			return len(out.Keys) > 0
    31  		}
    32  	}
    33  
    34  	cmp := testkeys.Comparer.Compare
    35  	var spans []Span
    36  	datadriven.RunTest(t, "testdata/filtering_iter", func(td *datadriven.TestData) string {
    37  		switch cmd := td.Cmd; cmd {
    38  		case "define":
    39  			spans = spans[:0]
    40  			lines := strings.Split(strings.TrimSpace(td.Input), "\n")
    41  			for _, line := range lines {
    42  				spans = append(spans, ParseSpan(line))
    43  			}
    44  			return ""
    45  
    46  		case "iter":
    47  			var filter FilterFunc
    48  			for _, cmdArg := range td.CmdArgs {
    49  				switch cmdArg.Key {
    50  				case "filter":
    51  					for _, s := range cmdArg.Vals {
    52  						switch s {
    53  						case "no-op":
    54  							filter = nil
    55  						case "key-kind-set":
    56  							filter = makeFilter(base.InternalKeyKindRangeKeySet)
    57  						case "key-kind-unset":
    58  							filter = makeFilter(base.InternalKeyKindRangeKeyUnset)
    59  						case "key-kind-del":
    60  							filter = makeFilter(base.InternalKeyKindRangeKeyDelete)
    61  						default:
    62  							return fmt.Sprintf("unknown filter: %s", s)
    63  						}
    64  					}
    65  				default:
    66  					return fmt.Sprintf("unknown command: %s", cmdArg.Key)
    67  				}
    68  			}
    69  			innerIter := NewIter(cmp, spans)
    70  			iter := Filter(innerIter, filter)
    71  			defer iter.Close()
    72  			s := runFragmentIteratorCmd(iter, td.Input, nil)
    73  			return s
    74  
    75  		default:
    76  			return fmt.Sprintf("unknown command: %s", cmd)
    77  		}
    78  	})
    79  }