co.cask.cdap.api.common.Bytes.putBytes()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(11.9k)|赞(0)|评价(0)|浏览(99)

本文整理了Java中co.cask.cdap.api.common.Bytes.putBytes()方法的一些代码示例,展示了Bytes.putBytes()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Bytes.putBytes()方法的具体详情如下:
包路径:co.cask.cdap.api.common.Bytes
类名称:Bytes
方法名:putBytes

Bytes.putBytes介绍

[英]Put bytes at the specified byte array position.
[中]将字节放在指定的字节数组位置。

代码示例

代码示例来源:origin: cdapio/cdap

/**
 * Convert a BigDecimal value to a byte array.
 *
 * @param val
 * @return the byte array
 */
public static byte[] toBytes(BigDecimal val) {
 byte[] valueBytes = val.unscaledValue().toByteArray();
 byte[] result = new byte[valueBytes.length + SIZEOF_INT];
 int offset = putInt(result, 0, val.scale());
 putBytes(result, offset, valueBytes, 0, valueBytes.length);
 return result;
}

代码示例来源:origin: co.cask.cdap/cdap-api-common

/**
 * Convert a BigDecimal value to a byte array.
 *
 * @param val
 * @return the byte array
 */
public static byte[] toBytes(BigDecimal val) {
 byte[] valueBytes = val.unscaledValue().toByteArray();
 byte[] result = new byte[valueBytes.length + SIZEOF_INT];
 int offset = putInt(result, 0, val.scale());
 putBytes(result, offset, valueBytes, 0, valueBytes.length);
 return result;
}

代码示例来源:origin: co.cask.cdap/cdap-api-common

/**
 * Put a BigDecimal value out to the specified byte array position.
 *
 * @param bytes  the byte array
 * @param offset position in the array
 * @param val    BigDecimal to write out
 * @return incremented offset
 */
public static int putBigDecimal(byte[] bytes, int offset, BigDecimal val) {
 if (bytes == null) {
  return offset;
 }
 byte[] valueBytes = val.unscaledValue().toByteArray();
 byte[] result = new byte[valueBytes.length + SIZEOF_INT];
 offset = putInt(result, offset, val.scale());
 return putBytes(result, offset, valueBytes, 0, valueBytes.length);
}

代码示例来源:origin: cdapio/cdap

/**
 * Put a BigDecimal value out to the specified byte array position.
 *
 * @param bytes  the byte array
 * @param offset position in the array
 * @param val    BigDecimal to write out
 * @return incremented offset
 */
public static int putBigDecimal(byte[] bytes, int offset, BigDecimal val) {
 if (bytes == null) {
  return offset;
 }
 byte[] valueBytes = val.unscaledValue().toByteArray();
 byte[] result = new byte[valueBytes.length + SIZEOF_INT];
 offset = putInt(result, offset, val.scale());
 return putBytes(result, offset, valueBytes, 0, valueBytes.length);
}

代码示例来源:origin: co.cask.cdap/cdap-app-fabric

private String getMultipartKey(String... parts) {
  int sizeOfParts = Stream.of(parts).mapToInt(String::length).reduce(0, (a, b) -> a + b);

  byte[] result = new byte[sizeOfParts + (parts.length * Bytes.SIZEOF_INT)];

  int offset = 0;
  for (String part : parts) {
   Bytes.putInt(result, offset, part.length());
   offset += Bytes.SIZEOF_INT;
   Bytes.putBytes(result, offset, part.getBytes(), 0, part.length());
   offset += part.length();
  }
  return Bytes.toString(result);
 }
}

代码示例来源:origin: co.cask.cdap/cdap-app-fabric

private byte[] getMultipartKey(String... parts) {
  int sizeOfParts = 0;
  for (String part : parts) {
   sizeOfParts += part.length();
  }

  byte[] result = new byte[1 + sizeOfParts + (parts.length * Bytes.SIZEOF_INT)];
  Bytes.putByte(result, 0, Constants.ConfigStore.VERSION);

  int offset = 1;
  for (String part : parts) {
   Bytes.putInt(result, offset, part.length());
   offset += Bytes.SIZEOF_INT;
   Bytes.putBytes(result, offset, part.getBytes(), 0, part.length());
   offset += part.length();
  }
  return result;
 }
}

