Java Code Examples for org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil#mergeFrom()

The following examples show how to use org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil#mergeFrom() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClusterId.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * @param bytes A pb serialized {@link ClusterId} instance with pb magic prefix
 * @return An instance of {@link ClusterId} made from <code>bytes</code>
 * @throws DeserializationException
 * @see #toByteArray()
 */
public static ClusterId parseFrom(final byte [] bytes) throws DeserializationException {
  if (ProtobufUtil.isPBMagicPrefix(bytes)) {
    int pblen = ProtobufUtil.lengthOfPBMagic();
    ClusterIdProtos.ClusterId.Builder builder = ClusterIdProtos.ClusterId.newBuilder();
    ClusterIdProtos.ClusterId cid = null;
    try {
      ProtobufUtil.mergeFrom(builder, bytes, pblen, bytes.length - pblen);
      cid = builder.build();
    } catch (IOException e) {
      throw new DeserializationException(e);
    }
    return convert(cid);
  } else {
    // Presume it was written out this way, the old way.
    return new ClusterId(Bytes.toString(bytes));
  }
}
 
Example 2
Source File: ZKUtil.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * @param bytes - Content of a WAL position znode.
 * @return long - The current WAL position.
 * @throws DeserializationException if the WAL position cannot be parsed
 */
public static long parseWALPositionFrom(final byte[] bytes) throws DeserializationException {
  if (bytes == null) {
    throw new DeserializationException("Unable to parse null WAL position.");
  }
  if (ProtobufUtil.isPBMagicPrefix(bytes)) {
    int pblen = ProtobufUtil.lengthOfPBMagic();
    ReplicationProtos.ReplicationHLogPosition.Builder builder =
        ReplicationProtos.ReplicationHLogPosition.newBuilder();
    ReplicationProtos.ReplicationHLogPosition position;
    try {
      ProtobufUtil.mergeFrom(builder, bytes, pblen, bytes.length - pblen);
      position = builder.build();
    } catch (IOException e) {
      throw new DeserializationException(e);
    }
    return position.getPosition();
  } else {
    if (bytes.length > 0) {
      return Bytes.toLong(bytes);
    }
    return 0;
  }
}
 
Example 3
Source File: CellSetModel.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Override
public ProtobufMessageHandler getObjectFromMessage(byte[] message)
    throws IOException {
  CellSet.Builder builder = CellSet.newBuilder();
  ProtobufUtil.mergeFrom(builder, message);
  for (CellSet.Row row : builder.getRowsList()) {
    RowModel rowModel = new RowModel(row.getKey().toByteArray());
    for (Cell cell : row.getValuesList()) {
      long timestamp = HConstants.LATEST_TIMESTAMP;
      if (cell.hasTimestamp()) {
        timestamp = cell.getTimestamp();
      }
      rowModel.addCell(
          new CellModel(cell.getColumn().toByteArray(), timestamp,
                cell.getData().toByteArray()));
    }
    addRow(rowModel);
  }
  return this;
}
 
Example 4
Source File: NettyRpcFrameDecoder.java    From hbase with Apache License 2.0 6 votes vote down vote up
private RPCProtos.RequestHeader getHeader(ByteBuf in, int headerSize) throws IOException {
  ByteBuf msg = in.readRetainedSlice(headerSize);
  try {
    byte[] array;
    int offset;
    int length = msg.readableBytes();
    if (msg.hasArray()) {
      array = msg.array();
      offset = msg.arrayOffset() + msg.readerIndex();
    } else {
      array = new byte[length];
      msg.getBytes(msg.readerIndex(), array, 0, length);
      offset = 0;
    }

    RPCProtos.RequestHeader.Builder builder = RPCProtos.RequestHeader.newBuilder();
    ProtobufUtil.mergeFrom(builder, array, offset, length);
    return builder.build();
  } finally {
    msg.release();
  }
}
 
Example 5
Source File: ReplicationPeerConfigUtil.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * @param bytes Content of a peer znode.
 * @return ClusterKey parsed from the passed bytes.
 * @throws DeserializationException
 */
public static ReplicationPeerConfig parsePeerFrom(final byte[] bytes)
    throws DeserializationException {
  if (ProtobufUtil.isPBMagicPrefix(bytes)) {
    int pbLen = ProtobufUtil.lengthOfPBMagic();
    ReplicationProtos.ReplicationPeer.Builder builder =
        ReplicationProtos.ReplicationPeer.newBuilder();
    ReplicationProtos.ReplicationPeer peer;
    try {
      ProtobufUtil.mergeFrom(builder, bytes, pbLen, bytes.length - pbLen);
      peer = builder.build();
    } catch (IOException e) {
      throw new DeserializationException(e);
    }
    return convert(peer);
  } else {
    if (bytes == null || bytes.length <= 0) {
      throw new DeserializationException("Bytes to deserialize should not be empty.");
    }
    return ReplicationPeerConfig.newBuilder().setClusterKey(Bytes.toString(bytes)).build();
  }
}
 
