github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/query/storage/write.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 "github.com/m3db/m3/src/query/models" 25 "github.com/m3db/m3/src/query/storage/m3/storagemetadata" 26 "github.com/m3db/m3/src/query/ts" 27 xerrors "github.com/m3db/m3/src/x/errors" 28 xtime "github.com/m3db/m3/src/x/time" 29 ) 30 31 // Validate will validate the write query options. 32 func (o WriteQueryOptions) Validate() error { 33 if err := o.validate(); err != nil { 34 // NB(r): Always make sure returns invalid params error 35 // here so that 4XX is returned to client on remote write endpoint. 36 return xerrors.NewInvalidParamsError(err) 37 } 38 return nil 39 } 40 41 func (o WriteQueryOptions) validate() error { 42 if len(o.Datapoints) == 0 { 43 return errWriteQueryNoDatapoints 44 } 45 if err := o.Unit.Validate(); err != nil { 46 return err 47 } 48 // Note: expensive check last. 49 if err := o.Tags.Validate(); err != nil { 50 return err 51 } 52 return nil 53 } 54 55 // NewWriteQuery returns a new write query after validating the options. 56 func NewWriteQuery(opts WriteQueryOptions) (*WriteQuery, error) { 57 q := &WriteQuery{} 58 if err := q.Reset(opts); err != nil { 59 return nil, err 60 } 61 return q, nil 62 } 63 64 // Reset resets the write query for reuse. 65 func (q *WriteQuery) Reset(opts WriteQueryOptions) error { 66 if err := opts.Validate(); err != nil { 67 return err 68 } 69 q.opts = opts 70 return nil 71 } 72 73 // Tags returns the tags. 74 func (q WriteQuery) Tags() models.Tags { 75 return q.opts.Tags 76 } 77 78 // Datapoints returns the datapoints. 79 func (q WriteQuery) Datapoints() ts.Datapoints { 80 return q.opts.Datapoints 81 } 82 83 // Unit returns the unit. 84 func (q WriteQuery) Unit() xtime.Unit { 85 return q.opts.Unit 86 } 87 88 // Annotation returns the annotation. 89 func (q WriteQuery) Annotation() []byte { 90 return q.opts.Annotation 91 } 92 93 // Attributes returns the attributes. 94 func (q WriteQuery) Attributes() storagemetadata.Attributes { 95 return q.opts.Attributes 96 } 97 98 // Validate validates the write query. 99 func (q *WriteQuery) Validate() error { 100 return q.opts.Validate() 101 } 102 103 // Options returns the options used to create the write query. 104 func (q WriteQuery) Options() WriteQueryOptions { 105 return q.opts 106 } 107 108 func (q *WriteQuery) String() string { 109 return string(q.opts.Tags.ID()) 110 }