From f519807dd159bc659523d693587dfc8138fd62a1 Mon Sep 17 00:00:00 2001 From: Woonduk Kang Date: Fri, 12 Oct 2012 02:32:01 +0000 Subject: [PATCH] =?UTF-8?q?[=EA=B0=95=EC=9A=B4=EB=8D=95]=20[LUCYSUS-1744]?= =?UTF-8?q?=20byteUtils=20=EC=B5=9C=EC=A0=81=ED=99=94=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=EC=BC=80=EC=9D=B4=EC=8A=A4=20=EC=B6=94=EA=B0=80.=20hb?= =?UTF-8?q?asetemplate2=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BC=80=EC=9D=B4?= =?UTF-8?q?=EC=8A=A4=20=EC=B6=94=EA=B0=80.?= 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@779 84d0f5b1-2673-498c-a247-62c4ff18d310 --- .../com/profiler/common/util/BytesUtils.java | 153 ++++++++++++++---- .../common/hbase/HbaseTemplate2Test.java | 65 ++++++++ .../profiler/common/util/BytesUtilsTest.java | 45 +++--- 3 files changed, 211 insertions(+), 52 deletions(-) create mode 100644 src/test/java/com/profiler/common/hbase/HbaseTemplate2Test.java diff --git a/src/main/java/com/profiler/common/util/BytesUtils.java b/src/main/java/com/profiler/common/util/BytesUtils.java index c70fafb0d..dcf7a1946 100644 --- a/src/main/java/com/profiler/common/util/BytesUtils.java +++ b/src/main/java/com/profiler/common/util/BytesUtils.java @@ -1,50 +1,135 @@ package com.profiler.common.util; + public class BytesUtils { - public static final byte[] longLongToBytes(long value1, long value2) { + + public static byte[] longLongToBytes(long value1, long value2) { byte[] buffer = new byte[16]; - writeLong(value1, buffer, 0); - writeLong(value2, buffer, 8); + writeFirstLong(value1, buffer); + writeSecondLong(value2, buffer); return buffer; } - public static final long[] bytesToLongLong(byte[] b) { - long[] result = new long[2]; + public static long[] bytesToLongLong(byte[] buf) { + if (buf == null) { + throw new NullPointerException("buf must not be null"); + } + if (buf.length < 16) { + throw new IllegalArgumentException("Illegal buf size."); + } + long[] result = new long[2]; - result[0] = byteToLong(b, 0); - result[1] = byteToLong(b, 8); + result[0] = byteToFirstLong(buf); + result[1] = byteToSecondLong(buf); - return result; - } + return result; + } - public static final long byteToLong(byte[] b, int offset) { - if (b.length < offset + 8) { - throw new IllegalArgumentException("Illegal bytes or offset."); - } + public static long byteToLong(byte[] buf, int offset) { + if (buf == null) { + throw new NullPointerException("buf must not be null"); + } + if (buf.length < offset + 8) { + throw new IllegalArgumentException("buf.length is too small. buf.length:" + buf.length + " offset:" + offset + 8); + } - long rv = 0; - for (int index = offset; index < offset + 8; index++) { - byte i = b[index]; - rv = (rv << 8) | (i < 0 ? 256 + i : i); - } + long rv = (((long) buf[offset] & 0xff) << 56) + | (((long) buf[offset + 1] & 0xff) << 48) + | (((long) buf[offset + 2] & 0xff) << 40) + | (((long) buf[offset + 3] & 0xff) << 32) + | (((long) buf[offset + 4] & 0xff) << 24) + | (((long) buf[offset + 5] & 0xff) << 16) + | (((long) buf[offset + 6] & 0xff) << 8) + | (((long) buf[offset + 7] & 0xff)); + return rv; + } - return rv; - } - - private static void writeLong(long value, byte[] buf, int offset) { -// for (int i = offset + 7; i > offset; i--) { -// buf [i]= (byte) val; -// val >>>= 8; -// } -// buf[offset] = (byte) val; + public static long byteToFirstLong(byte[] buf) { + if (buf == null) { + throw new NullPointerException("buf must not be null"); + } + if (buf.length < 8) { + throw new IllegalArgumentException("buf.length is too small(8). buf.length:" + buf.length); + } + long rv = (((long) buf[0] & 0xff) << 56) + | (((long) buf[1] & 0xff) << 48) + | (((long) buf[2] & 0xff) << 40) + | (((long) buf[3] & 0xff) << 32) + | (((long) buf[4] & 0xff) << 24) + | (((long) buf[5] & 0xff) << 16) + | (((long) buf[6] & 0xff) << 8) + | (((long) buf[7] & 0xff)); + return rv; + } + + public static long byteToSecondLong(byte[] buf) { + if (buf == null) { + throw new NullPointerException("buf must not be null"); + } + if (buf.length < 16) { + throw new IllegalArgumentException("buf.length is too small(16). buf.length:" + buf.length); + } + + long rv = (((long) buf[8] & 0xff) << 56) + | (((long) buf[9] & 0xff) << 48) + | (((long) buf[10] & 0xff) << 40) + | (((long) buf[11] & 0xff) << 32) + | (((long) buf[12] & 0xff) << 24) + | (((long) buf[13] & 0xff) << 16) + | (((long) buf[14] & 0xff) << 8) + | (((long) buf[15] & 0xff)); + return rv; + } + + public static void writeLong(long value, byte[] buf, int offset) { + if (buf == null) { + throw new NullPointerException("buf must not be null"); + } + if (buf.length < offset + 8) { + throw new IllegalArgumentException("buf.length is too small. buf.length:" + buf.length + " offset:" + offset + 8); + } buf[offset++] = (byte) (value >> 56); - buf[offset++] = (byte) (value >> 48); - buf[offset++] = (byte) (value >> 40); - buf[offset++] = (byte) (value >> 32); - buf[offset++] = (byte) (value >> 24); - buf[offset++] = (byte) (value >> 16); - buf[offset++] = (byte) (value >> 8); - buf[offset] = (byte) (value); + buf[offset++] = (byte) (value >> 48); + buf[offset++] = (byte) (value >> 40); + buf[offset++] = (byte) (value >> 32); + buf[offset++] = (byte) (value >> 24); + buf[offset++] = (byte) (value >> 16); + buf[offset++] = (byte) (value >> 8); + buf[offset] = (byte) (value); + } + + public static void writeFirstLong(long value, byte[] buf) { + if (buf == null) { + throw new NullPointerException("buf must not be null"); + } + if (buf.length < 8) { + throw new IllegalArgumentException("buf.length is too small(8). buf.length:" + buf.length); + } + buf[0] = (byte) (value >> 56); + buf[1] = (byte) (value >> 48); + buf[2] = (byte) (value >> 40); + buf[3] = (byte) (value >> 32); + buf[4] = (byte) (value >> 24); + buf[5] = (byte) (value >> 16); + buf[6] = (byte) (value >> 8); + buf[7] = (byte) (value); + } + + public static void writeSecondLong(long value, byte[] buf) { + if (buf == null) { + throw new NullPointerException("buf must not be null"); + } + if (buf.length < 16) { + throw new IllegalArgumentException("buf.length is too small(16). buf.length:" + buf.length); + } + buf[8] = (byte) (value >> 56); + buf[9] = (byte) (value >> 48); + buf[10] = (byte) (value >> 40); + buf[11] = (byte) (value >> 32); + buf[12] = (byte) (value >> 24); + buf[13] = (byte) (value >> 16); + buf[14] = (byte) (value >> 8); + buf[15] = (byte) (value); } } diff --git a/src/test/java/com/profiler/common/hbase/HbaseTemplate2Test.java b/src/test/java/com/profiler/common/hbase/HbaseTemplate2Test.java new file mode 100644 index 000000000..a1ce404b8 --- /dev/null +++ b/src/test/java/com/profiler/common/hbase/HbaseTemplate2Test.java @@ -0,0 +1,65 @@ +package com.profiler.common.hbase; + +import com.profiler.common.util.PropertyUtils; +import junit.framework.Assert; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.HBaseConfiguration; +import org.apache.hadoop.hbase.TableNotFoundException; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.hadoop.hbase.HbaseConfigurationFactoryBean; +import org.springframework.data.hadoop.hbase.HbaseSystemException; + +import java.io.IOException; +import java.util.Properties; + +/** + * + */ +public class HbaseTemplate2Test { + + private static HbaseConfigurationFactoryBean hbaseConfigurationFactoryBean; + + @BeforeClass + public static void beforeClass() throws IOException { + String path = HbaseTemplate2Test.class.getClassLoader().getResource("test-hbase.properties").getPath(); + Properties properties = PropertyUtils.readProperties(path); + + Configuration cfg = HBaseConfiguration.create(); + cfg.set("hbase.zookeeper.quorum", properties.getProperty("hbase.client.host")); + cfg.set("hbase.zookeeper.property.clientPort", properties.getProperty("hbase.client.port")); + hbaseConfigurationFactoryBean = new HbaseConfigurationFactoryBean(); + hbaseConfigurationFactoryBean.setConfiguration(cfg); + hbaseConfigurationFactoryBean.afterPropertiesSet(); + } + + @AfterClass + public static void afterClass() { + if (hbaseConfigurationFactoryBean != null) { + hbaseConfigurationFactoryBean.destroy(); + } + + } + + + @Test + public void notExist() throws Exception { + + HbaseTemplate2 hbaseTemplate2 = new HbaseTemplate2(); + hbaseTemplate2.setConfiguration(hbaseConfigurationFactoryBean.getObject()); + + try { + hbaseTemplate2.put("NOT_EXIST", new byte[0], "familyName".getBytes(), "columnName".getBytes(), new byte[0]); + Assert.fail("exceptions"); + } catch (HbaseSystemException e) { + if (!(e.getCause() instanceof TableNotFoundException)) { + Assert.fail("unexpected exception :" + e.getCause()); + } + } + + + } +} diff --git a/src/test/java/com/profiler/common/util/BytesUtilsTest.java b/src/test/java/com/profiler/common/util/BytesUtilsTest.java index 092d5fae4..6341fd05f 100644 --- a/src/test/java/com/profiler/common/util/BytesUtilsTest.java +++ b/src/test/java/com/profiler/common/util/BytesUtilsTest.java @@ -7,29 +7,38 @@ import org.junit.Assert; import org.junit.Test; 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); + Assert.assertArrayEquals(add, bytes); - byte[] bytes = BytesUtils.longLongToBytes(most, least); - long[] org = BytesUtils.bytesToLongLong(bytes); + long[] longLong = BytesUtils.bytesToLongLong(bytes); + Assert.assertEquals(most, longLong[0]); + Assert.assertEquals(least, longLong[1]); - Assert.assertEquals(most, org[0]); - Assert.assertEquals(least, org[1]); - Assert.assertArrayEquals(add, bytes); - } + long bMost = BytesUtils.byteToLong(bytes, 0); + long bLeast = BytesUtils.byteToLong(bytes, 8); + Assert.assertEquals(most, bMost); + Assert.assertEquals(least, bLeast); + + byte bBytes[] = new byte[16]; + BytesUtils.writeLong(most, bBytes, 0); + BytesUtils.writeLong(least, bBytes, 8); + Assert.assertArrayEquals(add, bBytes); + } }