代码示例来源:origin: cdapio/cdap

private String getMultipartKey(String... parts) {
  int sizeOfParts = Stream.of(parts).mapToInt(String::length).reduce(0, (a, b) -> a + b);

  byte[] result = new byte[sizeOfParts + (parts.length * Bytes.SIZEOF_INT)];

  int offset = 0;
  for (String part : parts) {
   Bytes.putInt(result, offset, part.length());
   offset += Bytes.SIZEOF_INT;
   Bytes.putBytes(result, offset, part.getBytes(), 0, part.length());
   offset += part.length();
  }
  return Bytes.toString(result);
 }
}

代码示例来源:origin: co.cask.cdap/cdap-data-fabric

@Override
public void getRowKeys(Iterable<ConsumerGroupConfig> consumerGroupConfigs, QueueEntry queueEntry, byte[] rowKeyPrefix,
            long writePointer, int counter, Collection<byte[]> rowKeys) {
 byte[] rowKey = new byte[rowKeyPrefix.length + Bytes.SIZEOF_LONG + Bytes.SIZEOF_INT];
 Bytes.putBytes(rowKey, 0, rowKeyPrefix, 0, rowKeyPrefix.length);
 Bytes.putLong(rowKey, rowKeyPrefix.length, writePointer);
 Bytes.putInt(rowKey, rowKey.length - Bytes.SIZEOF_INT, counter);
 rowKeys.add(rowKeyDistributor.getDistributedKey(rowKey));
}

代码示例来源:origin: cdapio/cdap

/**
 * Convert {@link TopicId} and generation id to byte array to be used for data tables (message and payload) as
 * row key prefix.
 *
 * @param topicId {@link TopicId}
 * @param generation generation id of the topic
 * @return byte array representation to be used as row key prefix for data tables
 */
public static byte[] toDataKeyPrefix(TopicId topicId, int generation) {
 byte[] metadataRowKey = toMetadataRowKey(topicId);
 byte[] keyPrefix = new byte[metadataRowKey.length + Bytes.SIZEOF_INT];
 Bytes.putBytes(keyPrefix, 0, metadataRowKey, 0, metadataRowKey.length);
 Bytes.putInt(keyPrefix, metadataRowKey.length, generation);
 return keyPrefix;
}

代码示例来源:origin: co.cask.cdap/cdap-hbase-compat-base

/**
 * Convert {@link TopicId} and generation id to byte array to be used for data tables (message and payload) as
 * row key prefix.
 *
 * @param topicId {@link TopicId}
 * @param generation generation id of the topic
 * @return byte array representation to be used as row key prefix for data tables
 */
public static byte[] toDataKeyPrefix(TopicId topicId, int generation) {
 byte[] metadataRowKey = toMetadataRowKey(topicId);
 byte[] keyPrefix = new byte[metadataRowKey.length + Bytes.SIZEOF_INT];
 Bytes.putBytes(keyPrefix, 0, metadataRowKey, 0, metadataRowKey.length);
 Bytes.putInt(keyPrefix, metadataRowKey.length, generation);
 return keyPrefix;
}

代码示例来源:origin: cdapio/cdap

public static KeyValue fromKey(byte[] key) {
  int len = key.length + (2 * Bytes.SIZEOF_INT);
  byte[] kvBytes = new byte[len];
  int pos = 0;
  pos = Bytes.putInt(kvBytes, pos, key.length);
  pos = Bytes.putInt(kvBytes, pos, 0);
  Bytes.putBytes(kvBytes, pos, key, 0, key.length);
  return new KeyValue(kvBytes);
 }
}

代码示例来源:origin: co.cask.cdap/cdap-data-fabric

public static KeyValue fromKey(byte[] key) {
  int len = key.length + (2 * Bytes.SIZEOF_INT);
  byte[] kvBytes = new byte[len];
  int pos = 0;
  pos = Bytes.putInt(kvBytes, pos, key.length);
  pos = Bytes.putInt(kvBytes, pos, 0);
  Bytes.putBytes(kvBytes, pos, key, 0, key.length);
  return new KeyValue(kvBytes);
 }
}

代码示例来源:origin: co.cask.cdap/cdap-data-fabric

