HBASE-14881 Provide a Put API that uses the provided row without coping

(Xiang Li)
This commit is contained in:
Ramkrishna 2016-08-01 14:35:49 +05:30
parent 7e6f562754
commit 2c5a0fcf1f
2 changed files with 53 additions and 0 deletions

View File

@ -116,6 +116,43 @@ public class Put extends Mutation implements HeapSize, Comparable<Row> {
}
}
/**
* 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 rowIsImmutable
checkRow(row);
if (rowIsImmutable) { // Row is immutable
this.row = row; // Do not make a local copy, but point to the provided byte array directly
} else { // Row is not immutable
this.row = Bytes.copy(row, 0, row.length); // Make a local copy
}
}
/**
* Copy constructor. Creates a Put operation cloned from the specified Put.
* @param putToCopy put to copy

View File

@ -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
}
}