kylin-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From liy...@apache.org
Subject [17/34] incubator-kylin git commit: KYLIN-665, add tests
Date Fri, 10 Apr 2015 22:47:54 GMT
KYLIN-665, add tests


Project: http://git-wip-us.apache.org/repos/asf/incubator-kylin/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-kylin/commit/f5262c85
Tree: http://git-wip-us.apache.org/repos/asf/incubator-kylin/tree/f5262c85
Diff: http://git-wip-us.apache.org/repos/asf/incubator-kylin/diff/f5262c85

Branch: refs/heads/streaming-localdict
Commit: f5262c859fbf52f4a007f79eaa6c0e5884f4fffc
Parents: ce1aef8
Author: Li, Yang <yangli9@ebay.com>
Authored: Wed Apr 8 15:19:59 2015 +0800
Committer: Li, Yang <yangli9@ebay.com>
Committed: Wed Apr 8 15:23:38 2015 +0800

----------------------------------------------------------------------
 .../common/hll/HyperLogLogPlusCounter.java      | 12 ++++++----
 .../org/apache/kylin/common/util/ByteArray.java |  5 +++-
 .../common/util/HyperLogLogCounterTest.java     | 25 ++++++++++++++++++++
 3 files changed, 36 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/f5262c85/common/src/main/java/org/apache/kylin/common/hll/HyperLogLogPlusCounter.java
----------------------------------------------------------------------
diff --git a/common/src/main/java/org/apache/kylin/common/hll/HyperLogLogPlusCounter.java
b/common/src/main/java/org/apache/kylin/common/hll/HyperLogLogPlusCounter.java
index 260c31c..f4678c9 100644
--- a/common/src/main/java/org/apache/kylin/common/hll/HyperLogLogPlusCounter.java
+++ b/common/src/main/java/org/apache/kylin/common/hll/HyperLogLogPlusCounter.java
@@ -176,7 +176,7 @@ public class HyperLogLogPlusCounter implements Comparable<HyperLogLogPlusCounter
 
         // decide output scheme -- map (3*size bytes) or array (2^p bytes)
         byte scheme;
-        if ((indexLen + 1) * size < m)
+        if (5 + (indexLen + 1) * size < m) // 5 is max len of vint
             scheme = 0; // map
         else
             scheme = 1; // array
@@ -190,9 +190,10 @@ public class HyperLogLogPlusCounter implements Comparable<HyperLogLogPlusCounter
                     out.put(registers[i]);
                 }
             }
-        } else { // array scheme
+        } else if (scheme == 1) { // array scheme
             out.put(registers);
-        }
+        } else
+            throw new IllegalStateException();
     }
 
     public void readRegisters(ByteBuffer in) throws IOException {
@@ -208,9 +209,10 @@ public class HyperLogLogPlusCounter implements Comparable<HyperLogLogPlusCounter
                 int key = readUnsigned(in, indexLen);
                 registers[key] = in.get();
             }
-        } else { // array scheme
+        } else if (scheme == 1) { // array scheme
             in.get(registers);
-        }
+        } else
+            throw new IllegalStateException();
     }
     
     public int peekLength(ByteBuffer in) {

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/f5262c85/common/src/main/java/org/apache/kylin/common/util/ByteArray.java
----------------------------------------------------------------------
diff --git a/common/src/main/java/org/apache/kylin/common/util/ByteArray.java b/common/src/main/java/org/apache/kylin/common/util/ByteArray.java
index 8c6ae91..b7362f6 100644
--- a/common/src/main/java/org/apache/kylin/common/util/ByteArray.java
+++ b/common/src/main/java/org/apache/kylin/common/util/ByteArray.java
@@ -18,6 +18,7 @@
 
 package org.apache.kylin.common.util;
 
+import java.io.Serializable;
 import java.nio.ByteBuffer;
 
 import org.apache.hadoop.hbase.util.Bytes;
@@ -25,7 +26,9 @@ import org.apache.hadoop.hbase.util.Bytes;
 /**
  * @author yangli9
  */
-public class ByteArray implements Comparable<ByteArray> {
+public class ByteArray implements Comparable<ByteArray>, Serializable {
+
+    private static final long serialVersionUID = 1L;
 
     public static ByteArray allocate(int length) {
         return new ByteArray(new byte[length]);

http://git-wip-us.apache.org/repos/asf/incubator-kylin/blob/f5262c85/common/src/test/java/org/apache/kylin/common/util/HyperLogLogCounterTest.java
----------------------------------------------------------------------
diff --git a/common/src/test/java/org/apache/kylin/common/util/HyperLogLogCounterTest.java
b/common/src/test/java/org/apache/kylin/common/util/HyperLogLogCounterTest.java
index 4a438fb..ee7de59 100644
--- a/common/src/test/java/org/apache/kylin/common/util/HyperLogLogCounterTest.java
+++ b/common/src/test/java/org/apache/kylin/common/util/HyperLogLogCounterTest.java
@@ -18,6 +18,8 @@
 
 package org.apache.kylin.common.util;
 
+import static org.junit.Assert.*;
+
 import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.util.HashSet;
@@ -43,6 +45,29 @@ public class HyperLogLogCounterTest {
     int errorCount2 = 0;
     int errorCount3 = 0;
 
+    @Test
+    public void testPeekLength() throws IOException {
+        HyperLogLogPlusCounter hllc = new HyperLogLogPlusCounter(10);
+        HyperLogLogPlusCounter copy = new HyperLogLogPlusCounter(10);
+        byte[] value = new byte[10];
+        for (int i = 0; i < 200000; i++) {
+            rand1.nextBytes(value);
+            hllc.add(value);
+            
+            buf.clear();
+            hllc.writeRegisters(buf);
+            
+            int len = buf.position();
+            buf.position(0);
+            assertEquals(len, hllc.peekLength(buf));
+            
+            copy.readRegisters(buf);
+            assertEquals(len, buf.position());
+            assertEquals(hllc, copy);
+        }
+        buf.clear();
+    }
+    
     private Set<String> generateTestData(int n) {
         Set<String> testData = new HashSet<String>();
         for (int i = 0; i < n; i++) {


Mime
View raw message