From b8ea061be62c30196d9cf287a59b587ab2019028 Mon Sep 17 00:00:00 2001 From: Chisu Yu Date: Mon, 24 Sep 2012 04:57:59 +0000 Subject: [PATCH] =?UTF-8?q?[=EC=9C=A0=EC=B9=98=EC=88=98]=20[NOBTS]=20add?= =?UTF-8?q?=20bytes=20to=20long[2]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://svn.bds.nhncorp.com/pe/hippo-commons/trunk@696 84d0f5b1-2673-498c-a247-62c4ff18d310 --- .../profiler/common/hbase/HBaseClient.java | 13 ------ .../com/profiler/common/util/BytesUtils.java | 23 +++++++++++ .../profiler/common/util/BytesUtilsTest.java | 40 ++++++++++--------- 3 files changed, 45 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/profiler/common/hbase/HBaseClient.java b/src/main/java/com/profiler/common/hbase/HBaseClient.java index 59354217c..6ff397c39 100644 --- a/src/main/java/com/profiler/common/hbase/HBaseClient.java +++ b/src/main/java/com/profiler/common/hbase/HBaseClient.java @@ -233,8 +233,6 @@ public class HBaseClient { HTableInterface htable = getHTable(tableName); if (query.isSingleRow()) { - System.out.println("Query single row"); - Get get = new Get(startRow); if (columns != null) { @@ -247,9 +245,7 @@ public class HBaseClient { List resultList = new ArrayList(1); resultList.add(result); resultIterator = resultList.iterator(); - } else { - System.out.println("Query multiple rows"); Scan scan = new Scan(); if (startRow != null) { @@ -269,19 +265,10 @@ public class HBaseClient { LOG.debug("Executing scanner: " + query); - System.out.println("executing scanner:" + query); - long start = System.currentTimeMillis(); resultScanner = htable.getScanner(scan); - System.out.println("result scanner:" + resultScanner); - - Iterator it = resultScanner.iterator(); - while (it.hasNext()) { - System.out.println("R=" + it.next()); - } - LOG.trace("Time taken for scanner: " + (System.currentTimeMillis() - start)); resultIterator = resultScanner.iterator(); diff --git a/src/main/java/com/profiler/common/util/BytesUtils.java b/src/main/java/com/profiler/common/util/BytesUtils.java index b78039663..c70fafb0d 100644 --- a/src/main/java/com/profiler/common/util/BytesUtils.java +++ b/src/main/java/com/profiler/common/util/BytesUtils.java @@ -8,6 +8,29 @@ public class BytesUtils { return buffer; } + public static final long[] bytesToLongLong(byte[] b) { + long[] result = new long[2]; + + result[0] = byteToLong(b, 0); + result[1] = byteToLong(b, 8); + + return result; + } + + public static final long byteToLong(byte[] b, int offset) { + if (b.length < offset + 8) { + throw new IllegalArgumentException("Illegal bytes or offset."); + } + + long rv = 0; + for (int index = offset; index < offset + 8; index++) { + byte i = b[index]; + rv = (rv << 8) | (i < 0 ? 256 + i : i); + } + + return rv; + } + private static void writeLong(long value, byte[] buf, int offset) { // for (int i = offset + 7; i > offset; i--) { // buf [i]= (byte) val; diff --git a/src/test/java/com/profiler/common/util/BytesUtilsTest.java b/src/test/java/com/profiler/common/util/BytesUtilsTest.java index 181e41690..092d5fae4 100644 --- a/src/test/java/com/profiler/common/util/BytesUtilsTest.java +++ b/src/test/java/com/profiler/common/util/BytesUtilsTest.java @@ -1,31 +1,35 @@ package com.profiler.common.util; +import java.util.UUID; + import org.apache.hadoop.hbase.util.Bytes; import org.junit.Assert; import org.junit.Test; -import java.util.UUID; - - public class BytesUtilsTest { - @Test - public void testLongLongToBytes() throws Exception { - long most = Long.MAX_VALUE; - long least = Long.MAX_VALUE-1; + @Test + public void testLongLongToBytes() throws Exception { + long most = Long.MAX_VALUE; + long least = Long.MAX_VALUE - 1; - test(most, least); + test(most, least); - UUID uuid = UUID.randomUUID(); - test(uuid.getMostSignificantBits(), uuid.getLeastSignificantBits()); - } + UUID uuid = UUID.randomUUID(); + test(uuid.getMostSignificantBits(), uuid.getLeastSignificantBits()); + } - private void test(long most, long least) { - byte[] bytes1 = Bytes.toBytes(most); - byte[] bytes2 = Bytes.toBytes(least); - byte[] add = Bytes.add(bytes1, bytes2); + private void test(long most, long least) { + byte[] bytes1 = Bytes.toBytes(most); + byte[] bytes2 = Bytes.toBytes(least); + byte[] add = Bytes.add(bytes1, bytes2); - byte[] bytes = BytesUtils.longLongToBytes(most, least); + byte[] bytes = BytesUtils.longLongToBytes(most, least); - Assert.assertArrayEquals(add, bytes); - } + long[] org = BytesUtils.bytesToLongLong(bytes); + + Assert.assertEquals(most, org[0]); + Assert.assertEquals(least, org[1]); + + Assert.assertArrayEquals(add, bytes); + } }