LCOV - code coverage report
Current view: top level - pebble/internal/base - filenames.go (source / functions) Hit Total Coverage
Test: 2024-02-03 08:15Z 087fd92a - tests + meta.lcov Lines: 100 108 92.6 %
Date: 2024-02-03 08:16:51 Functions: 0 0 -

          Line data    Source code
       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             :         "fmt"
       9             :         "strconv"
      10             :         "strings"
      11             : 
      12             :         "github.com/cockroachdb/errors/oserror"
      13             :         "github.com/cockroachdb/pebble/vfs"
      14             :         "github.com/cockroachdb/redact"
      15             : )
      16             : 
      17             : // FileNum is an internal DB identifier for a table. Tables can be physical (in
      18             : // which case the FileNum also identifies the backing object) or virtual.
      19             : type FileNum uint64
      20             : 
      21             : // String returns a string representation of the file number.
      22           2 : func (fn FileNum) String() string { return fmt.Sprintf("%06d", fn) }
      23             : 
      24             : // SafeFormat implements redact.SafeFormatter.
      25           2 : func (fn FileNum) SafeFormat(w redact.SafePrinter, _ rune) {
      26           2 :         w.Printf("%06d", redact.SafeUint(fn))
      27           2 : }
      28             : 
      29             : // PhysicalTableDiskFileNum converts the FileNum of a physical table to the
      30             : // backing DiskFileNum. The underlying numbers always match for physical tables.
      31           2 : func PhysicalTableDiskFileNum(n FileNum) DiskFileNum {
      32           2 :         return DiskFileNum(n)
      33           2 : }
      34             : 
      35             : // PhysicalTableFileNum converts the DiskFileNum backing a physical table into
      36             : // the table's FileNum. The underlying numbers always match for physical tables.
      37           2 : func PhysicalTableFileNum(f DiskFileNum) FileNum {
      38           2 :         return FileNum(f)
      39           2 : }
      40             : 
      41             : // A DiskFileNum identifies a file or object with exists on disk.
      42             : type DiskFileNum uint64
      43             : 
      44           2 : func (dfn DiskFileNum) String() string { return fmt.Sprintf("%06d", dfn) }
      45             : 
      46             : // SafeFormat implements redact.SafeFormatter.
      47           2 : func (dfn DiskFileNum) SafeFormat(w redact.SafePrinter, verb rune) {
      48           2 :         w.Printf("%06d", redact.SafeUint(dfn))
      49           2 : }
      50             : 
      51             : // FileType enumerates the types of files found in a DB.
      52             : type FileType int
      53             : 
      54             : // The FileType enumeration.
      55             : const (
      56             :         FileTypeLog FileType = iota
      57             :         FileTypeLock
      58             :         FileTypeTable
      59             :         FileTypeManifest
      60             :         FileTypeOptions
      61             :         FileTypeOldTemp
      62             :         FileTypeTemp
      63             : )
      64             : 
      65             : // MakeFilename builds a filename from components.
      66           2 : func MakeFilename(fileType FileType, dfn DiskFileNum) string {
      67           2 :         switch fileType {
      68             :         // TODO(sumeer): stop handling FileTypeLog in this function.
      69           2 :         case FileTypeLog:
      70           2 :                 return fmt.Sprintf("%s.log", dfn)
      71           2 :         case FileTypeLock:
      72           2 :                 return "LOCK"
      73           2 :         case FileTypeTable:
      74           2 :                 return fmt.Sprintf("%s.sst", dfn)
      75           2 :         case FileTypeManifest:
      76           2 :                 return fmt.Sprintf("MANIFEST-%s", dfn)
      77           2 :         case FileTypeOptions:
      78           2 :                 return fmt.Sprintf("OPTIONS-%s", dfn)
      79           1 :         case FileTypeOldTemp:
      80           1 :                 return fmt.Sprintf("CURRENT.%s.dbtmp", dfn)
      81           2 :         case FileTypeTemp:
      82           2 :                 return fmt.Sprintf("temporary.%s.dbtmp", dfn)
      83             :         }
      84           0 :         panic("unreachable")
      85             : }
      86             : 
      87             : // MakeFilepath builds a filepath from components.
      88           2 : func MakeFilepath(fs vfs.FS, dirname string, fileType FileType, dfn DiskFileNum) string {
      89           2 :         return fs.PathJoin(dirname, MakeFilename(fileType, dfn))
      90           2 : }
      91             : 
      92             : // ParseFilename parses the components from a filename.
      93           2 : func ParseFilename(fs vfs.FS, filename string) (fileType FileType, dfn DiskFileNum, ok bool) {
      94           2 :         filename = fs.PathBase(filename)
      95           2 :         switch {
      96           2 :         case filename == "LOCK":
      97           2 :                 return FileTypeLock, 0, true
      98           2 :         case strings.HasPrefix(filename, "MANIFEST-"):
      99           2 :                 dfn, ok = parseDiskFileNum(filename[len("MANIFEST-"):])
     100           2 :                 if !ok {
     101           1 :                         break
     102             :                 }
     103           2 :                 return FileTypeManifest, dfn, true
     104           2 :         case strings.HasPrefix(filename, "OPTIONS-"):
     105           2 :                 dfn, ok = parseDiskFileNum(filename[len("OPTIONS-"):])
     106           2 :                 if !ok {
     107           1 :                         break
     108             :                 }
     109           2 :                 return FileTypeOptions, dfn, ok
     110           1 :         case strings.HasPrefix(filename, "CURRENT.") && strings.HasSuffix(filename, ".dbtmp"):
     111           1 :                 s := strings.TrimSuffix(filename[len("CURRENT."):], ".dbtmp")
     112           1 :                 dfn, ok = parseDiskFileNum(s)
     113           1 :                 if !ok {
     114           1 :                         break
     115             :                 }
     116           1 :                 return FileTypeOldTemp, dfn, ok
     117           1 :         case strings.HasPrefix(filename, "temporary.") && strings.HasSuffix(filename, ".dbtmp"):
     118           1 :                 s := strings.TrimSuffix(filename[len("temporary."):], ".dbtmp")
     119           1 :                 dfn, ok = parseDiskFileNum(s)
     120           1 :                 if !ok {
     121           0 :                         break
     122             :                 }
     123           1 :                 return FileTypeTemp, dfn, ok
     124           2 :         default:
     125           2 :                 i := strings.IndexByte(filename, '.')
     126           2 :                 if i < 0 {
     127           2 :                         break
     128             :                 }
     129           2 :                 dfn, ok = parseDiskFileNum(filename[:i])
     130           2 :                 if !ok {
     131           2 :                         break
     132             :                 }
     133             :                 // TODO(sumeer): stop handling FileTypeLog in this function.
     134           2 :                 switch filename[i+1:] {
     135           2 :                 case "sst":
     136           2 :                         return FileTypeTable, dfn, true
     137           2 :                 case "log":
     138           2 :                         return FileTypeLog, dfn, true
     139             :                 }
     140             :         }
     141           2 :         return 0, dfn, false
     142             : }
     143             : 
     144           2 : func parseDiskFileNum(s string) (dfn DiskFileNum, ok bool) {
     145           2 :         u, err := strconv.ParseUint(s, 10, 64)
     146           2 :         if err != nil {
     147           2 :                 return dfn, false
     148           2 :         }
     149           2 :         return DiskFileNum(u), true
     150             : }
     151             : 
     152             : // A Fataler fatals a process with a message when called.
     153             : type Fataler interface {
     154             :         Fatalf(format string, args ...interface{})
     155             : }
     156             : 
     157             : // MustExist checks if err is an error indicating a file does not exist.
     158             : // If it is, it lists the containing directory's files to annotate the error
     159             : // with counts of the various types of files and invokes the provided fataler.
     160             : // See cockroachdb/cockroach#56490.
     161           2 : func MustExist(fs vfs.FS, filename string, fataler Fataler, err error) {
     162           2 :         if err == nil || !oserror.IsNotExist(err) {
     163           2 :                 return
     164           2 :         }
     165             : 
     166           1 :         ls, lsErr := fs.List(fs.PathDir(filename))
     167           1 :         if lsErr != nil {
     168           0 :                 // TODO(jackson): if oserror.IsNotExist(lsErr), the the data directory
     169           0 :                 // doesn't exist anymore. Another process likely deleted it before
     170           0 :                 // killing the process. We want to fatal the process, but without
     171           0 :                 // triggering error reporting like Sentry.
     172           0 :                 fataler.Fatalf("%s:\norig err: %s\nlist err: %s", redact.Safe(fs.PathBase(filename)), err, lsErr)
     173           0 :         }
     174           1 :         var total, unknown, tables, logs, manifests int
     175           1 :         total = len(ls)
     176           1 :         for _, f := range ls {
     177           1 :                 typ, _, ok := ParseFilename(fs, f)
     178           1 :                 if !ok {
     179           1 :                         unknown++
     180           1 :                         continue
     181             :                 }
     182           1 :                 switch typ {
     183           1 :                 case FileTypeTable:
     184           1 :                         tables++
     185           1 :                 case FileTypeLog:
     186           1 :                         logs++
     187           1 :                 case FileTypeManifest:
     188           1 :                         manifests++
     189             :                 }
     190             :         }
     191             : 
     192           1 :         fataler.Fatalf("%s:\n%s\ndirectory contains %d files, %d unknown, %d tables, %d logs, %d manifests",
     193           1 :                 fs.PathBase(filename), err, total, unknown, tables, logs, manifests)
     194             : }
     195             : 
     196             : // FileInfo provides some rudimentary information about a file.
     197             : type FileInfo struct {
     198             :         FileNum  DiskFileNum
     199             :         FileSize uint64
     200             : }

Generated by: LCOV version 1.14