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

     1  // Copyright (c) 2020 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 storage
    22  
    23  import (
    24  	"time"
    25  
    26  	"github.com/m3db/m3/src/query/models"
    27  	"github.com/m3db/m3/src/query/storage/m3/storagemetadata"
    28  
    29  	"github.com/uber-go/tally"
    30  )
    31  
    32  const (
    33  	defaultMaxMetricMetadataStats = 4
    34  )
    35  
    36  // NewFetchOptions creates a new fetch options.
    37  func NewFetchOptions() *FetchOptions {
    38  	return &FetchOptions{
    39  		SeriesLimit:            0,
    40  		DocsLimit:              0,
    41  		MaxMetricMetadataStats: defaultMaxMetricMetadataStats,
    42  		BlockType:              models.TypeSingleBlock,
    43  		FanoutOptions: &FanoutOptions{
    44  			FanoutUnaggregated:        FanoutDefault,
    45  			FanoutAggregated:          FanoutDefault,
    46  			FanoutAggregatedOptimized: FanoutDefault,
    47  		},
    48  		Scope: tally.NoopScope,
    49  	}
    50  }
    51  
    52  // LookbackDurationOrDefault returns either the default lookback duration or
    53  // overridden lookback duration if set.
    54  func (o *FetchOptions) LookbackDurationOrDefault(
    55  	defaultValue time.Duration,
    56  ) time.Duration {
    57  	if o.LookbackDuration == nil {
    58  		return defaultValue
    59  	}
    60  	return *o.LookbackDuration
    61  }
    62  
    63  // QueryFetchOptions returns fetch options for a given query.
    64  func (o *FetchOptions) QueryFetchOptions(
    65  	queryCtx *models.QueryContext,
    66  	blockType models.FetchedBlockType,
    67  ) (*FetchOptions, error) {
    68  	r := o.Clone()
    69  	if r.SeriesLimit <= 0 {
    70  		r.SeriesLimit = queryCtx.Options.LimitMaxTimeseries
    71  	}
    72  	if r.DocsLimit <= 0 {
    73  		r.DocsLimit = queryCtx.Options.LimitMaxDocs
    74  	}
    75  
    76  	// Use inbuilt options for type restriction if none found.
    77  	if r.RestrictQueryOptions.GetRestrictByType() == nil &&
    78  		queryCtx.Options.RestrictFetchType != nil {
    79  		v := queryCtx.Options.RestrictFetchType
    80  		restrict := &RestrictByType{
    81  			MetricsType:   storagemetadata.MetricsType(v.MetricsType),
    82  			StoragePolicy: v.StoragePolicy,
    83  		}
    84  
    85  		if err := restrict.Validate(); err != nil {
    86  			return nil, err
    87  		}
    88  
    89  		if r.RestrictQueryOptions == nil {
    90  			r.RestrictQueryOptions = &RestrictQueryOptions{}
    91  		}
    92  
    93  		r.RestrictQueryOptions.RestrictByType = restrict
    94  	}
    95  
    96  	return r, nil
    97  }
    98  
    99  // Clone will clone and return the fetch options.
   100  func (o *FetchOptions) Clone() *FetchOptions {
   101  	result := *o
   102  	return &result
   103  }