iterator.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. package attribute // import "go.opentelemetry.io/otel/attribute"
  15. // Iterator allows iterating over the set of labels in order,
  16. // sorted by key.
  17. type Iterator struct {
  18. storage *Set
  19. idx int
  20. }
  21. // MergeIterator supports iterating over two sets of labels while
  22. // eliminating duplicate values from the combined set. The first
  23. // iterator value takes precedence.
  24. type MergeIterator struct {
  25. one oneIterator
  26. two oneIterator
  27. current KeyValue
  28. }
  29. type oneIterator struct {
  30. iter Iterator
  31. done bool
  32. label KeyValue
  33. }
  34. // Next moves the iterator to the next position. Returns false if there
  35. // are no more labels.
  36. func (i *Iterator) Next() bool {
  37. i.idx++
  38. return i.idx < i.Len()
  39. }
  40. // Label returns current KeyValue. Must be called only after Next returns
  41. // true.
  42. func (i *Iterator) Label() KeyValue {
  43. kv, _ := i.storage.Get(i.idx)
  44. return kv
  45. }
  46. // Attribute is a synonym for Label().
  47. func (i *Iterator) Attribute() KeyValue {
  48. return i.Label()
  49. }
  50. // IndexedLabel returns current index and attribute. Must be called only
  51. // after Next returns true.
  52. func (i *Iterator) IndexedLabel() (int, KeyValue) {
  53. return i.idx, i.Label()
  54. }
  55. // Len returns a number of labels in the iterator's `*Set`.
  56. func (i *Iterator) Len() int {
  57. return i.storage.Len()
  58. }
  59. // ToSlice is a convenience function that creates a slice of labels
  60. // from the passed iterator. The iterator is set up to start from the
  61. // beginning before creating the slice.
  62. func (i *Iterator) ToSlice() []KeyValue {
  63. l := i.Len()
  64. if l == 0 {
  65. return nil
  66. }
  67. i.idx = -1
  68. slice := make([]KeyValue, 0, l)
  69. for i.Next() {
  70. slice = append(slice, i.Label())
  71. }
  72. return slice
  73. }
  74. // NewMergeIterator returns a MergeIterator for merging two label sets
  75. // Duplicates are resolved by taking the value from the first set.
  76. func NewMergeIterator(s1, s2 *Set) MergeIterator {
  77. mi := MergeIterator{
  78. one: makeOne(s1.Iter()),
  79. two: makeOne(s2.Iter()),
  80. }
  81. return mi
  82. }
  83. func makeOne(iter Iterator) oneIterator {
  84. oi := oneIterator{
  85. iter: iter,
  86. }
  87. oi.advance()
  88. return oi
  89. }
  90. func (oi *oneIterator) advance() {
  91. if oi.done = !oi.iter.Next(); !oi.done {
  92. oi.label = oi.iter.Label()
  93. }
  94. }
  95. // Next returns true if there is another label available.
  96. func (m *MergeIterator) Next() bool {
  97. if m.one.done && m.two.done {
  98. return false
  99. }
  100. if m.one.done {
  101. m.current = m.two.label
  102. m.two.advance()
  103. return true
  104. }
  105. if m.two.done {
  106. m.current = m.one.label
  107. m.one.advance()
  108. return true
  109. }
  110. if m.one.label.Key == m.two.label.Key {
  111. m.current = m.one.label // first iterator label value wins
  112. m.one.advance()
  113. m.two.advance()
  114. return true
  115. }
  116. if m.one.label.Key < m.two.label.Key {
  117. m.current = m.one.label
  118. m.one.advance()
  119. return true
  120. }
  121. m.current = m.two.label
  122. m.two.advance()
  123. return true
  124. }
  125. // Label returns the current value after Next() returns true.
  126. func (m *MergeIterator) Label() KeyValue {
  127. return m.current
  128. }