github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/x/xio/block_reader.go (about)

     1  // Copyright (c) 2018 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 xio
    22  
    23  import (
    24  	"time"
    25  
    26  	"github.com/m3db/m3/src/dbnode/ts"
    27  	"github.com/m3db/m3/src/x/pool"
    28  	xtime "github.com/m3db/m3/src/x/time"
    29  )
    30  
    31  // CloneBlock returns a clone of the block with the underlying data reset
    32  func (b BlockReader) CloneBlock(pool pool.CheckedBytesPool) (BlockReader, error) {
    33  	sr, err := b.SegmentReader.Clone(pool)
    34  	if err != nil {
    35  		return EmptyBlockReader, err
    36  	}
    37  	return BlockReader{
    38  		SegmentReader: sr,
    39  		Start:         b.Start,
    40  		BlockSize:     b.BlockSize,
    41  	}, nil
    42  }
    43  
    44  // IsEmpty returns true for the empty block
    45  func (b BlockReader) IsEmpty() bool {
    46  	return b.SegmentReader == nil && b.Start == timeZero && b.BlockSize == 0
    47  }
    48  
    49  // IsNotEmpty returns false for the empty block
    50  func (b BlockReader) IsNotEmpty() bool {
    51  	return !b.IsEmpty()
    52  }
    53  
    54  // ResetWindowed resets the underlying reader window, as well as start time and
    55  // blockSize for the block
    56  func (b *BlockReader) ResetWindowed(
    57  	segment ts.Segment,
    58  	start xtime.UnixNano,
    59  	blockSize time.Duration,
    60  ) {
    61  	b.Reset(segment)
    62  	b.Start = start
    63  	b.BlockSize = blockSize
    64  }
    65  
    66  // FilterEmptyBlockReadersSliceOfSlicesInPlace filters a [][]BlockReader in place (I.E by modifying
    67  // the existing data structures instead of allocating new ones) such that the returned [][]BlockReader
    68  // will only contain BlockReaders that contain non-empty segments.
    69  //
    70  // Note that if any of the Block/Segment readers are backed by async implementations then this function
    71  // will not return until all of the async execution has completed.
    72  func FilterEmptyBlockReadersSliceOfSlicesInPlace(brSliceOfSlices [][]BlockReader) ([][]BlockReader, error) {
    73  	filteredSliceOfSlices := brSliceOfSlices[:0]
    74  	for _, brSlice := range brSliceOfSlices {
    75  		filteredBrSlice, err := FilterEmptyBlockReadersInPlace(brSlice)
    76  		if err != nil {
    77  			return nil, err
    78  		}
    79  		if len(filteredBrSlice) > 0 {
    80  			filteredSliceOfSlices = append(filteredSliceOfSlices, filteredBrSlice)
    81  		}
    82  	}
    83  	return filteredSliceOfSlices, nil
    84  }
    85  
    86  // FilterEmptyBlockReadersInPlace is the same as FilterEmptyBlockReadersSliceOfSlicesInPlace except for
    87  // one dimensional slices instead of two.
    88  func FilterEmptyBlockReadersInPlace(brs []BlockReader) ([]BlockReader, error) {
    89  	filtered := brs[:0]
    90  	for _, br := range brs {
    91  		if br.SegmentReader == nil {
    92  			continue
    93  		}
    94  		segment, err := br.Segment()
    95  		if err != nil {
    96  			return nil, err
    97  		}
    98  		if segment.Len() > 0 {
    99  			filtered = append(filtered, br)
   100  		}
   101  	}
   102  	return filtered, nil
   103  }