github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/metrics/filters/mock_filter.go (about) 1 // Copyright (c) 2017 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 filters 22 23 import ( 24 "strings" 25 26 "github.com/m3db/m3/src/metrics/metric/id" 27 ) 28 29 const ( 30 mockTagPairSeparator = "," 31 mockTagValueSeparator = "=" 32 ) 33 34 type mockTagPair struct { 35 name []byte 36 value []byte 37 } 38 39 type mockSortedTagIterator struct { 40 idx int 41 err error 42 pairs []mockTagPair 43 } 44 45 func tagsToPairs(tags []byte) []mockTagPair { 46 tagPairs := strings.Split(string(tags), mockTagPairSeparator) 47 var pairs []mockTagPair 48 for _, pair := range tagPairs { 49 p := strings.Split(pair, mockTagValueSeparator) 50 pairs = append(pairs, mockTagPair{name: []byte(p[0]), value: []byte(p[1])}) 51 } 52 return pairs 53 } 54 55 // NewMockSortedTagIterator creates a mock SortedTagIterator based on given ID. 56 func NewMockSortedTagIterator(tags []byte) id.SortedTagIterator { 57 pairs := tagsToPairs(tags) 58 return &mockSortedTagIterator{idx: -1, pairs: pairs} 59 } 60 61 func (it *mockSortedTagIterator) Reset(tags []byte) { 62 it.idx = -1 63 it.err = nil 64 it.pairs = tagsToPairs(tags) 65 } 66 67 func (it *mockSortedTagIterator) Next() bool { 68 if it.err != nil || it.idx >= len(it.pairs) { 69 return false 70 } 71 it.idx++ 72 return it.err == nil && it.idx < len(it.pairs) 73 } 74 75 func (it *mockSortedTagIterator) Current() ([]byte, []byte) { 76 return it.pairs[it.idx].name, it.pairs[it.idx].value 77 } 78 79 func (it *mockSortedTagIterator) Err() error { 80 return it.err 81 } 82 83 func (it *mockSortedTagIterator) Close() {}