github.com/zuoyebang/bitalostable@v1.0.1-0.20240229032404-e3b99a834294/cleaner_test.go (about) 1 // Copyright 2019 The LevelDB-Go and Pebble and Bitalostored 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 bitalostable 6 7 import ( 8 "fmt" 9 "sort" 10 "strings" 11 "testing" 12 13 "github.com/stretchr/testify/require" 14 "github.com/zuoyebang/bitalostable/internal/datadriven" 15 "github.com/zuoyebang/bitalostable/vfs" 16 ) 17 18 func TestArchiveCleaner(t *testing.T) { 19 dbs := make(map[string]*DB) 20 defer func() { 21 for _, db := range dbs { 22 require.NoError(t, db.Close()) 23 } 24 }() 25 26 var buf syncedBuffer 27 mem := vfs.NewMem() 28 opts := &Options{ 29 Cleaner: ArchiveCleaner{}, 30 FS: loggingFS{mem, &buf}, 31 WALDir: "wal", 32 } 33 34 datadriven.RunTest(t, "testdata/cleaner", func(td *datadriven.TestData) string { 35 switch td.Cmd { 36 case "batch": 37 if len(td.CmdArgs) != 1 { 38 return "batch <db>" 39 } 40 buf.Reset() 41 d := dbs[td.CmdArgs[0].String()] 42 b := d.NewBatch() 43 if err := runBatchDefineCmd(td, b); err != nil { 44 return err.Error() 45 } 46 if err := b.Commit(Sync); err != nil { 47 return err.Error() 48 } 49 return buf.String() 50 51 case "compact": 52 if len(td.CmdArgs) != 1 { 53 return "compact <db>" 54 } 55 buf.Reset() 56 d := dbs[td.CmdArgs[0].String()] 57 if err := d.Compact(nil, []byte("\xff"), false); err != nil { 58 return err.Error() 59 } 60 return buf.String() 61 62 case "flush": 63 if len(td.CmdArgs) != 1 { 64 return "flush <db>" 65 } 66 buf.Reset() 67 d := dbs[td.CmdArgs[0].String()] 68 if err := d.Flush(); err != nil { 69 return err.Error() 70 } 71 return buf.String() 72 73 case "list": 74 if len(td.CmdArgs) != 1 { 75 return "list <dir>" 76 } 77 paths, err := mem.List(td.CmdArgs[0].String()) 78 if err != nil { 79 return err.Error() 80 } 81 sort.Strings(paths) 82 buf.Reset() 83 fmt.Fprintf(&buf, "%s\n", strings.Join(paths, "\n")) 84 return buf.String() 85 86 case "open": 87 if len(td.CmdArgs) != 1 && len(td.CmdArgs) != 2 { 88 return "open <dir> [readonly]" 89 } 90 opts.ReadOnly = false 91 if len(td.CmdArgs) == 2 { 92 if td.CmdArgs[1].String() != "readonly" { 93 return "open <dir> [readonly]" 94 } 95 opts.ReadOnly = true 96 } 97 98 buf.Reset() 99 dir := td.CmdArgs[0].String() 100 d, err := Open(dir, opts) 101 if err != nil { 102 return err.Error() 103 } 104 dbs[dir] = d 105 return buf.String() 106 107 default: 108 return fmt.Sprintf("unknown command: %s", td.Cmd) 109 } 110 }) 111 }