serkant.uluderya | e5afeff | 2021-02-23 18:00:23 +0300 | [diff] [blame] | 1 | // Copyright The OpenTelemetry Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package label |
| 16 | |
| 17 | // Iterator allows iterating over the set of labels in order, |
| 18 | // sorted by key. |
| 19 | type Iterator struct { |
| 20 | storage *Set |
| 21 | idx int |
| 22 | } |
| 23 | |
| 24 | // MergeIterator supports iterating over two sets of labels while |
| 25 | // eliminating duplicate values from the combined set. The first |
| 26 | // iterator value takes precedence. |
| 27 | type MergeItererator struct { |
| 28 | one oneIterator |
| 29 | two oneIterator |
| 30 | current KeyValue |
| 31 | } |
| 32 | |
| 33 | type oneIterator struct { |
| 34 | iter Iterator |
| 35 | done bool |
| 36 | label KeyValue |
| 37 | } |
| 38 | |
| 39 | // Next moves the iterator to the next position. Returns false if there |
| 40 | // are no more labels. |
| 41 | func (i *Iterator) Next() bool { |
| 42 | i.idx++ |
| 43 | return i.idx < i.Len() |
| 44 | } |
| 45 | |
| 46 | // Label returns current KeyValue. Must be called only after Next returns |
| 47 | // true. |
| 48 | func (i *Iterator) Label() KeyValue { |
| 49 | kv, _ := i.storage.Get(i.idx) |
| 50 | return kv |
| 51 | } |
| 52 | |
| 53 | // Attribute is a synonym for Label(). |
| 54 | func (i *Iterator) Attribute() KeyValue { |
| 55 | return i.Label() |
| 56 | } |
| 57 | |
| 58 | // IndexedLabel returns current index and label. Must be called only |
| 59 | // after Next returns true. |
| 60 | func (i *Iterator) IndexedLabel() (int, KeyValue) { |
| 61 | return i.idx, i.Label() |
| 62 | } |
| 63 | |
| 64 | // Len returns a number of labels in the iterator's `*Set`. |
| 65 | func (i *Iterator) Len() int { |
| 66 | return i.storage.Len() |
| 67 | } |
| 68 | |
| 69 | // ToSlice is a convenience function that creates a slice of labels |
| 70 | // from the passed iterator. The iterator is set up to start from the |
| 71 | // beginning before creating the slice. |
| 72 | func (i *Iterator) ToSlice() []KeyValue { |
| 73 | l := i.Len() |
| 74 | if l == 0 { |
| 75 | return nil |
| 76 | } |
| 77 | i.idx = -1 |
| 78 | slice := make([]KeyValue, 0, l) |
| 79 | for i.Next() { |
| 80 | slice = append(slice, i.Label()) |
| 81 | } |
| 82 | return slice |
| 83 | } |
| 84 | |
| 85 | // NewMergeIterator returns a MergeIterator for merging two label sets |
| 86 | // Duplicates are resolved by taking the value from the first set. |
| 87 | func NewMergeIterator(s1, s2 *Set) MergeItererator { |
| 88 | mi := MergeItererator{ |
| 89 | one: makeOne(s1.Iter()), |
| 90 | two: makeOne(s2.Iter()), |
| 91 | } |
| 92 | return mi |
| 93 | } |
| 94 | |
| 95 | func makeOne(iter Iterator) oneIterator { |
| 96 | oi := oneIterator{ |
| 97 | iter: iter, |
| 98 | } |
| 99 | oi.advance() |
| 100 | return oi |
| 101 | } |
| 102 | |
| 103 | func (oi *oneIterator) advance() { |
| 104 | if oi.done = !oi.iter.Next(); !oi.done { |
| 105 | oi.label = oi.iter.Label() |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // Next returns true if there is another label available. |
| 110 | func (m *MergeItererator) Next() bool { |
| 111 | if m.one.done && m.two.done { |
| 112 | return false |
| 113 | } |
| 114 | if m.one.done { |
| 115 | m.current = m.two.label |
| 116 | m.two.advance() |
| 117 | return true |
| 118 | } |
| 119 | if m.two.done { |
| 120 | m.current = m.one.label |
| 121 | m.one.advance() |
| 122 | return true |
| 123 | } |
| 124 | if m.one.label.Key == m.two.label.Key { |
| 125 | m.current = m.one.label // first iterator label value wins |
| 126 | m.one.advance() |
| 127 | m.two.advance() |
| 128 | return true |
| 129 | } |
| 130 | if m.one.label.Key < m.two.label.Key { |
| 131 | m.current = m.one.label |
| 132 | m.one.advance() |
| 133 | return true |
| 134 | } |
| 135 | m.current = m.two.label |
| 136 | m.two.advance() |
| 137 | return true |
| 138 | } |
| 139 | |
| 140 | // Label returns the current value after Next() returns true. |
| 141 | func (m *MergeItererator) Label() KeyValue { |
| 142 | return m.current |
| 143 | } |