@Override public int skip(PositionedByteRange src) { src.setPosition(src.getPosition() + Bytes.SIZEOF_LONG); return Bytes.SIZEOF_LONG; }
@Override public int skip(PositionedByteRange src) { src.setPosition(src.getPosition() + Bytes.SIZEOF_DOUBLE); return Bytes.SIZEOF_DOUBLE; }
@Override public int skip(PositionedByteRange src) { src.setPosition(src.getPosition() + this.length); return this.length; }
@Override public int skip(PositionedByteRange src) { src.setPosition(src.getPosition() + Bytes.SIZEOF_INT); return Bytes.SIZEOF_INT; }
@Override public int skip(PositionedByteRange src) { src.setPosition(src.getPosition() + Bytes.SIZEOF_SHORT); return Bytes.SIZEOF_SHORT; }
@Override public int skip(PositionedByteRange src) { src.setPosition(src.getPosition() + Bytes.SIZEOF_BYTE); return Bytes.SIZEOF_BYTE; }
@Override public int skip(PositionedByteRange src) { src.setPosition(src.getPosition() + Bytes.SIZEOF_FLOAT); return Bytes.SIZEOF_FLOAT; }
@Override public int skip(PositionedByteRange src) { int skipped = src.getRemaining(); src.setPosition(src.getLength()); return skipped; }
@Override public int skip(PositionedByteRange src) { int skipped = src.getRemaining(); src.setPosition(src.getLength()); return skipped; }
/** * Skip {@code src} over the encoded varuint64. * @param src source buffer * @param cmp if true, parse the compliment of the value. * @return the number of bytes skipped. */ @VisibleForTesting static int skipVaruint64(PositionedByteRange src, boolean cmp) { final int len = lengthVaruint64(src, cmp); src.setPosition(src.getPosition() + len); return len; }
@Override public int encode(PositionedByteRange dst, CellProtos.Cell val) { CodedOutputStream os = outputStreamFromByteRange(dst); try { int before = os.spaceLeft(), after, written; val.writeTo(os); after = os.spaceLeft(); written = before - after; dst.setPosition(dst.getPosition() + written); return written; } catch (IOException e) { throw new RuntimeException("Error while encoding type.", e); } } }
@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 {@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 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); } }
@Override public CellProtos.Cell decode(PositionedByteRange src) { CellProtos.Cell.Builder builder = CellProtos.Cell.newBuilder(); CodedInputStream is = inputStreamFromByteRange(src); is.setSizeLimit(src.getLength()); try { CellProtos.Cell ret = builder.mergeFrom(is).build(); src.setPosition(src.getPosition() + is.getTotalBytesRead()); return ret; } catch (IOException e) { throw new RuntimeException("Error while decoding type.", e); } }
@Test public void testEncodeDecode() { Integer intVal = Integer.valueOf(10); String strVal = "hello"; PositionedByteRange buff = new SimplePositionedMutableByteRange(10); SampleUnion1 type = new SampleUnion1(); type.encode(buff, intVal); buff.setPosition(0); assertTrue(0 == intVal.compareTo(type.decodeA(buff))); buff.setPosition(0); type.encode(buff, strVal); buff.setPosition(0); assertTrue(0 == strVal.compareTo(type.decodeB(buff))); }
/** * Return the number of encoded entries remaining in {@code buff}. The * state of {@code buff} is not modified through use of this method. */ public static int length(PositionedByteRange buff) { PositionedByteRange b = new SimplePositionedMutableByteRange(buff.getBytes(), buff.getOffset(), buff.getLength()); b.setPosition(buff.getPosition()); int cnt = 0; for (; isEncodedValue(b); skip(b), cnt++) ; return cnt; } }
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; }
@Test public void testEncodedLength() { PositionedByteRange buff = new SimplePositionedMutableByteRange(20); for (DataType<String> type : new OrderedString[] { OrderedString.ASCENDING, OrderedString.DESCENDING }) { for (String val : VALUES) { buff.setPosition(0); type.encode(buff, val); assertEquals( "encodedLength does not match actual, " + val, buff.getPosition(), type.encodedLength(val)); } } } }
@Test public void testEncodedLength() { PositionedByteRange buff = new SimplePositionedMutableByteRange(20); for (DataType<byte[]> type : new OrderedBlob[] { OrderedBlob.ASCENDING, OrderedBlob.DESCENDING }) { for (byte[] val : VALUES) { buff.setPosition(0); type.encode(buff, val); assertEquals( "encodedLength does not match actual, " + Bytes.toStringBinary(val), buff.getPosition(), type.encodedLength(val)); } } } }