Search in sources :

Example 1 with BytesStreamOutput

use of org.opensearch.common.io.stream.BytesStreamOutput in project OpenSearch by opensearch-project.

the class GrokProcessorGetActionTests method testRequest.

public void testRequest() throws Exception {
    GrokProcessorGetAction.Request request = new GrokProcessorGetAction.Request(false);
    BytesStreamOutput out = new BytesStreamOutput();
    request.writeTo(out);
    StreamInput streamInput = out.bytes().streamInput();
    GrokProcessorGetAction.Request otherRequest = new GrokProcessorGetAction.Request(streamInput);
    assertThat(otherRequest.validate(), nullValue());
}
Also used : StreamInput(org.opensearch.common.io.stream.StreamInput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput)

Example 2 with BytesStreamOutput

use of org.opensearch.common.io.stream.BytesStreamOutput in project OpenSearch by opensearch-project.

the class DebugTests method testPainlessExplainErrorSerialization.

/**
 * {@link PainlessExplainError} doesn't serialize but the headers still make it.
 */
public void testPainlessExplainErrorSerialization() throws IOException {
    Map<String, Object> params = singletonMap("a", "jumped over the moon");
    ScriptException e = expectThrows(ScriptException.class, () -> exec("Debug.explain(params.a)", params, true));
    assertEquals(singletonList("jumped over the moon"), e.getMetadata("opensearch.to_string"));
    assertEquals(singletonList("java.lang.String"), e.getMetadata("opensearch.java_class"));
    assertEquals(singletonList("java.lang.String"), e.getMetadata("opensearch.painless_class"));
    try (BytesStreamOutput out = new BytesStreamOutput()) {
        out.writeException(e);
        try (StreamInput in = out.bytes().streamInput()) {
            OpenSearchException read = (ScriptException) in.readException();
            assertEquals(singletonList("jumped over the moon"), read.getMetadata("opensearch.to_string"));
            assertEquals(singletonList("java.lang.String"), read.getMetadata("opensearch.java_class"));
            assertEquals(singletonList("java.lang.String"), read.getMetadata("opensearch.painless_class"));
        }
    }
}
Also used : ScriptException(org.opensearch.script.ScriptException) StreamInput(org.opensearch.common.io.stream.StreamInput) OpenSearchException(org.opensearch.OpenSearchException) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput)

Example 3 with BytesStreamOutput

use of org.opensearch.common.io.stream.BytesStreamOutput in project OpenSearch by opensearch-project.

the class SearchContextId method encode.

public static String encode(List<SearchPhaseResult> searchPhaseResults, Map<String, AliasFilter> aliasFilter, Version version) {
    final Map<ShardId, SearchContextIdForNode> shards = new HashMap<>();
    for (SearchPhaseResult searchPhaseResult : searchPhaseResults) {
        final SearchShardTarget target = searchPhaseResult.getSearchShardTarget();
        shards.put(target.getShardId(), new SearchContextIdForNode(target.getClusterAlias(), target.getNodeId(), searchPhaseResult.getContextId()));
    }
    try (BytesStreamOutput out = new BytesStreamOutput()) {
        out.setVersion(version);
        Version.writeVersion(version, out);
        out.writeMap(shards, (o, k) -> k.writeTo(o), (o, v) -> v.writeTo(o));
        out.writeMap(aliasFilter, StreamOutput::writeString, (o, v) -> v.writeTo(o));
        return Base64.getUrlEncoder().encodeToString(BytesReference.toBytes(out.bytes()));
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    }
}
Also used : ShardId(org.opensearch.index.shard.ShardId) HashMap(java.util.HashMap) SearchPhaseResult(org.opensearch.search.SearchPhaseResult) SearchShardTarget(org.opensearch.search.SearchShardTarget) IOException(java.io.IOException) StreamOutput(org.opensearch.common.io.stream.StreamOutput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput)

Example 4 with BytesStreamOutput

use of org.opensearch.common.io.stream.BytesStreamOutput in project OpenSearch by opensearch-project.

the class RoundTripTests method toInputByteStream.

private StreamInput toInputByteStream(Version version, Writeable example) throws IOException {
    BytesStreamOutput out = new BytesStreamOutput();
    out.setVersion(version);
    example.writeTo(out);
    StreamInput in = out.bytes().streamInput();
    in.setVersion(version);
    return in;
}
Also used : StreamInput(org.opensearch.common.io.stream.StreamInput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput)

Example 5 with BytesStreamOutput

use of org.opensearch.common.io.stream.BytesStreamOutput in project OpenSearch by opensearch-project.

the class Streams method readFully.

/**
 * Reads all bytes from the given {@link InputStream} and closes it afterwards.
 */
public static BytesReference readFully(InputStream in) throws IOException {
    BytesStreamOutput out = new BytesStreamOutput();
    org.opensearch.core.internal.io.Streams.copy(in, out);
    return out.bytes();
}
Also used : BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput)

Aggregations

BytesStreamOutput (org.opensearch.common.io.stream.BytesStreamOutput)276 StreamInput (org.opensearch.common.io.stream.StreamInput)140 IOException (java.io.IOException)41 BytesReference (org.opensearch.common.bytes.BytesReference)40 NamedWriteableAwareStreamInput (org.opensearch.common.io.stream.NamedWriteableAwareStreamInput)33 Test (org.junit.Test)31 Version (org.opensearch.Version)29 ArrayList (java.util.ArrayList)27 ReleasableBytesReference (org.opensearch.common.bytes.ReleasableBytesReference)22 NamedWriteableRegistry (org.opensearch.common.io.stream.NamedWriteableRegistry)18 HashMap (java.util.HashMap)16 OpenSearchException (org.opensearch.OpenSearchException)15 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)15 BytesArray (org.opensearch.common.bytes.BytesArray)14 LegacyESVersion (org.opensearch.LegacyESVersion)12 List (java.util.List)11 TimeValue (org.opensearch.common.unit.TimeValue)9 Collections (java.util.Collections)8 HashSet (java.util.HashSet)8 Map (java.util.Map)8