HBASE-19897 RowMutations should follow the fluent pattern

This commit is contained in:
Chia-Ping Tsai 2018-02-01 13:58:54 +08:00
parent 7fe215db5f
commit 6a1ed25581
2 changed files with 61 additions and 29 deletions

View File

@ -22,9 +22,9 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.apache.yetus.audience.InterfaceAudience;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.CollectionUtils;
import org.apache.yetus.audience.InterfaceAudience;
/**
* Performs multiple mutations atomically on a single row.
@ -38,6 +38,21 @@ import org.apache.hadoop.hbase.util.Bytes;
*/
@InterfaceAudience.Public
public class RowMutations implements Row {
/**
* Create a {@link RowMutations} with the specified mutations.
* @param mutations the mutations to send
* @return RowMutations
* @throws IOException if any row in mutations is different to another
*/
public static RowMutations of(List<? extends Mutation> mutations) throws IOException {
if (CollectionUtils.isEmpty(mutations)) {
throw new IllegalArgumentException("Can't instantiate a RowMutations by empty list");
}
return new RowMutations(mutations.get(0).getRow(), mutations.size())
.add(mutations);
}
private final List<Mutation> mutations;
private final byte [] row;
@ -50,8 +65,7 @@ public class RowMutations implements Row {
* @param initialCapacity the initial capacity of the RowMutations
*/
public RowMutations(byte [] row, int initialCapacity) {
Mutation.checkRow(row);
this.row = Bytes.copy(row);
this.row = Bytes.copy(Mutation.checkRow(row));
if (initialCapacity <= 0) {
this.mutations = new ArrayList<>();
} else {
@ -62,29 +76,53 @@ public class RowMutations implements Row {
/**
* Add a {@link Put} operation to the list of mutations
* @param p The {@link Put} to add
* @throws IOException
* @throws IOException if the row of added mutation doesn't match the original row
* @deprecated since 2.0 version and will be removed in 3.0 version.
* use {@link #add(Mutation)}
*/
@Deprecated
public void add(Put p) throws IOException {
internalAdd(p);
add((Mutation) p);
}
/**
* Add a {@link Delete} operation to the list of mutations
* @param d The {@link Delete} to add
* @throws IOException
* @throws IOException if the row of added mutation doesn't match the original row
* @deprecated since 2.0 version and will be removed in 3.0 version.
* use {@link #add(Mutation)}
*/
@Deprecated
public void add(Delete d) throws IOException {
internalAdd(d);
add((Mutation) d);
}
private void internalAdd(Mutation m) throws IOException {
int res = Bytes.compareTo(this.row, m.getRow());
if (res != 0) {
throw new WrongRowIOException("The row in the recently added Put/Delete <" +
Bytes.toStringBinary(m.getRow()) + "> doesn't match the original one <" +
/**
* Currently only supports {@link Put} and {@link Delete} mutations.
*
* @param mutation The data to send.
* @throws IOException if the row of added mutation doesn't match the original row
*/
public RowMutations add(Mutation mutation) throws IOException {
return add(Collections.singletonList(mutation));
}
/**
* Currently only supports {@link Put} and {@link Delete} mutations.
*
* @param mutations The data to send.
* @throws IOException if the row of added mutation doesn't match the original row
*/
public RowMutations add(List<? extends Mutation> mutations) throws IOException {
for (Mutation mutation : mutations) {
if (!Bytes.equals(row, mutation.getRow())) {
throw new WrongRowIOException("The row in the recently added Put/Delete <" +
Bytes.toStringBinary(mutation.getRow()) + "> doesn't match the original one <" +
Bytes.toStringBinary(this.row) + ">");
}
}
mutations.add(m);
this.mutations.addAll(mutations);
return this;
}
/**

View File

@ -26,6 +26,7 @@ import static org.junit.Assert.fail;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Random;
@ -452,10 +453,9 @@ public class TestFromClientSide3 {
byte [][] QUALIFIERS = new byte [][] {
Bytes.toBytes("a"), Bytes.toBytes("b")
};
RowMutations arm = new RowMutations(ROW);
Put p = new Put(ROW);
p.addColumn(FAMILY, QUALIFIERS[0], VALUE);
arm.add(p);
RowMutations arm = RowMutations.of(Collections.singletonList(
new Put(ROW).addColumn(FAMILY, QUALIFIERS[0], VALUE)));
Object[] batchResult = new Object[1];
t.batch(Arrays.asList(arm), batchResult);
@ -463,13 +463,9 @@ public class TestFromClientSide3 {
Result r = t.get(g);
assertEquals(0, Bytes.compareTo(VALUE, r.getValue(FAMILY, QUALIFIERS[0])));
arm = new RowMutations(ROW);
p = new Put(ROW);
p.addColumn(FAMILY, QUALIFIERS[1], VALUE);
arm.add(p);
Delete d = new Delete(ROW);
d.addColumns(FAMILY, QUALIFIERS[0]);
arm.add(d);
arm = RowMutations.of(Arrays.asList(
new Put(ROW).addColumn(FAMILY, QUALIFIERS[1], VALUE),
new Delete(ROW).addColumns(FAMILY, QUALIFIERS[0])));
t.batch(Arrays.asList(arm), batchResult);
r = t.get(g);
assertEquals(0, Bytes.compareTo(VALUE, r.getValue(FAMILY, QUALIFIERS[1])));
@ -477,10 +473,8 @@ public class TestFromClientSide3 {
// Test that we get the correct remote exception for RowMutations from batch()
try {
arm = new RowMutations(ROW);
p = new Put(ROW);
p.addColumn(new byte[]{'b', 'o', 'g', 'u', 's'}, QUALIFIERS[0], VALUE);
arm.add(p);
arm = RowMutations.of(Collections.singletonList(
new Put(ROW).addColumn(new byte[]{'b', 'o', 'g', 'u', 's'}, QUALIFIERS[0], VALUE)));
t.batch(Arrays.asList(arm), batchResult);
fail("Expected RetriesExhaustedWithDetailsException with NoSuchColumnFamilyException");
} catch(RetriesExhaustedWithDetailsException e) {