當前位置: 首頁>>代碼示例>>Java>>正文


Java ReleasableBytesStreamOutput類代碼示例

本文整理匯總了Java中org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput的典型用法代碼示例。如果您正苦於以下問題:Java ReleasableBytesStreamOutput類的具體用法?Java ReleasableBytesStreamOutput怎麽用?Java ReleasableBytesStreamOutput使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


ReleasableBytesStreamOutput類屬於org.elasticsearch.common.io.stream包,在下文中一共展示了ReleasableBytesStreamOutput類的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getRandomizedBytesReference

import org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput; //導入依賴的package包/類
private BytesReference getRandomizedBytesReference(int length) throws IOException {
    // we know bytes stream output always creates a paged bytes reference, we use it to create randomized content
    ReleasableBytesStreamOutput out = new ReleasableBytesStreamOutput(length, bigarrays);
    for (int i = 0; i < length; i++) {
        out.writeByte((byte) random().nextInt(1 << 8));
    }
    assertEquals(out.size(), length);
    BytesReference ref = out.bytes();
    assertEquals(ref.length(), length);
    if (randomBoolean()) {
        return new BytesArray(ref.toBytesRef());
    } else if (randomBoolean()) {
        BytesRef bytesRef = ref.toBytesRef();
        return Netty4Utils.toBytesReference(Unpooled.wrappedBuffer(bytesRef.bytes, bytesRef.offset,
            bytesRef.length));
    } else {
        return ref;
    }
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:20,代碼來源:Netty4UtilsTests.java

示例2: writeOperations

import org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput; //導入依賴的package包/類
/**
 * Writes all operations in the given iterable to the given output stream including the size of the array
 * use {@link #readOperations(StreamInput)} to read it back.
 */
public static void writeOperations(StreamOutput outStream, List<Operation> toWrite) throws IOException {
    final ReleasableBytesStreamOutput out = new ReleasableBytesStreamOutput(BigArrays.NON_RECYCLING_INSTANCE);
    try {
        outStream.writeInt(toWrite.size());
        final BufferedChecksumStreamOutput checksumStreamOutput = new BufferedChecksumStreamOutput(out);
        for (Operation op : toWrite) {
            out.reset();
            final long start = out.position();
            out.skip(Integer.BYTES);
            writeOperationNoSize(checksumStreamOutput, op);
            long end = out.position();
            int operationSize = (int) (out.position() - Integer.BYTES - start);
            out.seek(start);
            out.writeInt(operationSize);
            out.seek(end);
            ReleasablePagedBytesReference bytes = out.bytes();
            bytes.writeTo(outStream);
        }
    } finally {
        Releasables.close(out.bytes());
    }

}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:28,代碼來源:Translog.java

示例3: writeOperations

import org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput; //導入依賴的package包/類
/**
 * Writes all operations in the given iterable to the given output stream including the size of the array
 * use {@link #readOperations(StreamInput)} to read it back.
 */
public static void writeOperations(StreamOutput outStream, List<Operation> toWrite) throws IOException {
    final ReleasableBytesStreamOutput out = new ReleasableBytesStreamOutput(BigArrays.NON_RECYCLING_INSTANCE);
    try {
        outStream.writeInt(toWrite.size());
        final BufferedChecksumStreamOutput checksumStreamOutput = new BufferedChecksumStreamOutput(out);
        for (Operation op : toWrite) {
            out.reset();
            final long start = out.position();
            out.skip(RamUsageEstimator.NUM_BYTES_INT);
            writeOperationNoSize(checksumStreamOutput, op);
            long end = out.position();
            int operationSize = (int) (out.position() - RamUsageEstimator.NUM_BYTES_INT - start);
            out.seek(start);
            out.writeInt(operationSize);
            out.seek(end);
            ReleasablePagedBytesReference bytes = out.bytes();
            bytes.writeTo(outStream);
        }
    } finally {
        Releasables.close(out.bytes());
    }

}
 
開發者ID:baidu,項目名稱:Elasticsearch,代碼行數:28,代碼來源:Translog.java

示例4: newBytesReference

import org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput; //導入依賴的package包/類
@Override
protected BytesReference newBytesReference(int length) throws IOException {
    ReleasableBytesStreamOutput out = new ReleasableBytesStreamOutput(length, bigarrays);
    for (int i = 0; i < length; i++) {
        out.writeByte((byte) random().nextInt(1 << 8));
    }
    assertEquals(out.size(), length);
    BytesReference ref = out.bytes();
    assertEquals(ref.length(), length);
    BytesRef bytesRef = ref.toBytesRef();
    final ByteBuf buffer = Unpooled.wrappedBuffer(bytesRef.bytes, bytesRef.offset, bytesRef.length);
    return Netty4Utils.toBytesReference(buffer);
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:14,代碼來源:ByteBufBytesReferenceTests.java

示例5: newBytesReference

import org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput; //導入依賴的package包/類
protected BytesReference newBytesReference(int length) throws IOException {
    // we know bytes stream output always creates a paged bytes reference, we use it to create randomized content
    ReleasableBytesStreamOutput out = new ReleasableBytesStreamOutput(length, bigarrays);
    for (int i = 0; i < length; i++) {
        out.writeByte((byte) random().nextInt(1 << 8));
    }
    assertThat(out.size(), Matchers.equalTo(length));
    BytesReference ref = out.bytes();
    assertThat(ref.length(), Matchers.equalTo(length));
    assertThat(ref, Matchers.instanceOf(PagedBytesReference.class));
    return ref;
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:13,代碼來源:PagedBytesReferenceTests.java

示例6: newRefList

import org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput; //導入依賴的package包/類
private List<BytesReference> newRefList(int length) throws IOException {
    List<BytesReference> referenceList = new ArrayList<>();
    for (int i = 0; i < length;) {
        int remaining = length-i;
        int sliceLength = randomIntBetween(1, remaining);
        ReleasableBytesStreamOutput out = new ReleasableBytesStreamOutput(sliceLength, bigarrays);
        for (int j = 0; j < sliceLength; j++) {
            out.writeByte((byte) random().nextInt(1 << 8));
        }
        assertEquals(sliceLength, out.size());
        referenceList.add(out.bytes());
        i+=sliceLength;
    }
    return referenceList;
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:16,代碼來源:CompositeBytesReferenceTests.java

示例7: sendResponse

import org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput; //導入依賴的package包/類
@Override
public void sendResponse(TransportResponse response, TransportResponseOptions options) throws IOException {
    if (transport.compress) {
        options = TransportResponseOptions.builder(options).withCompress(transport.compress).build();
    }

    byte status = 0;
    status = TransportStatus.setResponse(status);

    ReleasableBytesStreamOutput bStream = new ReleasableBytesStreamOutput(transport.bigArrays);
    boolean addedReleaseListener = false;
    try {
        bStream.skip(NettyHeader.HEADER_SIZE);
        StreamOutput stream = bStream;
        if (options.compress()) {
            status = TransportStatus.setCompress(status);
            stream = CompressorFactory.defaultCompressor().streamOutput(stream);
        }
        stream.setVersion(version);
        response.writeTo(stream);
        stream.close();

        ReleasablePagedBytesReference bytes = bStream.bytes();
        ChannelBuffer buffer = bytes.toChannelBuffer();
        NettyHeader.writeHeader(buffer, requestId, status, version);
        ChannelFuture future = channel.write(buffer);
        ReleaseChannelFutureListener listener = new ReleaseChannelFutureListener(bytes);
        future.addListener(listener);
        addedReleaseListener = true;
        transportServiceAdapter.onResponseSent(requestId, action, response, options);
    } finally {
        if (!addedReleaseListener) {
            Releasables.close(bStream.bytes());
        }
    }
}
 
開發者ID:baidu,項目名稱:Elasticsearch,代碼行數:37,代碼來源:NettyTransportChannel.java

示例8: newBytesOutput

import org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput; //導入依賴的package包/類
@Override
public BytesStreamOutput newBytesOutput() {
    return new ReleasableBytesStreamOutput(transport.bigArrays);
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:5,代碼來源:Netty4HttpChannel.java

示例9: testCompareTo

import org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput; //導入依賴的package包/類
public void testCompareTo() throws IOException {
    final int iters = randomIntBetween(5, 10);
    for (int i = 0; i < iters; i++) {
        int length = randomIntBetween(10, PAGE_SIZE * randomIntBetween(2, 8));
        BytesReference bytesReference = newBytesReference(length);
        assertTrue(bytesReference.compareTo(new BytesArray("")) > 0);
        assertTrue(new BytesArray("").compareTo(bytesReference) < 0);


        assertEquals(0, bytesReference.compareTo(bytesReference));
        int sliceFrom = randomIntBetween(0, bytesReference.length());
        int sliceLength = randomIntBetween(0, bytesReference.length() - sliceFrom);
        BytesReference slice = bytesReference.slice(sliceFrom, sliceLength);

        assertEquals(bytesReference.toBytesRef().compareTo(slice.toBytesRef()),
            new BytesArray(bytesReference.toBytesRef(), true).compareTo(new BytesArray(slice.toBytesRef(), true)));

        assertEquals(bytesReference.toBytesRef().compareTo(slice.toBytesRef()),
            bytesReference.compareTo(slice));
        assertEquals(slice.toBytesRef().compareTo(bytesReference.toBytesRef()),
            slice.compareTo(bytesReference));

        assertEquals(0, slice.compareTo(new BytesArray(slice.toBytesRef())));
        assertEquals(0, new BytesArray(slice.toBytesRef()).compareTo(slice));

        final int crazyLength = length + randomIntBetween(10, PAGE_SIZE * randomIntBetween(2, 8));
        ReleasableBytesStreamOutput crazyStream = new ReleasableBytesStreamOutput(length, bigarrays);
        final int offset = randomIntBetween(0, crazyLength - length);
        for (int j = 0; j < offset; j++) {
            crazyStream.writeByte((byte) random().nextInt(1 << 8));
        }
        bytesReference.writeTo(crazyStream);
        for (int j = crazyStream.size(); j < crazyLength; j++) {
            crazyStream.writeByte((byte) random().nextInt(1 << 8));
        }
        PagedBytesReference crazyReference = crazyStream.bytes();

        assertFalse(crazyReference.compareTo(bytesReference) == 0);
        assertEquals(0, crazyReference.slice(offset, length).compareTo(
            bytesReference));
        assertEquals(0, bytesReference.compareTo(
            crazyReference.slice(offset, length)));
    }
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:45,代碼來源:AbstractBytesReferenceTestCase.java

示例10: sendRequestToChannel

import org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput; //導入依賴的package包/類
private void sendRequestToChannel(DiscoveryNode node, final Channel targetChannel, final long requestId, final String action,
                                    final TransportRequest request, TransportRequestOptions options, Version channelVersion,
                                  byte status) throws IOException,
    TransportException {
    if (compress) {
        options = TransportRequestOptions.builder(options).withCompress(true).build();
    }
    status = TransportStatus.setRequest(status);
    ReleasableBytesStreamOutput bStream = new ReleasableBytesStreamOutput(bigArrays);
    // we wrap this in a release once since if the onRequestSent callback throws an exception
    // we might release things twice and this should be prevented
    final Releasable toRelease = Releasables.releaseOnce(() -> Releasables.close(bStream.bytes()));
    boolean addedReleaseListener = false;
    StreamOutput stream = bStream;
    try {
        // only compress if asked, and, the request is not bytes, since then only
        // the header part is compressed, and the "body" can't be extracted as compressed
        if (options.compress() && canCompress(request)) {
            status = TransportStatus.setCompress(status);
            stream = CompressorFactory.COMPRESSOR.streamOutput(stream);
        }

        // we pick the smallest of the 2, to support both backward and forward compatibility
        // note, this is the only place we need to do this, since from here on, we use the serialized version
        // as the version to use also when the node receiving this request will send the response with
        Version version = Version.min(getCurrentVersion(), channelVersion);

        stream.setVersion(version);
        threadPool.getThreadContext().writeTo(stream);
        stream.writeString(action);
        BytesReference message = buildMessage(requestId, status, node.getVersion(), request, stream, bStream);
        final TransportRequestOptions finalOptions = options;
        Runnable onRequestSent = () -> { // this might be called in a different thread
            try {
                toRelease.close();
            } finally {
                transportServiceAdapter.onRequestSent(node, requestId, action, request, finalOptions);
            }
        };
        addedReleaseListener = internalSendMessage(targetChannel, message, onRequestSent);
    } finally {
        IOUtils.close(stream);
        if (!addedReleaseListener) {
            toRelease.close();
        }
    }
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:48,代碼來源:TcpTransport.java

示例11: sendResponse

import org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput; //導入依賴的package包/類
private void sendResponse(Version nodeVersion, Channel channel, final TransportResponse response, final long requestId,
    final String action, TransportResponseOptions options, byte status) throws IOException {
    if (compress) {
        options = TransportResponseOptions.builder(options).withCompress(true).build();
    }
    status = TransportStatus.setResponse(status); // TODO share some code with sendRequest
    ReleasableBytesStreamOutput bStream = new ReleasableBytesStreamOutput(bigArrays);
    // we wrap this in a release once since if the onRequestSent callback throws an exception
    // we might release things twice and this should be prevented
    final Releasable toRelease = Releasables.releaseOnce(() -> Releasables.close(bStream.bytes()));
    boolean addedReleaseListener = false;
    StreamOutput stream = bStream;
    try {
        if (options.compress()) {
            status = TransportStatus.setCompress(status);
            stream = CompressorFactory.COMPRESSOR.streamOutput(stream);
        }
        threadPool.getThreadContext().writeTo(stream);
        stream.setVersion(nodeVersion);
        BytesReference reference = buildMessage(requestId, status, nodeVersion, response, stream, bStream);

        final TransportResponseOptions finalOptions = options;
        Runnable onRequestSent = () -> { // this might be called in a different thread
            try {
                toRelease.close();
            } finally {
                transportServiceAdapter.onResponseSent(requestId, action, response, finalOptions);
            }
        };
        addedReleaseListener = internalSendMessage(channel, reference, onRequestSent);
    } finally {
        try {
            IOUtils.close(stream);
        } finally {
            if (!addedReleaseListener) {

                toRelease.close();
            }
        }

    }
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:43,代碼來源:TcpTransport.java


注:本文中的org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。