Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1506,6 +1506,21 @@ private static MutationProto.Builder getMutationBuilderAndSetCommonFields(final
* @return the converted protocol buffer Result
*/
public static ClientProtos.Result toResult(final Result result) {
return toResult(result, false);
}

/**
* Convert a client Result to a protocol buffer Result
* @param result the client Result to convert
* @param encodeTags whether to includeTags in converted protobuf result or not
* When @encodeTags is set to true, it will return all the tags in the response.
* These tags may contain some sensitive data like acl permissions, etc.
* Only the tools like Export, Import which needs to take backup needs to set
* it to true so that cell tags are persisted in backup.
* Refer to HBASE-25246 for more context.
* @return the converted protocol buffer Result
*/
public static ClientProtos.Result toResult(final Result result, boolean encodeTags) {
if (result.getExists() != null) {
return toResult(result.getExists(), result.isStale());
}
Expand All @@ -1517,7 +1532,7 @@ public static ClientProtos.Result toResult(final Result result) {

ClientProtos.Result.Builder builder = ClientProtos.Result.newBuilder();
for (Cell c : cells) {
builder.addCell(toCell(c));
builder.addCell(toCell(c, encodeTags));
}

builder.setStale(result.isStale());
Expand Down Expand Up @@ -1564,6 +1579,22 @@ public static ClientProtos.Result toResultNoData(final Result result) {
* @return the converted client Result
*/
public static Result toResult(final ClientProtos.Result proto) {
return toResult(proto, false);
}

/**
* Convert a protocol buffer Result to a client Result
*
* @param proto the protocol buffer Result to convert
* @param decodeTags whether to decode tags into converted client Result
* When @decodeTags is set to true, it will decode all the tags from the
* response. These tags may contain some sensitive data like acl permissions,
* etc. Only the tools like Export, Import which needs to take backup needs to
* set it to true so that cell tags are persisted in backup.
* Refer to HBASE-25246 for more context.
* @return the converted client Result
*/
public static Result toResult(final ClientProtos.Result proto, boolean decodeTags) {
if (proto.hasExists()) {
if (proto.getStale()) {
return proto.getExists() ? EMPTY_RESULT_EXISTS_TRUE_STALE :EMPTY_RESULT_EXISTS_FALSE_STALE;
Expand All @@ -1578,7 +1609,7 @@ public static Result toResult(final ClientProtos.Result proto) {

List<Cell> cells = new ArrayList<Cell>(values.size());
for (CellProtos.Cell c : values) {
cells.add(toCell(c));
cells.add(toCell(c, decodeTags));
}
return Result.create(cells, null, proto.getStale(), proto.getPartial());
}
Expand Down Expand Up @@ -1620,7 +1651,7 @@ public static Result toResult(final ClientProtos.Result proto, final CellScanner
if (!values.isEmpty()){
if (cells == null) cells = new ArrayList<Cell>(values.size());
for (CellProtos.Cell c: values) {
cells.add(toCell(c));
cells.add(toCell(c, false));
}
}

Expand Down Expand Up @@ -2802,7 +2833,7 @@ public static void toIOException(ServiceException se) throws IOException {
throw new IOException(se);
}

public static CellProtos.Cell toCell(final Cell kv) {
public static CellProtos.Cell toCell(final Cell kv, boolean encodeTags) {
// Doing this is going to kill us if we do it for all data passed.
// St.Ack 20121205
CellProtos.Cell.Builder kvbuilder = CellProtos.Cell.newBuilder();
Expand All @@ -2816,18 +2847,27 @@ public static CellProtos.Cell toCell(final Cell kv) {
kvbuilder.setTimestamp(kv.getTimestamp());
kvbuilder.setValue(ByteStringer.wrap(kv.getValueArray(), kv.getValueOffset(),
kv.getValueLength()));
if (encodeTags && kv.getTagsLength() > 0) {
kvbuilder.setTags(ByteStringer.wrap(kv.getTagsArray(), kv.getTagsOffset(),
kv.getTagsLength()));
}
return kvbuilder.build();
}

public static Cell toCell(final CellProtos.Cell cell) {
public static Cell toCell(final CellProtos.Cell cell, boolean decodeTags) {
// Doing this is going to kill us if we do it for all data passed.
// St.Ack 20121205
byte[] tags = null;
if (decodeTags && cell.hasTags()) {
tags = cell.getTags().toByteArray();
}
return CellUtil.createCell(cell.getRow().toByteArray(),
cell.getFamily().toByteArray(),
cell.getQualifier().toByteArray(),
cell.getTimestamp(),
(byte)cell.getCellType().getNumber(),
cell.getValue().toByteArray());
KeyValue.Type.codeToType((byte)(cell.getCellType().getNumber())),
cell.getValue().toByteArray(),
tags);
}

public static HBaseProtos.NamespaceDescriptor toProtoNamespaceDescriptor(NamespaceDescriptor ns) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ public class TestPBCell {
public void testRoundTrip() {
final Cell cell = new KeyValue(Bytes.toBytes("row"), Bytes.toBytes("fam"),
Bytes.toBytes("qual"), Bytes.toBytes("val"));
CellProtos.Cell c = ProtobufUtil.toCell(cell), decoded;
CellProtos.Cell c = ProtobufUtil.toCell(cell, false), decoded;
PositionedByteRange pbr = new SimplePositionedByteRange(c.getSerializedSize());
pbr.setPosition(0);
int encodedLength = CODEC.encode(pbr, c);
pbr.setPosition(0);
decoded = CODEC.decode(pbr);
assertEquals(encodedLength, pbr.getPosition());
assertTrue(CellComparator.equals(cell, ProtobufUtil.toCell(decoded)));
assertTrue(CellComparator.equals(cell, ProtobufUtil.toCell(decoded, false)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.KeyValueUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.Tag;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.classification.InterfaceAudience;
import org.apache.hadoop.hbase.classification.InterfaceStability;
Expand Down Expand Up @@ -495,6 +496,10 @@ private static Cell convertKv(Cell kv, Map<byte[], byte[]> cfRenameMap) {
// If there's a rename mapping for this CF, create a new KeyValue
byte[] newCfName = cfRenameMap.get(CellUtil.cloneFamily(kv));
if(newCfName != null) {
List<Tag> tags = null;
if (kv.getTagsLength() > 0) {
tags = Tag.asList(kv.getTagsArray(), kv.getTagsOffset(), kv.getTagsLength());
}
kv = new KeyValue(kv.getRowArray(), // row buffer
kv.getRowOffset(), // row offset
kv.getRowLength(), // row length
Expand All @@ -508,7 +513,8 @@ private static Cell convertKv(Cell kv, Map<byte[], byte[]> cfRenameMap) {
KeyValue.Type.codeToType(kv.getTypeByte()), // KV Type
kv.getValueArray(), // value buffer
kv.getValueOffset(), // value offset
kv.getValueLength()); // value length
kv.getValueLength(), // value length
tags);
}
}
return kv;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public Result deserialize(Result mutation) throws IOException {
ClientProtos.Result.Builder builder = ClientProtos.Result.newBuilder();
ProtobufUtil.mergeDelimitedFrom(builder, in);
ClientProtos.Result proto = builder.build();
return ProtobufUtil.toResult(proto);
return ProtobufUtil.toResult(proto, true);
}

@Override
Expand All @@ -156,7 +156,7 @@ public void open(OutputStream out) throws IOException {

@Override
public void serialize(Result result) throws IOException {
ProtobufUtil.toResult(result).writeDelimitedTo(out);
ProtobufUtil.toResult(result, true).writeDelimitedTo(out);
}
}
}
Loading