From 5527169b2cfef8f98053ab02cb7353f8e63d4c7a 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 | 41 ++++++++++++++++++++++ .../org/apache/hadoop/hbase/client/TestPut.java | 16 +++++++++ 2 files changed, 57 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..ff03ff0 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,47 @@ public class Put extends Mutation implements HeapSize, Comparable { } /** + * Create a Put operation for an immutable row key. + * + * @param row row key + * @param rowIsImmutable whether the input row is immutable. + * Set to true if the caller can guarantee that + * the row will not be changed for the Put duration. + */ + public Put(byte [] row, boolean rowIsImmutable) { + this(row, HConstants.LATEST_TIMESTAMP, rowIsImmutable); + } + + /** + * Create a Put operation for an immutable row key, using a given timestamp. + * + * @param row row key + * @param ts timestamp + * @param rowIsImmutable whether the input row is immutable. + * Set to true if the caller can guarantee that + * the row will not be changed for the Put duration. + */ + public Put(byte[] row, long ts, boolean rowIsImmutable) { + // Check and set timestamp + if (ts < 0) { + throw new IllegalArgumentException("Timestamp cannot be negative. ts=" + ts); + } + this.ts = ts; + + // Deal with row according to inputIsImmutable + checkRow(row); + + if (rowIsImmutable) { // Row is immutable + // Do not make a local copy, but point to the provided byte array directly + this.row = row; + } else { // Row is not immutable + // Make a local copy + checkRow(row, 0, row.length); + this.row = Bytes.copy(row, 0, row.length); + } + } + + /** * 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..8603fe1 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 testRowIsImmutableOrNot() { + byte[] rowKey = Bytes.toBytes("immutable"); + + // Test when row key is immutable + Put putRowIsImmutable = new Put(rowKey, true); + assertTrue(rowKey == putRowIsImmutable.getRow()); // No local copy is made + + // Test when row key is not immutable + Put putRowIsNotImmutable = new Put(rowKey, 1000L, false); + assertTrue(rowKey != putRowIsNotImmutable.getRow()); // A local copy is made + } } -- 2.7.4 (Apple Git-66)