Example 6
Source File: RegionNormalizerTracker.java    From hbase with Apache License 2.0 5 votes vote down vote up
private RegionNormalizerProtos.RegionNormalizerState parseFrom(byte [] pbBytes)
  throws DeserializationException {
  ProtobufUtil.expectPBMagicPrefix(pbBytes);
  RegionNormalizerProtos.RegionNormalizerState.Builder builder =
    RegionNormalizerProtos.RegionNormalizerState.newBuilder();
  try {
    int magicLen = ProtobufUtil.lengthOfPBMagic();
    ProtobufUtil.mergeFrom(builder, pbBytes, magicLen, pbBytes.length - magicLen);
  } catch (IOException e) {
    throw new DeserializationException(e);
  }
  return builder.build();
}
 
Example 7
Source File: TableInfoModel.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
public ProtobufMessageHandler getObjectFromMessage(byte[] message)
    throws IOException {
  TableInfo.Builder builder = TableInfo.newBuilder();
  ProtobufUtil.mergeFrom(builder, message);
  setName(builder.getName());
  for (TableInfo.Region region: builder.getRegionsList()) {
    add(new TableRegionModel(builder.getName(), region.getId(),
        region.getStartKey().toByteArray(),
        region.getEndKey().toByteArray(),
        region.getLocation()));
  }
  return this;
}
 
Example 8
Source File: LoadBalancerTracker.java    From hbase with Apache License 2.0 5 votes vote down vote up
private LoadBalancerProtos.LoadBalancerState parseFrom(byte [] pbBytes)
  throws DeserializationException {
  ProtobufUtil.expectPBMagicPrefix(pbBytes);
  LoadBalancerProtos.LoadBalancerState.Builder builder =
    LoadBalancerProtos.LoadBalancerState.newBuilder();
  try {
    int magicLen = ProtobufUtil.lengthOfPBMagic();
    ProtobufUtil.mergeFrom(builder, pbBytes, magicLen, pbBytes.length - magicLen);
  } catch (IOException e) {
    throw new DeserializationException(e);
  }
  return builder.build();
}
 
Example 9
Source File: SnapshotCleanupTracker.java    From hbase with Apache License 2.0 5 votes vote down vote up
private SnapshotCleanupProtos.SnapshotCleanupState parseFrom(final byte[] pbBytes)
    throws DeserializationException {
  ProtobufUtil.expectPBMagicPrefix(pbBytes);
  SnapshotCleanupProtos.SnapshotCleanupState.Builder builder =
      SnapshotCleanupProtos.SnapshotCleanupState.newBuilder();
  try {
    int magicLen = ProtobufUtil.lengthOfPBMagic();
    ProtobufUtil.mergeFrom(builder, pbBytes, magicLen, pbBytes.length - magicLen);
  } catch (IOException e) {
    throw new DeserializationException(e);
  }
  return builder.build();
}
 
Example 10
Source File: CellModel.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
public ProtobufMessageHandler getObjectFromMessage(byte[] message)
    throws IOException {
  Cell.Builder builder = Cell.newBuilder();
  ProtobufUtil.mergeFrom(builder, message);
  setColumn(builder.getColumn().toByteArray());
  setValue(builder.getData().toByteArray());
  if (builder.hasTimestamp()) {
    setTimestamp(builder.getTimestamp());
  }
  return this;
}
 
Example 11
Source File: Mutation.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Convert a protocol buffer CellVisibility bytes to a client CellVisibility
 *
 * @param protoBytes
 * @return the converted client CellVisibility
 * @throws DeserializationException
 */
private static CellVisibility toCellVisibility(byte[] protoBytes) throws DeserializationException {
  if (protoBytes == null) return null;
  ClientProtos.CellVisibility.Builder builder = ClientProtos.CellVisibility.newBuilder();
  ClientProtos.CellVisibility proto = null;
  try {
    ProtobufUtil.mergeFrom(builder, protoBytes);
    proto = builder.build();
  } catch (IOException e) {
    throw new DeserializationException(e);
  }
  return toCellVisibility(proto);
}
 
Example 12
Source File: TableDescriptorBuilder.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * @param bytes A pb serialized {@link ModifyableTableDescriptor} instance
 * with pb magic prefix
 * @return An instance of {@link ModifyableTableDescriptor} made from
 * <code>bytes</code>
 * @throws DeserializationException
 * @see #toByteArray()
 */
