github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/query/block/info.go (about)

     1  // Copyright (c) 2019 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package block
    22  
    23  // String returns the block type as a string.
    24  func (t BlockType) String() string {
    25  	switch t {
    26  	case BlockM3TSZCompressed:
    27  		return "compressed_m3tsz"
    28  	case BlockDecompressed:
    29  		return "decompressed"
    30  	case BlockScalar:
    31  		return "scalar"
    32  	case BlockLazy:
    33  		return "lazy"
    34  	case BlockTime:
    35  		return "time"
    36  	case BlockContainer:
    37  		return "container"
    38  	case BlockEmpty:
    39  		return "empty"
    40  	case BlockTest:
    41  		return "test"
    42  	}
    43  
    44  	return "unknown"
    45  }
    46  
    47  // BlockInfo describes information about the block.
    48  type BlockInfo struct {
    49  	blockType BlockType
    50  	inner     []BlockType
    51  }
    52  
    53  // NewBlockInfo creates a BlockInfo of the specified type.
    54  func NewBlockInfo(blockType BlockType) BlockInfo {
    55  	return BlockInfo{blockType: blockType}
    56  }
    57  
    58  // NewWrappedBlockInfo creates a BlockInfo of the specified type, wrapping an
    59  // existing BlockInfo.
    60  func NewWrappedBlockInfo(
    61  	blockType BlockType,
    62  	wrap BlockInfo,
    63  ) BlockInfo {
    64  	inner := make([]BlockType, 0, len(wrap.inner)+1)
    65  	inner = append(inner, wrap.blockType)
    66  	inner = append(inner, wrap.inner...)
    67  	return BlockInfo{
    68  		blockType: blockType,
    69  		inner:     inner,
    70  	}
    71  }
    72  
    73  // Type is the block type for this block.
    74  func (b BlockInfo) Type() BlockType {
    75  	return b.blockType
    76  }
    77  
    78  // InnerType is the block type for any block wrapped by this block, or this
    79  // block itself if it doesn't wrap anything.
    80  func (b BlockInfo) InnerType() BlockType {
    81  	if b.inner == nil {
    82  		return b.Type()
    83  	}
    84  
    85  	return b.inner[0]
    86  }
    87  
    88  // BaseType is the block type for the innermost block wrapped by this block, or
    89  // the block itself if it doesn't wrap anything.
    90  func (b BlockInfo) BaseType() BlockType {
    91  	if b.inner == nil {
    92  		return b.Type()
    93  	}
    94  
    95  	return b.inner[len(b.inner)-1]
    96  }