From 23f5cccb795f89a9a177aa87215cc11437cd6720 Mon Sep 17 00:00:00 2001 From: Aditya Kishore Date: Thu, 18 Jul 2013 10:25:59 -0700 Subject: [PATCH] HBASE-9032 - Result.getBytes() returns null if backed by KeyValue array --- .../org/apache/hadoop/hbase/client/Result.java | 17 ++++++++++++++++ .../org/apache/hadoop/hbase/client/TestResult.java | 23 +++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/apache/hadoop/hbase/client/Result.java b/src/main/java/org/apache/hadoop/hbase/client/Result.java index bf8bda3..05f528c 100644 --- a/src/main/java/org/apache/hadoop/hbase/client/Result.java +++ b/src/main/java/org/apache/hadoop/hbase/client/Result.java @@ -36,6 +36,7 @@ import org.apache.hadoop.hbase.KeyValue.SplitKeyValue; import org.apache.hadoop.hbase.io.ImmutableBytesWritable; import org.apache.hadoop.hbase.io.WritableWithSize; import org.apache.hadoop.hbase.util.Bytes; +import org.apache.hadoop.io.DataOutputBuffer; import org.apache.hadoop.io.Writable; /** @@ -401,6 +402,22 @@ public class Result implements Writable, WritableWithSize { * @return pointer to raw binary of Result */ public ImmutableBytesWritable getBytes() { + if (this.bytes == null && this.kvs != null) { + int totalLen = 0; + for(KeyValue kv : kvs) { + totalLen += kv.getLength() + Bytes.SIZEOF_INT; + } + DataOutputBuffer out = new DataOutputBuffer(totalLen); + try { + for(KeyValue kv : kvs) { + kv.write(out); + } + out.close(); + } catch (IOException e) { + throw new RuntimeException("IOException in Result.getBytes()", e); + } + this.bytes = new ImmutableBytesWritable(out.getData(), 0, out.getLength()); + } return this.bytes; } diff --git a/src/test/java/org/apache/hadoop/hbase/client/TestResult.java b/src/test/java/org/apache/hadoop/hbase/client/TestResult.java index f9e29c2..685ff2e 100644 --- a/src/test/java/org/apache/hadoop/hbase/client/TestResult.java +++ b/src/test/java/org/apache/hadoop/hbase/client/TestResult.java @@ -21,8 +21,10 @@ package org.apache.hadoop.hbase.client; import junit.framework.TestCase; + import org.apache.hadoop.hbase.KeyValue; import org.apache.hadoop.hbase.SmallTests; +import org.apache.hadoop.hbase.io.ImmutableBytesWritable; import org.apache.hadoop.hbase.util.Bytes; import org.junit.experimental.categories.Category; @@ -122,8 +124,27 @@ public class TestResult extends TestCase { } } + /** + * Verify that Result.getBytes(...) behaves correctly. + */ + public void testResultGetBytes() throws Exception { + byte [] value1 = Bytes.toBytes("value1"); + byte [] qual = Bytes.toBytes("qual"); + + KeyValue kv1 = new KeyValue(row, family, qual, value); + KeyValue kv2 = new KeyValue(row, family, qual, value1); + + Result r1 = new Result(new KeyValue[] {kv1, kv2}); + + ImmutableBytesWritable bytes = r1.getBytes(); + assertNotNull(bytes); + + Result r2 = new Result(bytes); + // no exception thrown + Result.compareResults(r1, r2); + } + @org.junit.Rule public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu = new org.apache.hadoop.hbase.ResourceCheckerJUnitRule(); } - -- 1.8.1.msysgit.1