github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/encoding/iterator_test.go (about) 1 // Copyright (c) 2016 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 encoding 22 23 import ( 24 "time" 25 26 "github.com/m3db/m3/src/dbnode/namespace" 27 "github.com/m3db/m3/src/dbnode/ts" 28 "github.com/m3db/m3/src/dbnode/x/xio" 29 "github.com/m3db/m3/src/x/pool" 30 xtime "github.com/m3db/m3/src/x/time" 31 ) 32 33 type testValue struct { 34 value float64 35 t xtime.UnixNano 36 unit xtime.Unit 37 annotation ts.Annotation 38 } 39 40 type testIterator struct { 41 values []testValue 42 idx int 43 closed bool 44 err error 45 onNext func(oldIdx, newIdx int) 46 onReset func(r xio.Reader64, descr namespace.SchemaDescr) 47 } 48 49 func newTestIterator(values []testValue) ReaderIterator { 50 return &testIterator{values: values, idx: -1} 51 } 52 53 func (it *testIterator) Next() bool { 54 if it.onNext != nil { 55 it.onNext(it.idx, it.idx+1) 56 } 57 if it.Err() != nil { 58 return false 59 } 60 if it.idx+1 >= len(it.values) { 61 return false 62 } 63 it.idx++ 64 return true 65 } 66 67 func (it *testIterator) Current() (ts.Datapoint, xtime.Unit, ts.Annotation) { 68 idx := it.idx 69 if idx == -1 { 70 idx = 0 71 } 72 v := it.values[idx] 73 dp := ts.Datapoint{TimestampNanos: v.t, Value: v.value} 74 return dp, v.unit, v.annotation 75 } 76 77 func (it *testIterator) Err() error { 78 return it.err 79 } 80 81 func (it *testIterator) Close() { 82 it.closed = true 83 } 84 85 func (it *testIterator) Reset(r xio.Reader64, descr namespace.SchemaDescr) { 86 it.onReset(r, descr) 87 } 88 89 func (it *testIterator) ResetSliceOfSlices(readers xio.ReaderSliceOfSlicesIterator, descr namespace.SchemaDescr) { 90 l, _, _ := readers.CurrentReaders() 91 for i := 0; i < l; i++ { 92 r := readers.CurrentReaderAt(i) 93 it.onReset(r, descr) 94 } 95 } 96 97 type testMultiIterator struct { 98 values []testValue 99 idx int 100 closed bool 101 err error 102 onNext func(oldIdx, newIdx int) 103 onReset func(r xio.Reader64) 104 } 105 106 func newTestMultiIterator(values []testValue, err error) MultiReaderIterator { 107 return &testMultiIterator{values: values, idx: -1, err: err} 108 } 109 110 func (it *testMultiIterator) Next() bool { 111 if it.onNext != nil { 112 it.onNext(it.idx, it.idx+1) 113 } 114 if it.Err() != nil { 115 return false 116 } 117 if it.idx+1 >= len(it.values) { 118 return false 119 } 120 it.idx++ 121 return true 122 } 123 124 func (it *testMultiIterator) Current() (ts.Datapoint, xtime.Unit, ts.Annotation) { 125 idx := it.idx 126 if idx == -1 { 127 idx = 0 128 } 129 v := it.values[idx] 130 dp := ts.Datapoint{TimestampNanos: v.t, Value: v.value} 131 return dp, v.unit, v.annotation 132 } 133 134 func (it *testMultiIterator) Err() error { 135 return it.err 136 } 137 138 func (it *testMultiIterator) Close() { 139 it.closed = true 140 } 141 142 func (it *testMultiIterator) Reset(r []xio.SegmentReader, _ xtime.UnixNano, 143 _ time.Duration, _ namespace.SchemaDescr) { 144 for _, reader := range r { 145 it.onReset(reader) 146 } 147 } 148 149 func (it *testMultiIterator) ResetSliceOfSlices( 150 readers xio.ReaderSliceOfSlicesIterator, _ namespace.SchemaDescr) { 151 l, _, _ := readers.CurrentReaders() 152 for i := 0; i < l; i++ { 153 r := readers.CurrentReaderAt(i) 154 it.onReset(r) 155 } 156 } 157 158 func (it *testMultiIterator) Schema() namespace.SchemaDescr { 159 return nil 160 } 161 162 func (it *testMultiIterator) Readers() xio.ReaderSliceOfSlicesIterator { 163 return nil 164 } 165 166 type testReaderSliceOfSlicesIterator struct { 167 blocks [][]xio.BlockReader 168 idx int 169 closed bool 170 } 171 172 func newTestReaderSliceOfSlicesIterator( 173 blocks [][]xio.BlockReader, 174 ) xio.ReaderSliceOfSlicesIterator { 175 return &testReaderSliceOfSlicesIterator{blocks: blocks, idx: -1} 176 } 177 178 func (it *testReaderSliceOfSlicesIterator) Next() bool { 179 if !(it.idx+1 < len(it.blocks)) { 180 return false 181 } 182 it.idx++ 183 return true 184 } 185 186 func (it *testReaderSliceOfSlicesIterator) CurrentReaders() (int, xtime.UnixNano, time.Duration) { 187 return len(it.blocks[it.arrayIdx()]), 0, 0 188 } 189 190 func (it *testReaderSliceOfSlicesIterator) CurrentReaderAt(idx int) xio.BlockReader { 191 return it.blocks[it.arrayIdx()][idx] 192 } 193 194 func (it *testReaderSliceOfSlicesIterator) Close() { 195 it.closed = true 196 } 197 198 func (it *testReaderSliceOfSlicesIterator) Size() (int, error) { 199 return 0, nil 200 } 201 202 func (it *testReaderSliceOfSlicesIterator) RewindToIndex(idx int) { 203 it.idx = idx 204 } 205 206 func (it *testReaderSliceOfSlicesIterator) Index() int { 207 return it.idx 208 } 209 210 func (it *testReaderSliceOfSlicesIterator) arrayIdx() int { 211 idx := it.idx 212 if idx == -1 { 213 idx = 0 214 } 215 return idx 216 } 217 218 type testNoopReader struct { 219 n byte // return for "n", also required so that each struct construction has its address 220 } 221 222 func (r *testNoopReader) Read64() (word uint64, n byte, err error) { return 0, r.n, nil } 223 func (r *testNoopReader) Peek64() (word uint64, n byte, err error) { return 0, r.n, nil } 224 func (r *testNoopReader) Segment() (ts.Segment, error) { return ts.Segment{}, nil } 225 func (r *testNoopReader) Reset(ts.Segment) {} 226 func (r *testNoopReader) Finalize() {} 227 func (r *testNoopReader) Clone( 228 _ pool.CheckedBytesPool, 229 ) (xio.SegmentReader, error) { 230 return r, nil 231 }