iterator.go 3.5 KB

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