Search in sources :

Example 6 with ExtendedCellBuilder

use of org.apache.hadoop.hbase.ExtendedCellBuilder in project hbase by apache.

the class ProtobufUtil method toPut.

/**
 * Convert a protocol buffer Mutate to a Put.
 *
 * @param proto The protocol buffer MutationProto to convert
 * @param cellScanner If non-null, the Cell data that goes with this proto.
 * @return A client Put.
 * @throws IOException
 */
public static Put toPut(final MutationProto proto, final CellScanner cellScanner) throws IOException {
    // TODO: Server-side at least why do we convert back to the Client types?  Why not just pb it?
    MutationType type = proto.getMutateType();
    assert type == MutationType.PUT : type.name();
    long timestamp = proto.hasTimestamp() ? proto.getTimestamp() : HConstants.LATEST_TIMESTAMP;
    Put put = proto.hasRow() ? new Put(proto.getRow().toByteArray(), timestamp) : null;
    int cellCount = proto.hasAssociatedCellCount() ? proto.getAssociatedCellCount() : 0;
    if (cellCount > 0) {
        // The proto has metadata only and the data is separate to be found in the cellScanner.
        if (cellScanner == null) {
            throw new DoNotRetryIOException("Cell count of " + cellCount + " but no cellScanner: " + toShortString(proto));
        }
        for (int i = 0; i < cellCount; i++) {
            if (!cellScanner.advance()) {
                throw new DoNotRetryIOException("Cell count of " + cellCount + " but at index " + i + " no cell returned: " + toShortString(proto));
            }
            Cell cell = cellScanner.current();
            if (put == null) {
                put = new Put(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength(), timestamp);
            }
            put.add(cell);
        }
    } else {
        if (put == null) {
            throw new IllegalArgumentException("row cannot be null");
        }
        // The proto has the metadata and the data itself
        ExtendedCellBuilder cellBuilder = ExtendedCellBuilderFactory.create(CellBuilderType.SHALLOW_COPY);
        for (ColumnValue column : proto.getColumnValueList()) {
            byte[] family = column.getFamily().toByteArray();
            for (QualifierValue qv : column.getQualifierValueList()) {
                if (!qv.hasValue()) {
                    throw new DoNotRetryIOException("Missing required field: qualifier value");
                }
                long ts = timestamp;
                if (qv.hasTimestamp()) {
                    ts = qv.getTimestamp();
                }
                byte[] allTagsBytes;
                if (qv.hasTags()) {
                    allTagsBytes = qv.getTags().toByteArray();
                    if (qv.hasDeleteType()) {
                        put.add(cellBuilder.clear().setRow(proto.getRow().toByteArray()).setFamily(family).setQualifier(qv.hasQualifier() ? qv.getQualifier().toByteArray() : null).setTimestamp(ts).setType(fromDeleteType(qv.getDeleteType()).getCode()).setTags(allTagsBytes).build());
                    } else {
                        put.add(cellBuilder.clear().setRow(put.getRow()).setFamily(family).setQualifier(qv.hasQualifier() ? qv.getQualifier().toByteArray() : null).setTimestamp(ts).setType(Cell.Type.Put).setValue(qv.hasValue() ? qv.getValue().toByteArray() : null).setTags(allTagsBytes).build());
                    }
                } else {
                    if (qv.hasDeleteType()) {
                        put.add(cellBuilder.clear().setRow(put.getRow()).setFamily(family).setQualifier(qv.hasQualifier() ? qv.getQualifier().toByteArray() : null).setTimestamp(ts).setType(fromDeleteType(qv.getDeleteType()).getCode()).build());
                    } else {
                        put.add(cellBuilder.clear().setRow(put.getRow()).setFamily(family).setQualifier(qv.hasQualifier() ? qv.getQualifier().toByteArray() : null).setTimestamp(ts).setType(Type.Put).setValue(qv.hasValue() ? qv.getValue().toByteArray() : null).build());
                    }
                }
            }
        }
    }
    put.setDurability(toDurability(proto.getDurability()));
    for (NameBytesPair attribute : proto.getAttributeList()) {
        put.setAttribute(attribute.getName(), attribute.getValue().toByteArray());
    }
    return put;
}
Also used : ExtendedCellBuilder(org.apache.hadoop.hbase.ExtendedCellBuilder) MutationType(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutationProto.MutationType) NameBytesPair(org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.NameBytesPair) DoNotRetryIOException(org.apache.hadoop.hbase.DoNotRetryIOException) QualifierValue(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutationProto.ColumnValue.QualifierValue) ColumnValue(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutationProto.ColumnValue) Cell(org.apache.hadoop.hbase.Cell) ByteBufferExtendedCell(org.apache.hadoop.hbase.ByteBufferExtendedCell) Put(org.apache.hadoop.hbase.client.Put)

