github.com/petermattis/pebble@v0.0.0-20190905164901-ab51a2166067/internal/rangedel/iter_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 rangedel
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/petermattis/pebble/internal/base"
    14  	"github.com/petermattis/pebble/internal/datadriven"
    15  )
    16  
    17  func TestIter(t *testing.T) {
    18  	var tombstones []Tombstone
    19  	datadriven.RunTest(t, "testdata/iter", func(d *datadriven.TestData) string {
    20  		switch d.Cmd {
    21  		case "define":
    22  			tombstones = nil
    23  			for _, key := range strings.Split(d.Input, "\n") {
    24  				j := strings.Index(key, ":")
    25  				tombstones = append(tombstones, Tombstone{
    26  					Start: base.ParseInternalKey(key[:j]),
    27  					End:   []byte(key[j+1:]),
    28  				})
    29  			}
    30  			return ""
    31  
    32  		case "iter":
    33  			iter := NewIter(base.DefaultComparer.Compare, tombstones)
    34  			defer iter.Close()
    35  
    36  			var b bytes.Buffer
    37  			for _, line := range strings.Split(d.Input, "\n") {
    38  				parts := strings.Fields(line)
    39  				if len(parts) == 0 {
    40  					continue
    41  				}
    42  				switch parts[0] {
    43  				case "seek-ge":
    44  					if len(parts) != 2 {
    45  						return fmt.Sprintf("seek-ge <key>\n")
    46  					}
    47  					iter.SeekGE([]byte(strings.TrimSpace(parts[1])))
    48  				case "seek-lt":
    49  					if len(parts) != 2 {
    50  						return fmt.Sprintf("seek-lt <key>\n")
    51  					}
    52  					iter.SeekLT([]byte(strings.TrimSpace(parts[1])))
    53  				case "first":
    54  					iter.First()
    55  				case "last":
    56  					iter.Last()
    57  				case "next":
    58  					iter.Next()
    59  				case "prev":
    60  					iter.Prev()
    61  				default:
    62  					return fmt.Sprintf("unknown op: %s", parts[0])
    63  				}
    64  				if iter.Valid() {
    65  					fmt.Fprintf(&b, "%s-%s#%d\n", iter.Key().UserKey, iter.Value(), iter.Key().SeqNum())
    66  				} else if err := iter.Error(); err != nil {
    67  					fmt.Fprintf(&b, "err=%v\n", err)
    68  				} else {
    69  					fmt.Fprintf(&b, ".\n")
    70  				}
    71  			}
    72  			return b.String()
    73  
    74  		default:
    75  			return fmt.Sprintf("unknown command: %s", d.Cmd)
    76  		}
    77  	})
    78  }