From d3e2f41191f40fe50ef5de50f4b36cd4f9d4fe39 Mon Sep 17 00:00:00 2001 From: James Lee Date: Fri, 20 Mar 2015 14:26:45 +0900 Subject: [PATCH 1/3] Fix bug that getValue returns null when without transaction context. Fixes #37. --- .../kr/co/vcnc/haeinsa/HaeinsaResult.java | 33 +++++----- .../vcnc/haeinsa/WithOutTransactionTest.java | 62 +++++++++++++++++++ 2 files changed, 81 insertions(+), 14 deletions(-) create mode 100644 src/test/java/kr/co/vcnc/haeinsa/WithOutTransactionTest.java diff --git a/src/main/java/kr/co/vcnc/haeinsa/HaeinsaResult.java b/src/main/java/kr/co/vcnc/haeinsa/HaeinsaResult.java index 313956d..b00600d 100644 --- a/src/main/java/kr/co/vcnc/haeinsa/HaeinsaResult.java +++ b/src/main/java/kr/co/vcnc/haeinsa/HaeinsaResult.java @@ -39,20 +39,7 @@ public class HaeinsaResult { * @param result HBase's result */ public HaeinsaResult(Result result) { - if (result.isEmpty()) { - List emptyList = Collections.emptyList(); - this.sortedKVs = emptyList; - } else { - List transformed = Lists.transform( - result.list(), - new Function() { - @Override - public HaeinsaKeyValue apply(KeyValue kv) { - return new HaeinsaKeyValue(kv); - } - }); - this.sortedKVs = transformed; - } + this(toHaeinsaKVs(result)); } /** @@ -102,4 +89,22 @@ public boolean containsColumn(byte[] family, byte[] qualifier) { public boolean isEmpty() { return sortedKVs.size() == 0; } + + /** + * Transform HBase result to List of HaeinsaKeyValue + */ + private static List toHaeinsaKVs(Result result) { + List sorted = Collections.emptyList(); + if (!result.isEmpty()) { + sorted = Lists.transform( + result.list(), + new Function() { + @Override + public HaeinsaKeyValue apply(KeyValue kv) { + return new HaeinsaKeyValue(kv); + } + }); + } + return sorted; + } } diff --git a/src/test/java/kr/co/vcnc/haeinsa/WithOutTransactionTest.java b/src/test/java/kr/co/vcnc/haeinsa/WithOutTransactionTest.java new file mode 100644 index 0000000..f944e4c --- /dev/null +++ b/src/test/java/kr/co/vcnc/haeinsa/WithOutTransactionTest.java @@ -0,0 +1,62 @@ +package kr.co.vcnc.haeinsa; + +import org.apache.hadoop.hbase.util.Bytes; +import org.testng.Assert; +import org.testng.annotations.Test; + +public class WithOutTransactionTest extends HaeinsaTestBase { + + @Test + public void testGet() throws Exception { + final HaeinsaTransactionManager tm = context().getTransactionManager(); + final HaeinsaTableIface testTable = context().getHaeinsaTableIface("test"); + + byte[] row = Bytes.toBytes("row"); + byte[] family = Bytes.toBytes("data"); + byte[] col1 = Bytes.toBytes("col1"); + byte[] col2 = Bytes.toBytes("col2"); + + // Insert data wih transaction + { + HaeinsaTransaction tx = tm.begin(); + HaeinsaPut put = new HaeinsaPut(row); + put.add(family, col1, Bytes.toBytes("value1")); + testTable.put(tx, put); + put = new HaeinsaPut(row); + put.add(family, col2, Bytes.toBytes("value2")); + testTable.put(tx, put); + tx.commit(); + } + // Get with transaction + { + + HaeinsaTransaction tx = tm.begin(); + HaeinsaGet get = new HaeinsaGet(row); + get.addFamily(family); + HaeinsaResult result = testTable.get(tx, get); + tx.commit(); + + byte[] value1 = result.getValue(family, col1); + byte[] value2 = result.getValue(family, col2); + + Assert.assertEquals(result.containsColumn(family, col1), true); + Assert.assertEquals(result.containsColumn(family, col2), true); + Assert.assertEquals(value1, Bytes.toBytes("value1")); + Assert.assertEquals(value2, Bytes.toBytes("value2")); + } + // Get without transaction + { + HaeinsaGet get = new HaeinsaGet(row); + get.addFamily(family); + HaeinsaResult result = testTable.get(null, get); + + byte[] value1 = result.getValue(family, col1); + byte[] value2 = result.getValue(family, col2); + + Assert.assertEquals(result.containsColumn(family, col1), true); + Assert.assertEquals(result.containsColumn(family, col2), true); + Assert.assertEquals(value1, Bytes.toBytes("value1")); + Assert.assertEquals(value2, Bytes.toBytes("value2")); + } + } +} From eae6ba75f2f39e689f191333e59de2353847edc9 Mon Sep 17 00:00:00 2001 From: James Lee Date: Fri, 20 Mar 2015 14:34:58 +0900 Subject: [PATCH 2/3] Fix check style error --- src/test/java/kr/co/vcnc/haeinsa/WithOutTransactionTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/kr/co/vcnc/haeinsa/WithOutTransactionTest.java b/src/test/java/kr/co/vcnc/haeinsa/WithOutTransactionTest.java index f944e4c..846f980 100644 --- a/src/test/java/kr/co/vcnc/haeinsa/WithOutTransactionTest.java +++ b/src/test/java/kr/co/vcnc/haeinsa/WithOutTransactionTest.java @@ -29,7 +29,6 @@ public void testGet() throws Exception { } // Get with transaction { - HaeinsaTransaction tx = tm.begin(); HaeinsaGet get = new HaeinsaGet(row); get.addFamily(family); From 81388df248e62557b35a7730f3777445f732cd78 Mon Sep 17 00:00:00 2001 From: James Lee Date: Fri, 20 Mar 2015 14:40:24 +0900 Subject: [PATCH 3/3] Add license header --- .../co/vcnc/haeinsa/WithOutTransactionTest.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/test/java/kr/co/vcnc/haeinsa/WithOutTransactionTest.java b/src/test/java/kr/co/vcnc/haeinsa/WithOutTransactionTest.java index 846f980..e9244d1 100644 --- a/src/test/java/kr/co/vcnc/haeinsa/WithOutTransactionTest.java +++ b/src/test/java/kr/co/vcnc/haeinsa/WithOutTransactionTest.java @@ -1,3 +1,18 @@ +/** + * Copyright (C) 2013-2015 VCNC Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package kr.co.vcnc.haeinsa; import org.apache.hadoop.hbase.util.Bytes;