Search in sources :

Example 1 with StreamInput

use of org.opensearch.common.io.stream.StreamInput 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 StreamInput

use of org.opensearch.common.io.stream.StreamInput 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 StreamInput

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

the class PercolatorFieldMapperTests method testImplicitlySetDefaultScriptLang.

public void testImplicitlySetDefaultScriptLang() throws Exception {
    addQueryFieldMappings();
    XContentBuilder query = jsonBuilder();
    query.startObject();
    query.startObject("script");
    if (randomBoolean()) {
        query.field("script", "return true");
    } else {
        query.startObject("script");
        query.field("source", "return true");
        query.endObject();
    }
    query.endObject();
    query.endObject();
    ParsedDocument doc = mapperService.documentMapper("doc").parse(new SourceToParse("test", "doc", "1", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().rawField(fieldName, new BytesArray(Strings.toString(query)).streamInput(), query.contentType()).endObject()), XContentType.JSON));
    BytesRef querySource = doc.rootDoc().getFields(fieldType.queryBuilderField.name())[0].binaryValue();
    try (InputStream in = new ByteArrayInputStream(querySource.bytes, querySource.offset, querySource.length)) {
        try (StreamInput input = new NamedWriteableAwareStreamInput(new InputStreamStreamInput(in), writableRegistry())) {
            // Query builder's content is stored via BinaryFieldMapper, which has a custom encoding
            // to encode multiple binary values into a single binary doc values field.
            // This is the reason we need to first need to read the number of values and
            // then the length of the field value in bytes.
            input.readVInt();
            input.readVInt();
            ScriptQueryBuilder queryBuilder = (ScriptQueryBuilder) input.readNamedWriteable(QueryBuilder.class);
            assertEquals(Script.DEFAULT_SCRIPT_LANG, queryBuilder.script().getLang());
        }
    }
    query = jsonBuilder();
    query.startObject();
    query.startObject("function_score");
    query.startArray("functions");
    query.startObject();
    query.startObject("script_score");
    if (randomBoolean()) {
        query.field("script", "return true");
    } else {
        query.startObject("script");
        query.field("source", "return true");
        query.endObject();
    }
    query.endObject();
    query.endObject();
    query.endArray();
    query.endObject();
    query.endObject();
    doc = mapperService.documentMapper("doc").parse(new SourceToParse("test", "doc", "1", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().rawField(fieldName, new BytesArray(Strings.toString(query)).streamInput(), query.contentType()).endObject()), XContentType.JSON));
    querySource = doc.rootDoc().getFields(fieldType.queryBuilderField.name())[0].binaryValue();
    try (InputStream in = new ByteArrayInputStream(querySource.bytes, querySource.offset, querySource.length)) {
        try (StreamInput input = new NamedWriteableAwareStreamInput(new InputStreamStreamInput(in), writableRegistry())) {
            input.readVInt();
            input.readVInt();
            FunctionScoreQueryBuilder queryBuilder = (FunctionScoreQueryBuilder) input.readNamedWriteable(QueryBuilder.class);
            ScriptScoreFunctionBuilder function = (ScriptScoreFunctionBuilder) queryBuilder.filterFunctionBuilders()[0].getScoreFunction();
            assertEquals(Script.DEFAULT_SCRIPT_LANG, function.getScript().getLang());
        }
    }
}
Also used : BytesArray(org.opensearch.common.bytes.BytesArray) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SourceToParse(org.opensearch.index.mapper.SourceToParse) BoostingQueryBuilder(org.opensearch.index.query.BoostingQueryBuilder) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) HasChildQueryBuilder(org.opensearch.join.query.HasChildQueryBuilder) ConstantScoreQueryBuilder(org.opensearch.index.query.ConstantScoreQueryBuilder) FunctionScoreQueryBuilder(org.opensearch.index.query.functionscore.FunctionScoreQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) ScriptQueryBuilder(org.opensearch.index.query.ScriptQueryBuilder) DisMaxQueryBuilder(org.opensearch.index.query.DisMaxQueryBuilder) RangeQueryBuilder(org.opensearch.index.query.RangeQueryBuilder) HasParentQueryBuilder(org.opensearch.join.query.HasParentQueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) ScriptQueryBuilder(org.opensearch.index.query.ScriptQueryBuilder) ScriptScoreFunctionBuilder(org.opensearch.index.query.functionscore.ScriptScoreFunctionBuilder) FunctionScoreQueryBuilder(org.opensearch.index.query.functionscore.FunctionScoreQueryBuilder) ParsedDocument(org.opensearch.index.mapper.ParsedDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) InputStreamStreamInput(org.opensearch.common.io.stream.InputStreamStreamInput) StreamInput(org.opensearch.common.io.stream.StreamInput) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) BytesRef(org.apache.lucene.util.BytesRef) InputStreamStreamInput(org.opensearch.common.io.stream.InputStreamStreamInput)