private byte[] getShardedKey(ConsumerGroupConfig groupConfig, int instanceId,
                byte[] originalRowKey) {
  // Need to subtract the SALT_BYTES as the row key distributor will prefix the key with salted bytes
  byte[] result = new byte[PREFIX_BYTES - SaltedHBaseQueueStrategy.SALT_BYTES + originalRowKey.length];
  Bytes.putBytes(result, PREFIX_BYTES - SaltedHBaseQueueStrategy.SALT_BYTES,
          originalRowKey, 0, originalRowKey.length);
  Bytes.putLong(result, 0, groupConfig.getGroupId());

  // Default for FIFO case.
  int shardId = groupConfig.getDequeueStrategy() == DequeueStrategy.FIFO ? -1 : instanceId;
  Bytes.putInt(result, Bytes.SIZEOF_LONG, shardId);

  return result;
 }
}

代码示例来源:origin: caskdata/cdap

@Override
public CloseableIterator<Entry> fetch(TopicMetadata metadata, long startTime, int limit,
                   @Nullable Transaction transaction) throws IOException {
 byte[] topic = MessagingUtils.toDataKeyPrefix(metadata.getTopicId(), metadata.getGeneration());
 byte[] startRow = new byte[topic.length + Bytes.SIZEOF_LONG];
 Bytes.putBytes(startRow, 0, topic, 0, topic.length);
 Bytes.putLong(startRow, topic.length, startTime);
 byte[] stopRow = Bytes.stopKeyForPrefix(topic);
 final CloseableIterator<RawMessageTableEntry> scanner = read(startRow, stopRow);
 return new FetchIterator(scanner, limit, null, transaction);
}

代码示例来源:origin: co.cask.cdap/cdap-tms

@Override
public CloseableIterator<Entry> fetch(TopicMetadata metadata, long startTime, int limit,
                   @Nullable Transaction transaction) throws IOException {
 byte[] topic = MessagingUtils.toDataKeyPrefix(metadata.getTopicId(), metadata.getGeneration());
 byte[] startRow = new byte[topic.length + Bytes.SIZEOF_LONG];
 Bytes.putBytes(startRow, 0, topic, 0, topic.length);
 Bytes.putLong(startRow, topic.length, startTime);
 byte[] stopRow = Bytes.stopKeyForPrefix(topic);
 final CloseableIterator<RawMessageTableEntry> scanner = read(startRow, stopRow);
 return new FetchIterator(scanner, limit, null, transaction);
}

代码示例来源:origin: caskdata/cdap

@Override
public CloseableIterator<Entry> fetch(TopicMetadata metadata, MessageId messageId, boolean inclusive,
                   final int limit, @Nullable final Transaction transaction) throws IOException {
 byte[] topic = MessagingUtils.toDataKeyPrefix(metadata.getTopicId(), metadata.getGeneration());
 byte[] startRow = new byte[topic.length + Bytes.SIZEOF_LONG + Bytes.SIZEOF_SHORT];
 Bytes.putBytes(startRow, 0, topic, 0, topic.length);
 Bytes.putLong(startRow, topic.length, messageId.getPublishTimestamp());
 Bytes.putShort(startRow, topic.length + Bytes.SIZEOF_LONG, messageId.getSequenceId());
 byte[] stopRow = Bytes.stopKeyForPrefix(topic);
 final CloseableIterator<RawMessageTableEntry> scanner = read(startRow, stopRow);
 return new FetchIterator(scanner, limit, inclusive ? null : startRow, transaction);
}

代码示例来源:origin: co.cask.cdap/cdap-tms

@Override
public CloseableIterator<Entry> fetch(TopicMetadata metadata, MessageId messageId, boolean inclusive,
                   final int limit, @Nullable final Transaction transaction) throws IOException {
 byte[] topic = MessagingUtils.toDataKeyPrefix(metadata.getTopicId(), metadata.getGeneration());
 byte[] startRow = new byte[topic.length + Bytes.SIZEOF_LONG + Bytes.SIZEOF_SHORT];
 Bytes.putBytes(startRow, 0, topic, 0, topic.length);
 Bytes.putLong(startRow, topic.length, messageId.getPublishTimestamp());
 Bytes.putShort(startRow, topic.length + Bytes.SIZEOF_LONG, messageId.getSequenceId());
 byte[] stopRow = Bytes.stopKeyForPrefix(topic);
 final CloseableIterator<RawMessageTableEntry> scanner = read(startRow, stopRow);
 return new FetchIterator(scanner, limit, inclusive ? null : startRow, transaction);
}

