Search in sources :

Example 1 with Page

use of org.opensearch.nio.Page in project OpenSearch by opensearch-project.

the class PagedByteBufTests method testBytesAreUsed.

public void testBytesAreUsed() {
    byte[] bytes1 = new byte[10];
    byte[] bytes2 = new byte[10];
    for (int i = 0; i < 10; ++i) {
        bytes1[i] = (byte) i;
    }
    for (int i = 10; i < 20; ++i) {
        bytes2[i - 10] = (byte) i;
    }
    Page[] pages = new Page[2];
    pages[0] = new Page(ByteBuffer.wrap(bytes1), () -> {
    });
    pages[1] = new Page(ByteBuffer.wrap(bytes2), () -> {
    });
    ByteBuf byteBuf = PagedByteBuf.byteBufFromPages(pages);
    assertEquals(20, byteBuf.readableBytes());
    for (int i = 0; i < 20; ++i) {
        assertEquals((byte) i, byteBuf.getByte(i));
    }
    Page[] pages2 = new Page[2];
    ByteBuffer firstBuffer = ByteBuffer.wrap(bytes1);
    firstBuffer.position(2);
    ByteBuffer secondBuffer = ByteBuffer.wrap(bytes2);
    secondBuffer.limit(8);
    pages2[0] = new Page(firstBuffer, () -> {
    });
    pages2[1] = new Page(secondBuffer, () -> {
    });
    ByteBuf byteBuf2 = PagedByteBuf.byteBufFromPages(pages2);
    assertEquals(16, byteBuf2.readableBytes());
    for (int i = 2; i < 18; ++i) {
        assertEquals((byte) i, byteBuf2.getByte(i - 2));
    }
}
Also used : Page(org.opensearch.nio.Page) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer)

Example 2 with Page

use of org.opensearch.nio.Page in project OpenSearch by opensearch-project.

the class PagedByteBufTests method testReleasingPage.

public void testReleasingPage() {
    AtomicInteger integer = new AtomicInteger(0);
    int pageCount = randomInt(10) + 1;
    ArrayList<Page> pages = new ArrayList<>();
    for (int i = 0; i < pageCount; ++i) {
        pages.add(new Page(ByteBuffer.allocate(10), integer::incrementAndGet));
    }
    ByteBuf byteBuf = PagedByteBuf.byteBufFromPages(pages.toArray(new Page[0]));
    assertEquals(0, integer.get());
    byteBuf.retain();
    byteBuf.release();
    assertEquals(0, integer.get());
    ByteBuf secondBuf = byteBuf.retainedSlice();
    byteBuf.release();
    assertEquals(0, integer.get());
    secondBuf.release();
    assertEquals(pageCount, integer.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) Page(org.opensearch.nio.Page) ByteBuf(io.netty.buffer.ByteBuf)

Example 3 with Page

use of org.opensearch.nio.Page in project OpenSearch by opensearch-project.

the class TcpReadWriteHandler method consumeReads.

@Override
public int consumeReads(InboundChannelBuffer channelBuffer) throws IOException {
    Page[] pages = channelBuffer.sliceAndRetainPagesTo(channelBuffer.getIndex());
    BytesReference[] references = new BytesReference[pages.length];
    for (int i = 0; i < pages.length; ++i) {
        references[i] = BytesReference.fromByteBuffer(pages[i].byteBuffer());
    }
    Releasable releasable = () -> IOUtils.closeWhileHandlingException(pages);
    try (ReleasableBytesReference reference = new ReleasableBytesReference(CompositeBytesReference.of(references), releasable)) {
        pipeline.handleBytes(channel, reference);
        return reference.length();
    }
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) CompositeBytesReference(org.opensearch.common.bytes.CompositeBytesReference) ReleasableBytesReference(org.opensearch.common.bytes.ReleasableBytesReference) ReleasableBytesReference(org.opensearch.common.bytes.ReleasableBytesReference) Page(org.opensearch.nio.Page) Releasable(org.opensearch.common.lease.Releasable)

Aggregations

Page (org.opensearch.nio.Page)3 ByteBuf (io.netty.buffer.ByteBuf)2 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 BytesReference (org.opensearch.common.bytes.BytesReference)1 CompositeBytesReference (org.opensearch.common.bytes.CompositeBytesReference)1 ReleasableBytesReference (org.opensearch.common.bytes.ReleasableBytesReference)1 Releasable (org.opensearch.common.lease.Releasable)1