Example 7 with ExtendedCellBuilder

use of org.apache.hadoop.hbase.ExtendedCellBuilder in project hbase by apache.

the class ProtobufUtil method toResult.

/**
 * Convert a protocol buffer Result to a client Result
 *
 * @param proto the protocol buffer Result to convert
 * @param scanner Optional cell scanner.
 * @return the converted client Result
 * @throws IOException
 */
public static Result toResult(final ClientProtos.Result proto, final CellScanner scanner) throws IOException {
    List<CellProtos.Cell> values = proto.getCellList();
    if (proto.hasExists()) {
        if ((values != null && !values.isEmpty()) || (proto.hasAssociatedCellCount() && proto.getAssociatedCellCount() > 0)) {
            throw new IllegalArgumentException("bad proto: exists with cells is no allowed " + proto);
        }
        if (proto.getStale()) {
            return proto.getExists() ? EMPTY_RESULT_EXISTS_TRUE_STALE : EMPTY_RESULT_EXISTS_FALSE_STALE;
        }
        return proto.getExists() ? EMPTY_RESULT_EXISTS_TRUE : EMPTY_RESULT_EXISTS_FALSE;
    }
    // TODO: Unit test that has some Cells in scanner and some in the proto.
    List<Cell> cells = null;
    if (proto.hasAssociatedCellCount()) {
        int count = proto.getAssociatedCellCount();
        cells = new ArrayList<>(count + values.size());
        for (int i = 0; i < count; i++) {
            if (!scanner.advance())
                throw new IOException("Failed get " + i + " of " + count);
            cells.add(scanner.current());
        }
    }
    if (!values.isEmpty()) {
        if (cells == null)
            cells = new ArrayList<>(values.size());
        ExtendedCellBuilder builder = ExtendedCellBuilderFactory.create(CellBuilderType.SHALLOW_COPY);
        for (CellProtos.Cell c : values) {
            cells.add(toCell(builder, c, false));
        }
    }
    return (cells == null || cells.isEmpty()) ? (proto.getStale() ? EMPTY_RESULT_STALE : EMPTY_RESULT) : Result.create(cells, null, proto.getStale());
}
Also used : ExtendedCellBuilder(org.apache.hadoop.hbase.ExtendedCellBuilder) ArrayList(java.util.ArrayList) IOException(java.io.IOException) DoNotRetryIOException(org.apache.hadoop.hbase.DoNotRetryIOException) HBaseIOException(org.apache.hadoop.hbase.HBaseIOException) CellProtos(org.apache.hadoop.hbase.shaded.protobuf.generated.CellProtos) Cell(org.apache.hadoop.hbase.Cell) ByteBufferExtendedCell(org.apache.hadoop.hbase.ByteBufferExtendedCell)

Aggregations

ExtendedCellBuilder (org.apache.hadoop.hbase.ExtendedCellBuilder)7 ArrayList (java.util.ArrayList)5 Cell (org.apache.hadoop.hbase.Cell)4 ByteBufferExtendedCell (org.apache.hadoop.hbase.ByteBufferExtendedCell)3 Put (org.apache.hadoop.hbase.client.Put)3 DoNotRetryIOException (org.apache.hadoop.hbase.DoNotRetryIOException)2 Mutation (org.apache.hadoop.hbase.client.Mutation)2 OperationStatus (org.apache.hadoop.hbase.regionserver.OperationStatus)2 CellProtos (org.apache.hadoop.hbase.shaded.protobuf.generated.CellProtos)2 IOException (java.io.IOException)1 ArrayBackedTag (org.apache.hadoop.hbase.ArrayBackedTag)1 HBaseIOException (org.apache.hadoop.hbase.HBaseIOException)1 Tag (org.apache.hadoop.hbase.Tag)1 ColumnValue (org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutationProto.ColumnValue)1 QualifierValue (org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutationProto.ColumnValue.QualifierValue)1 MutationType (org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutationProto.MutationType)1 NameBytesPair (org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.NameBytesPair)1 TColumnValue (org.apache.hadoop.hbase.thrift2.generated.TColumnValue)1