github.com/cockroachdb/pebble@v0.0.0-20231214172447-ab4952c5f87b/internal/manifest/level.go (about)

     1  // Copyright 2020 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 manifest
     6  
     7  import "fmt"
     8  
     9  const (
    10  	// 3 bits are necessary to represent level values from 0-6.
    11  	levelBits = 3
    12  	levelMask = (1 << levelBits) - 1
    13  	// invalidSublevel denotes an invalid or non-applicable sublevel.
    14  	invalidSublevel = -1
    15  )
    16  
    17  // Level encodes a level and optional sublevel for use in log and error
    18  // messages. The encoding has the property that Level(0) ==
    19  // L0Sublevel(invalidSublevel).
    20  type Level uint32
    21  
    22  func makeLevel(level, sublevel int) Level {
    23  	return Level(((sublevel + 1) << levelBits) | level)
    24  }
    25  
    26  // LevelToInt returns the int representation of a Level
    27  func LevelToInt(l Level) int {
    28  	return int(l) & levelMask
    29  }
    30  
    31  // L0Sublevel returns a Level representing the specified L0 sublevel.
    32  func L0Sublevel(sublevel int) Level {
    33  	if sublevel < 0 {
    34  		panic(fmt.Sprintf("invalid L0 sublevel: %d", sublevel))
    35  	}
    36  	return makeLevel(0, sublevel)
    37  }
    38  
    39  func (l Level) String() string {
    40  	level := int(l) & levelMask
    41  	sublevel := (int(l) >> levelBits) - 1
    42  	if sublevel != invalidSublevel {
    43  		return fmt.Sprintf("L%d.%d", level, sublevel)
    44  	}
    45  	return fmt.Sprintf("L%d", level)
    46  }