Search in sources :

Example 1 with StreamOutput

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

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

the class PublicationTransportHandler method serializeDiffClusterState.

private static BytesReference serializeDiffClusterState(Diff<ClusterState> diff, Version nodeVersion) throws IOException {
    final BytesStreamOutput bStream = new BytesStreamOutput();
    try (StreamOutput stream = new OutputStreamStreamOutput(CompressorFactory.COMPRESSOR.threadLocalOutputStream(bStream))) {
        stream.setVersion(nodeVersion);
        stream.writeBoolean(false);
        diff.writeTo(stream);
    }
    return bStream.bytes();
}
Also used : OutputStreamStreamOutput(org.opensearch.common.io.stream.OutputStreamStreamOutput) StreamOutput(org.opensearch.common.io.stream.StreamOutput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) OutputStreamStreamOutput(org.opensearch.common.io.stream.OutputStreamStreamOutput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput)

Example 3 with StreamOutput

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

the class SearchHit method writeTo.

@Override
public void writeTo(StreamOutput out) throws IOException {
    out.writeFloat(score);
    out.writeOptionalText(id);
    if (out.getVersion().before(Version.V_2_0_0)) {
        out.writeOptionalText(SINGLE_MAPPING_TYPE);
    }
    out.writeOptionalWriteable(nestedIdentity);
    out.writeLong(version);
    out.writeZLong(seqNo);
    out.writeVLong(primaryTerm);
    out.writeBytesReference(source);
    if (explanation == null) {
        out.writeBoolean(false);
    } else {
        out.writeBoolean(true);
        writeExplanation(out, explanation);
    }
    if (out.getVersion().onOrAfter(LegacyESVersion.V_7_8_0)) {
        out.writeMap(documentFields, StreamOutput::writeString, (stream, documentField) -> documentField.writeTo(stream));
        out.writeMap(metaFields, StreamOutput::writeString, (stream, documentField) -> documentField.writeTo(stream));
    } else {
        writeFields(out, this.getFields());
    }
    if (highlightFields == null) {
        out.writeVInt(0);
    } else {
        out.writeVInt(highlightFields.size());
        for (HighlightField highlightField : highlightFields.values()) {
            highlightField.writeTo(out);
        }
    }
    sortValues.writeTo(out);
    if (matchedQueries.length == 0) {
        out.writeVInt(0);
    } else {
        out.writeVInt(matchedQueries.length);
        for (String matchedFilter : matchedQueries) {
            out.writeString(matchedFilter);
        }
    }
    out.writeOptionalWriteable(shard);
    if (innerHits == null) {
        out.writeVInt(0);
    } else {
        out.writeVInt(innerHits.size());
        for (Map.Entry<String, SearchHits> entry : innerHits.entrySet()) {
            out.writeString(entry.getKey());
            entry.getValue().writeTo(out);
        }
    }
}
Also used : HighlightField(org.opensearch.search.fetch.subphase.highlight.HighlightField) StreamOutput(org.opensearch.common.io.stream.StreamOutput) Map(java.util.Map) HashMap(java.util.HashMap) Collections.singletonMap(java.util.Collections.singletonMap) Collections.emptyMap(java.util.Collections.emptyMap) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap)

Example 4 with StreamOutput

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

the class BucketHelpersTests method testReturnMultiValueObject.

public void testReturnMultiValueObject() {
    MultiBucketsAggregation agg = new MultiBucketsAggregation() {

        @Override
        public List<? extends Bucket> getBuckets() {
            return null;
        }

        @Override
        public String getName() {
            return "foo";
        }

        @Override
        public String getType() {
            return null;
        }

        @Override
        public Map<String, Object> getMetadata() {
            return null;
        }

        @Override
        public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
            return null;
        }
    };
    InternalMultiBucketAggregation.InternalBucket bucket = new InternalMultiBucketAggregation.InternalBucket() {

        @Override
        public void writeTo(StreamOutput out) throws IOException {
        }

        @Override
        public Object getKey() {
            return null;
        }

        @Override
        public String getKeyAsString() {
            return null;
        }

        @Override
        public long getDocCount() {
            return 0;
        }

        @Override
        public Aggregations getAggregations() {
            return null;
        }

        @Override
        public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
            return null;
        }

        @Override
        public Object getProperty(String containingAggName, List<String> path) {
            return mock(InternalTDigestPercentiles.class);
        }
    };
    AggregationExecutionException e = expectThrows(AggregationExecutionException.class, () -> BucketHelpers.resolveBucketValue(agg, bucket, "foo>bar", BucketHelpers.GapPolicy.SKIP));
    assertThat(e.getMessage(), equalTo("buckets_path must reference either a number value or a single value numeric " + "metric aggregation, but [foo] contains multiple values. Please specify which to use."));
}
Also used : MultiBucketsAggregation(org.opensearch.search.aggregations.bucket.MultiBucketsAggregation) List(java.util.List) InternalMultiBucketAggregation(org.opensearch.search.aggregations.InternalMultiBucketAggregation) StreamOutput(org.opensearch.common.io.stream.StreamOutput) AggregationExecutionException(org.opensearch.search.aggregations.AggregationExecutionException) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 5 with StreamOutput

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

