github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/bitpage/filename.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 bitpage
    16  
    17  import (
    18  	"fmt"
    19  	"path/filepath"
    20  	"strconv"
    21  	"strings"
    22  
    23  	"github.com/zuoyebang/bitalosdb/internal/utils"
    24  )
    25  
    26  type FileNum uint32
    27  
    28  func (fn FileNum) String() string { return fmt.Sprintf("%d", fn) }
    29  
    30  type PageNum uint32
    31  
    32  func (fn PageNum) String() string { return fmt.Sprintf("%d", fn) }
    33  
    34  func (fn PageNum) ToByte() []byte { return utils.Uint32ToBytes(uint32(fn)) }
    35  
    36  type FileType int
    37  
    38  const (
    39  	fileTypeManifest FileType = iota
    40  	fileTypeSklTable
    41  	fileTypeSuperTable
    42  	fileTypeSuperTableIndex
    43  	fileTypeArrayTable
    44  )
    45  
    46  const (
    47  	manifestFileName = "BITPAGEMANIFEST"
    48  )
    49  
    50  func makeFilename(fileType FileType, pageNum PageNum, fileNum FileNum) string {
    51  	switch fileType {
    52  	case fileTypeManifest:
    53  		return manifestFileName
    54  	case fileTypeSklTable:
    55  		return fmt.Sprintf("%s_%s.st", pageNum, fileNum)
    56  	case fileTypeSuperTable:
    57  		return fmt.Sprintf("%s_%s.xt", pageNum, fileNum)
    58  	case fileTypeSuperTableIndex:
    59  		return fmt.Sprintf("%s_%s.xti", pageNum, fileNum)
    60  	case fileTypeArrayTable:
    61  		return fmt.Sprintf("%s_%s.at", pageNum, fileNum)
    62  	default:
    63  		return ""
    64  	}
    65  }
    66  
    67  func makeFilepath(dirname string, fileType FileType, pageNum PageNum, fileNum FileNum) string {
    68  	switch fileType {
    69  	case fileTypeManifest:
    70  		return filepath.Join(dirname, manifestFileName)
    71  	case fileTypeSklTable, fileTypeSuperTable, fileTypeSuperTableIndex, fileTypeArrayTable:
    72  		return filepath.Join(dirname, makeFilename(fileType, pageNum, fileNum))
    73  	default:
    74  		return ""
    75  	}
    76  }
    77  
    78  func parseFilename(filename string) (fileType FileType, pageNum PageNum, fileNum FileNum, ok bool) {
    79  	filename = filepath.Base(filename)
    80  	switch filename {
    81  	case manifestFileName:
    82  		return fileTypeManifest, 0, 0, true
    83  	default:
    84  		i := strings.IndexByte(filename, '.')
    85  		if i < 0 {
    86  			break
    87  		}
    88  
    89  		nums := strings.Split(filename[:i], "_")
    90  		if len(nums) != 2 {
    91  			break
    92  		}
    93  
    94  		pn, err := strconv.ParseUint(nums[0], 10, 64)
    95  		if err != nil {
    96  			break
    97  		}
    98  
    99  		fn, err := strconv.ParseUint(nums[1], 10, 64)
   100  		if err != nil {
   101  			break
   102  		}
   103  
   104  		pageNum = PageNum(pn)
   105  		fileNum = FileNum(fn)
   106  
   107  		switch filename[i+1:] {
   108  		case "st":
   109  			return fileTypeSklTable, pageNum, fileNum, true
   110  		case "xt":
   111  			return fileTypeSuperTable, pageNum, fileNum, true
   112  		case "xti":
   113  			return fileTypeSuperTableIndex, pageNum, fileNum, true
   114  		case "at":
   115  			return fileTypeArrayTable, pageNum, fileNum, true
   116  		}
   117  	}
   118  
   119  	return 0, pageNum, fileNum, false
   120  }