github.com/zuoyebang/bitalostable@v1.0.1-0.20240229032404-e3b99a834294/internal/manifest/level_metadata_test.go (about) 1 // Copyright 2020 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 manifest 6 7 import ( 8 "bytes" 9 "fmt" 10 "strings" 11 "testing" 12 13 "github.com/zuoyebang/bitalostable/internal/base" 14 "github.com/zuoyebang/bitalostable/internal/datadriven" 15 ) 16 17 func TestLevelIterator(t *testing.T) { 18 var level LevelSlice 19 datadriven.RunTest(t, "testdata/level_iterator", 20 func(d *datadriven.TestData) string { 21 switch d.Cmd { 22 case "define": 23 var files []*FileMetadata 24 var startReslice int 25 var endReslice int 26 for _, metaStr := range strings.Split(d.Input, " ") { 27 switch metaStr { 28 case "[": 29 startReslice = len(files) 30 continue 31 case "]": 32 endReslice = len(files) 33 continue 34 case " ", "": 35 continue 36 default: 37 parts := strings.Split(metaStr, "-") 38 if len(parts) != 2 { 39 t.Fatalf("malformed table spec: %q", metaStr) 40 } 41 m := &FileMetadata{FileNum: base.FileNum(len(files) + 1)} 42 m.ExtendPointKeyBounds( 43 base.DefaultComparer.Compare, 44 base.ParseInternalKey(strings.TrimSpace(parts[0])), 45 base.ParseInternalKey(strings.TrimSpace(parts[1])), 46 ) 47 m.SmallestSeqNum = m.Smallest.SeqNum() 48 m.LargestSeqNum = m.Largest.SeqNum() 49 files = append(files, m) 50 } 51 } 52 level = NewLevelSliceKeySorted(base.DefaultComparer.Compare, files) 53 level = level.Reslice(func(start, end *LevelIterator) { 54 for i := 0; i < startReslice; i++ { 55 start.Next() 56 } 57 for i := len(files); i > endReslice; i-- { 58 end.Prev() 59 } 60 }) 61 return "" 62 63 case "iter": 64 iter := level.Iter() 65 var buf bytes.Buffer 66 for _, line := range strings.Split(d.Input, "\n") { 67 parts := strings.Fields(line) 68 if len(parts) == 0 { 69 continue 70 } 71 var m *FileMetadata 72 switch parts[0] { 73 case "first": 74 m = iter.First() 75 case "last": 76 m = iter.Last() 77 case "next": 78 m = iter.Next() 79 case "prev": 80 m = iter.Prev() 81 case "seek-ge": 82 m = iter.SeekGE(base.DefaultComparer.Compare, []byte(parts[1])) 83 case "seek-lt": 84 m = iter.SeekLT(base.DefaultComparer.Compare, []byte(parts[1])) 85 default: 86 return fmt.Sprintf("unknown command %q", parts[0]) 87 } 88 if m == nil { 89 fmt.Fprintln(&buf, ".") 90 } else { 91 fmt.Fprintln(&buf, m) 92 } 93 } 94 return buf.String() 95 96 default: 97 return fmt.Sprintf("unknown command %q", d.Cmd) 98 } 99 }) 100 }