github.com/zuoyebang/bitalostable@v1.0.1-0.20240229032404-e3b99a834294/sstable/format.go (about)

     1  // Copyright 2022 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 sstable
     6  
     7  import (
     8  	"github.com/cockroachdb/errors"
     9  	"github.com/zuoyebang/bitalostable/internal/base"
    10  )
    11  
    12  // TableFormat specifies the format version for sstables. The legacy LevelDB
    13  // format is format version 1.
    14  type TableFormat uint32
    15  
    16  // The available table formats, representing the tuple (magic number, version
    17  // number). Note that these values are not (and should not) be serialized to
    18  // disk. The ordering should follow the order the versions were introduced to
    19  // Pebble (i.e. the history is linear).
    20  const (
    21  	TableFormatUnspecified TableFormat = iota
    22  	TableFormatLevelDB
    23  	TableFormatRocksDBv2
    24  	TableFormatPebblev1 // Block properties.
    25  	TableFormatPebblev2 // Range keys.
    26  
    27  	TableFormatMax = TableFormatPebblev2
    28  )
    29  
    30  // ParseTableFormat parses the given magic bytes and version into its
    31  // corresponding internal TableFormat.
    32  func ParseTableFormat(magic []byte, version uint32) (TableFormat, error) {
    33  	switch string(magic) {
    34  	case levelDBMagic:
    35  		return TableFormatLevelDB, nil
    36  	case rocksDBMagic:
    37  		if version != rocksDBFormatVersion2 {
    38  			return TableFormatUnspecified, base.CorruptionErrorf(
    39  				"bitalostable/table: unsupported rocksdb format version %d", errors.Safe(version),
    40  			)
    41  		}
    42  		return TableFormatRocksDBv2, nil
    43  	case bitalostableDBMagic:
    44  		switch version {
    45  		case 1:
    46  			return TableFormatPebblev1, nil
    47  		case 2:
    48  			return TableFormatPebblev2, nil
    49  		default:
    50  			return TableFormatUnspecified, base.CorruptionErrorf(
    51  				"bitalostable/table: unsupported bitalostable format version %d", errors.Safe(version),
    52  			)
    53  		}
    54  	default:
    55  		return TableFormatUnspecified, base.CorruptionErrorf(
    56  			"bitalostable/table: invalid table (bad magic number)",
    57  		)
    58  	}
    59  }
    60  
    61  // AsTuple returns the TableFormat's (Magic String, Version) tuple.
    62  func (f TableFormat) AsTuple() (string, uint32) {
    63  	switch f {
    64  	case TableFormatLevelDB:
    65  		return levelDBMagic, 0
    66  	case TableFormatRocksDBv2:
    67  		return rocksDBMagic, 2
    68  	case TableFormatPebblev1:
    69  		return bitalostableDBMagic, 1
    70  	case TableFormatPebblev2:
    71  		return bitalostableDBMagic, 2
    72  	default:
    73  		panic("sstable: unknown table format version tuple")
    74  	}
    75  }
    76  
    77  // String returns the TableFormat (Magic String,Version) tuple.
    78  func (f TableFormat) String() string {
    79  	switch f {
    80  	case TableFormatLevelDB:
    81  		return "(LevelDB)"
    82  	case TableFormatRocksDBv2:
    83  		return "(RocksDB,v2)"
    84  	case TableFormatPebblev1:
    85  		return "(Pebble,v1)"
    86  	case TableFormatPebblev2:
    87  		return "(Pebble,v2)"
    88  	default:
    89  		panic("sstable: unknown table format version tuple")
    90  	}
    91  }