use of org.apache.hadoop.hbase.client.Increment in project hbase by apache.
the class RequestConverter method buildMultiRequest.
private static ClientProtos.MultiRequest buildMultiRequest(final byte[] regionName, final RowMutations rowMutations, final Condition condition, long nonceGroup, long nonce) throws IOException {
RegionAction.Builder builder = getRegionActionBuilderWithRegion(RegionAction.newBuilder(), regionName);
builder.setAtomic(true);
boolean hasNonce = false;
ClientProtos.Action.Builder actionBuilder = ClientProtos.Action.newBuilder();
MutationProto.Builder mutationBuilder = MutationProto.newBuilder();
for (Mutation mutation : rowMutations.getMutations()) {
mutationBuilder.clear();
MutationProto mp;
if (mutation instanceof Increment || mutation instanceof Append) {
mp = ProtobufUtil.toMutation(getMutationType(mutation), mutation, mutationBuilder, nonce);
hasNonce = true;
} else {
mp = ProtobufUtil.toMutation(getMutationType(mutation), mutation, mutationBuilder);
}
actionBuilder.clear();
actionBuilder.setMutation(mp);
builder.addAction(actionBuilder.build());
}
if (condition != null) {
builder.setCondition(condition);
}
MultiRequest.Builder multiRequestBuilder = MultiRequest.newBuilder();
if (hasNonce) {
multiRequestBuilder.setNonceGroup(nonceGroup);
}
return multiRequestBuilder.addRegionAction(builder.build()).build();
}
use of org.apache.hadoop.hbase.client.Increment in project hbase by apache.
the class ProtobufUtil method toMutation.
public static MutationProto toMutation(final MutationType type, final Mutation mutation, MutationProto.Builder builder, long nonce) throws IOException {
builder = getMutationBuilderAndSetCommonFields(type, mutation, builder);
if (nonce != HConstants.NO_NONCE) {
builder.setNonce(nonce);
}
if (type == MutationType.INCREMENT) {
builder.setTimeRange(ProtobufUtil.toTimeRange(((Increment) mutation).getTimeRange()));
}
if (type == MutationType.APPEND) {
builder.setTimeRange(ProtobufUtil.toTimeRange(((Append) mutation).getTimeRange()));
}
ColumnValue.Builder columnBuilder = ColumnValue.newBuilder();
QualifierValue.Builder valueBuilder = QualifierValue.newBuilder();
for (Map.Entry<byte[], List<Cell>> family : mutation.getFamilyCellMap().entrySet()) {
columnBuilder.clear();
columnBuilder.setFamily(UnsafeByteOperations.unsafeWrap(family.getKey()));
for (Cell cell : family.getValue()) {
valueBuilder.clear();
valueBuilder.setQualifier(UnsafeByteOperations.unsafeWrap(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength()));
valueBuilder.setValue(UnsafeByteOperations.unsafeWrap(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));
valueBuilder.setTimestamp(cell.getTimestamp());
if (type == MutationType.DELETE || (type == MutationType.PUT && CellUtil.isDelete(cell))) {
KeyValue.Type keyValueType = KeyValue.Type.codeToType(cell.getTypeByte());
valueBuilder.setDeleteType(toDeleteType(keyValueType));
}
columnBuilder.addQualifierValue(valueBuilder.build());
}
builder.addColumnValue(columnBuilder.build());
}
return builder.build();
}
use of org.apache.hadoop.hbase.client.Increment in project hbase by apache.
the class TestProtobufUtil method testIncrement.
/**
* Test Increment Mutate conversions.
*
* @throws IOException if converting to an {@link Increment} or
* {@link org.apache.hadoop.hbase.client.Mutation} fails
*/
@Test
public void testIncrement() throws IOException {
MutationProto proto = getIncrementMutation(111111L);
// default fields
assertEquals(MutationProto.Durability.USE_DEFAULT, proto.getDurability());
// set the default value for equal comparison
MutationProto.Builder mutateBuilder = MutationProto.newBuilder(proto);
mutateBuilder.setDurability(MutationProto.Durability.USE_DEFAULT);
Increment increment = ProtobufUtil.toIncrement(proto, null);
mutateBuilder.setTimestamp(increment.getTimestamp());
mutateBuilder.setTimeRange(ProtobufUtil.toTimeRange(increment.getTimeRange()));
assertEquals(mutateBuilder.build(), ProtobufUtil.toMutation(MutationType.INCREMENT, increment));
}
use of org.apache.hadoop.hbase.client.Increment in project hbase by apache.
the class TestRegionServerReadRequestMetrics method testReadRequestsCountNotFiltered.
@Test
public void testReadRequestsCountNotFiltered() throws Exception {
int resultCount;
Scan scan;
Append append;
Put put;
Increment increment;
Get get;
// test for scan
scan = new Scan();
try (ResultScanner scanner = table.getScanner(scan)) {
resultCount = 0;
for (Result ignore : scanner) {
resultCount++;
}
testReadRequests(resultCount, 3, 0);
}
// test for scan
scan = new Scan().withStartRow(ROW2).withStopRow(ROW3);
try (ResultScanner scanner = table.getScanner(scan)) {
resultCount = 0;
for (Result ignore : scanner) {
resultCount++;
}
testReadRequests(resultCount, 1, 0);
}
// test for get
get = new Get(ROW2);
Result result = table.get(get);
resultCount = result.isEmpty() ? 0 : 1;
testReadRequests(resultCount, 1, 0);
// test for increment
increment = new Increment(ROW1);
increment.addColumn(CF1, COL3, 1);
result = table.increment(increment);
resultCount = result.isEmpty() ? 0 : 1;
testReadRequests(resultCount, 1, 0);
// test for checkAndPut
put = new Put(ROW1);
put.addColumn(CF1, COL2, VAL2);
boolean checkAndPut = table.checkAndMutate(ROW1, CF1).qualifier(COL2).ifEquals(VAL2).thenPut(put);
resultCount = checkAndPut ? 1 : 0;
testReadRequests(resultCount, 1, 0);
// test for append
append = new Append(ROW1);
append.addColumn(CF1, COL2, VAL2);
result = table.append(append);
resultCount = result.isEmpty() ? 0 : 1;
testReadRequests(resultCount, 1, 0);
// test for checkAndMutate
put = new Put(ROW1);
put.addColumn(CF1, COL1, VAL1);
RowMutations rm = new RowMutations(ROW1);
rm.add(put);
boolean checkAndMutate = table.checkAndMutate(ROW1, CF1).qualifier(COL1).ifEquals(VAL1).thenMutate(rm);
resultCount = checkAndMutate ? 1 : 0;
testReadRequests(resultCount, 1, 0);
}
use of org.apache.hadoop.hbase.client.Increment in project hbase by apache.
the class TestRegionIncrement method testUnContendedSingleCellIncrement.
/**
* Have each thread update its own Cell. Avoid contention with another thread.
*/
@Test
public void testUnContendedSingleCellIncrement() throws IOException, InterruptedException {
final HRegion region = getRegion(TEST_UTIL.getConfiguration(), TestIncrementsFromClientSide.filterStringSoTableNameSafe(this.name.getMethodName()));
long startTime = EnvironmentEdgeManager.currentTime();
try {
SingleCellIncrementer[] threads = new SingleCellIncrementer[THREAD_COUNT];
for (int i = 0; i < threads.length; i++) {
byte[] rowBytes = Bytes.toBytes(i);
Increment increment = new Increment(rowBytes);
increment.addColumn(INCREMENT_BYTES, INCREMENT_BYTES, 1);
threads[i] = new SingleCellIncrementer(i, INCREMENT_COUNT, region, increment);
}
for (int i = 0; i < threads.length; i++) {
threads[i].start();
}
for (int i = 0; i < threads.length; i++) {
threads[i].join();
}
RegionScanner regionScanner = region.getScanner(new Scan());
List<Cell> cells = new ArrayList<>(THREAD_COUNT);
while (regionScanner.next(cells)) continue;
assertEquals(THREAD_COUNT, cells.size());
long total = 0;
for (Cell cell : cells) total += Bytes.toLong(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
assertEquals(INCREMENT_COUNT * THREAD_COUNT, total);
} finally {
closeRegion(region);
LOG.info(this.name.getMethodName() + " " + (EnvironmentEdgeManager.currentTime() - startTime) + "ms");
}
}
Aggregations