use of org.elasticsearch.common.io.stream.OutputStreamStreamOutput in project elasticsearch by elastic.
the class BlobStoreRepository method writeIndexGen.
protected void writeIndexGen(final RepositoryData repositoryData, final long repositoryStateId) throws IOException {
// can not write to a read only repository
assert isReadOnly() == false;
final long currentGen = latestIndexBlobId();
if (repositoryStateId != SnapshotsInProgress.UNDEFINED_REPOSITORY_STATE_ID && currentGen != repositoryStateId) {
// repository data
throw new RepositoryException(metadata.name(), "concurrent modification of the index-N file, expected current generation [" + repositoryStateId + "], actual current generation [" + currentGen + "] - possibly due to simultaneous snapshot deletion requests");
}
final long newGen = currentGen + 1;
final BytesReference snapshotsBytes;
try (BytesStreamOutput bStream = new BytesStreamOutput()) {
try (StreamOutput stream = new OutputStreamStreamOutput(bStream)) {
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON, stream);
repositoryData.snapshotsToXContent(builder, ToXContent.EMPTY_PARAMS);
builder.close();
}
snapshotsBytes = bStream.bytes();
}
// write the index file
writeAtomic(INDEX_FILE_PREFIX + Long.toString(newGen), snapshotsBytes);
// delete the N-2 index file if it exists, keep the previous one around as a backup
if (isReadOnly() == false && newGen - 2 >= 0) {
final String oldSnapshotIndexFile = INDEX_FILE_PREFIX + Long.toString(newGen - 2);
if (snapshotsBlobContainer.blobExists(oldSnapshotIndexFile)) {
snapshotsBlobContainer.deleteBlob(oldSnapshotIndexFile);
}
}
// write the current generation to the index-latest file
final BytesReference genBytes;
try (BytesStreamOutput bStream = new BytesStreamOutput()) {
bStream.writeLong(newGen);
genBytes = bStream.bytes();
}
if (snapshotsBlobContainer.blobExists(INDEX_LATEST_BLOB)) {
snapshotsBlobContainer.deleteBlob(INDEX_LATEST_BLOB);
}
writeAtomic(INDEX_LATEST_BLOB, genBytes);
}
use of org.elasticsearch.common.io.stream.OutputStreamStreamOutput in project elasticsearch by elastic.
the class TermVectorsUnitTests method testStreamRequest.
public void testStreamRequest() throws IOException {
for (int i = 0; i < 10; i++) {
TermVectorsRequest request = new TermVectorsRequest("index", "type", "id");
request.offsets(random().nextBoolean());
request.fieldStatistics(random().nextBoolean());
request.payloads(random().nextBoolean());
request.positions(random().nextBoolean());
request.termStatistics(random().nextBoolean());
String parent = random().nextBoolean() ? "someParent" : null;
request.parent(parent);
String pref = random().nextBoolean() ? "somePreference" : null;
request.preference(pref);
request.doc(new BytesArray("{}"), randomBoolean(), XContentType.JSON);
// write
ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
OutputStreamStreamOutput out = new OutputStreamStreamOutput(outBuffer);
request.writeTo(out);
// read
ByteArrayInputStream esInBuffer = new ByteArrayInputStream(outBuffer.toByteArray());
InputStreamStreamInput esBuffer = new InputStreamStreamInput(esInBuffer);
TermVectorsRequest req2 = new TermVectorsRequest(null, null, null);
req2.readFrom(esBuffer);
assertThat(request.offsets(), equalTo(req2.offsets()));
assertThat(request.fieldStatistics(), equalTo(req2.fieldStatistics()));
assertThat(request.payloads(), equalTo(req2.payloads()));
assertThat(request.positions(), equalTo(req2.positions()));
assertThat(request.termStatistics(), equalTo(req2.termStatistics()));
assertThat(request.preference(), equalTo(pref));
assertThat(request.routing(), equalTo(null));
assertEquals(new BytesArray("{}"), request.doc());
assertEquals(XContentType.JSON, request.xContentType());
}
}
use of org.elasticsearch.common.io.stream.OutputStreamStreamOutput in project elasticsearch by elastic.
the class StartRecoveryRequestTests method testSerialization.
public void testSerialization() throws Exception {
final Version targetNodeVersion = randomVersion(random());
final StartRecoveryRequest outRequest = new StartRecoveryRequest(new ShardId("test", "_na_", 0), new DiscoveryNode("a", buildNewFakeTransportAddress(), emptyMap(), emptySet(), targetNodeVersion), new DiscoveryNode("b", buildNewFakeTransportAddress(), emptyMap(), emptySet(), targetNodeVersion), Store.MetadataSnapshot.EMPTY, randomBoolean(), randomNonNegativeLong(), randomBoolean() ? SequenceNumbersService.UNASSIGNED_SEQ_NO : randomNonNegativeLong());
final ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
final OutputStreamStreamOutput out = new OutputStreamStreamOutput(outBuffer);
out.setVersion(targetNodeVersion);
outRequest.writeTo(out);
final ByteArrayInputStream inBuffer = new ByteArrayInputStream(outBuffer.toByteArray());
InputStreamStreamInput in = new InputStreamStreamInput(inBuffer);
in.setVersion(targetNodeVersion);
final StartRecoveryRequest inRequest = new StartRecoveryRequest();
inRequest.readFrom(in);
assertThat(outRequest.shardId(), equalTo(inRequest.shardId()));
assertThat(outRequest.sourceNode(), equalTo(inRequest.sourceNode()));
assertThat(outRequest.targetNode(), equalTo(inRequest.targetNode()));
assertThat(outRequest.metadataSnapshot().asMap(), equalTo(inRequest.metadataSnapshot().asMap()));
assertThat(outRequest.isPrimaryRelocation(), equalTo(inRequest.isPrimaryRelocation()));
assertThat(outRequest.recoveryId(), equalTo(inRequest.recoveryId()));
if (targetNodeVersion.onOrAfter(Version.V_6_0_0_alpha1_UNRELEASED)) {
assertThat(outRequest.startingSeqNo(), equalTo(inRequest.startingSeqNo()));
} else {
assertThat(SequenceNumbersService.UNASSIGNED_SEQ_NO, equalTo(inRequest.startingSeqNo()));
}
}
use of org.elasticsearch.common.io.stream.OutputStreamStreamOutput in project elasticsearch by elastic.
the class DeflateCompressor method streamOutput.
@Override
public StreamOutput streamOutput(StreamOutput out) throws IOException {
out.writeBytes(HEADER);
final boolean nowrap = true;
final Deflater deflater = new Deflater(LEVEL, nowrap);
final boolean syncFlush = true;
OutputStream compressedOut = new DeflaterOutputStream(out, deflater, BUFFER_SIZE, syncFlush);
compressedOut = new BufferedOutputStream(compressedOut, BUFFER_SIZE);
return new OutputStreamStreamOutput(compressedOut) {
final AtomicBoolean closed = new AtomicBoolean(false);
public void close() throws IOException {
try {
super.close();
} finally {
if (closed.compareAndSet(false, true)) {
// important to release native memory
deflater.end();
}
}
}
};
}
use of org.elasticsearch.common.io.stream.OutputStreamStreamOutput in project elasticsearch by elastic.
the class ExplainActionIT method testStreamExplain.
public void testStreamExplain() throws Exception {
Explanation exp = Explanation.match(2f, "some explanation");
// write
ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
OutputStreamStreamOutput out = new OutputStreamStreamOutput(outBuffer);
Lucene.writeExplanation(out, exp);
// read
ByteArrayInputStream esInBuffer = new ByteArrayInputStream(outBuffer.toByteArray());
InputStreamStreamInput esBuffer = new InputStreamStreamInput(esInBuffer);
Explanation result = Lucene.readExplanation(esBuffer);
assertThat(exp.toString(), equalTo(result.toString()));
exp = Explanation.match(2.0f, "some explanation", Explanation.match(2.0f, "another explanation"));
// write complex
outBuffer = new ByteArrayOutputStream();
out = new OutputStreamStreamOutput(outBuffer);
Lucene.writeExplanation(out, exp);
// read complex
esInBuffer = new ByteArrayInputStream(outBuffer.toByteArray());
esBuffer = new InputStreamStreamInput(esInBuffer);
result = Lucene.readExplanation(esBuffer);
assertThat(exp.toString(), equalTo(result.toString()));
}
Aggregations