HBASE-15616 Allow null qualifier for all table operations

This commit is contained in:
Guanghao Zhang 2017-05-18 13:12:01 +08:00
parent 7a535f5843
commit 3fe4b28bb0
5 changed files with 103 additions and 26 deletions

View File

@ -716,8 +716,6 @@ public class HTable implements Table {
npe = new NullPointerException("row is null");
} else if (family == null) {
npe = new NullPointerException("family is null");
} else if (qualifier == null) {
npe = new NullPointerException("qualifier is null");
}
if (npe != null) {
throw new IOException(

View File

@ -119,9 +119,6 @@ public class Increment extends Mutation implements Comparable<Row> {
if (family == null) {
throw new IllegalArgumentException("family cannot be null");
}
if (qualifier == null) {
throw new IllegalArgumentException("qualifier cannot be null");
}
List<Cell> list = getCellList(family);
KeyValue kv = createPutKeyValue(family, qualifier, ts, Bytes.toBytes(amount));
list.add(kv);

View File

@ -190,7 +190,8 @@ public final class RequestConverter {
columnBuilder.setFamily(UnsafeByteOperations.unsafeWrap(family));
QualifierValue.Builder valueBuilder = QualifierValue.newBuilder();
valueBuilder.setValue(UnsafeByteOperations.unsafeWrap(Bytes.toBytes(amount)));
valueBuilder.setQualifier(UnsafeByteOperations.unsafeWrap(qualifier));
valueBuilder.setQualifier(UnsafeByteOperations
.unsafeWrap(qualifier == null ? HConstants.EMPTY_BYTE_ARRAY : qualifier));
columnBuilder.addQualifierValue(valueBuilder.build());
mutateBuilder.addColumnValue(columnBuilder.build());
if (nonce != HConstants.NO_NONCE) {
@ -1006,14 +1007,14 @@ public final class RequestConverter {
* @return a Condition
* @throws IOException
*/
private static Condition buildCondition(final byte[] row,
final byte[] family, final byte [] qualifier,
final ByteArrayComparable comparator,
final CompareType compareType) throws IOException {
private static Condition buildCondition(final byte[] row, final byte[] family,
final byte[] qualifier, final ByteArrayComparable comparator, final CompareType compareType)
throws IOException {
Condition.Builder builder = Condition.newBuilder();
builder.setRow(UnsafeByteOperations.unsafeWrap(row));
builder.setFamily(UnsafeByteOperations.unsafeWrap(family));
builder.setQualifier(UnsafeByteOperations.unsafeWrap(qualifier));
builder.setQualifier(UnsafeByteOperations
.unsafeWrap(qualifier == null ? HConstants.EMPTY_BYTE_ARRAY : qualifier));
builder.setComparator(ProtobufUtil.toComparator(comparator));
builder.setCompareType(compareType);
return builder.build();

View File

@ -1426,6 +1426,62 @@ public class TestFromClientSide {
}
}
@Test
public void testNullQualifier() throws Exception {
final TableName tableName = TableName.valueOf(name.getMethodName());
Table table = TEST_UTIL.createTable(tableName, FAMILY);
// Work for Put
Put put = new Put(ROW);
put.addColumn(FAMILY, null, VALUE);
table.put(put);
// Work for Get, Scan
getTestNull(table, ROW, FAMILY, VALUE);
scanTestNull(table, ROW, FAMILY, VALUE);
// Work for Delete
Delete delete = new Delete(ROW);
delete.addColumns(FAMILY, null);
table.delete(delete);
Get get = new Get(ROW);
Result result = table.get(get);
assertEmptyResult(result);
// Work for Increment/Append
Increment increment = new Increment(ROW);
increment.addColumn(FAMILY, null, 1L);
table.increment(increment);
getTestNull(table, ROW, FAMILY, 1L);
table.incrementColumnValue(ROW, FAMILY, null, 1L);
getTestNull(table, ROW, FAMILY, 2L);
delete = new Delete(ROW);
delete.addColumns(FAMILY, null);
table.delete(delete);
Append append = new Append(ROW);
append.add(FAMILY, null, VALUE);
table.append(append);
getTestNull(table, ROW, FAMILY, VALUE);
// Work for checkAndMutate, checkAndPut, checkAndDelete
put = new Put(ROW);
put.addColumn(FAMILY, null, Bytes.toBytes("checkAndPut"));
table.put(put);
table.checkAndPut(ROW, FAMILY, null, VALUE, put);
RowMutations mutate = new RowMutations(ROW);
mutate.add(new Put(ROW).addColumn(FAMILY, null, Bytes.toBytes("checkAndMutate")));
table.checkAndMutate(ROW, FAMILY, null, CompareOp.EQUAL, Bytes.toBytes("checkAndPut"), mutate);
delete = new Delete(ROW);
delete.addColumns(FAMILY, null);
table.checkAndDelete(ROW, FAMILY, null, Bytes.toBytes("checkAndMutate"), delete);
}
@Test
public void testVersions() throws Exception {
final TableName tableName = TableName.valueOf(name.getMethodName());
@ -2815,6 +2871,27 @@ public class TestFromClientSide {
}
private void getTestNull(Table ht, byte[] row, byte[] family, long value) throws Exception {
Get get = new Get(row);
get.addColumn(family, null);
Result result = ht.get(get);
assertSingleResult(result, row, family, null, value);
get = new Get(row);
get.addColumn(family, HConstants.EMPTY_BYTE_ARRAY);
result = ht.get(get);
assertSingleResult(result, row, family, HConstants.EMPTY_BYTE_ARRAY, value);
get = new Get(row);
get.addFamily(family);
result = ht.get(get);
assertSingleResult(result, row, family, HConstants.EMPTY_BYTE_ARRAY, value);
get = new Get(row);
result = ht.get(get);
assertSingleResult(result, row, family, HConstants.EMPTY_BYTE_ARRAY, value);
}
private void scanTestNull(Table ht, byte[] row, byte[] family, byte[] value)
throws Exception {
scanTestNull(ht, row, family, value, false);
@ -3386,6 +3463,25 @@ public class TestFromClientSide {
equals(value, CellUtil.cloneValue(kv)));
}
private void assertSingleResult(Result result, byte[] row, byte[] family, byte[] qualifier,
long value) throws Exception {
assertTrue(
"Expected row [" + Bytes.toString(row) + "] " + "Got row [" + Bytes.toString(result.getRow())
+ "]", equals(row, result.getRow()));
assertTrue("Expected a single key but result contains " + result.size(), result.size() == 1);
Cell kv = result.rawCells()[0];
assertTrue(
"Expected family [" + Bytes.toString(family) + "] " + "Got family ["
+ Bytes.toString(CellUtil.cloneFamily(kv)) + "]",
equals(family, CellUtil.cloneFamily(kv)));
assertTrue("Expected qualifier [" + Bytes.toString(qualifier) + "] " + "Got qualifier ["
+ Bytes.toString(CellUtil.cloneQualifier(kv)) + "]",
equals(qualifier, CellUtil.cloneQualifier(kv)));
assertTrue(
"Expected value [" + value + "] " + "Got value [" + Bytes.toLong(CellUtil.cloneValue(kv))
+ "]", value == Bytes.toLong(CellUtil.cloneValue(kv)));
}
private void assertSingleResult(Result result, byte [] row, byte [] family,
byte [] qualifier, long ts, byte [] value)
throws Exception {

View File

@ -225,13 +225,6 @@ public class TestIncrementsFromClientSide {
} catch (IOException iox) {
// success
}
try {
// try null qualifier
ht.incrementColumnValue(ROW, FAMILY, null, 5);
fail("Should have thrown IOException");
} catch (IOException iox) {
// success
}
// try null row
try {
Increment incNoRow = new Increment((byte [])null);
@ -250,14 +243,6 @@ public class TestIncrementsFromClientSide {
} catch (IllegalArgumentException iax) {
// success
}
// try null qualifier
try {
Increment incNoQualifier = new Increment(ROW);
incNoQualifier.addColumn(FAMILY, null, 5);
fail("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException iax) {
// success
}
}
@Test