@Override public SingleByteBuff putInt(int value) { ByteBufferUtils.putInt(this.buf, value); return this; }
@Override public int compareTo(ByteBuffer value, int offset, int length) { if (this.value.length <= length) { length = this.value.length; } return ByteBufferUtils.compareTo(this.value, 0, this.value.length, value, offset, length); }
/** * Reads a double value at the given buffer's offset. * @param buffer * @param offset offset where double is * @return double value at offset */ public static double toDouble(ByteBuffer buffer, int offset) { return Double.longBitsToDouble(toLong(buffer, offset)); }
public static boolean equals(ByteBuffer buf1, int o1, int l1, ByteBuffer buf2, int o2, int l2) { if ((l1 == 0) || (l2 == 0)) { // both 0 length, return true, or else false return l1 == l2; } // Since we're often comparing adjacent sorted data, // it's usual to have equal arrays except for the very last byte // so check that first if (toByte(buf1, o1 + l1 - 1) != toByte(buf2, o2 + l2 - 1)) return false; return compareTo(buf1, o1, l1, buf2, o2, l2) == 0; }
/** * Copy the data to the output stream and update position in buffer. * @param out the stream to write bytes to * @param in the buffer to read bytes from * @param length the number of bytes to copy */ public static void moveBufferToStream(OutputStream out, ByteBuffer in, int length) throws IOException { copyBufferToStream(out, in, in.position(), length); skip(in, length); }
/** * Made into a static method so as to reuse the logic * within ValueAndTagRewriteByteBufferExtendedCell */ static void write(ByteBuffer buf, int offset, Cell cell, byte[] value, byte[] tags) { offset = ByteBufferUtils.putInt(buf, offset, KeyValueUtil.keyLength(cell));// Key length offset = ByteBufferUtils.putInt(buf, offset, value.length);// Value length offset = KeyValueUtil.appendKeyTo(cell, buf, offset); ByteBufferUtils.copyFromArrayToBuffer(buf, offset, value, 0, value.length); offset += value.length; int tagsLen = tags == null ? 0 : tags.length; if (tagsLen > 0) { offset = ByteBufferUtils.putAsShort(buf, offset, tagsLen); ByteBufferUtils.copyFromArrayToBuffer(buf, offset, tags, 0, tagsLen); } }
timestamp = -timestamp; timestampFitsInBytes = ByteBufferUtils.longFitsIn(timestamp); flag |= (timestampFitsInBytes - 1) << SHIFT_TIMESTAMP_LENGTH; timestamp = -timestamp; timestampFitsInBytes = ByteBufferUtils.longFitsIn(timestamp); boolean minusDiffTimestamp = diffTimestamp < 0; if (minusDiffTimestamp) { diffTimestamp = -diffTimestamp; diffTimestampFitsInBytes = ByteBufferUtils.longFitsIn(diffTimestamp); if (diffTimestampFitsInBytes < timestampFitsInBytes) { flag |= (diffTimestampFitsInBytes - 1) << SHIFT_TIMESTAMP_LENGTH; ByteBufferUtils.putCompressedInt(out, kLength); ByteBufferUtils.putCompressedInt(out, vLength); ByteBufferUtils.putCompressedInt(out, commonPrefix); short rLen = cell.getRowLength(); if (commonPrefix < rLen + KeyValue.ROW_LENGTH_SIZE) { ByteBufferUtils.putLong(out, timestamp, timestampFitsInBytes); } else { ByteBufferUtils.putLong(out, diffTimestamp, diffTimestampFitsInBytes);
keyLength = state.keyLength; } else { keyLength = ByteBufferUtils.readCompressedInt(source); valueLength = ByteBufferUtils.readCompressedInt(source); int commonPrefix = ByteBufferUtils.readCompressedInt(source); ByteBufferUtils.copyFromBufferToBuffer(buffer, buffer, state.prevOffset + KeyValue.ROW_OFFSET, commonPrefix); ByteBufferUtils.copyFromStreamToBuffer(buffer, source, KeyValue.ROW_LENGTH_SIZE - commonPrefix); ByteBufferUtils.skip(buffer, -KeyValue.ROW_LENGTH_SIZE); rowLength = buffer.getShort(); rowRestLength = rowLength; ByteBufferUtils.copyFromStreamToBuffer(buffer, source, rowRestLength); state.rowLength = rowLength; ByteBufferUtils.copyFromStreamToBuffer(buffer, source, keyRestLength); long timestamp = ByteBufferUtils.readLong(source, timestampFitsInBytes); if ((flag & FLAG_TIMESTAMP_SIGN) != 0) { timestamp = -timestamp; ByteBufferUtils.copyFromStreamToBuffer(buffer, source, valueLength);
timestampFitsInBytes = ByteBufferUtils.longFitsIn(timestamp); ByteBufferUtils.skip(in, currentState.rowLength + KeyValue.ROW_LENGTH_SIZE); ByteBufferUtils.moveBufferToStream(out, in, currentState.familyLength + KeyValue.FAMILY_LENGTH_SIZE); in.reset(); ByteBufferUtils.findCommonPrefix(in, in.position(), previousState.prevOffset + KeyValue.ROW_OFFSET, keyLength - KeyValue.TIMESTAMP_TYPE_SIZE); timestampFitsInBytes = ByteBufferUtils.longFitsIn(timestamp); diffTimestampFitsInBytes = ByteBufferUtils.longFitsIn(diffTimestamp); if (diffTimestampFitsInBytes < timestampFitsInBytes) { flag |= (diffTimestampFitsInBytes - 1) << SHIFT_TIMESTAMP_LENGTH; ByteBufferUtils.putCompressedInt(out, keyLength); ByteBufferUtils.putCompressedInt(out, valueLength); ByteBufferUtils.putCompressedInt(out, commonPrefix); ByteBufferUtils.skip(in, commonPrefix); int restRowLength = currentState.rowLength + KeyValue.ROW_LENGTH_SIZE - commonPrefix; ByteBufferUtils.moveBufferToStream(out, in, restRowLength); ByteBufferUtils.skip(in, currentState.familyLength +
private int decodeKeyValue(DataInputStream source, ByteBuffer buffer, int prevKeyOffset) throws IOException, EncoderBufferTooSmallException { int keyLength = ByteBufferUtils.readCompressedInt(source); int valueLength = ByteBufferUtils.readCompressedInt(source); int commonLength = ByteBufferUtils.readCompressedInt(source); int keyOffset; keyLength += commonLength; ensureSpace(buffer, keyLength + valueLength + KeyValue.ROW_OFFSET); buffer.putInt(keyLength); buffer.putInt(valueLength); // copy the prefix if (commonLength > 0) { keyOffset = buffer.position(); ByteBufferUtils.copyFromBufferToBuffer(buffer, buffer, prevKeyOffset, commonLength); } else { keyOffset = buffer.position(); } // copy rest of the key and value int len = keyLength - commonLength + valueLength; ByteBufferUtils.copyFromStreamToBuffer(buffer, source, len); return keyOffset; }
@Test public void testReadWriteVLong() { for (long l : testNumbers) { ByteBuffer b = ByteBuffer.allocate(MAX_VLONG_LENGTH); ByteBufferUtils.writeVLong(b, l); b.flip(); assertEquals(l, ByteBufferUtils.readVLong(b)); } }
protected final void afterDecodingKeyValue(DataInputStream source, ByteBuffer dest, HFileBlockDefaultDecodingContext decodingCtx) throws IOException { if (decodingCtx.getHFileContext().isIncludesTags()) { int tagsLength = ByteBufferUtils.readCompressedInt(source); tagCompressionContext.uncompressTags(source, dest, tagsLength); } else { ByteBufferUtils.copyFromStreamToBuffer(dest, source, tagsLength); ByteBufferUtils.writeVLong(dest, memstoreTS); } catch (IOException ex) { throw new RuntimeException("Unable to copy memstore timestamp " +
private void testCompressedInt(int value) throws IOException { int parsedValue = 0; ByteArrayOutputStream bos = new ByteArrayOutputStream(); ByteBufferUtils.putCompressedInt(bos, value); ByteArrayInputStream bis = new ByteArrayInputStream( bos.toByteArray()); parsedValue = ByteBufferUtils.readCompressedInt(bis); assertEquals(value, parsedValue); }
public static void appendToByteBuffer(final ByteBuffer bb, final KeyValue kv, final boolean includeMvccVersion) { // keep pushing the limit out. assume enough capacity bb.limit(bb.position() + kv.getLength()); bb.put(kv.getBuffer(), kv.getOffset(), kv.getLength()); if (includeMvccVersion) { int numMvccVersionBytes = WritableUtils.getVIntSize(kv.getSequenceId()); ByteBufferUtils.extendLimit(bb, numMvccVersionBytes); ByteBufferUtils.writeVLong(bb, kv.getSequenceId()); } }
@Test public void testToPrimitiveTypes() { ByteBuffer buffer = ByteBuffer.allocate(15); long l = 988L; int i = 135; short s = 7; buffer.putLong(l); buffer.putShort(s); buffer.putInt(i); assertEquals(l, ByteBufferUtils.toLong(buffer, 0)); assertEquals(s, ByteBufferUtils.toShort(buffer, 8)); assertEquals(i, ByteBufferUtils.toInt(buffer, 10)); }
@Override byte[] getContents() { // This ideally should not be called byte[] copy = new byte[this.length]; ByteBufferUtils.copyFromBufferToArray(copy, (ByteBuffer) this.container, this.offset, 0, length); return copy; }
@Override public int write(OutputStream out, boolean withTags) throws IOException { int lenToWrite = getSerializedSize(withTags); ByteBufferUtils.putInt(out, keyBuffer.capacity()); ByteBufferUtils.putInt(out, valueLength); // Write key out.write(keyBuffer.array()); // Write value ByteBufferUtils.copyBufferToStream(out, this.valueBuffer, this.valueOffset, this.valueLength); if (withTags && this.tagsLength > 0) { // 2 bytes tags length followed by tags bytes // tags length is serialized with 2 bytes only(short way) even if the type is int. // As this is non -ve numbers, we save the sign bit. See HBASE-11437 out.write((byte) (0xff & (this.tagsLength >> 8))); out.write((byte) (0xff & this.tagsLength)); ByteBufferUtils.copyBufferToStream(out, this.tagsBuffer, this.tagsOffset, this.tagsLength); } return lenToWrite; }
@Override public void write(ByteBuffer b, int off, int len) throws IOException { ByteBufferUtils.copyBufferToStream(out, b, off, len); written += len; } }
private int addKV(int prevKeyOffset, DataOutputStream out, ByteBuffer in, int prevKeyLength) throws IOException { int keyLength = in.getInt(); int valueLength = in.getInt(); if (prevKeyOffset == -1) { // copy the key, there is no common prefix with none ByteBufferUtils.putCompressedInt(out, keyLength); ByteBufferUtils.putCompressedInt(out, valueLength); ByteBufferUtils.putCompressedInt(out, 0); ByteBufferUtils.moveBufferToStream(out, in, keyLength + valueLength); } else { // find a common prefix and skip it int common = ByteBufferUtils.findCommonPrefix( in, prevKeyOffset + KeyValue.ROW_OFFSET, in.position(), Math.min(prevKeyLength, keyLength)); ByteBufferUtils.putCompressedInt(out, keyLength - common); ByteBufferUtils.putCompressedInt(out, valueLength); ByteBufferUtils.putCompressedInt(out, common); ByteBufferUtils.skip(in, common); ByteBufferUtils.moveBufferToStream(out, in, keyLength - common + valueLength); } return keyLength; }
private int createCellReference(ByteBufferKeyValue cell, ByteBuffer idxBuffer, int idxOffset) { int offset = idxOffset; int dataChunkID = cell.getChunkId(); offset = ByteBufferUtils.putInt(idxBuffer, offset, dataChunkID); // write data chunk id offset = ByteBufferUtils.putInt(idxBuffer, offset, cell.getOffset()); // offset offset = ByteBufferUtils.putInt(idxBuffer, offset, cell.getSerializedSize()); // length offset = ByteBufferUtils.putLong(idxBuffer, offset, cell.getSequenceId()); // seqId return offset; }