github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/query/models/options.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 models
    22  
    23  import (
    24  	"bytes"
    25  	"errors"
    26  
    27  	"github.com/prometheus/common/model"
    28  
    29  	"github.com/m3db/m3/src/x/serialize"
    30  )
    31  
    32  const (
    33  	defaultAllowTagNameDuplicates = false
    34  	defaultAllowTagValueEmpty     = false
    35  	defaultMaxTagLiteralLength    = serialize.DefaultMaxTagLiteralLength
    36  )
    37  
    38  var (
    39  	defaultMetricName = []byte(model.MetricNameLabel)
    40  	defaultBucketName = []byte("le")
    41  
    42  	errNoName                   = errors.New("metric name is missing or empty")
    43  	errNoBucket                 = errors.New("bucket name is missing or empty")
    44  	errNonPositiveLiteralLength = errors.New("max literal length should be positive")
    45  )
    46  
    47  type tagOptions struct {
    48  	version                int
    49  	idScheme               IDSchemeType
    50  	bucketName             []byte
    51  	metricName             []byte
    52  	filters                Filters
    53  	allowTagNameDuplicates bool
    54  	allowTagValueEmpty     bool
    55  	maxTagLiteralLength    uint16
    56  }
    57  
    58  // NewTagOptions builds a new tag options with default values.
    59  func NewTagOptions() TagOptions {
    60  	return &tagOptions{
    61  		version:                0,
    62  		metricName:             defaultMetricName,
    63  		bucketName:             defaultBucketName,
    64  		idScheme:               TypeQuoted,
    65  		allowTagNameDuplicates: defaultAllowTagNameDuplicates,
    66  		allowTagValueEmpty:     defaultAllowTagValueEmpty,
    67  		maxTagLiteralLength:    defaultMaxTagLiteralLength,
    68  	}
    69  }
    70  
    71  func (o *tagOptions) Validate() error {
    72  	if o.metricName == nil || len(o.metricName) == 0 {
    73  		return errNoName
    74  	}
    75  
    76  	if o.bucketName == nil || len(o.bucketName) == 0 {
    77  		return errNoBucket
    78  	}
    79  
    80  	if o.maxTagLiteralLength == 0 {
    81  		return errNonPositiveLiteralLength
    82  	}
    83  
    84  	return o.idScheme.Validate()
    85  }
    86  
    87  func (o *tagOptions) SetMetricName(value []byte) TagOptions {
    88  	opts := *o
    89  	opts.metricName = value
    90  	return &opts
    91  }
    92  
    93  func (o *tagOptions) MetricName() []byte {
    94  	return o.metricName
    95  }
    96  
    97  func (o *tagOptions) SetBucketName(value []byte) TagOptions {
    98  	opts := *o
    99  	opts.bucketName = value
   100  	return &opts
   101  }
   102  
   103  func (o *tagOptions) BucketName() []byte {
   104  	return o.bucketName
   105  }
   106  
   107  func (o *tagOptions) SetIDSchemeType(value IDSchemeType) TagOptions {
   108  	opts := *o
   109  	opts.idScheme = value
   110  	return &opts
   111  }
   112  
   113  func (o *tagOptions) IDSchemeType() IDSchemeType {
   114  	return o.idScheme
   115  }
   116  
   117  func (o *tagOptions) SetFilters(value Filters) TagOptions {
   118  	opts := *o
   119  	opts.filters = value
   120  	return &opts
   121  }
   122  
   123  func (o *tagOptions) Filters() Filters {
   124  	return o.filters
   125  }
   126  
   127  func (o *tagOptions) SetAllowTagNameDuplicates(value bool) TagOptions {
   128  	opts := *o
   129  	opts.allowTagNameDuplicates = value
   130  	return &opts
   131  }
   132  
   133  func (o *tagOptions) AllowTagNameDuplicates() bool {
   134  	return o.allowTagNameDuplicates
   135  }
   136  
   137  func (o *tagOptions) SetAllowTagValueEmpty(value bool) TagOptions {
   138  	opts := *o
   139  	opts.allowTagValueEmpty = value
   140  	return &opts
   141  }
   142  
   143  func (o *tagOptions) AllowTagValueEmpty() bool {
   144  	return o.allowTagValueEmpty
   145  }
   146  
   147  // SetMaxTagLiteralLength sets the maximum length of a tag Name/Value.
   148  func (o *tagOptions) SetMaxTagLiteralLength(value uint16) TagOptions {
   149  	opts := *o
   150  	opts.maxTagLiteralLength = value
   151  	return &opts
   152  }
   153  
   154  // MaxTagLiteralLength returns the maximum length of a tag Name/Value.
   155  func (o *tagOptions) MaxTagLiteralLength() uint16 {
   156  	return o.maxTagLiteralLength
   157  }
   158  
   159  func (o *tagOptions) Equals(other TagOptions) bool {
   160  	return o.idScheme == other.IDSchemeType() &&
   161  		bytes.Equal(o.metricName, other.MetricName()) &&
   162  		bytes.Equal(o.bucketName, other.BucketName()) &&
   163  		o.allowTagNameDuplicates == other.AllowTagNameDuplicates() &&
   164  		o.allowTagValueEmpty == other.AllowTagValueEmpty() &&
   165  		o.maxTagLiteralLength == other.MaxTagLiteralLength()
   166  }