From d49ac1ef8a48bcf23a267c9555809947e1c3ef4c Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Sun, 17 Jul 2016 12:37:58 +0800 Subject: [PATCH] HBASE-14881: Provide a Put API that uses the provided row without coping --- .../java/org/apache/hadoop/hbase/client/Put.java | 39 ++++++++++++++++++++++ .../org/apache/hadoop/hbase/client/TestPut.java | 16 +++++++++ 2 files changed, 55 insertions(+) diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Put.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Put.java index 6b06875..544404e 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Put.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Put.java @@ -117,6 +117,45 @@ public class Put extends Mutation implements HeapSize, Comparable { } /** + * Create a Put operation for the specified row key. + * It is up to the caller whether to make a copy of the passed-in row key to keep local or not. + * + * @param row row key + * @param copyRow whether to make a copy of the passed-in row key to keep local. + * Set to false if the caller can make sure that the row is immutable + * and will not be changed for the Put duration. + */ + public Put(byte [] row, boolean copyRow) { + this(row, HConstants.LATEST_TIMESTAMP, copyRow); + } + + /** + * Create a Put operation for the specified row, using a given timestamp. + * It is up to the caller whether to make a copy of the passed-in row key to keep local or not. + * + * @param row row key + * @param ts timestamp + * @param copyRow whether to make a copy of the passed-in row key to keep local. + * Set to false if the caller can make sure that the row is immutable + * and will not be changed for the Put duration. + */ + public Put(byte[] row, long ts, boolean copyRow) { + // Check and set timestamp + if (ts < 0) { + throw new IllegalArgumentException("Timestamp cannot be negative. ts=" + ts); + } + this.ts = ts; + + // Deal with row according to copyRow + if (copyRow) { // make a copy to keep local + checkRow(row, 0, row.length); + this.row = Bytes.copy(row, 0, row.length); + } else { // do not make a copy to keep local, but point to the provided byte array directly + this.row = row; + } + } + + /** * Copy constructor. Creates a Put operation cloned from the specified Put. * @param putToCopy put to copy */ diff --git a/hbase-client/src/test/java/org/apache/hadoop/hbase/client/TestPut.java b/hbase-client/src/test/java/org/apache/hadoop/hbase/client/TestPut.java index 3a877dc..b4efb96 100644 --- a/hbase-client/src/test/java/org/apache/hadoop/hbase/client/TestPut.java +++ b/hbase-client/src/test/java/org/apache/hadoop/hbase/client/TestPut.java @@ -23,6 +23,8 @@ import org.apache.hadoop.hbase.util.Bytes; import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertTrue; + public class TestPut { @Test public void testCopyConstructor() { @@ -40,4 +42,18 @@ public class TestPut { assertNotEquals(origin.getCellList(family), clone.getCellList(family)); } + + // HBASE-14881 + @Test + public void testCopyRowAndNotCopyRow() { + byte[] rowKey = Bytes.toBytes("immutable"); + + // Do not make a copy of the passed-in row key to keep local + Put putNotCopyRow = new Put(rowKey, false); + assertTrue(rowKey == putNotCopyRow.getRow()); + + // Make a copy of the passed-in row key to keep local + Put putCopyRow = new Put(rowKey, 1000L, true); + assertTrue(rowKey != putCopyRow.getRow()); + } } -- 2.7.4 (Apple Git-66)