github.com/cockroachdb/pebble@v0.0.0-20231214172447-ab4952c5f87b/tool/testdata/mixed/main.go (about) 1 // Command main generates test fixtures for a DB containing a mixture of point 2 // and range keys. 3 // 4 // Upon running this command, the DB directory will contain: 5 // 6 // 1. A single SSTable (000005.sst), containing: 7 // a. 26 point keys, a@1 through z@1. 8 // b. a RANGEKEYSET [a, z)@1 9 // c. a RANGEKEYUNSET [a, z)@2 10 // d. a RANGEKEYDEL [a, b) 11 // 2. A WAL for an unflushed memtable containing: 12 // a. A single point key a@2. 13 // b. a RANGEKEYSET [a, z)@3 14 // c. a RANGEKEYUNSET [a, z)@4 15 // d. a RANGEKEYDEL [a, b) 16 package main 17 18 import ( 19 "io/fs" 20 "os" 21 "path/filepath" 22 23 "github.com/cockroachdb/pebble" 24 "github.com/cockroachdb/pebble/internal/testkeys" 25 "github.com/cockroachdb/pebble/vfs" 26 ) 27 28 const outDir = "./tool/testdata/mixed" 29 30 func main() { 31 err := filepath.WalkDir(outDir, func(path string, d fs.DirEntry, err error) error { 32 if err != nil { 33 return err 34 } 35 if filepath.Base(path) == "main.go" || d.IsDir() { 36 return nil 37 } 38 return os.Remove(path) 39 }) 40 if err != nil { 41 panic(err) 42 } 43 lel := pebble.MakeLoggingEventListener(pebble.DefaultLogger) 44 45 opts := &pebble.Options{ 46 FS: vfs.Default, 47 Comparer: testkeys.Comparer, 48 FormatMajorVersion: pebble.FormatNewest, 49 EventListener: &lel, 50 DisableAutomaticCompactions: true, 51 } 52 db, err := pebble.Open(outDir, opts) 53 if err != nil { 54 panic(err) 55 } 56 57 writePoint := func(b *pebble.Batch, k []byte) { 58 if err := b.Set(k, nil, nil); err != nil { 59 panic(err) 60 } 61 } 62 writeRangeKeySet := func(b *pebble.Batch, start, end, suffix []byte) { 63 if err := b.RangeKeySet(start, end, suffix, nil, nil); err != nil { 64 panic(err) 65 } 66 } 67 writeRangeKeyUnset := func(b *pebble.Batch, start, end, suffix []byte) { 68 if err := b.RangeKeyUnset(start, end, suffix, nil); err != nil { 69 panic(err) 70 } 71 } 72 writeRangeKeyDel := func(b *pebble.Batch, start, end []byte) { 73 if err := b.RangeKeyDelete(start, end, nil); err != nil { 74 panic(err) 75 } 76 } 77 78 // Write some point and range keys. 79 ks := testkeys.Alpha(1) 80 b := db.NewBatch() 81 for i := int64(0); i < ks.Count(); i++ { 82 writePoint(b, testkeys.KeyAt(ks, i, 1)) 83 } 84 start, end := testkeys.Key(ks, 0), testkeys.Key(ks, ks.Count()-1) 85 writeRangeKeySet(b, start, end, testkeys.Suffix(1)) // SET [a, z)@1 86 writeRangeKeyUnset(b, start, end, testkeys.Suffix(2)) // UNSET [a, z)@2 87 writeRangeKeyDel(b, start, testkeys.Key(ks, 1)) // DEL [a, b) 88 if err := b.Commit(nil); err != nil { 89 panic(err) 90 } 91 92 // Flush memtables. 93 if err := db.Flush(); err != nil { 94 panic(err) 95 } 96 97 // Write one more point and range key into a memtable before closing, so the 98 // WAL is not empty. 99 b = db.NewBatch() 100 writePoint(b, testkeys.KeyAt(ks, 0, 2)) 101 writeRangeKeySet(b, start, end, testkeys.Suffix(3)) // SET [a, z)@3 102 writeRangeKeyUnset(b, start, end, testkeys.Suffix(4)) // UNSET [a, z)@4 103 writeRangeKeyDel(b, start, testkeys.Key(ks, 1)) // DEL [a, b) 104 if err := b.Commit(nil); err != nil { 105 panic(err) 106 } 107 }