private static TableDescriptor parseFrom(final byte[] bytes)
        throws DeserializationException {
  if (!ProtobufUtil.isPBMagicPrefix(bytes)) {
    throw new DeserializationException("Expected PB encoded ModifyableTableDescriptor");
  }
  int pblen = ProtobufUtil.lengthOfPBMagic();
  HBaseProtos.TableSchema.Builder builder = HBaseProtos.TableSchema.newBuilder();
  try {
    ProtobufUtil.mergeFrom(builder, bytes, pblen, bytes.length - pblen);
    return ProtobufUtil.toTableDescriptor(builder.build());
  } catch (IOException e) {
    throw new DeserializationException(e);
  }
}
 
Example 13
Source File: ZKUtil.java    From hbase with Apache License 2.0 5 votes vote down vote up
private static void appendPeerState(ZKWatcher zkw, String znodeToProcess, StringBuilder sb)
        throws KeeperException, InvalidProtocolBufferException {
  String peerState = zkw.getConfiguration().get("zookeeper.znode.replication.peers.state",
    "peer-state");
  int pblen = ProtobufUtil.lengthOfPBMagic();
  for (String child : ZKUtil.listChildrenNoWatch(zkw, znodeToProcess)) {
    if (!child.equals(peerState)) {
      continue;
    }

    String peerStateZnode = ZNodePaths.joinZNode(znodeToProcess, child);
    sb.append("\n").append(peerStateZnode).append(": ");
    byte[] peerStateData;
    try {
      peerStateData = ZKUtil.getData(zkw, peerStateZnode);
      ReplicationProtos.ReplicationState.Builder builder =
          ReplicationProtos.ReplicationState.newBuilder();
      ProtobufUtil.mergeFrom(builder, peerStateData, pblen, peerStateData.length - pblen);
      sb.append(builder.getState().name());
    } catch (IOException ipbe) {
      LOG.warn("Got Exception while parsing peer: " + znodeToProcess, ipbe);
    } catch (InterruptedException e) {
      zkw.interruptedException(e);
      return;
    }
  }
}
 
Example 14
Source File: AuthenticationTokenIdentifier.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
public void readFields(DataInput in) throws IOException {
  int len = in.readInt();
  byte[] inBytes = new byte[len];
  in.readFully(inBytes);
  AuthenticationProtos.TokenIdentifier.Builder builder =
    AuthenticationProtos.TokenIdentifier.newBuilder();
  ProtobufUtil.mergeFrom(builder, inBytes);
  AuthenticationProtos.TokenIdentifier identifier = builder.build();
  // sanity check on type
  if (!identifier.hasKind() ||
      identifier.getKind() != AuthenticationProtos.TokenIdentifier.Kind.HBASE_AUTH_TOKEN) {
    throw new IOException("Invalid TokenIdentifier kind from input "+identifier.getKind());
  }

  // copy the field values
  if (identifier.hasUsername()) {
    username = identifier.getUsername().toStringUtf8();
  }
  if (identifier.hasKeyId()) {
    keyId = identifier.getKeyId();
  }
  if (identifier.hasIssueDate()) {
    issueDate = identifier.getIssueDate();
  }
  if (identifier.hasExpirationDate()) {
    expirationDate = identifier.getExpirationDate();
  }
  if (identifier.hasSequenceNumber()) {
    sequenceNumber = identifier.getSequenceNumber();
  }
}
 
Example 15
Source File: TimeRangeTracker.java    From hbase with Apache License 2.0 5 votes vote down vote up
public static TimeRangeTracker parseFrom(final byte[] data, Type type) throws IOException {
  Preconditions.checkNotNull(data, "input data is null!");
  if (ProtobufUtil.isPBMagicPrefix(data)) {
    int pblen = ProtobufUtil.lengthOfPBMagic();
    HBaseProtos.TimeRangeTracker.Builder builder = HBaseProtos.TimeRangeTracker.newBuilder();
    ProtobufUtil.mergeFrom(builder, data, pblen, data.length - pblen);
    return TimeRangeTracker.create(type, builder.getFrom(), builder.getTo());
  } else {
    try (DataInputStream in = new DataInputStream(new ByteArrayInputStream(data))) {
      return TimeRangeTracker.create(type, in.readLong(), in.readLong());
    }
  }
}
 
Example 16
Source File: SplitOrMergeTracker.java    From hbase with Apache License 2.0 5 votes vote down vote up
private SwitchState parseFrom(byte [] bytes)
  throws DeserializationException {
  ProtobufUtil.expectPBMagicPrefix(bytes);
  SwitchState.Builder builder = SwitchState.newBuilder();
  try {
    int magicLen = ProtobufUtil.lengthOfPBMagic();
    ProtobufUtil.mergeFrom(builder, bytes, magicLen, bytes.length - magicLen);
  } catch (IOException e) {
    throw new DeserializationException(e);
  }
  return builder.build();
}
 
