|
| 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 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 CircularBuffer<ValueTuple<long, SortedMultiSet<T>>> previousValues; |
| 82 | + public SortedMultiSet<T> currentValues; |
| 83 | + |
| 84 | + public int k; |
| 85 | + |
| 86 | + public Comparison<T> rankComparer; |
| 87 | + private Func<SortedDictionary<T, long>> generator; |
| 88 | + private ItemAndCount<T> minValue; // The minimum threshold value in TopK |
| 89 | + |
| 90 | + public HoppingTopKState(int k, Comparison<T> rankComparer, int hoppingWindowSize, Func<SortedDictionary<T, long>> generator) |
| 91 | + { |
| 92 | + this.k = k; |
| 93 | + this.rankComparer = rankComparer; |
| 94 | + this.currentValues = new SortedMultiSet<T>(generator); |
| 95 | + this.previousValues = new CircularBuffer<ValueTuple<long, SortedMultiSet<T>>>(hoppingWindowSize); |
| 96 | + this.generator = generator; |
| 97 | + } |
| 98 | + |
| 99 | + public ITopKState<T> Add(T input, long timestamp) |
| 100 | + { |
| 101 | + // Verify that input is added in non-decreasing order |
| 102 | + if (timestamp < this.currentTimestamp) |
| 103 | + { |
| 104 | + throw new ArgumentException("Invalid timestamp"); |
| 105 | + } |
| 106 | + |
| 107 | + // First entry in new hop window, just add the value |
| 108 | + if (timestamp > this.currentTimestamp) |
| 109 | + { |
| 110 | + MergeCurrentToPrevious(); |
| 111 | + this.currentTimestamp = timestamp; |
| 112 | + this.currentValues.Add(input); |
| 113 | + this.minValue = new ItemAndCount<T>(input, 1); |
| 114 | + return this; |
| 115 | + } |
| 116 | + |
| 117 | + // these are subsequent entries |
| 118 | + int compare = rankComparer(input, this.minValue.Item); |
| 119 | + |
| 120 | + if (this.currentValues.TotalCount < this.k) // if we have not reached k yet, add it |
| 121 | + { |
| 122 | + if (compare > 0) |
| 123 | + this.minValue = new ItemAndCount<T>(input, 1); |
| 124 | + else if (compare == 0) |
| 125 | + this.minValue.Count++; |
| 126 | + |
| 127 | + this.currentValues.Add(input); |
| 128 | + return this; |
| 129 | + } |
| 130 | + else if (compare > 0) // We have reached k and new input is smaller than minimum |
| 131 | + { |
| 132 | + return this; |
| 133 | + } |
| 134 | + else if (compare == 0) // We have reached k and new input is equal to the minimum |
| 135 | + { |
| 136 | + this.currentValues.Add(input); // add to get ties |
| 137 | + this.minValue.Count++; |
| 138 | + return this; |
| 139 | + } |
| 140 | + else // The new item is less than minValue, so we need to remove some entries to make place for the new entry |
| 141 | + { |
| 142 | + this.currentValues.Add(input); |
| 143 | + var toRemove = this.currentValues.TotalCount - this.k; |
| 144 | + if (toRemove >= minValue.Count) |
| 145 | + { |
| 146 | + this.currentValues.RemoveAll(this.minValue.Item); |
| 147 | + this.minValue = this.currentValues.GetMinItem(); |
| 148 | + } |
| 149 | + return this; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + public ITopKState<T> Remove(T input, long timestamp) |
| 154 | + { |
| 155 | + throw new NotImplementedException("Cannot remove single elements from this state"); |
| 156 | + } |
| 157 | + |
| 158 | + public ITopKState<T> RemoveAll(ITopKState<T> other) |
| 159 | + { |
| 160 | + if (other.Count != 0) |
| 161 | + { |
| 162 | + if (other is HoppingTopKState<T> otherTopK) |
| 163 | + { |
| 164 | + if (otherTopK.currentTimestamp > this.currentTimestamp) |
| 165 | + { |
| 166 | + throw new ArgumentException("Cannot remove entries with current or future timestamp"); |
| 167 | + } |
| 168 | + else if (otherTopK.currentTimestamp == this.currentTimestamp) |
| 169 | + { |
| 170 | + if (this.currentValues.TotalCount != otherTopK.currentValues.TotalCount) |
| 171 | + throw new InvalidOperationException("Invalid removal"); |
| 172 | + |
| 173 | + this.currentValues.Clear(); |
| 174 | + this.previousValues.Clear(); |
| 175 | + } |
| 176 | + else |
| 177 | + { |
| 178 | + while (this.previousValues.Count > 0) |
| 179 | + { |
| 180 | + var first = this.previousValues.PeekFirst(); |
| 181 | + |
| 182 | + if (first.Item1 > otherTopK.currentTimestamp) |
| 183 | + { |
| 184 | + break; |
| 185 | + } |
| 186 | + |
| 187 | + if (first.Item1 == otherTopK.currentTimestamp && |
| 188 | + first.Item2.TotalCount != otherTopK.currentValues.TotalCount) |
| 189 | + throw new InvalidOperationException("Invalid removal"); |
| 190 | + |
| 191 | + this.previousValues.Dequeue(); |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + else |
| 196 | + { |
| 197 | + throw new InvalidOperationException("Cannot remove non-HoppingTopKState from HoppingTopKState"); |
| 198 | + } |
| 199 | + } |
| 200 | + return this; |
| 201 | + } |
| 202 | + |
| 203 | + // This function merges the current values to previous and is expensive |
| 204 | + // Currently it is only called by ComputeResult |
| 205 | + public SortedMultiSet<T> GetSortedValues() |
| 206 | + { |
| 207 | + var sortedMultiset = new SortedMultiSet<T>(generator); |
| 208 | + |
| 209 | + foreach (var dictItem in this.previousValues.Iterate()) |
| 210 | + { |
| 211 | + sortedMultiset.AddAll(dictItem.Item2); |
| 212 | + } |
| 213 | + sortedMultiset.AddAll(this.currentValues); |
| 214 | + |
| 215 | + return sortedMultiset; |
| 216 | + } |
| 217 | + |
| 218 | + private void MergeCurrentToPrevious() |
| 219 | + { |
| 220 | + if (!this.currentValues.IsEmpty) |
| 221 | + { |
| 222 | + var newEntry = ValueTuple.Create(this.currentTimestamp, this.currentValues); |
| 223 | + this.previousValues.Enqueue(ref newEntry); |
| 224 | + this.currentValues = new SortedMultiSet<T>(generator); |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + public long Count => this.currentValues.TotalCount + this.previousValues.Iterate().Sum(e => e.Item2.TotalCount); |
| 229 | + } |
| 230 | +} |
0 commit comments