github.com/m3db/m3@v1.5.0/src/query/storage/m3/encoded_block.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 m3
    22  
    23  import (
    24  	"github.com/m3db/m3/src/dbnode/encoding"
    25  	"github.com/m3db/m3/src/query/block"
    26  	"github.com/m3db/m3/src/query/models"
    27  	"github.com/m3db/m3/src/query/storage/m3/consolidators"
    28  	xtime "github.com/m3db/m3/src/x/time"
    29  )
    30  
    31  type consolidationSettings struct {
    32  	consolidationFn consolidators.ConsolidationFunc
    33  	currentTime     xtime.UnixNano
    34  	bounds          models.Bounds
    35  }
    36  
    37  type encodedBlock struct {
    38  	// There is slightly different execution for the last block in the series.
    39  	lastBlock            bool
    40  	meta                 block.Metadata
    41  	consolidation        consolidationSettings
    42  	seriesMetas          []block.SeriesMeta
    43  	seriesBlockIterators []encoding.SeriesIterator
    44  	options              Options
    45  	resultMeta           block.ResultMetadata
    46  }
    47  
    48  // NewEncodedBlock builds an encoded block.
    49  func NewEncodedBlock(
    50  	result consolidators.SeriesFetchResult,
    51  	bounds models.Bounds,
    52  	lastBlock bool,
    53  	opts Options,
    54  ) (block.Block, error) {
    55  	if err := result.Verify(); err != nil {
    56  		return nil, err
    57  	}
    58  
    59  	consolidation := consolidationSettings{
    60  		consolidationFn: consolidators.TakeLast,
    61  		currentTime:     bounds.Start,
    62  		bounds:          bounds,
    63  	}
    64  
    65  	bl, err := newEncodedBlock(
    66  		result,
    67  		consolidation,
    68  		lastBlock,
    69  		opts,
    70  	)
    71  
    72  	if err != nil {
    73  		return nil, err
    74  	}
    75  
    76  	return bl, nil
    77  }
    78  
    79  func newEncodedBlock(
    80  	result consolidators.SeriesFetchResult,
    81  	consolidation consolidationSettings,
    82  	lastBlock bool,
    83  	options Options,
    84  ) (*encodedBlock, error) {
    85  	count := result.Count()
    86  	seriesMetas := make([]block.SeriesMeta, 0, count)
    87  	for i := 0; i < count; i++ {
    88  		iter, tags, err := result.IterTagsAtIndex(i, options.TagOptions())
    89  		if err != nil {
    90  			return nil, err
    91  		}
    92  
    93  		seriesMetas = append(seriesMetas, block.SeriesMeta{
    94  			Name: iter.ID().Bytes(),
    95  			Tags: tags,
    96  		})
    97  	}
    98  
    99  	return &encodedBlock{
   100  		seriesBlockIterators: result.SeriesIterators(),
   101  		consolidation:        consolidation,
   102  		lastBlock:            lastBlock,
   103  		resultMeta:           result.Metadata,
   104  		options:              options,
   105  		seriesMetas:          seriesMetas,
   106  		meta: block.Metadata{
   107  			Tags:           models.NewTags(0, options.TagOptions()),
   108  			Bounds:         consolidation.bounds,
   109  			ResultMetadata: result.Metadata,
   110  		},
   111  	}, nil
   112  }
   113  
   114  func (b *encodedBlock) SeriesIter() (block.SeriesIter, error) {
   115  	return NewEncodedSeriesIter(
   116  		b.meta, b.seriesMetas, b.seriesBlockIterators,
   117  		b.options.Instrumented(),
   118  	), nil
   119  }
   120  
   121  func (b *encodedBlock) Close() error {
   122  	for _, bl := range b.seriesBlockIterators {
   123  		bl.Close()
   124  	}
   125  
   126  	return nil
   127  }
   128  
   129  func (b *encodedBlock) Meta() block.Metadata {
   130  	return b.meta
   131  }
   132  
   133  func (b *encodedBlock) Info() block.BlockInfo {
   134  	return block.NewBlockInfo(block.BlockM3TSZCompressed)
   135  }