代码示例来源:origin: co.cask.cdap/cdap-tms

@Override
public void rollback(TopicMetadata metadata, RollbackDetail rollbackDetail) throws IOException {
 //long startTimestamp, short startSequenceId,
 //long endTimestamp, short endSequenceId
 byte[] topic = MessagingUtils.toDataKeyPrefix(metadata.getTopicId(), metadata.getGeneration());
 byte[] startRow = new byte[topic.length + Bytes.SIZEOF_LONG + Bytes.SIZEOF_SHORT];
 Bytes.putBytes(startRow, 0, topic, 0, topic.length);
 Bytes.putLong(startRow, topic.length, rollbackDetail.getStartTimestamp());
 Bytes.putShort(startRow, topic.length + Bytes.SIZEOF_LONG, (short) rollbackDetail.getStartSequenceId());
 byte[] stopRow = new byte[topic.length + Bytes.SIZEOF_LONG + Bytes.SIZEOF_SHORT];
 Bytes.putBytes(stopRow, 0, topic, 0, topic.length);
 Bytes.putLong(stopRow, topic.length, rollbackDetail.getEndTimestamp());
 Bytes.putShort(stopRow, topic.length + Bytes.SIZEOF_LONG, (short) rollbackDetail.getEndSequenceId());
 rollback(startRow, Bytes.stopKeyForPrefix(stopRow),
      Bytes.toBytes(-1 * rollbackDetail.getTransactionWritePointer()));
}

代码示例来源:origin: caskdata/cdap

@Override
public void rollback(TopicMetadata metadata, RollbackDetail rollbackDetail) throws IOException {
 //long startTimestamp, short startSequenceId,
 //long endTimestamp, short endSequenceId
 byte[] topic = MessagingUtils.toDataKeyPrefix(metadata.getTopicId(), metadata.getGeneration());
 byte[] startRow = new byte[topic.length + Bytes.SIZEOF_LONG + Bytes.SIZEOF_SHORT];
 Bytes.putBytes(startRow, 0, topic, 0, topic.length);
 Bytes.putLong(startRow, topic.length, rollbackDetail.getStartTimestamp());
 Bytes.putShort(startRow, topic.length + Bytes.SIZEOF_LONG, (short) rollbackDetail.getStartSequenceId());
 byte[] stopRow = new byte[topic.length + Bytes.SIZEOF_LONG + Bytes.SIZEOF_SHORT];
 Bytes.putBytes(stopRow, 0, topic, 0, topic.length);
 Bytes.putLong(stopRow, topic.length, rollbackDetail.getEndTimestamp());
 Bytes.putShort(stopRow, topic.length + Bytes.SIZEOF_LONG, (short) rollbackDetail.getEndSequenceId());
 rollback(startRow, Bytes.stopKeyForPrefix(stopRow),
      Bytes.toBytes(-1 * rollbackDetail.getTransactionWritePointer()));
}

代码示例来源:origin: caskdata/cdap

@Override
 protected RawPayloadTableEntry computeNext() {
  if (!entries.hasNext()) {
   return endOfData();
  }
  Entry entry = entries.next();
  if (topicId == null || (!topicId.equals(entry.getTopicId())) || (generation != entry.getGeneration())) {
   topicId = entry.getTopicId();
   generation = entry.getGeneration();
   topic = MessagingUtils.toDataKeyPrefix(topicId, entry.getGeneration());
   rowKey = new byte[topic.length + (2 * Bytes.SIZEOF_LONG) + Bytes.SIZEOF_SHORT];
  }
  Bytes.putBytes(rowKey, 0, topic, 0, topic.length);
  Bytes.putLong(rowKey, topic.length, entry.getTransactionWritePointer());
  Bytes.putLong(rowKey, topic.length + Bytes.SIZEOF_LONG, entry.getPayloadWriteTimestamp());
  Bytes.putShort(rowKey, topic.length + (2 * Bytes.SIZEOF_LONG), entry.getPayloadSequenceId());
  return tableEntry.set(rowKey, entry.getPayload());
 }
}

相关文章