github.com/zuoyebang/bitalostable@v1.0.1-0.20240229032404-e3b99a834294/internal/base/filenames_test.go (about) 1 // Copyright 2012 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 base 6 7 import ( 8 "bytes" 9 "fmt" 10 "os" 11 "testing" 12 13 "github.com/stretchr/testify/require" 14 "github.com/zuoyebang/bitalostable/vfs" 15 ) 16 17 func TestParseFilename(t *testing.T) { 18 testCases := map[string]bool{ 19 "000000.log": true, 20 "000000.log.zip": false, 21 "000000..log": false, 22 "a000000.log": false, 23 "abcdef.log": false, 24 "000001ldb": false, 25 "000001.sst": true, 26 "CURRENT": true, 27 "CURRaNT": false, 28 "LOCK": true, 29 "xLOCK": false, 30 "x.LOCK": false, 31 "MANIFEST": false, 32 "MANIFEST123456": false, 33 "MANIFEST-": false, 34 "MANIFEST-123456": true, 35 "MANIFEST-123456.doc": false, 36 "OPTIONS": false, 37 "OPTIONS123456": false, 38 "OPTIONS-": false, 39 "OPTIONS-123456": true, 40 "OPTIONS-123456.doc": false, 41 "CURRENT.123456": false, 42 "CURRENT.dbtmp": false, 43 "CURRENT.123456.dbtmp": true, 44 "temporary.123456.dbtmp": true, 45 } 46 fs := vfs.NewMem() 47 for tc, want := range testCases { 48 _, _, got := ParseFilename(fs, fs.PathJoin("foo", tc)) 49 if got != want { 50 t.Errorf("%q: got %v, want %v", tc, got, want) 51 } 52 } 53 } 54 55 func TestFilenameRoundTrip(t *testing.T) { 56 testCases := map[FileType]bool{ 57 // CURRENT and LOCK files aren't numbered. 58 FileTypeCurrent: false, 59 FileTypeLock: false, 60 // The remaining file types are numbered. 61 FileTypeLog: true, 62 FileTypeManifest: true, 63 FileTypeTable: true, 64 FileTypeOptions: true, 65 FileTypeOldTemp: true, 66 FileTypeTemp: true, 67 } 68 fs := vfs.NewMem() 69 for fileType, numbered := range testCases { 70 fileNums := []FileNum{0} 71 if numbered { 72 fileNums = []FileNum{0, 1, 2, 3, 10, 42, 99, 1001} 73 } 74 for _, fileNum := range fileNums { 75 filename := MakeFilepath(fs, "foo", fileType, fileNum) 76 gotFT, gotFN, gotOK := ParseFilename(fs, filename) 77 if !gotOK { 78 t.Errorf("could not parse %q", filename) 79 continue 80 } 81 if gotFT != fileType || gotFN != fileNum { 82 t.Errorf("filename=%q: got %v, %v, want %v, %v", filename, gotFT, gotFN, fileType, fileNum) 83 continue 84 } 85 } 86 } 87 } 88 89 type bufferFataler struct { 90 buf bytes.Buffer 91 } 92 93 func (b *bufferFataler) Fatalf(msg string, args ...interface{}) { 94 fmt.Fprintf(&b.buf, msg, args...) 95 } 96 97 func TestMustExist(t *testing.T) { 98 err := os.ErrNotExist 99 fs := vfs.Default 100 var buf bufferFataler 101 filename := fs.PathJoin("..", "..", "testdata", "db-stage-4", "000000.sst") 102 103 MustExist(fs, filename, &buf, err) 104 require.Equal(t, `000000.sst: 105 file does not exist 106 directory contains 10 files, 3 unknown, 1 tables, 1 logs, 1 manifests`, buf.buf.String()) 107 }