Example 4 with StreamInput

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

the class PainlessExecuteRequestTests method testFromXContent.

// Testing XContent serialization manually here, because the xContentType field in ContextSetup determines
// how the request needs to parse and the xcontent serialization framework randomizes that. The XContentType
// is not known and accessable when the test request instance is created in the xcontent serialization framework.
// Changing that is a big change. Writing a custom xcontent test here is the best option for now, because as far
// as I know this request class is the only case where this is a problem.
public final void testFromXContent() throws Exception {
    for (int i = 0; i < 20; i++) {
        PainlessExecuteAction.Request testInstance = createTestInstance();
        ContextSetup contextSetup = testInstance.getContextSetup();
        XContent xContent = randomFrom(XContentType.values()).xContent();
        if (contextSetup != null && contextSetup.getXContentType() != null) {
            xContent = contextSetup.getXContentType().xContent();
        }
        try (XContentBuilder builder = XContentBuilder.builder(xContent)) {
            builder.value(testInstance);
            StreamInput instanceInput = BytesReference.bytes(builder).streamInput();
            try (XContentParser parser = xContent.createParser(xContentRegistry(), LoggingDeprecationHandler.INSTANCE, instanceInput)) {
                PainlessExecuteAction.Request result = PainlessExecuteAction.Request.parse(parser);
                assertThat(result, equalTo(testInstance));
            }
        }
    }
}
Also used : ContextSetup(org.opensearch.painless.action.PainlessExecuteAction.Request.ContextSetup) XContent(org.opensearch.common.xcontent.XContent) StreamInput(org.opensearch.common.io.stream.StreamInput) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 5 with StreamInput

use of org.opensearch.common.io.stream.StreamInput 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)

Aggregations

StreamInput (org.opensearch.common.io.stream.StreamInput)206 BytesStreamOutput (org.opensearch.common.io.stream.BytesStreamOutput)139 IOException (java.io.IOException)42 NamedWriteableAwareStreamInput (org.opensearch.common.io.stream.NamedWriteableAwareStreamInput)40 Version (org.opensearch.Version)27 OpenSearchException (org.opensearch.OpenSearchException)21 InputStreamStreamInput (org.opensearch.common.io.stream.InputStreamStreamInput)19 BytesReference (org.opensearch.common.bytes.BytesReference)18 Matchers.containsString (org.hamcrest.Matchers.containsString)17 Matchers.hasToString (org.hamcrest.Matchers.hasToString)17 NamedWriteableRegistry (org.opensearch.common.io.stream.NamedWriteableRegistry)17 UncheckedIOException (java.io.UncheckedIOException)15 CountDownLatch (java.util.concurrent.CountDownLatch)14 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)13 ArrayList (java.util.ArrayList)12 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 UnknownHostException (java.net.UnknownHostException)11 ExecutionException (java.util.concurrent.ExecutionException)11