From 11dd21e976fd612dc25f7202d94fcf8eabe69b6c Mon Sep 17 00:00:00 2001 From: carlnayak Date: Fri, 28 Apr 2017 15:53:01 -0400 Subject: [PATCH] Handle the insert of the maxSize(th) element for the first time and subsequent updates When you insert the maxSize(th) value for the first time, update the lowest count and add the element as well. When modifying the TopNList, just perform inplace updates instead of inserts --- .../cloudera/sa/examples/tablestats/model/TopNList.scala | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/scala/com/cloudera/sa/examples/tablestats/model/TopNList.scala b/src/main/scala/com/cloudera/sa/examples/tablestats/model/TopNList.scala index 3913e39..caf0584 100644 --- a/src/main/scala/com/cloudera/sa/examples/tablestats/model/TopNList.scala +++ b/src/main/scala/com/cloudera/sa/examples/tablestats/model/TopNList.scala @@ -11,13 +11,14 @@ class TopNList(val maxSize:Int) extends Serializable { var lowestValue = Long.MaxValue def add(newValue:Any, newCount:Long): Unit = { - if (topNCountsForColumnArray.length < maxSize -1) { + if (topNCountsForColumnArray.length < maxSize - 1) { + topNCountsForColumnArray += ((newValue, newCount)) + } else if (topNCountsForColumnArray.length == maxSize - 1) { topNCountsForColumnArray += ((newValue, newCount)) - } else if (topNCountsForColumnArray.length == maxSize) { updateLowestValue } else { if (newCount > lowestValue) { - topNCountsForColumnArray.insert(lowestColumnCountIndex, (newValue, newCount)) + topNCountsForColumnArray.update(lowestColumnCountIndex, (newValue, newCount)) updateLowestValue } }