|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | + |
| 5 | +namespace Microsoft.StreamProcessing.Aggregates |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// State used by TopK Aggregate |
| 9 | + /// </summary> |
| 10 | + /// <typeparam name="T"></typeparam> |
| 11 | + public interface ITopKState<T> |
| 12 | + { |
| 13 | + /// <summary> |
| 14 | + /// Add a single entry |
| 15 | + /// </summary> |
| 16 | + /// <param name="input"></param> |
| 17 | + /// <param name="timestamp"></param> |
| 18 | + ITopKState<T> Add(T input, long timestamp); |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Removes the specified entry |
| 22 | + /// </summary> |
| 23 | + /// <param name="input"></param> |
| 24 | + /// <param name="timestamp"></param> |
| 25 | + ITopKState<T> Remove(T input, long timestamp); |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Removes entries from other |
| 29 | + /// </summary> |
| 30 | + /// <param name="other"></param> |
| 31 | + ITopKState<T> RemoveAll(ITopKState<T> other); |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Gets the values as sorted set |
| 35 | + /// </summary> |
| 36 | + /// <returns></returns> |
| 37 | + SortedMultiSet<T> GetSortedValues(); |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Returns total number of values in the set |
| 41 | + /// </summary> |
| 42 | + long Count { get; } |
| 43 | + } |
| 44 | + |
| 45 | + internal class SimpleTopKState<T> : ITopKState<T> |
| 46 | + { |
| 47 | + private SortedMultiSet<T> values; |
| 48 | + |
| 49 | + public SimpleTopKState(Func<SortedDictionary<T, long>> generator) |
| 50 | + { |
| 51 | + this.values = new SortedMultiSet<T>(generator); |
| 52 | + } |
| 53 | + |
| 54 | + public long Count => this.values.TotalCount; |
| 55 | + |
| 56 | + public virtual ITopKState<T> Add(T input, long timestamp) |
| 57 | + { |
| 58 | + this.values.Add(input); |
| 59 | + return this; |
| 60 | + } |
| 61 | + |
| 62 | + public SortedMultiSet<T> GetSortedValues() => this.values; |
| 63 | + |
| 64 | + public ITopKState<T> Remove(T input, long timestamp) |
| 65 | + { |
| 66 | + this.values.Remove(input); |
| 67 | + return this; |
| 68 | + } |
| 69 | + |
| 70 | + public ITopKState<T> RemoveAll(ITopKState<T> other) |
| 71 | + { |
| 72 | + this.values.RemoveAll(other.GetSortedValues()); |
| 73 | + return this; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + internal class HoppingTopKState<T> : ITopKState<T> |
| 78 | + { |
| 79 | + public long currentTimestamp; |
| 80 | + |
| 81 | + public SortedMultiSet<T> previousValues; |
| 82 | + public SortedMultiSet<T> currentValues; |
| 83 | + |
| 84 | + public int k; |
| 85 | + |
| 86 | + public Comparison<T> rankComparer; |
| 87 | + |
| 88 | + public HoppingTopKState(int k, Comparison<T> rankComparer, Func<SortedDictionary<T, long>> generator) |
| 89 | + { |
| 90 | + this.k = k; |
| 91 | + this.rankComparer = rankComparer; |
| 92 | + this.currentValues = new SortedMultiSet<T>(generator); |
| 93 | + this.previousValues = new SortedMultiSet<T>(generator); |
| 94 | + } |
| 95 | + |
| 96 | + public ITopKState<T> Add(T input, long timestamp) |
| 97 | + { |
| 98 | + if (timestamp > this.currentTimestamp) |
| 99 | + { |
| 100 | + MergeCurrentToPrevious(); |
| 101 | + this.currentTimestamp = timestamp; |
| 102 | + } |
| 103 | + else if (timestamp < currentTimestamp) |
| 104 | + { |
| 105 | + throw new ArgumentException("Invalid timestamp"); |
| 106 | + } |
| 107 | + |
| 108 | + this.currentValues.Add(input); |
| 109 | + |
| 110 | + var toRemove = this.currentValues.TotalCount - k; |
| 111 | + if (toRemove > 0) |
| 112 | + { |
| 113 | + var min = this.currentValues.GetMinItem(); |
| 114 | + if (toRemove == min.Count) |
| 115 | + { |
| 116 | + this.currentValues.Remove(min.Item, min.Count); |
| 117 | + } |
| 118 | + else if (toRemove > min.Count) |
| 119 | + { |
| 120 | + throw new InvalidOperationException("CurrentValues has more items than required"); |
| 121 | + } |
| 122 | + } |
| 123 | + return this; |
| 124 | + } |
| 125 | + |
| 126 | + public ITopKState<T> Remove(T input, long timestamp) |
| 127 | + { |
| 128 | + if (timestamp < this.currentTimestamp) |
| 129 | + { |
| 130 | + this.previousValues.Remove(input); |
| 131 | + } |
| 132 | + else if (timestamp == this.currentTimestamp) |
| 133 | + { |
| 134 | + this.currentValues.Remove(input); |
| 135 | + } |
| 136 | + else |
| 137 | + { |
| 138 | + throw new ArgumentException("Invalid timestamp"); |
| 139 | + } |
| 140 | + return this; |
| 141 | + } |
| 142 | + |
| 143 | + public ITopKState<T> RemoveAll(ITopKState<T> other) |
| 144 | + { |
| 145 | + if (other.Count != 0) |
| 146 | + { |
| 147 | + if (other is HoppingTopKState<T> otherTopK) |
| 148 | + { |
| 149 | + if (otherTopK.currentTimestamp >= this.currentTimestamp) |
| 150 | + { |
| 151 | + throw new ArgumentException("Cannot remove entries with current or future timestamp"); |
| 152 | + } |
| 153 | + this.previousValues.RemoveAll(otherTopK.currentValues); |
| 154 | + this.previousValues.RemoveAll(otherTopK.previousValues); |
| 155 | + } |
| 156 | + else |
| 157 | + { |
| 158 | + throw new InvalidOperationException("Cannot remove non-HoppingTopKState from HoppingTopKState"); |
| 159 | + } |
| 160 | + } |
| 161 | + return this; |
| 162 | + } |
| 163 | + |
| 164 | + // This function merges the current values to previous and is expensive |
| 165 | + // Currently it is only called by ComputeResult |
| 166 | + public SortedMultiSet<T> GetSortedValues() |
| 167 | + { |
| 168 | + if (this.previousValues.IsEmpty) |
| 169 | + return this.currentValues; |
| 170 | + else |
| 171 | + { |
| 172 | + MergeCurrentToPrevious(); |
| 173 | + return this.previousValues; |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + private void MergeCurrentToPrevious() |
| 178 | + { |
| 179 | + // Swap so we merge small onto larger |
| 180 | + if (this.previousValues.UniqueCount < this.currentValues.UniqueCount) |
| 181 | + { |
| 182 | + var temp = this.previousValues; |
| 183 | + this.previousValues = this.currentValues; |
| 184 | + this.currentValues = temp; |
| 185 | + } |
| 186 | + |
| 187 | + if (!this.currentValues.IsEmpty) |
| 188 | + { |
| 189 | + this.previousValues.AddAll(this.currentValues); |
| 190 | + this.currentValues.Clear(); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + public long Count => this.currentValues.TotalCount + this.previousValues.TotalCount; |
| 195 | + } |
| 196 | +} |
0 commit comments