private void TrimItems() { while (_lastFreqForItem.Count > TopKMax) { if (Topk.Count == 0) { break; } var last = Topk.LastOrDefault(); if (last.Value is Deque <ByteBuffer> deque) { var valueRemoved = deque.RemoveLast(); _lastFreqForItem.Remove(valueRemoved); if (deque.IsEmpty()) { Topk.Remove(last.Key); } } else { Topk.Remove(last.Key); _lastFreqForItem.Remove((ByteBuffer)last.Value); } } }
public void Add(byte[] bytes, int count) { Hashes.Add(bytes, count); if (Topk != null) { long frequency = Hashes.EstimateCount(bytes); Topk.UpdateExpectIncreasing(bytes, frequency); } }
private void RemoveItemFromSorted( long frequency, ByteBuffer value) { var existing = Topk.Get(frequency); if (existing is Deque <ByteBuffer> deque) { deque.Remove(value); if (deque.IsEmpty()) { Topk.Remove(frequency); } } else if (existing != null) { Topk.Remove(frequency); } }
private void AddItemToSorted( long frequency, ByteBuffer value) { var existing = Topk.Get(frequency); if (existing == null) { Topk.Put(frequency, value); } else if (existing is Deque <ByteBuffer> existingDeque) { existingDeque.Add(value); } else { Deque <ByteBuffer> deque = new ArrayDeque <ByteBuffer>(2); deque.Add((ByteBuffer)existing); deque.Add(value); Topk.Put(frequency, deque); } }
public void UpdateExpectIncreasing( byte[] value, long frequency) { var filled = _lastFreqForItem.Count == TopKMax; if (!filled) { var valueBuffer = new ByteBuffer(value); UpdateInternal(valueBuffer, frequency); } else { var lastKey = Topk.Last().Key; if (frequency > lastKey) { var valueBuffer = new ByteBuffer(value); UpdateInternal(valueBuffer, frequency); } } TrimItems(); }