/** * Write {@code val} into {@code dst}, respecting {@code voff} and {@code vlen}. * @return number of bytes written. */ public int encode(PositionedByteRange dst, byte[] val, int voff, int vlen) { Bytes.putBytes(dst.getBytes(), dst.getOffset() + dst.getPosition(), val, voff, vlen); dst.setPosition(dst.getPosition() + vlen); return vlen; } }
@Override public String decode(PositionedByteRange src) { if (Order.ASCENDING == this.order) { // avoid unnecessary array copy for ASC case. String val = Bytes.toString(src.getBytes(), src.getOffset() + src.getPosition(), src.getRemaining()); src.setPosition(src.getLength()); return val; } else { byte[] b = new byte[src.getRemaining()]; src.get(b); order.apply(b, 0, b.length); return Bytes.toString(b); } }
/** * Write instance {@code val} into buffer {@code dst}. * @throws IllegalArgumentException when the encoded representation of * {@code val} contains the {@code term} sequence. */ @Override public int encode(PositionedByteRange dst, T val) { final int start = dst.getPosition(); int written = wrapped.encode(dst, val); PositionedByteRange b = dst.shallowCopy(); b.setLength(dst.getPosition()); b.setPosition(start); if (-1 != terminatorPosition(b)) { dst.setPosition(start); throw new IllegalArgumentException("Encoded value contains terminator sequence."); } dst.put(term); return written + term.length; } }
@Override public int skip(PositionedByteRange src) { int skipped = src.getRemaining(); src.setPosition(src.getLength()); return skipped; }
@Override public boolean hasNext() { // hasNext can return true when position == length in the case of a // nullable field trailing a struct. return idx < types.length && src.getPosition() <= src.getLength(); }
@Override public int skip(PositionedByteRange src) { src.setPosition(src.getPosition() + Bytes.SIZEOF_LONG); return Bytes.SIZEOF_LONG; }
public static byte[] serialize(Object o) { PositionedByteRange buffer = new DynamicPositionedMutableByteRange(4096); serialize(buffer, o); buffer.setLength(buffer.getPosition()); buffer.setPosition(0); byte[] bytes = new byte[buffer.getRemaining()]; buffer.get(bytes); return bytes; }
final byte header = src.get(); if (header == NULL || header == DESCENDING.apply(NULL)) { return null; if (src.peek() == ord.apply(TERM)) { src.get(); return new byte[0]; final int offset = src.getOffset(), start = src.getPosition(); int end; byte[] a = src.getBytes(); for (end = start; (byte) (ord.apply(a[offset + end]) & 0x80) != TERM; end++) for (int i = start + 1; i < end; i++) { if (s == 7) { ret.put((byte) (t | (ord.apply(a[offset + i]) & 0x7f))); i++; ret.put((byte) (t | ((ord.apply(a[offset + i]) & 0x7f) >>> s))); s = s == 1 ? 7 : s - 1; src.setPosition(end); assert t == 0 : "Unexpected bits remaining after decoding blob."; assert ret.getPosition() == ret.getLength() : "Allocated unnecessarily large return buffer."; return ret.getBytes();
@Override public Byte decode(PositionedByteRange src) { byte val = src.getBytes()[src.getOffset() + src.getPosition()]; skip(src); return val; }
buf1.setPosition(1); 2, OrderedBytes.encodeInt8(buf1, vals[i], ord)); assertEquals("Broken test: serialization did not consume entire buffer.", buf1.getLength(), buf1.getPosition()); assertEquals("Surprising serialized length.", 2, buf1.getPosition() - 1); assertEquals("Buffer underflow.", 0, a[0]); assertEquals("Buffer underflow.", 0, a[1]); buf1.setPosition(1); assertEquals("Surprising return value.", 2, OrderedBytes.skip(buf1)); assertEquals("Did not skip enough bytes.", 2, buf1.getPosition() - 1); buf1.setPosition(1); assertEquals("Deserialization failed.", vals[i].byteValue(), OrderedBytes.decodeInt8(buf1)); assertEquals("Did not consume enough bytes.", 2, buf1.getPosition() - 1); PositionedByteRange pbr = new SimplePositionedMutableByteRange(); for (int i = 0; i < vals.length; i++) { OrderedBytes.encodeInt8(pbr.set(encoded[i]), vals[i], ord); int decoded = OrderedBytes.decodeInt8(pbr.set(encoded[i])); assertEquals( String.format(
@Override public int skip(PositionedByteRange src) { CellProtos.Cell.Builder builder = CellProtos.Cell.newBuilder(); CodedInputStream is = inputStreamFromByteRange(src); is.setSizeLimit(src.getLength()); try { builder.mergeFrom(is); int consumed = is.getTotalBytesRead(); src.setPosition(src.getPosition() + consumed); return consumed; } catch (IOException e) { throw new RuntimeException("Error while skipping type.", e); } }
static KeyValue readCell(PositionedByteRange pbr) throws Exception { int kvStartPos = pbr.getPosition(); int keyLen = pbr.getInt(); int valLen = pbr.getInt(); pbr.setPosition(pbr.getPosition() + keyLen + valLen); // Skip the key and value section int tagsLen = ((pbr.get() & 0xff) << 8) ^ (pbr.get() & 0xff); pbr.setPosition(pbr.getPosition() + tagsLen); // Skip the tags section long mvcc = pbr.getVLong(); KeyValue kv = new KeyValue(pbr.getBytes(), kvStartPos, (int) KeyValue.getKeyValueDataStructureSize(keyLen, valLen, tagsLen)); kv.setSequenceId(mvcc); return kv; }
r.put(Bytes.toBytes("f")[0]) .put(Bytes.toBytes("o")[0]) .put(Bytes.toBytes("o")[0]); Assert.assertEquals(3, r.getPosition()); Assert.assertArrayEquals( new byte[] { 0, Bytes.toBytes("f")[0], Bytes.toBytes("o")[0], Bytes.toBytes("o")[0], 0 }, r.getBytes()); r.setPosition(0); r.put(Bytes.toBytes("f")) .put(Bytes.toBytes("o")) .put(Bytes.toBytes("o")); Assert.assertEquals(3, r.getPosition()); Assert.assertArrayEquals( new byte[] { 0, Bytes.toBytes("f")[0], Bytes.toBytes("o")[0], Bytes.toBytes("o")[0], 0 }, r.getBytes()); r.setPosition(0); Assert.assertEquals(Bytes.toBytes("f")[0], r.get()); Assert.assertEquals(Bytes.toBytes("o")[0], r.get()); Assert.assertEquals(Bytes.toBytes("o")[0], r.get()); r.setPosition(1); Assert.assertEquals(Bytes.toBytes("o")[0], r.get()); r.setPosition(0); byte[] dst = new byte[3]; r.get(dst); Assert.assertArrayEquals(Bytes.toBytes("foo"), dst);
@Override public int encode(PositionedByteRange dst, T val) { if (dst.getRemaining() < length) { throw new IllegalArgumentException("Not enough buffer remaining. dst.offset: " + dst.getOffset() + " dst.length: " + dst.getLength() + " dst.position: " + dst.getPosition() + " max length: " + length); } int written = base.encode(dst, val); if (written > length) { throw new IllegalArgumentException("Length of encoded value (" + written + ") exceeds max length (" + length + ")."); } // TODO: is the zero-padding appropriate? for (; written < length; written++) { dst.put((byte) 0x00); } return written; } }
@Override public T decode(PositionedByteRange src) { if (wrapped.isSkippable()) { T ret = wrapped.decode(src); src.setPosition(src.getPosition() + term.length); return ret; } else { // find the terminator position int term = terminatorPosition(src); if (-1 == term) { throw new IllegalArgumentException("Terminator sequence not found."); } byte[] b = new byte[term - src.getPosition()]; src.get(b); // TODO: should we assert that b.position == b.length? T ret = wrapped.decode(new SimplePositionedMutableByteRange(b)); src.get(this.term); return ret; } }
/** * Read a {@code byte[]} from the buffer {@code src}. */ public byte[] decode(PositionedByteRange src, int length) { byte[] val = new byte[length]; src.get(val); return val; }
/** * Decode a Blob value, byte-for-byte copy. * @see #encodeBlobCopy(PositionedByteRange, byte[], int, int, Order) */ public static byte[] decodeBlobCopy(PositionedByteRange src) { byte header = src.get(); if (header == NULL || header == DESCENDING.apply(NULL)) { return null; } assert header == BLOB_COPY || header == DESCENDING.apply(BLOB_COPY); Order ord = header == BLOB_COPY ? ASCENDING : DESCENDING; final int length = src.getRemaining() - (ASCENDING == ord ? 0 : 1); byte[] ret = new byte[length]; src.get(ret); ord.apply(ret, 0, ret.length); // DESCENDING ordered BlobCopy requires a termination bit to preserve // sort-order semantics of null values. if (DESCENDING == ord) src.get(); return ret; }
/** * Decode a {@link BigDecimal} value from the variable-length encoding. * @throws IllegalArgumentException when the encoded value is not a Numeric. * @see #encodeNumeric(PositionedByteRange, BigDecimal, Order) */ public static BigDecimal decodeNumericAsBigDecimal(PositionedByteRange src) { if (isNull(src)) { src.get(); return null; } if (!isNumeric(src)) throw unexpectedHeader(src.peek()); if (isNumericNaN(src)) throw unexpectedHeader(src.peek()); if (isNumericInfinite(src)) throw unexpectedHeader(src.peek()); return decodeNumericValue(src); }
@Override public void eval() { buffer.clear(); br.set(bytes); org.apache.hadoop.hbase.util.OrderedBytes.encodeInt64(br, in.value, org.apache.hadoop.hbase.util.Order.ASCENDING); buffer.setBytes(0, bytes, 0, 9); out.buffer = buffer; out.start = 0; out.end = 9; } }