the class TransportDecompressorTests method testIncrementalMultiPageCompression.

public void testIncrementalMultiPageCompression() throws IOException {
    try (BytesStreamOutput output = new BytesStreamOutput()) {
        try (StreamOutput deflateStream = new OutputStreamStreamOutput(CompressorFactory.COMPRESSOR.threadLocalOutputStream(Streams.flushOnCloseStream(output)))) {
            for (int i = 0; i < 10000; ++i) {
                deflateStream.writeInt(i);
            }
        }
        BytesReference bytes = output.bytes();
        TransportDecompressor decompressor = new TransportDecompressor(PageCacheRecycler.NON_RECYCLING_INSTANCE);
        int split1 = (int) (bytes.length() * 0.3);
        int split2 = (int) (bytes.length() * 0.65);
        BytesReference inbound1 = bytes.slice(0, split1);
        BytesReference inbound2 = bytes.slice(split1, split2 - split1);
        BytesReference inbound3 = bytes.slice(split2, bytes.length() - split2);
        int bytesConsumed1 = decompressor.decompress(inbound1);
        assertEquals(inbound1.length(), bytesConsumed1);
        assertFalse(decompressor.isEOS());
        int bytesConsumed2 = decompressor.decompress(inbound2);
        assertEquals(inbound2.length(), bytesConsumed2);
        assertFalse(decompressor.isEOS());
        int bytesConsumed3 = decompressor.decompress(inbound3);
        assertEquals(inbound3.length(), bytesConsumed3);
        assertTrue(decompressor.isEOS());
        ReleasableBytesReference reference1 = decompressor.pollDecompressedPage();
        ReleasableBytesReference reference2 = decompressor.pollDecompressedPage();
        ReleasableBytesReference reference3 = decompressor.pollDecompressedPage();
        assertNull(decompressor.pollDecompressedPage());
        BytesReference composite = CompositeBytesReference.of(reference1, reference2, reference3);
        assertEquals(4 * 10000, composite.length());
        StreamInput streamInput = composite.streamInput();
        for (int i = 0; i < 10000; ++i) {
            assertEquals(i, streamInput.readInt());
        }
        Releasables.close(reference1, reference2, reference3);
    }
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) CompositeBytesReference(org.opensearch.common.bytes.CompositeBytesReference) ReleasableBytesReference(org.opensearch.common.bytes.ReleasableBytesReference) ReleasableBytesReference(org.opensearch.common.bytes.ReleasableBytesReference) OutputStreamStreamOutput(org.opensearch.common.io.stream.OutputStreamStreamOutput) StreamInput(org.opensearch.common.io.stream.StreamInput) StreamOutput(org.opensearch.common.io.stream.StreamOutput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) OutputStreamStreamOutput(org.opensearch.common.io.stream.OutputStreamStreamOutput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput)

Aggregations

StreamOutput (org.opensearch.common.io.stream.StreamOutput)26 BytesStreamOutput (org.opensearch.common.io.stream.BytesStreamOutput)19 Test (org.junit.Test)14 IOException (java.io.IOException)11 ActionRequest (org.opensearch.action.ActionRequest)10 UncheckedIOException (java.io.UncheckedIOException)7 ActionResponse (org.opensearch.action.ActionResponse)4 OutputStreamStreamOutput (org.opensearch.common.io.stream.OutputStreamStreamOutput)4 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)4 HashMap (java.util.HashMap)3 BytesReference (org.opensearch.common.bytes.BytesReference)3 StreamInput (org.opensearch.common.io.stream.StreamInput)3 List (java.util.List)2 CompositeBytesReference (org.opensearch.common.bytes.CompositeBytesReference)2 ReleasableBytesReference (org.opensearch.common.bytes.ReleasableBytesReference)2 MLTaskResponse (org.opensearch.ml.common.transport.MLTaskResponse)2 AggregationExecutionException (org.opensearch.search.aggregations.AggregationExecutionException)2 InternalMultiBucketAggregation (org.opensearch.search.aggregations.InternalMultiBucketAggregation)2 MultiBucketsAggregation (org.opensearch.search.aggregations.bucket.MultiBucketsAggregation)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1