iterator.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 attributes in order, sorted by
  16. // key.
  17. type Iterator struct {
  18. storage *Set
  19. idx int
  20. }
  21. // MergeIterator supports iterating over two sets of attributes while
  22. // eliminating duplicate values from the combined set. The first iterator
  23. // 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. attr KeyValue
  33. }
  34. // Next moves the iterator to the next position. Returns false if there are no
  35. // more attributes.
  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. //
  43. // Deprecated: Use Attribute instead.
  44. func (i *Iterator) Label() KeyValue {
  45. return i.Attribute()
  46. }
  47. // Attribute returns the current KeyValue of the Iterator. It must be called
  48. // only after Next returns true.
  49. func (i *Iterator) Attribute() KeyValue {
  50. kv, _ := i.storage.Get(i.idx)
  51. return kv
  52. }
  53. // IndexedLabel returns current index and attribute. Must be called only
  54. // after Next returns true.
  55. //
  56. // Deprecated: Use IndexedAttribute instead.
  57. func (i *Iterator) IndexedLabel() (int, KeyValue) {
  58. return i.idx, i.Attribute()
  59. }
  60. // IndexedAttribute returns current index and attribute. Must be called only
  61. // after Next returns true.
  62. func (i *Iterator) IndexedAttribute() (int, KeyValue) {
  63. return i.idx, i.Attribute()
  64. }
  65. // Len returns a number of attributes in the iterated set.
  66. func (i *Iterator) Len() int {
  67. return i.storage.Len()
  68. }
  69. // ToSlice is a convenience function that creates a slice of attributes from
  70. // the passed iterator. The iterator is set up to start from the beginning
  71. // 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.Attribute())
  81. }
  82. return slice
  83. }
  84. // NewMergeIterator returns a MergeIterator for merging two attribute sets.
  85. // Duplicates are resolved by taking the value from the first set.
  86. func NewMergeIterator(s1, s2 *Set) MergeIterator {
  87. mi := MergeIterator{
  88. one: makeOne(s1.Iter()),
  89. two: makeOne(s2.Iter()),
  90. }
  91. return mi
  92. }
  93. func makeOne(iter Iterator) oneIterator {
  94. oi := oneIterator{
  95. iter: iter,
  96. }
  97. oi.advance()
  98. return oi
  99. }
  100. func (oi *oneIterator) advance() {
  101. if oi.done = !oi.iter.Next(); !oi.done {
  102. oi.attr = oi.iter.Attribute()
  103. }
  104. }
  105. // Next returns true if there is another attribute available.
  106. func (m *MergeIterator) Next() bool {
  107. if m.one.done && m.two.done {
  108. return false
  109. }
  110. if m.one.done {
  111. m.current = m.two.attr
  112. m.two.advance()
  113. return true
  114. }
  115. if m.two.done {
  116. m.current = m.one.attr
  117. m.one.advance()
  118. return true
  119. }
  120. if m.one.attr.Key == m.two.attr.Key {
  121. m.current = m.one.attr // first iterator attribute value wins
  122. m.one.advance()
  123. m.two.advance()
  124. return true
  125. }
  126. if m.one.attr.Key < m.two.attr.Key {
  127. m.current = m.one.attr
  128. m.one.advance()
  129. return true
  130. }
  131. m.current = m.two.attr
  132. m.two.advance()
  133. return true
  134. }
  135. // Label returns the current value after Next() returns true.
  136. //
  137. // Deprecated: Use Attribute instead.
  138. func (m *MergeIterator) Label() KeyValue {
  139. return m.current
  140. }
  141. // Attribute returns the current value after Next() returns true.
  142. func (m *MergeIterator) Attribute() KeyValue {
  143. return m.current
  144. }