github.com/m3db/m3@v1.5.0/src/query/block/types.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 block 22 23 import ( 24 "io" 25 26 "github.com/m3db/m3/src/query/models" 27 xtime "github.com/m3db/m3/src/x/time" 28 ) 29 30 // BlockType describes a block type. 31 type BlockType uint8 32 33 const ( 34 // BlockM3TSZCompressed is an M3TSZ compressed block. 35 BlockM3TSZCompressed BlockType = iota 36 // BlockDecompressed is a decompressed raw data block. 37 BlockDecompressed 38 // BlockScalar is a scalar block with a single value throughout its range. 39 BlockScalar 40 // BlockTime is a block with datapoint values given by a function of their 41 // timestamps. 42 BlockTime 43 // BlockLazy is a wrapper for an inner block that lazily applies transforms. 44 BlockLazy 45 // BlockContainer is a block that contains multiple inner blocks that share 46 // common metadata. 47 BlockContainer 48 // BlockEmpty is a block with metadata but no series or values. 49 BlockEmpty 50 // BlockTest is a block used for testing only. 51 BlockTest 52 ) 53 54 // Block represents a group of series across a time bound. 55 type Block interface { 56 io.Closer 57 // StepIter returns a step-wise block iterator, giving consolidated values 58 // across all series comprising the box at a single time step. 59 StepIter() (StepIter, error) 60 // SeriesIter returns a series-wise block iterator, giving unconsolidated 61 // by series. 62 SeriesIter() (SeriesIter, error) 63 // MultiSeriesIter returns batched series iterators for the block based on 64 // given concurrency. 65 MultiSeriesIter(concurrency int) ([]SeriesIterBatch, error) 66 // Meta returns the metadata for the block. 67 Meta() Metadata 68 // Info returns information about the block. 69 Info() BlockInfo 70 } 71 72 // AccumulatorBlock accumulates incoming blocks and presents them as a single 73 // Block. 74 type AccumulatorBlock interface { 75 Block 76 // AddBlock adds a block to this accumulator. 77 AddBlock(bl Block) error 78 } 79 80 // SeriesMeta is metadata data for the series. 81 type SeriesMeta struct { 82 Tags models.Tags 83 Name []byte 84 } 85 86 // Iterator is the base iterator. 87 type Iterator interface { 88 // Next moves to the next item in the iterator. It will return false if there 89 // are no more items, or if encountering an error. 90 // 91 // NB: it is important to check that Err() is nil after Next returns false, to 92 // ensure that any errors during iteration are detected and accounted for. 93 Next() bool 94 // Err returns any error encountered during iteration. 95 Err() error 96 // Close frees up resources held by the iterator. 97 Close() 98 } 99 100 // SeriesIterBatch is a batch of SeriesIterators. 101 type SeriesIterBatch struct { 102 // Iter is the series iterator. 103 Iter SeriesIter 104 // Size is the batch size. 105 Size int 106 } 107 108 // SeriesIter iterates through a block horizontally. 109 type SeriesIter interface { 110 Iterator 111 // SeriesMeta returns the metadata for each series in the block. 112 SeriesMeta() []SeriesMeta 113 // SeriesCount returns the number of series. 114 SeriesCount() int 115 // Current returns the current series for the block. 116 Current() UnconsolidatedSeries 117 } 118 119 // StepIter iterates through a block vertically. 120 type StepIter interface { 121 Iterator 122 // SeriesMeta returns the metadata for each series in the block. 123 SeriesMeta() []SeriesMeta 124 // StepCount returns the number of steps. 125 StepCount() int 126 // Current returns the current step for the block. 127 Current() Step 128 } 129 130 // Step is a single time step within a block. 131 type Step interface { 132 Time() xtime.UnixNano 133 Values() []float64 134 } 135 136 // Builder builds Blocks. 137 type Builder interface { 138 // AddCols adds the given number of columns to the block. 139 AddCols(num int) error 140 // SetRow sets a given block row to the given values and metadata. 141 SetRow(idx int, values []float64, meta SeriesMeta) error 142 // PopulateColumns sets all columns to the given size. 143 PopulateColumns(size int) 144 // AppendValue adds a single value to the column at the given index. 145 AppendValue(idx int, value float64) error 146 // AppendValues adds a slice of values to the column at the given index. 147 AppendValues(idx int, values []float64) error 148 // Build builds the block. 149 Build() Block 150 // BuildAsType builds the block, forcing it to the given BlockType. 151 BuildAsType(blockType BlockType) Block 152 } 153 154 // Result is a fetch result containing multiple blocks optionally split across 155 // time boundaries. 156 type Result struct { 157 // Blocks is a list of blocks, optionally split across time boundaries. 158 Blocks []Block 159 // Metadata contains information on fetch status. 160 Metadata ResultMetadata 161 } 162 163 // TimeTransform transforms a timestamp. 164 type TimeTransform func(xtime.UnixNano) xtime.UnixNano 165 166 // MetaTransform transforms meta data. 167 type MetaTransform func(meta Metadata) Metadata 168 169 // SeriesMetaTransform transforms series meta data. 170 type SeriesMetaTransform func(meta []SeriesMeta) []SeriesMeta 171 172 // ValueTransform transform a float64. 173 type ValueTransform func(float64) float64 174 175 // LazyOptions describes options for lazy blocks. 176 type LazyOptions interface { 177 // SetTimeTransform sets the time transform function. 178 SetTimeTransform(TimeTransform) LazyOptions 179 // TimeTransform returns the time transform function. 180 TimeTransform() TimeTransform 181 // SetValueTransform sets the value transform function. 182 SetValueTransform(ValueTransform) LazyOptions 183 // ValueTransform returns the value transform function. 184 ValueTransform() ValueTransform 185 // SetMetaTransform sets the meta transform function. 186 SetMetaTransform(MetaTransform) LazyOptions 187 // MetaTransform returns the meta transform function. 188 MetaTransform() MetaTransform 189 // SetSeriesMetaTransform sets the series meta transform function. 190 SetSeriesMetaTransform(SeriesMetaTransform) LazyOptions 191 // SeriesMetaTransform returns the series meta transform function. 192 SeriesMetaTransform() SeriesMetaTransform 193 }