sparse.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Unless explicitly stated otherwise all files in this repository are licensed
  2. // under the Apache License 2.0.
  3. // This product includes software developed at Datadog (https://www.datadoghq.com/).
  4. // Copyright 2021 Datadog, Inc.
  5. package store
  6. import (
  7. "errors"
  8. "sort"
  9. enc "github.com/DataDog/sketches-go/ddsketch/encoding"
  10. "github.com/DataDog/sketches-go/ddsketch/pb/sketchpb"
  11. )
  12. type SparseStore struct {
  13. counts map[int]float64
  14. }
  15. func NewSparseStore() *SparseStore {
  16. return &SparseStore{counts: make(map[int]float64)}
  17. }
  18. func (s *SparseStore) Add(index int) {
  19. s.counts[index]++
  20. }
  21. func (s *SparseStore) AddBin(bin Bin) {
  22. s.AddWithCount(bin.index, bin.count)
  23. }
  24. func (s *SparseStore) AddWithCount(index int, count float64) {
  25. if count == 0 {
  26. return
  27. }
  28. s.counts[index] += count
  29. }
  30. func (s *SparseStore) Bins() <-chan Bin {
  31. orderedBins := s.orderedBins()
  32. ch := make(chan Bin)
  33. go func() {
  34. defer close(ch)
  35. for _, bin := range orderedBins {
  36. ch <- bin
  37. }
  38. }()
  39. return ch
  40. }
  41. func (s *SparseStore) orderedBins() []Bin {
  42. bins := make([]Bin, 0, len(s.counts))
  43. for index, count := range s.counts {
  44. bins = append(bins, Bin{index: index, count: count})
  45. }
  46. sort.Slice(bins, func(i, j int) bool { return bins[i].index < bins[j].index })
  47. return bins
  48. }
  49. func (s *SparseStore) ForEach(f func(index int, count float64) (stop bool)) {
  50. for index, count := range s.counts {
  51. if f(index, count) {
  52. return
  53. }
  54. }
  55. }
  56. func (s *SparseStore) Copy() Store {
  57. countsCopy := make(map[int]float64)
  58. for index, count := range s.counts {
  59. countsCopy[index] = count
  60. }
  61. return &SparseStore{counts: countsCopy}
  62. }
  63. func (s *SparseStore) Clear() {
  64. for index := range s.counts {
  65. delete(s.counts, index)
  66. }
  67. }
  68. func (s *SparseStore) IsEmpty() bool {
  69. return len(s.counts) == 0
  70. }
  71. func (s *SparseStore) MaxIndex() (int, error) {
  72. if s.IsEmpty() {
  73. return 0, errUndefinedMaxIndex
  74. }
  75. maxIndex := minInt
  76. for index := range s.counts {
  77. if index > maxIndex {
  78. maxIndex = index
  79. }
  80. }
  81. return maxIndex, nil
  82. }
  83. func (s *SparseStore) MinIndex() (int, error) {
  84. if s.IsEmpty() {
  85. return 0, errUndefinedMinIndex
  86. }
  87. minIndex := maxInt
  88. for index := range s.counts {
  89. if index < minIndex {
  90. minIndex = index
  91. }
  92. }
  93. return minIndex, nil
  94. }
  95. func (s *SparseStore) TotalCount() float64 {
  96. totalCount := float64(0)
  97. for _, count := range s.counts {
  98. totalCount += count
  99. }
  100. return totalCount
  101. }
  102. func (s *SparseStore) KeyAtRank(rank float64) int {
  103. orderedBins := s.orderedBins()
  104. cumulCount := float64(0)
  105. for _, bin := range orderedBins {
  106. cumulCount += bin.count
  107. if cumulCount > rank {
  108. return bin.index
  109. }
  110. }
  111. maxIndex, err := s.MaxIndex()
  112. if err == nil {
  113. return maxIndex
  114. } else {
  115. // FIXME: make Store's KeyAtRank consistent with MinIndex and MaxIndex
  116. return 0
  117. }
  118. }
  119. func (s *SparseStore) MergeWith(store Store) {
  120. for bin := range store.Bins() {
  121. s.AddBin(bin)
  122. }
  123. }
  124. func (s *SparseStore) ToProto() *sketchpb.Store {
  125. binCounts := make(map[int32]float64)
  126. for index, count := range s.counts {
  127. binCounts[int32(index)] = count
  128. }
  129. return &sketchpb.Store{BinCounts: binCounts}
  130. }
  131. func (s *SparseStore) Reweight(w float64) error {
  132. if w <= 0 {
  133. return errors.New("can't reweight by a negative factor")
  134. }
  135. if w == 1 {
  136. return nil
  137. }
  138. for index := range s.counts {
  139. s.counts[index] *= w
  140. }
  141. return nil
  142. }
  143. func (s *SparseStore) Encode(b *[]byte, t enc.FlagType) {
  144. if s.IsEmpty() {
  145. return
  146. }
  147. enc.EncodeFlag(b, enc.NewFlag(t, enc.BinEncodingIndexDeltasAndCounts))
  148. enc.EncodeUvarint64(b, uint64(len(s.counts)))
  149. previousIndex := 0
  150. for index, count := range s.counts {
  151. enc.EncodeVarint64(b, int64(index-previousIndex))
  152. enc.EncodeVarfloat64(b, count)
  153. previousIndex = index
  154. }
  155. }
  156. func (s *SparseStore) DecodeAndMergeWith(b *[]byte, encodingMode enc.SubFlag) error {
  157. return DecodeAndMergeWith(s, b, encodingMode)
  158. }
  159. var _ Store = (*SparseStore)(nil)