/** * Write instance {@code val} into buffer {@code buff}. */ public int encodeInt(byte[] buff, int offset, int val) { return Bytes.putInt(buff, offset, val); } }
/** * @param bytes byte array * @param offset offset to write to * @param f float value * @return New offset in <code>bytes</code> */ public static int putFloat(byte [] bytes, int offset, float f) { return putInt(bytes, offset, Float.floatToRawIntBits(f)); }
private static int encodeFloat(float v, byte[] b, int o) { checkForSufficientLength(b, o, Bytes.SIZEOF_FLOAT); int i = Float.floatToIntBits(v); i = (i ^ ((i >> Integer.SIZE - 1) | Integer.MIN_VALUE)) + 1; Bytes.putInt(b, o, i); return Bytes.SIZEOF_FLOAT; }
private static int encodeUnsignedInt(int v, byte[] b, int o) { checkForSufficientLength(b, o, Bytes.SIZEOF_INT); if (v < 0) { throw new RuntimeException(); } Bytes.putInt(b, o, v); return Bytes.SIZEOF_INT; }
/** * @param b * @param o * @param l * @return A KeyValue made of a byte array that holds the key-only part. * Needed to convert hfile index members to KeyValues. */ public static KeyValue createKeyValueFromKey(final byte[] b, final int o, final int l) { byte[] newb = new byte[l + KeyValue.ROW_OFFSET]; System.arraycopy(b, o, newb, KeyValue.ROW_OFFSET, l); Bytes.putInt(newb, 0, l); Bytes.putInt(newb, Bytes.SIZEOF_INT, 0); return new KeyValue(newb); }
@Override public void writeInt(int i) { checkSizeAndGrow(Bytes.SIZEOF_INT); Bytes.putInt(this.buf, this.pos, i); this.pos += Bytes.SIZEOF_INT; }
/** * 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; }
private static int encodeUnsignedTimestamp(Object v, byte[] b, int o) { if (v instanceof Timestamp) { Timestamp ts = (Timestamp) v; encodeUnsignedLong(ts.getTime(), b, o); Bytes.putInt(b, Bytes.SIZEOF_LONG, ts.getNanos() % 1000000); } else { encodeUnsignedDate(v, b, o); } return Bytes.SIZEOF_LONG + Bytes.SIZEOF_INT; }
private static int encodeTimestamp(Object v, byte[] b, int o) { if (v instanceof Timestamp) { Timestamp ts = (Timestamp) v; encodeLong(ts.getTime(), b, o); Bytes.putInt(b, Bytes.SIZEOF_LONG, ts.getNanos() % 1000000); } else { encodeDate(v, b, o); } return Bytes.SIZEOF_LONG + Bytes.SIZEOF_INT; }
/** * 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); }
/** * Creates a new KeyValue that only contains the key portion (the value is * set to be null). * * TODO only used by KeyOnlyFilter -- move there. * @param lenAsVal replace value with the actual value length (false=empty) */ public KeyValue createKeyOnly(boolean lenAsVal) { // KV format: <keylen:4><valuelen:4><key:keylen><value:valuelen> // Rebuild as: <keylen:4><0:4><key:keylen> int dataLen = lenAsVal? Bytes.SIZEOF_INT : 0; byte [] newBuffer = new byte[getKeyLength() + ROW_OFFSET + dataLen]; System.arraycopy(this.bytes, this.offset, newBuffer, 0, Math.min(newBuffer.length,this.length)); Bytes.putInt(newBuffer, Bytes.SIZEOF_INT, dataLen); if (lenAsVal) { Bytes.putInt(newBuffer, newBuffer.length - dataLen, this.getValueLength()); } return new KeyValue(newBuffer); }
@Override public void endBlockEncoding(HFileBlockEncodingContext encodingCtx, DataOutputStream out, byte[] uncompressedBytesWithHeader) throws IOException { BufferedDataBlockEncodingState state = (BufferedDataBlockEncodingState) encodingCtx .getEncodingState(); // Write the unencodedDataSizeWritten (with header size) Bytes.putInt(uncompressedBytesWithHeader, HConstants.HFILEBLOCK_HEADER_SIZE + DataBlockEncoding.ID_SIZE, state.unencodedDataSizeWritten ); postEncoding(encodingCtx); }
/** * Put the header into the given byte array at the given offset. * @param onDiskSize size of the block on disk header + data + checksum * @param uncompressedSize size of the block after decompression (but * before optional data block decoding) including header * @param onDiskDataSize size of the block on disk with header * and data but not including the checksums */ private void putHeader(byte[] dest, int offset, int onDiskSize, int uncompressedSize, int onDiskDataSize) { offset = blockType.put(dest, offset); offset = Bytes.putInt(dest, offset, onDiskSize - HConstants.HFILEBLOCK_HEADER_SIZE); offset = Bytes.putInt(dest, offset, uncompressedSize - HConstants.HFILEBLOCK_HEADER_SIZE); offset = Bytes.putLong(dest, offset, prevOffset); offset = Bytes.putByte(dest, offset, fileContext.getChecksumType().getCode()); offset = Bytes.putInt(dest, offset, fileContext.getBytesPerChecksum()); Bytes.putInt(dest, offset, onDiskDataSize); }
public static byte[] appendMetaData(byte[] id, byte[] data) { if (data == null || data.length == 0) { return data; } byte[] salt = Bytes.toBytes(ThreadLocalRandom.current().nextLong()); int idLength = id.length + salt.length; byte[] newData = new byte[MAGIC_SIZE + ID_LENGTH_SIZE + idLength + data.length]; int pos = 0; pos = Bytes.putByte(newData, pos, MAGIC); pos = Bytes.putInt(newData, pos, idLength); pos = Bytes.putBytes(newData, pos, id, 0, id.length); pos = Bytes.putBytes(newData, pos, salt, 0, salt.length); pos = Bytes.putBytes(newData, pos, data, 0, data.length); return newData; }
@Override public int encode(PositionedByteRange dst, Integer val) { Bytes.putInt(dst.getBytes(), dst.getOffset() + dst.getPosition(), val); return skip(dst); }
/**************** copy key and value *********************/ public static int appendToByteArray(Cell cell, byte[] output, int offset, boolean withTags) { int pos = offset; pos = Bytes.putInt(output, pos, keyLength(cell)); pos = Bytes.putInt(output, pos, cell.getValueLength()); pos = appendKeyTo(cell, output, pos); pos = CellUtil.copyValueTo(cell, output, pos); if (withTags && (cell.getTagsLength() > 0)) { pos = Bytes.putAsShort(output, pos, cell.getTagsLength()); pos = PrivateCellUtil.copyTagsTo(cell, output, pos); } return pos; }
protected List<RegionInfo> uniformRegions(int numRegions) { List<RegionInfo> regions = new ArrayList<>(numRegions); byte[] start = new byte[16]; byte[] end = new byte[16]; Random rand = ThreadLocalRandom.current(); rand.nextBytes(start); rand.nextBytes(end); for (int i = 0; i < numRegions; i++) { Bytes.putInt(start, 0, numRegions << 1); Bytes.putInt(end, 0, (numRegions << 1) + 1); TableName tableName = TableName.valueOf("table" + i); RegionInfo hri = RegionInfoBuilder.newBuilder(tableName) .setStartKey(start) .setEndKey(end) .setSplit(false) .build(); regions.add(hri); } return regions; }
@Test public void testCopyFromArrayToBuffer() { byte[] b = new byte[15]; b[0] = -1; long l = 988L; int i = 135; short s = 7; Bytes.putLong(b, 1, l); Bytes.putShort(b, 9, s); Bytes.putInt(b, 11, i); ByteBuffer buffer = ByteBuffer.allocate(14); ByteBufferUtils.copyFromArrayToBuffer(buffer, b, 1, 14); buffer.rewind(); assertEquals(l, buffer.getLong()); assertEquals(s, buffer.getShort()); assertEquals(i, buffer.getInt()); }
/** * Generate assigned regions to a given server using group information. * * @param numRegions the num regions to generate * @param sn the servername * @return the list of regions * @throws java.io.IOException Signals that an I/O exception has occurred. */ protected List<RegionInfo> assignedRegions(int numRegions, ServerName sn) throws IOException { List<RegionInfo> regions = new ArrayList<>(numRegions); byte[] start = new byte[16]; byte[] end = new byte[16]; Bytes.putInt(start, 0, numRegions << 1); Bytes.putInt(end, 0, (numRegions << 1) + 1); for (int i = 0; i < numRegions; i++) { TableName tableName = getTableName(sn); regions.add(RegionInfoBuilder.newBuilder(tableName) .setStartKey(start) .setEndKey(end) .setSplit(false) .setRegionId(regionId++) .build()); } return regions; }
/** * Write random values to the writer assuming a table created using * {@link #FAMILIES} as column family descriptors */ private void writeRandomKeyValues(RecordWriter<ImmutableBytesWritable, Cell> writer, TaskAttemptContext context, Set<byte[]> families, int numRows) throws IOException, InterruptedException { byte keyBytes[] = new byte[Bytes.SIZEOF_INT]; int valLength = 10; byte valBytes[] = new byte[valLength]; int taskId = context.getTaskAttemptID().getTaskID().getId(); assert taskId < Byte.MAX_VALUE : "Unit tests dont support > 127 tasks!"; final byte [] qualifier = Bytes.toBytes("data"); Random random = new Random(); for (int i = 0; i < numRows; i++) { Bytes.putInt(keyBytes, 0, i); random.nextBytes(valBytes); ImmutableBytesWritable key = new ImmutableBytesWritable(keyBytes); for (byte[] family : families) { Cell kv = new KeyValue(keyBytes, family, qualifier, valBytes); writer.write(key, kv); } } }