You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kylin.apache.org by sh...@apache.org on 2015/10/13 04:14:08 UTC
incubator-kylin git commit: KYLIN-1068 code small refactor
Repository: incubator-kylin
Updated Branches:
refs/heads/KYLIN-1068 207cd455d -> 4d3950c14
KYLIN-1068 code small refactor
Project: http://git-wip-us.apache.org/repos/asf/incubator-kylin/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-kylin/commit/4d3950c1
Tree: http://git-wip-us.apache.org/repos/asf/incubator-kylin/tree/4d3950c1
Diff: http://git-wip-us.apache.org/repos/asf/incubator-kylin/diff/4d3950c1
Branch: refs/heads/KYLIN-1068
Commit: 4d3950c14cec7a5dc170279ab5ca832356e87b0b
Parents: 207cd45
Author: shaofengshi <sh...@apache.org>
Authored: Tue Oct 13 10:13:44 2015 +0800
Committer: shaofengshi <sh...@apache.org>
Committed: Tue Oct 13 10:13:44 2015 +0800
----------------------------------------------------------------------
.../apache/kylin/common/topn/TopNCounter.java | 57 +++++++++-----------
1 file changed, 26 insertions(+), 31 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/4d3950c1/core-common/src/main/java/org/apache/kylin/common/topn/TopNCounter.java
----------------------------------------------------------------------
diff --git a/core-common/src/main/java/org/apache/kylin/common/topn/TopNCounter.java b/core-common/src/main/java/org/apache/kylin/common/topn/TopNCounter.java
index 6ce4a89..90967d1 100644
--- a/core-common/src/main/java/org/apache/kylin/common/topn/TopNCounter.java
+++ b/core-common/src/main/java/org/apache/kylin/common/topn/TopNCounter.java
@@ -39,7 +39,7 @@ public class TopNCounter<T> implements ITopK<T>, Iterable<Counter<T>> {
protected int capacity;
private HashMap<T, ListNode2<Counter<T>>> counterMap;
- protected DoublyLinkedList<Counter<T>> bucketList;
+ protected DoublyLinkedList<Counter<T>> counterList;
/**
* @param capacity maximum size (larger capacities improve accuracy)
@@ -47,7 +47,7 @@ public class TopNCounter<T> implements ITopK<T>, Iterable<Counter<T>> {
public TopNCounter(int capacity) {
this.capacity = capacity;
counterMap = new HashMap<T, ListNode2<Counter<T>>>();
- bucketList = new DoublyLinkedList<Counter<T>>();
+ counterList = new DoublyLinkedList<Counter<T>>();
}
public int getCapacity() {
@@ -95,9 +95,9 @@ public class TopNCounter<T> implements ITopK<T>, Iterable<Counter<T>> {
if (isNewItem) {
if (size() < capacity) {
- counterNode = bucketList.enqueue(new Counter<T>(item));
+ counterNode = counterList.enqueue(new Counter<T>(item));
} else {
- counterNode = bucketList.tail();
+ counterNode = counterList.tail();
Counter<T> counter = counterNode.getValue();
droppedItem = counter.item;
counterMap.remove(droppedItem);
@@ -115,25 +115,20 @@ public class TopNCounter<T> implements ITopK<T>, Iterable<Counter<T>> {
protected void incrementCounter(ListNode2<Counter<T>> counterNode, double incrementCount) {
Counter<T> counter = counterNode.getValue();
- counter.count = counter.count + incrementCount;
+ counter.count += incrementCount;
- ListNode2<Counter<T>> bucketNodeNext = counterNode.getNext();
- bucketList.remove(counterNode);
+ ListNode2<Counter<T>> nodeNext = counterNode.getNext();
+ counterList.remove(counterNode);
counterNode.prev = null;
counterNode.next = null;
- while (bucketNodeNext != null) {
- Counter<T> bucketNext = bucketNodeNext.getValue();
- if (counter.count >= bucketNext.count) {
- bucketNodeNext = bucketNodeNext.getNext();
- } else {
- break;
- }
+ while (nodeNext != null && counter.count >= nodeNext.getValue().count) {
+ nodeNext = nodeNext.getNext();
}
- if (bucketNodeNext != null) {
- bucketList.addBefore(bucketNodeNext, counterNode);
+ if (nodeNext != null) {
+ counterList.addBefore(nodeNext, counterNode);
} else {
- bucketList.add(counterNode);
+ counterList.add(counterNode);
}
}
@@ -142,7 +137,7 @@ public class TopNCounter<T> implements ITopK<T>, Iterable<Counter<T>> {
public List<T> peek(int k) {
List<T> topK = new ArrayList<T>(k);
- for (ListNode2<Counter<T>> bNode = bucketList.head(); bNode != null; bNode = bNode.getPrev()) {
+ for (ListNode2<Counter<T>> bNode = counterList.head(); bNode != null; bNode = bNode.getPrev()) {
Counter<T> b = bNode.getValue();
if (topK.size() == k) {
return topK;
@@ -156,7 +151,7 @@ public class TopNCounter<T> implements ITopK<T>, Iterable<Counter<T>> {
public List<Counter<T>> topK(int k) {
List<Counter<T>> topK = new ArrayList<Counter<T>>(k);
- for (ListNode2<Counter<T>> bNode = bucketList.head(); bNode != null; bNode = bNode.getPrev()) {
+ for (ListNode2<Counter<T>> bNode = counterList.head(); bNode != null; bNode = bNode.getPrev()) {
Counter<T> b = bNode.getValue();
if (topK.size() == k) {
return topK;
@@ -178,7 +173,7 @@ public class TopNCounter<T> implements ITopK<T>, Iterable<Counter<T>> {
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append('[');
- for (ListNode2<Counter<T>> bNode = bucketList.head(); bNode != null; bNode = bNode.getPrev()) {
+ for (ListNode2<Counter<T>> bNode = counterList.head(); bNode != null; bNode = bNode.getPrev()) {
Counter<T> b = bNode.getValue();
sb.append(b.item);
sb.append(':');
@@ -189,7 +184,7 @@ public class TopNCounter<T> implements ITopK<T>, Iterable<Counter<T>> {
}
public void fromExternal(int size, double[] counters, List<T> items) {
- this.bucketList = new DoublyLinkedList<Counter<T>>();
+ this.counterList = new DoublyLinkedList<Counter<T>>();
this.counterMap = new HashMap<T, ListNode2<Counter<T>>>(size);
@@ -197,7 +192,7 @@ public class TopNCounter<T> implements ITopK<T>, Iterable<Counter<T>> {
Counter<T> c = new Counter<T>();
c.count = counters[i];
c.item = items.get(i);
- ListNode2<Counter<T>> node = bucketList.add(c);
+ ListNode2<Counter<T>> node = counterList.add(c);
counterMap.put(c.item, node);
}
}
@@ -216,11 +211,11 @@ public class TopNCounter<T> implements ITopK<T>, Iterable<Counter<T>> {
public TopNCounter<T> merge(TopNCounter<T> another) {
double m1 = 0.0, m2 = 0.0;
if (this.size() >= this.capacity) {
- m1 = this.bucketList.tail().getValue().count;
+ m1 = this.counterList.tail().getValue().count;
}
if (another.size() >= another.capacity) {
- m2 = another.bucketList.tail().getValue().count;
+ m2 = another.counterList.tail().getValue().count;
}
for (Map.Entry<T, ListNode2<Counter<T>>> entry : this.counterMap.entrySet()) {
@@ -256,12 +251,12 @@ public class TopNCounter<T> implements ITopK<T>, Iterable<Counter<T>> {
assert newCapacity > 0;
this.capacity = newCapacity;
if (newCapacity < this.size()) {
- ListNode2<Counter<T>> tail = bucketList.tail;
+ ListNode2<Counter<T>> tail = counterList.tail;
while (tail != null && this.size() > newCapacity) {
- Counter<T> bucket = bucketList.tail.getValue();
+ Counter<T> bucket = counterList.tail.getValue();
this.counterMap.remove(bucket.getItem());
- this.bucketList.remove(tail);
- tail = this.bucketList.tail;
+ this.counterList.remove(tail);
+ tail = this.counterList.tail;
}
}
@@ -275,7 +270,7 @@ public class TopNCounter<T> implements ITopK<T>, Iterable<Counter<T>> {
double[] counters = new double[size()];
int index = 0;
- for (ListNode2<Counter<T>> bNode = bucketList.tail(); bNode != null; bNode = bNode.getNext()) {
+ for (ListNode2<Counter<T>> bNode = counterList.tail(); bNode != null; bNode = bNode.getNext()) {
Counter<T> b = bNode.getValue();
counters[index] = b.count;
index++;
@@ -290,7 +285,7 @@ public class TopNCounter<T> implements ITopK<T>, Iterable<Counter<T>> {
*/
public List<T> getItems() {
List<T> items = Lists.newArrayList();
- for (ListNode2<Counter<T>> bNode = bucketList.tail(); bNode != null; bNode = bNode.getNext()) {
+ for (ListNode2<Counter<T>> bNode = counterList.tail(); bNode != null; bNode = bNode.getNext()) {
Counter<T> b = bNode.getValue();
items.add(b.item);
}
@@ -309,7 +304,7 @@ public class TopNCounter<T> implements ITopK<T>, Iterable<Counter<T>> {
private ListNode2<Counter<T>> currentBNode;
private TopNCounterIterator() {
- currentBNode = bucketList.head();
+ currentBNode = counterList.head();
}
@Override