github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/internal/base/filenames.go (about)

     1  // Copyright 2021 The Bitalosdb author(hustxrb@163.com) and other contributors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package base
    16  
    17  import (
    18  	"fmt"
    19  	"path/filepath"
    20  	"strconv"
    21  	"strings"
    22  
    23  	"github.com/zuoyebang/bitalosdb/internal/vfs"
    24  )
    25  
    26  type FileNum uint64
    27  
    28  func (fn FileNum) String() string { return fmt.Sprintf("%06d", fn) }
    29  
    30  type FileType int
    31  
    32  const (
    33  	FileTypeLog FileType = iota
    34  	FileTypeLock
    35  	FileTypeManifest
    36  	FileTypeCurrent
    37  	FileTypeMeta
    38  )
    39  
    40  const (
    41  	BitreeFilePrefix  = "bitree."
    42  	BitpageFilePrefix = "bitpage."
    43  	BithashPathPrefix = "bithash."
    44  	BitablePathPrefix = "bitable."
    45  	WalPathPrefix     = "wal."
    46  )
    47  
    48  func MakeFilename(fileType FileType, fileNum FileNum) string {
    49  	switch fileType {
    50  	case FileTypeLog:
    51  		return fmt.Sprintf("%s.log", fileNum)
    52  	case FileTypeLock:
    53  		return "LOCK"
    54  	case FileTypeManifest:
    55  		return fmt.Sprintf("MANIFEST-%s", fileNum)
    56  	case FileTypeMeta:
    57  		return "BITALOSMETA"
    58  	case FileTypeCurrent:
    59  		return "CURRENT"
    60  	}
    61  	panic("unreachable")
    62  }
    63  
    64  func MakeFilepath(fs vfs.FS, dirname string, fileType FileType, fileNum FileNum) string {
    65  	return fs.PathJoin(dirname, MakeFilename(fileType, fileNum))
    66  }
    67  
    68  func MakeBitreeFilepath(dir string, i int) string {
    69  	return filepath.Join(dir, BitreeFilePrefix+strconv.Itoa(i))
    70  }
    71  
    72  func MakeBitpagepath(dir string, i int) string {
    73  	return filepath.Join(dir, BitpageFilePrefix+strconv.Itoa(i))
    74  }
    75  
    76  func MakeBithashpath(dir string, i int) string {
    77  	return filepath.Join(dir, BithashPathPrefix+strconv.Itoa(i))
    78  }
    79  
    80  func MakeBitablepath(dir string, i int) string {
    81  	return filepath.Join(dir, BitablePathPrefix+strconv.Itoa(i))
    82  }
    83  
    84  func MakeWalpath(dir string, i int) string {
    85  	return filepath.Join(dir, WalPathPrefix+strconv.Itoa(i))
    86  }
    87  
    88  func ParseFilename(fs vfs.FS, filename string) (fileType FileType, fileNum FileNum, ok bool) {
    89  	filename = fs.PathBase(filename)
    90  	switch {
    91  	case filename == "CURRENT":
    92  		return FileTypeCurrent, 0, true
    93  	case filename == "LOCK":
    94  		return FileTypeLock, 0, true
    95  	case filename == "BITALOSMETA":
    96  		return FileTypeMeta, 0, true
    97  	case strings.HasPrefix(filename, "MANIFEST-"):
    98  		fileNum, ok = parseFileNum(filename[len("MANIFEST-"):])
    99  		if !ok {
   100  			break
   101  		}
   102  		return FileTypeManifest, fileNum, true
   103  	default:
   104  		i := strings.IndexByte(filename, '.')
   105  		if i < 0 {
   106  			break
   107  		}
   108  		fileNum, ok = parseFileNum(filename[:i])
   109  		if !ok {
   110  			break
   111  		}
   112  		switch filename[i+1:] {
   113  		case "log":
   114  			return FileTypeLog, fileNum, true
   115  		}
   116  	}
   117  	return 0, fileNum, false
   118  }
   119  
   120  func parseFileNum(s string) (fileNum FileNum, ok bool) {
   121  	u, err := strconv.ParseUint(s, 10, 64)
   122  	if err != nil {
   123  		return fileNum, false
   124  	}
   125  	return FileNum(u), true
   126  }
   127  
   128  func GetFilePathBase(path string) string {
   129  	if path == "" {
   130  		return "."
   131  	}
   132  
   133  	for len(path) > 0 && path[len(path)-1] == '/' {
   134  		path = path[0 : len(path)-1]
   135  	}
   136  
   137  	pos := strings.LastIndex(path, "/")
   138  	if pos >= 0 {
   139  		pos = strings.LastIndex(path[:pos], "/")
   140  		if pos >= 0 {
   141  			path = path[pos+1:]
   142  		}
   143  	}
   144  
   145  	if path == "" {
   146  		return "/"
   147  	}
   148  	return path
   149  }