Example usage for io.netty.buffer PooledByteBufAllocator DEFAULT

List of usage examples for io.netty.buffer PooledByteBufAllocator DEFAULT

Introduction

In this page you can find the example usage for io.netty.buffer PooledByteBufAllocator DEFAULT.

Prototype

PooledByteBufAllocator DEFAULT

To view the source code for io.netty.buffer PooledByteBufAllocator DEFAULT.

Click Source Link

Usage

From source file:com.addthis.hydra.data.tree.prop.DataCounting.java

License:Apache License

@Override
public byte[] bytesEncode(long version) {
    preEncode();//from  ww  w. ja va 2  s  . com
    ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
    try {
        Varint.writeUnsignedVarInt(ver, buffer);
        Varint.writeUnsignedVarInt(M.length, buffer);
        buffer.writeBytes(M);
        byte[] bytes = new byte[buffer.readableBytes()];
        buffer.readBytes(bytes);
        return bytes;
    } finally {
        buffer.release();
    }
}

From source file:com.addthis.hydra.data.tree.prop.DataKeyTop.java

License:Apache License

@Override
public byte[] bytesEncode(long version) {
    byte[] bytes = null;
    ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer();
    try {//  w  ww.  j  a v  a2s  .c om
        byte[] topBytes = top.bytesEncode(version);
        Varint.writeUnsignedVarInt(topBytes.length, buf);
        buf.writeBytes(topBytes);
        Varint.writeUnsignedVarInt(size, buf);
        bytes = new byte[buf.readableBytes()];
        buf.readBytes(bytes);
    } finally {
        buf.release();
    }
    return bytes;
}

From source file:com.addthis.hydra.data.tree.prop.DataMap.java

License:Apache License

@Override
public byte[] bytesEncode(long version) {
    byte[] encodedBytes = null;
    ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer();
    try {//from w ww  . j  a v a2 s.  c om
        synchronized (map) {
            preEncode();
            Varint.writeUnsignedVarInt(keys.length, buf);
            for (String key : keys) {
                writeString(buf, key);
            }
            for (String val : vals) {
                writeString(buf, val);
            }
            Varint.writeUnsignedVarInt(size, buf);
        }
        encodedBytes = new byte[buf.readableBytes()];
        buf.readBytes(encodedBytes);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    } finally {
        buf.release();
    }
    return encodedBytes;
}

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. ja v  a 2 s .  com
    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.DataTime.java

License:Apache License