Example 17
Source File: RegionServerTracker.java    From hbase with Apache License 2.0 5 votes vote down vote up
private Pair<ServerName, RegionServerInfo> getServerInfo(String name)
    throws KeeperException, IOException {
  ServerName serverName = ServerName.parseServerName(name);
  String nodePath = ZNodePaths.joinZNode(watcher.getZNodePaths().rsZNode, name);
  byte[] data;
  try {
    data = ZKUtil.getData(watcher, nodePath);
  } catch (InterruptedException e) {
    throw (InterruptedIOException) new InterruptedIOException().initCause(e);
  }
  if (data == null) {
    // we should receive a children changed event later and then we will expire it, so we still
    // need to add it to the region server set.
    LOG.warn("Server node {} does not exist, already dead?", name);
    return Pair.newPair(serverName, null);
  }
  if (data.length == 0 || !ProtobufUtil.isPBMagicPrefix(data)) {
    // this should not happen actually, unless we have bugs or someone has messed zk up.
    LOG.warn("Invalid data for region server node {} on zookeeper, data length = {}", name,
      data.length);
    return Pair.newPair(serverName, null);
  }
  RegionServerInfo.Builder builder = RegionServerInfo.newBuilder();
  int magicLen = ProtobufUtil.lengthOfPBMagic();
  ProtobufUtil.mergeFrom(builder, data, magicLen, data.length - magicLen);
  return Pair.newPair(serverName, builder.build());
}
 
Example 18
Source File: SplitLogTask.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * @param data Serialized date to parse.
 * @return An SplitLogTaskState instance made of the passed <code>data</code>
 * @throws DeserializationException
 * @see #toByteArray()
 */
public static SplitLogTask parseFrom(final byte [] data) throws DeserializationException {
  ProtobufUtil.expectPBMagicPrefix(data);
  try {
    int prefixLen = ProtobufUtil.lengthOfPBMagic();
    ZooKeeperProtos.SplitLogTask.Builder builder = ZooKeeperProtos.SplitLogTask.newBuilder();
    ProtobufUtil.mergeFrom(builder, data, prefixLen, data.length - prefixLen);
    return new SplitLogTask(builder.build());
  } catch (IOException e) {
    throw new DeserializationException(Bytes.toStringBinary(data, 0, 64), e);
  }
}
 
Example 19
Source File: VisibilityUtils.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Reads back User auth data written to zookeeper.
 * @param data
 * @return User auth details
 * @throws DeserializationException
 */
public static MultiUserAuthorizations readUserAuthsFromZKData(byte[] data) 
    throws DeserializationException {
  if (ProtobufUtil.isPBMagicPrefix(data)) {
    int pblen = ProtobufUtil.lengthOfPBMagic();
    try {
      MultiUserAuthorizations.Builder builder = MultiUserAuthorizations.newBuilder();
      ProtobufUtil.mergeFrom(builder, data, pblen, data.length - pblen);
      return builder.build();
    } catch (IOException e) {
      throw new DeserializationException(e);
    }
  }
  return null;
}
 
Example 20
Source File: PermissionStorage.java    From hbase with Apache License 2.0 4 votes vote down vote up
public static List<Permission> getCellPermissionsForUser(User user, Cell cell)
    throws IOException {
  // Save an object allocation where we can
  if (cell.getTagsLength() == 0) {
    return null;
  }
  List<Permission> results = Lists.newArrayList();
  Iterator<Tag> tagsIterator = PrivateCellUtil.tagsIterator(cell);
  while (tagsIterator.hasNext()) {
    Tag tag = tagsIterator.next();
    if (tag.getType() == ACL_TAG_TYPE) {
      // Deserialize the table permissions from the KV
      // TODO: This can be improved. Don't build UsersAndPermissions just to unpack it again,
      // use the builder
      AccessControlProtos.UsersAndPermissions.Builder builder =
          AccessControlProtos.UsersAndPermissions.newBuilder();
      if (tag.hasArray()) {
        ProtobufUtil.mergeFrom(builder, tag.getValueArray(), tag.getValueOffset(),
          tag.getValueLength());
      } else {
        ProtobufUtil.mergeFrom(builder, Tag.cloneValue(tag));
      }
      ListMultimap<String,Permission> kvPerms =
          AccessControlUtil.toUsersAndPermissions(builder.build());
      // Are there permissions for this user?
      List<Permission> userPerms = kvPerms.get(user.getShortName());
      if (userPerms != null) {
        results.addAll(userPerms);
      }
      // Are there permissions for any of the groups this user belongs to?
      String[] groupNames = user.getGroupNames();
      if (groupNames != null) {
        for (String group : groupNames) {
          List<Permission> groupPerms = kvPerms.get(AuthUtil.toGroupEntry(group));
          if (results != null) {
            results.addAll(groupPerms);
          }
        }
      }
    }
  }
  return results;
}