forked from cockroachdb/pebble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filenames.go
105 lines (97 loc) · 2.6 KB
/
filenames.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2012 The LevelDB-Go and Pebble Authors. All rights reserved. Use
// of this source code is governed by a BSD-style license that can be found in
// the LICENSE file.
package pebble
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/petermattis/pebble/vfs"
)
type fileType int
const (
fileTypeLog fileType = iota
fileTypeLock
fileTypeTable
fileTypeManifest
fileTypeCurrent
fileTypeOptions
)
func dbFilename(dirname string, fileType fileType, fileNum uint64) string {
for len(dirname) > 0 && dirname[len(dirname)-1] == os.PathSeparator {
dirname = dirname[:len(dirname)-1]
}
switch fileType {
case fileTypeLog:
return fmt.Sprintf("%s%c%06d.log", dirname, os.PathSeparator, fileNum)
case fileTypeLock:
return fmt.Sprintf("%s%cLOCK", dirname, os.PathSeparator)
case fileTypeTable:
return fmt.Sprintf("%s%c%06d.sst", dirname, os.PathSeparator, fileNum)
case fileTypeManifest:
return fmt.Sprintf("%s%cMANIFEST-%06d", dirname, os.PathSeparator, fileNum)
case fileTypeCurrent:
return fmt.Sprintf("%s%cCURRENT", dirname, os.PathSeparator)
case fileTypeOptions:
return fmt.Sprintf("%s%cOPTIONS-%06d", dirname, os.PathSeparator, fileNum)
}
panic("unreachable")
}
func parseDBFilename(filename string) (fileType fileType, fileNum uint64, ok bool) {
filename = filepath.Base(filename)
switch {
case filename == "CURRENT":
return fileTypeCurrent, 0, true
case filename == "LOCK":
return fileTypeLock, 0, true
case strings.HasPrefix(filename, "MANIFEST-"):
u, err := strconv.ParseUint(filename[len("MANIFEST-"):], 10, 64)
if err != nil {
break
}
return fileTypeManifest, u, true
case strings.HasPrefix(filename, "OPTIONS-"):
u, err := strconv.ParseUint(filename[len("OPTIONS-"):], 10, 64)
if err != nil {
break
}
return fileTypeOptions, u, true
default:
i := strings.IndexByte(filename, '.')
if i < 0 {
break
}
u, err := strconv.ParseUint(filename[:i], 10, 64)
if err != nil {
break
}
switch filename[i+1:] {
case "sst":
return fileTypeTable, u, true
case "log":
return fileTypeLog, u, true
}
}
return 0, 0, false
}
func setCurrentFile(dirname string, fs vfs.FS, fileNum uint64) error {
newFilename := dbFilename(dirname, fileTypeCurrent, fileNum)
oldFilename := fmt.Sprintf("%s.%06d.dbtmp", newFilename, fileNum)
fs.Remove(oldFilename)
f, err := fs.Create(oldFilename)
if err != nil {
return err
}
if _, err := fmt.Fprintf(f, "MANIFEST-%06d\n", fileNum); err != nil {
return err
}
if err := f.Sync(); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
return fs.Rename(oldFilename, newFilename)
}