本文整理汇总了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;
}
}
示例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());
}
}
示例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());
}
}
示例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);
}
示例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;
}
示例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;
}
示例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());
}
}
}
示例8: newBytesOutput
import org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput; //导入依赖的package包/类
@Override
public BytesStreamOutput newBytesOutput() {
return new ReleasableBytesStreamOutput(transport.bigArrays);
}
示例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)));
}
}
示例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();
}
}
}
示例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();
}
}
}
}