iterator.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. Returns false if there are no
  24. // 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 returns true if there is another attribute available.
  95. func (m *MergeIterator) Next() bool {
  96. if m.one.done && m.two.done {
  97. return false
  98. }
  99. if m.one.done {
  100. m.current = m.two.attr
  101. m.two.advance()
  102. return true
  103. }
  104. if m.two.done {
  105. m.current = m.one.attr
  106. m.one.advance()
  107. return true
  108. }
  109. if m.one.attr.Key == m.two.attr.Key {
  110. m.current = m.one.attr // first iterator attribute value wins
  111. m.one.advance()
  112. m.two.advance()
  113. return true
  114. }
  115. if m.one.attr.Key < m.two.attr.Key {
  116. m.current = m.one.attr
  117. m.one.advance()
  118. return true
  119. }
  120. m.current = m.two.attr
  121. m.two.advance()
  122. return true
  123. }
  124. // Label returns the current value after Next() returns true.
  125. //
  126. // Deprecated: Use Attribute instead.
  127. func (m *MergeIterator) Label() KeyValue {
  128. return m.current
  129. }
  130. // Attribute returns the current value after Next() returns true.
  131. func (m *MergeIterator) Attribute() KeyValue {
  132. return m.current
  133. }