github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/bithash/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 bithash
    16  
    17  import (
    18  	"fmt"
    19  	"strconv"
    20  	"strings"
    21  
    22  	"github.com/zuoyebang/bitalosdb/internal/vfs"
    23  )
    24  
    25  type FileNum uint32
    26  
    27  func (fn FileNum) String() string { return fmt.Sprintf("%d", fn) }
    28  
    29  type FileType int
    30  
    31  const (
    32  	fileTypeManifest FileType = iota
    33  	fileTypeFileNumMap
    34  	fileTypeFileNumMapTmp
    35  	fileTypeCompactLog
    36  	fileTypeTable
    37  	fileTypeManifestTemp
    38  )
    39  
    40  func MakeFilename(fileType FileType, fileNum FileNum) string {
    41  	switch fileType {
    42  	case fileTypeManifest:
    43  		return "BITHASHMANIFEST"
    44  	case fileTypeManifestTemp:
    45  		return "BITHASHMANIFESTTEMP"
    46  	case fileTypeFileNumMap:
    47  		return "FILENUMMAP"
    48  	case fileTypeFileNumMapTmp:
    49  		return "FILENUMMAPTMP"
    50  	case fileTypeCompactLog:
    51  		return "COMPACTLOG"
    52  	case fileTypeTable:
    53  		return fmt.Sprintf("%s.bht", fileNum)
    54  	default:
    55  		return ""
    56  	}
    57  }
    58  
    59  func MakeFilepath(fs vfs.FS, dirname string, fileType FileType, fileNum FileNum) string {
    60  	switch fileType {
    61  	case fileTypeManifest:
    62  		return fs.PathJoin(dirname, "BITHASHMANIFEST")
    63  	case fileTypeManifestTemp:
    64  		return fs.PathJoin(dirname, "BITHASHMANIFESTTEMP")
    65  	case fileTypeFileNumMap:
    66  		return fs.PathJoin(dirname, "FILENUMMAP")
    67  	case fileTypeFileNumMapTmp:
    68  		return fs.PathJoin(dirname, "FILENUMMAPTMP")
    69  	case fileTypeCompactLog:
    70  		return fs.PathJoin(dirname, "COMPACTLOG")
    71  	case fileTypeTable:
    72  		return fs.PathJoin(dirname, MakeFilename(fileType, fileNum))
    73  	default:
    74  		return ""
    75  	}
    76  }
    77  
    78  func ParseFilename(fs vfs.FS, filename string) (fileType FileType, fileNum FileNum, ok bool) {
    79  	filename = fs.PathBase(filename)
    80  	switch filename {
    81  	case "BITHASHMANIFEST":
    82  		return fileTypeManifest, 0, true
    83  	case "BITHASHMANIFESTTEMP":
    84  		return fileTypeManifestTemp, 0, true
    85  	case "FILENUMMAP":
    86  		return fileTypeFileNumMap, 0, true
    87  	case "FILENUMMAPTMP":
    88  		return fileTypeFileNumMapTmp, 0, true
    89  	case "COMPACTLOG":
    90  		return fileTypeCompactLog, 0, true
    91  	default:
    92  		i := strings.IndexByte(filename, '.')
    93  		if i < 0 {
    94  			break
    95  		}
    96  		fileNum, ok = parseFileNum(filename[:i])
    97  		if !ok {
    98  			break
    99  		}
   100  		switch filename[i+1:] {
   101  		case "bht":
   102  			return fileTypeTable, fileNum, true
   103  		}
   104  	}
   105  	return 0, fileNum, false
   106  }
   107  
   108  func parseFileNum(s string) (fileNum FileNum, ok bool) {
   109  	u, err := strconv.ParseUint(s, 10, 64)
   110  	if err != nil {
   111  		return fileNum, false
   112  	}
   113  	return FileNum(u), true
   114  }