@Override
public byte[] bytesEncode(long version) {
    byte[] encodedBytes = null;
    ByteBuf byteBuf = PooledByteBufAllocator.DEFAULT.buffer();
    try {/*from www . j a  v  a2 s  .co  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.util.ConcurrentKeyTopper.java

License:Apache License

@Override
public byte[] bytesEncode(long version) {
    preEncode();/*from w  w  w  .j a va  2s. c o m*/
    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.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// w ww . j  av  a 2s .  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.query.web.QueryServer.java

License:Apache License

public void run() throws Exception {
    bossGroup = new NioEventLoopGroup(1);
    workerGroup = new NioEventLoopGroup();
    executorGroup = new DefaultEventExecutorGroup(AggregateConfig.FRAME_READER_THREADS,
            new ThreadFactoryBuilder().setDaemon(true).setNameFormat("frame-reader-%d").build());
    for (EventExecutor executor : executorGroup) {
        executor.execute(new NextQueryTask(queryQueue, executor));
    }//from   www  .j a  va 2s  .  c o m
    ServerBootstrap b = new ServerBootstrap();
    b.option(ChannelOption.SO_BACKLOG, 1024);
    b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    b.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    b.childOption(ChannelOption.MESSAGE_SIZE_ESTIMATOR, new DefaultMessageSizeEstimator(200));
    b.childOption(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 100000000);
    b.childOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 50000000);
    b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(queryServerInitializer);
    b.bind(webPort).sync();
}

From source file:com.addthis.hydra.store.skiplist.SkipListCache.java

License:Apache License

/**
 * If the value of the {@link Page#nextFirstKey} field of a Page
 * is detected to be incorrect, then this method will correct that
 * field.//from   ww  w . java  2  s. c o  m
 */
private void updateNextFirstKey(Page<K, V> prevPage, K newNextFirstKey, K targetKey, byte[] encodedTargetKey) {
    assert (prevPage.isWriteLockedByCurrentThread());

    Map.Entry<byte[], byte[]> entry = externalStore.floorEntry(encodedTargetKey);
    K floorKey = keyCoder.keyDecode(entry.getKey());
    if (floorKey.equals(prevPage.firstKey)) {
        if (prevPage.keys == null) {
            pullPageHelper(prevPage, entry.getValue());
        }
        assert (prevPage.nextFirstKey.equals(targetKey));
        prevPage.nextFirstKey = newNextFirstKey;
        if (prevPage.state == ExternalMode.DISK_MEMORY_IDENTICAL) {
            prevPage.state = ExternalMode.DISK_MEMORY_DIRTY;
        }
    } else {
        Page<K, V> diskPage = pageFactory.generateEmptyPage(SkipListCache.this, floorKey,
                KeyCoder.EncodeType.SPARSE);
        diskPage.decode(entry.getValue());
        assert (diskPage.nextFirstKey.equals(targetKey));
        assert (compareKeys(prevPage.firstKey, diskPage.firstKey) <= 0);
        diskPage.nextFirstKey = newNextFirstKey;
        ByteBufOutputStream byteBufOutputStream = new ByteBufOutputStream(
                PooledByteBufAllocator.DEFAULT.buffer());
        try {
            externalStore.put(entry.getKey(), diskPage.encode(byteBufOutputStream));
        } finally {
            byteBufOutputStream.buffer().release();
        }
    }
}

From source file:com.addthis.hydra.store.skiplist.SkipListCache.java

License:Apache License

/**
 * Invoked by the constructor. If the left sentinel page is not
 * found in the external storage, then create the left sentinel
 * page.//from w w w  .ja  va 2 s .  com
 */
private void loadFromExternalStore() {
    Page<K, V> leftSentinel = pageFactory.generateEmptyPage(this, negInf, KeyCoder.EncodeType.SPARSE);
    ByteBufOutputStream byteBufOutputStream = null;
    try {
        if (externalStore.count() == 0) {
            byteBufOutputStream = new ByteBufOutputStream(PooledByteBufAllocator.DEFAULT.buffer());
            leftSentinel.initialize();
            byte[] encodeKey = keyCoder.keyEncode(negInf);
            byte[] encodePage = leftSentinel.encode(byteBufOutputStream);
            externalStore.put(encodeKey, encodePage);
        } else {
            byte[] encodedFirstKey = externalStore.firstKey();
            K firstKey = keyCoder.keyDecode(encodedFirstKey);
            byte[] page = externalStore.get(encodedFirstKey);

            if (firstKey.equals(negInf)) {
                leftSentinel.decode(page);
                updateMemoryEstimate(leftSentinel.getMemoryEstimate());
            } else {
                byteBufOutputStream = new ByteBufOutputStream(PooledByteBufAllocator.DEFAULT.buffer());
                leftSentinel.initialize();
                leftSentinel.nextFirstKey = firstKey;

                byte[] encodeKey = keyCoder.keyEncode(negInf);
                byte[] encodePage = leftSentinel.encode(byteBufOutputStream);
                externalStore.put(encodeKey, encodePage);

                Page<K, V> minPage = pageFactory.generateEmptyPage(this, firstKey,
                        leftSentinel.getEncodeType());
                minPage.decode(page);

                cache.put(firstKey, minPage);
                updateMemoryEstimate(minPage.getMemoryEstimate());
                cacheSize.getAndIncrement();
                numPagesInMemory.getAndIncrement();
                evictionQueue.offer(minPage);
            }
        }
    } finally {
        if (byteBufOutputStream != null) {
            byteBufOutputStream.buffer().release();
        }
    }
    cache.put(negInf, leftSentinel);
    cacheSize.getAndIncrement();
    numPagesInMemory.getAndIncrement();
    evictionQueue.offer(leftSentinel);
}