Search in sources :

Example 1 with OutputStreamStreamOutput

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);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) OutputStreamStreamOutput(org.elasticsearch.common.io.stream.OutputStreamStreamOutput) RepositoryException(org.elasticsearch.repositories.RepositoryException) StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) OutputStreamStreamOutput(org.elasticsearch.common.io.stream.OutputStreamStreamOutput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 2 with OutputStreamStreamOutput

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());
    }
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) OutputStreamStreamOutput(org.elasticsearch.common.io.stream.OutputStreamStreamOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InputStreamStreamInput(org.elasticsearch.common.io.stream.InputStreamStreamInput)

Example 3 with OutputStreamStreamOutput

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()));
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) OutputStreamStreamOutput(org.elasticsearch.common.io.stream.OutputStreamStreamOutput) Version(org.elasticsearch.Version) VersionUtils.randomVersion(org.elasticsearch.test.VersionUtils.randomVersion) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InputStreamStreamInput(org.elasticsearch.common.io.stream.InputStreamStreamInput)

Example 4 with OutputStreamStreamOutput

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();
                }
            }
        }
    };
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) OutputStreamStreamOutput(org.elasticsearch.common.io.stream.OutputStreamStreamOutput) Deflater(java.util.zip.Deflater) OutputStream(java.io.OutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Example 5 with OutputStreamStreamOutput

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()));
}
Also used : OutputStreamStreamOutput(org.elasticsearch.common.io.stream.OutputStreamStreamOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) Explanation(org.apache.lucene.search.Explanation) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InputStreamStreamInput(org.elasticsearch.common.io.stream.InputStreamStreamInput)

Aggregations

OutputStreamStreamOutput (org.elasticsearch.common.io.stream.OutputStreamStreamOutput)22 ByteArrayOutputStream (java.io.ByteArrayOutputStream)17 ByteArrayInputStream (java.io.ByteArrayInputStream)16 InputStreamStreamInput (org.elasticsearch.common.io.stream.InputStreamStreamInput)16 StreamOutput (org.elasticsearch.common.io.stream.StreamOutput)9 Test (org.junit.Test)6 CrateUnitTest (io.crate.test.integration.CrateUnitTest)5 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)3 ShardId (org.elasticsearch.index.shard.ShardId)3 ThreadPools (io.crate.monitor.ThreadPools)2 BufferedOutputStream (java.io.BufferedOutputStream)2 OutputStream (java.io.OutputStream)2 Collections.unmodifiableMap (java.util.Collections.unmodifiableMap)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Deflater (java.util.zip.Deflater)2 DeflaterOutputStream (java.util.zip.DeflaterOutputStream)2 Version (org.elasticsearch.Version)2 BytesReference (org.elasticsearch.common.bytes.BytesReference)2