List of usage examples for io.netty.buffer ByteBuf release
boolean release();
From source file:com.addthis.hydra.data.tree.prop.DataReservoir.java
License:Apache License
@Override public byte[] bytesEncode(long version) { if (reservoir == null) { return EMPTY_BYTES; }//from w w w. j a va2 s .co m byte[] retBytes = null; ByteBuf byteBuf = PooledByteBufAllocator.DEFAULT.buffer(); try { Varint.writeUnsignedVarLong(minEpoch, byteBuf); Varint.writeUnsignedVarInt(reservoir.length, byteBuf); for (int element : reservoir) { Varint.writeUnsignedVarInt(element, byteBuf); } retBytes = new byte[byteBuf.readableBytes()]; byteBuf.readBytes(retBytes); } finally { byteBuf.release(); } return retBytes; }
From source file:com.addthis.hydra.data.tree.prop.DataReservoir.java
License:Apache License
@Override public void bytesDecode(byte[] b, long version) { if (b.length == 0) { return;/*w ww. j a va 2 s. c o m*/ } ByteBuf byteBuf = Unpooled.wrappedBuffer(b); try { minEpoch = Varint.readUnsignedVarLong(byteBuf); int length = Varint.readUnsignedVarInt(byteBuf); reservoir = new int[length]; for (int i = 0; i < reservoir.length; i++) { reservoir[i] = Varint.readUnsignedVarInt(byteBuf); } } finally { byteBuf.release(); } }
From source file:com.addthis.hydra.data.tree.prop.DataTime.java
License:Apache License
@Override public byte[] bytesEncode(long version) { byte[] encodedBytes = null; ByteBuf byteBuf = PooledByteBufAllocator.DEFAULT.buffer(); try {//from w w w. j a v a2 s.c o m long delta = last - first; Varint.writeUnsignedVarLong(first, byteBuf); Varint.writeUnsignedVarLong(delta, byteBuf); encodedBytes = new byte[byteBuf.readableBytes()]; byteBuf.readBytes(encodedBytes); } finally { byteBuf.release(); } return encodedBytes; }
From source file:com.addthis.hydra.data.tree.prop.DataTime.java
License:Apache License
@Override public void bytesDecode(byte[] b, long version) { ByteBuf byteBuf = Unpooled.wrappedBuffer(b); try {//ww w .ja v a2 s.c o m first = Varint.readUnsignedVarLong(byteBuf); last = first + Varint.readUnsignedVarLong(byteBuf); } finally { byteBuf.release(); } }
From source file:com.addthis.hydra.data.util.ConcurrentKeyTopper.java
License:Apache License
@Override public byte[] bytesEncode(long version) { preEncode();// ww w . ja v a 2 s. c om if (map.size() == 0) { return EMPTY; } byte[] retBytes = null; ByteBuf byteBuf = PooledByteBufAllocator.DEFAULT.buffer(); try { Varint.writeUnsignedVarInt(map.size(), byteBuf); for (Map.Entry<String, Long> mapEntry : map.entrySet()) { String key = mapEntry.getKey(); if (key == null) { throw new IllegalStateException("ConcurrentKeyTopper decoded null key"); } byte[] keyBytes = key.getBytes("UTF-8"); Varint.writeUnsignedVarInt(keyBytes.length, byteBuf); byteBuf.writeBytes(keyBytes); Varint.writeUnsignedVarLong(mapEntry.getValue(), byteBuf); } retBytes = new byte[byteBuf.readableBytes()]; byteBuf.readBytes(retBytes); } catch (UnsupportedEncodingException e) { e.printStackTrace(); throw new RuntimeException(e); } finally { byteBuf.release(); } return retBytes; }
From source file:com.addthis.hydra.data.util.ConcurrentKeyTopper.java
License:Apache License
@Override public void bytesDecode(byte[] b, long version) { if (b.length == 0) { map = new ConcurrentHashMapV8<>(16, 0.75f, 4); return;//from w w w . jav a 2 s . c om } ByteBuf byteBuf = Unpooled.wrappedBuffer(b); try { int mapSize = Varint.readUnsignedVarInt(byteBuf); int i; try { if (mapSize > 0) { map = new ConcurrentHashMapV8<>(mapSize + 16, 0.75f, 4); for (i = 0; i < mapSize; i++) { int keyLength = Varint.readUnsignedVarInt(byteBuf); byte[] keybytes = new byte[keyLength]; byteBuf.readBytes(keybytes); String k = new String(keybytes, "UTF-8"); long value = Varint.readUnsignedVarLong(byteBuf); map.put(k, value); } } else { map = new ConcurrentHashMapV8<>(16, 0.75f, 4); } } catch (Exception e) { throw new RuntimeException(e); } } finally { byteBuf.release(); } postDecode(); }
From source file:com.addthis.hydra.data.util.KeyTopper.java
License:Apache License
/** * Encode the data structure into a serialized representation. * Encode the number of elements followed by each (key, value) * pair. If the error estimation is used then encode the special * byte value 0 (since we will never encode 0 as the size * of a non-empty map) at the head of the byte array. * @param version/*from ww w . j av a2 s .c om*/ * @return */ @Override public byte[] bytesEncode(long version) { if (map.size() == 0) { return EMPTY; } byte[] retBytes = null; ByteBuf byteBuf = PooledByteBufAllocator.DEFAULT.buffer(); try { if (hasErrors()) { byteBuf.writeByte(0); } Varint.writeUnsignedVarInt(map.size(), byteBuf); for (Map.Entry<String, Long> mapEntry : map.entrySet()) { String key = mapEntry.getKey(); if (key == null) { throw new NullPointerException("KeyTopper decoded null key"); } byte[] keyBytes = key.getBytes("UTF-8"); Varint.writeUnsignedVarInt(keyBytes.length, byteBuf); byteBuf.writeBytes(keyBytes); Varint.writeUnsignedVarLong(mapEntry.getValue(), byteBuf); if (hasErrors()) { Long error = errors.get(key); if (error != null) { Varint.writeUnsignedVarLong(error, byteBuf); } else { Varint.writeUnsignedVarLong(0, byteBuf); } } } retBytes = new byte[byteBuf.readableBytes()]; byteBuf.readBytes(retBytes); } catch (UnsupportedEncodingException e) { throw Throwables.propagate(e); } finally { byteBuf.release(); } return retBytes; }
From source file:com.addthis.hydra.data.util.KeyTopper.java
License:Apache License
@Override public void bytesDecode(byte[] b, long version) { map = new HashMap<>(); errors = null;/*from ww w .j a va 2 s.co m*/ if (b.length == 0) { return; } ByteBuf byteBuf = Unpooled.wrappedBuffer(b); try { byte marker = byteBuf.getByte(byteBuf.readerIndex()); if (marker == 0) { errors = new HashMap<>(); // Consume the sentinel byte value byteBuf.readByte(); } int mapSize = Varint.readUnsignedVarInt(byteBuf); try { if (mapSize > 0) { for (int i = 0; i < mapSize; i++) { int keyLength = Varint.readUnsignedVarInt(byteBuf); byte[] keybytes = new byte[keyLength]; byteBuf.readBytes(keybytes); String k = new String(keybytes, "UTF-8"); long value = Varint.readUnsignedVarLong(byteBuf); map.put(k, value); if (hasErrors()) { long error = Varint.readUnsignedVarLong(byteBuf); if (error != 0) { errors.put(k, error); } } } } } catch (Exception e) { throw Throwables.propagate(e); } } finally { byteBuf.release(); } }
From source file:com.addthis.hydra.store.skiplist.Page.java
License:Apache License
public void decode(byte[] page) { parent.numPagesDecoded.getAndIncrement(); ByteBuf buffer = Unpooled.wrappedBuffer(page); try {/* w w w . ja v a 2 s.com*/ InputStream in = new ByteBufInputStream(buffer); int flags = in.read() & 0xff; int gztype = flags & 0x0f; boolean isSparse = (flags & FLAGS_IS_SPARSE) != 0; boolean hasEstimates = (flags & FLAGS_HAS_ESTIMATES) != 0; int readEstimateTotal, readEstimates; switch (gztype) { case 1: in = new InflaterInputStream(in); break; case 2: in = new GZIPInputStream(in); break; case 3: in = new LZFInputStream(in); break; case 4: in = new SnappyInputStream(in); break; } K firstKey; byte[] nextFirstKey; if (isSparse) { encodeType = KeyCoder.EncodeType.SPARSE; DataInputStream dis = new DataInputStream(in); int entries = Varint.readUnsignedVarInt(dis); firstKey = keyCoder.keyDecode(Bytes.readBytes(in, Varint.readUnsignedVarInt(dis))); int nextFirstKeyLength = Varint.readUnsignedVarInt(dis); if (nextFirstKeyLength > 0) { nextFirstKey = Bytes.readBytes(in, nextFirstKeyLength); } else { nextFirstKey = null; } int bytes = 0; size = entries; keys = new ArrayList<>(size); values = new ArrayList<>(size); rawValues = new ArrayList<>(size); for (int i = 0; i < entries; i++) { byte kb[] = Bytes.readBytes(in, Varint.readUnsignedVarInt(dis)); byte vb[] = Bytes.readBytes(in, Varint.readUnsignedVarInt(dis)); bytes += kb.length + vb.length; keys.add(keyCoder.keyDecode(kb)); values.add(null); rawValues.add(vb); } if (hasEstimates) { readEstimateTotal = Varint.readUnsignedVarInt(dis); readEstimates = Varint.readUnsignedVarInt(dis); setAverage(readEstimateTotal, readEstimates); } else { /** use a pessimistic/conservative byte/entry estimate */ setAverage(bytes * estimateMissingFactor, entries); } } else { encodeType = KeyCoder.EncodeType.LEGACY; int entries = (int) Bytes.readLength(in); firstKey = keyCoder.keyDecode(Bytes.readBytes(in)); nextFirstKey = Bytes.readBytes(in); int bytes = 0; size = entries; keys = new ArrayList<>(size); values = new ArrayList<>(size); rawValues = new ArrayList<>(size); for (int i = 0; i < entries; i++) { byte kb[] = Bytes.readBytes(in); byte vb[] = Bytes.readBytes(in); bytes += kb.length + vb.length; keys.add(keyCoder.keyDecode(kb)); values.add(null); rawValues.add(vb); } if (hasEstimates) { readEstimateTotal = (int) Bytes.readLength(in); readEstimates = (int) Bytes.readLength(in); setAverage(readEstimateTotal, readEstimates); } else { /** use a pessimistic/conservative byte/entry estimate */ setAverage(bytes * estimateMissingFactor, entries); } } updateMemoryEstimate(); assert (this.firstKey.equals(firstKey)); this.nextFirstKey = keyCoder.keyDecode(nextFirstKey); in.close(); } catch (RuntimeException ex) { throw ex; } catch (Exception ex) { throw new RuntimeException(ex); } finally { buffer.release(); } }
From source file:com.addthis.hydra.task.source.Mark.java
License:Apache License
@Override public byte[] bytesEncode(long version) { byte[] retBytes = null; ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer(); try {// www. java 2 s . c o m byte[] valBytes = getValue().getBytes(); Varint.writeUnsignedVarInt(valBytes.length, buffer); buffer.writeBytes(valBytes); Varint.writeUnsignedVarLong(getIndex(), buffer); buffer.writeByte(isEnd() ? 1 : 0); Varint.writeUnsignedVarInt(error, buffer); retBytes = new byte[buffer.readableBytes()]; buffer.readBytes(retBytes); } finally { buffer.release(); } return retBytes; }