github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/m3ninx/index/segment/mem/bytes_slice_iterator.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 mem 22 23 import ( 24 "bytes" 25 "sort" 26 27 sgmt "github.com/m3db/m3/src/m3ninx/index/segment" 28 ) 29 30 type bytesSliceIter struct { 31 err error 32 done bool 33 34 currentIdx int 35 current []byte 36 backingSlice [][]byte 37 opts Options 38 } 39 40 var _ sgmt.FieldsIterator = &bytesSliceIter{} 41 42 func newBytesSliceIter(slice [][]byte, opts Options) *bytesSliceIter { 43 sortSliceOfByteSlices(slice) 44 return &bytesSliceIter{ 45 currentIdx: -1, 46 backingSlice: slice, 47 opts: opts, 48 } 49 } 50 51 func (b *bytesSliceIter) Empty() bool { 52 return len(b.backingSlice) == 0 53 } 54 55 func (b *bytesSliceIter) Next() bool { 56 if b.done || b.err != nil { 57 return false 58 } 59 b.currentIdx++ 60 if b.currentIdx >= len(b.backingSlice) { 61 b.done = true 62 return false 63 } 64 b.current = b.backingSlice[b.currentIdx] 65 return true 66 } 67 68 func (b *bytesSliceIter) Current() []byte { 69 return b.current 70 } 71 72 func (b *bytesSliceIter) Err() error { 73 return nil 74 } 75 76 func (b *bytesSliceIter) Len() int { 77 return len(b.backingSlice) 78 } 79 80 func (b *bytesSliceIter) Close() error { 81 b.current = nil 82 b.opts.BytesSliceArrayPool().Put(b.backingSlice) 83 return nil 84 } 85 86 func sortSliceOfByteSlices(b [][]byte) { 87 sort.Slice(b, func(i, j int) bool { 88 return bytes.Compare(b[i], b[j]) < 0 89 }) 90 }