Search in sources :

Example 51 with BytesStreamOutput

use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.

the class ThreadPoolStatsTests method testThreadPoolStatsToXContent.

public void testThreadPoolStatsToXContent() throws IOException {
    try (BytesStreamOutput os = new BytesStreamOutput()) {
        List<ThreadPoolStats.Stats> stats = new ArrayList<>();
        stats.add(new ThreadPoolStats.Stats(ThreadPool.Names.SEARCH, -1, 0, 0, 0, 0, 0L));
        stats.add(new ThreadPoolStats.Stats(ThreadPool.Names.WARMER, -1, 0, 0, 0, 0, 0L));
        stats.add(new ThreadPoolStats.Stats(ThreadPool.Names.GENERIC, -1, 0, 0, 0, 0, 0L));
        stats.add(new ThreadPoolStats.Stats(ThreadPool.Names.FORCE_MERGE, -1, 0, 0, 0, 0, 0L));
        stats.add(new ThreadPoolStats.Stats(ThreadPool.Names.SAME, -1, 0, 0, 0, 0, 0L));
        ThreadPoolStats threadPoolStats = new ThreadPoolStats(stats);
        try (XContentBuilder builder = new XContentBuilder(XContentType.JSON.xContent(), os)) {
            builder.startObject();
            threadPoolStats.toXContent(builder, ToXContent.EMPTY_PARAMS);
            builder.endObject();
        }
        try (XContentParser parser = createParser(JsonXContent.jsonXContent, os.bytes())) {
            XContentParser.Token token = parser.currentToken();
            assertNull(token);
            token = parser.nextToken();
            assertThat(token, equalTo(XContentParser.Token.START_OBJECT));
            token = parser.nextToken();
            assertThat(token, equalTo(XContentParser.Token.FIELD_NAME));
            assertThat(parser.currentName(), equalTo(ThreadPoolStats.Fields.THREAD_POOL));
            token = parser.nextToken();
            assertThat(token, equalTo(XContentParser.Token.START_OBJECT));
            token = parser.nextToken();
            assertThat(token, equalTo(XContentParser.Token.FIELD_NAME));
            List<String> names = new ArrayList<>();
            while (token == XContentParser.Token.FIELD_NAME) {
                names.add(parser.currentName());
                token = parser.nextToken();
                assertThat(token, equalTo(XContentParser.Token.START_OBJECT));
                parser.skipChildren();
                token = parser.nextToken();
            }
            assertThat(names, contains(ThreadPool.Names.FORCE_MERGE, ThreadPool.Names.GENERIC, ThreadPool.Names.SAME, ThreadPool.Names.SEARCH, ThreadPool.Names.WARMER));
        }
    }
}
Also used : ArrayList(java.util.ArrayList) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 52 with BytesStreamOutput

use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.

the class RoundTripTests method roundTrip.

private void roundTrip(Version version, Streamable example, Streamable empty) throws IOException {
    BytesStreamOutput out = new BytesStreamOutput();
    out.setVersion(version);
    example.writeTo(out);
    StreamInput in = out.bytes().streamInput();
    in.setVersion(version);
    empty.readFrom(in);
}
Also used : StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 53 with BytesStreamOutput

use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.

the class FieldStatsTests method assertSerialization.

private void assertSerialization(FieldStats stats, Version version) throws IOException {
    BytesStreamOutput output = new BytesStreamOutput();
    output.setVersion(version);
    stats.writeTo(output);
    output.flush();
    StreamInput input = output.bytes().streamInput();
    input.setVersion(version);
    FieldStats deserializedStats = FieldStats.readFrom(input);
    assertEquals(stats, deserializedStats);
    assertEquals(stats.hashCode(), deserializedStats.hashCode());
}
Also used : StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) FieldStats(org.elasticsearch.action.fieldstats.FieldStats)

Example 54 with BytesStreamOutput

use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.

the class BinaryFieldMapperTests method testStoredValue.

public void testStoredValue() throws IOException {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", "binary").field("store", true).endObject().endObject().endObject().endObject().string();
    DocumentMapper mapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));
    // case 1: a simple binary value
    final byte[] binaryValue1 = new byte[100];
    binaryValue1[56] = 1;
    // case 2: a value that looks compressed: this used to fail in 1.x
    BytesStreamOutput out = new BytesStreamOutput();
    try (StreamOutput compressed = CompressorFactory.COMPRESSOR.streamOutput(out)) {
        new BytesArray(binaryValue1).writeTo(compressed);
    }
    final byte[] binaryValue2 = BytesReference.toBytes(out.bytes());
    assertTrue(CompressorFactory.isCompressed(new BytesArray(binaryValue2)));
    for (byte[] value : Arrays.asList(binaryValue1, binaryValue2)) {
        ParsedDocument doc = mapper.parse("test", "type", "id", XContentFactory.jsonBuilder().startObject().field("field", value).endObject().bytes());
        BytesRef indexedValue = doc.rootDoc().getBinaryValue("field");
        assertEquals(new BytesRef(value), indexedValue);
        FieldMapper fieldMapper = mapper.mappers().smartNameFieldMapper("field");
        Object originalValue = fieldMapper.fieldType().valueForDisplay(indexedValue);
        assertEquals(new BytesArray(value), originalValue);
    }
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString) StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) BytesRef(org.apache.lucene.util.BytesRef)

Example 55 with BytesStreamOutput

use of org.elasticsearch.common.io.stream.BytesStreamOutput in project elasticsearch by elastic.

the class InnerHitBuilderTests method testSerializationOrder.

/**
     * Test that if we serialize and deserialize an object, further
     * serialization leads to identical bytes representation.
     *
     * This is necessary to ensure because we use the serialized BytesReference
     * of this builder as part of the cacheKey in
     * {@link ShardSearchLocalRequest} (via
     * {@link SearchSourceBuilder#collapse(org.elasticsearch.search.collapse.CollapseBuilder)})
     */
public void testSerializationOrder() throws Exception {
    for (int runs = 0; runs < NUMBER_OF_TESTBUILDERS; runs++) {
        InnerHitBuilder original = randomInnerHits();
        InnerHitBuilder deserialized = serializedCopy(original);
        assertEquals(deserialized, original);
        assertEquals(deserialized.hashCode(), original.hashCode());
        assertNotSame(deserialized, original);
        BytesStreamOutput out1 = new BytesStreamOutput();
        BytesStreamOutput out2 = new BytesStreamOutput();
        original.writeTo(out1);
        deserialized.writeTo(out2);
        assertEquals(out1.bytes(), out2.bytes());
    }
}
Also used : BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Aggregations

BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)222 StreamInput (org.elasticsearch.common.io.stream.StreamInput)147 Test (org.junit.Test)45 CrateUnitTest (io.crate.test.integration.CrateUnitTest)36 NamedWriteableAwareStreamInput (org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput)24 IOException (java.io.IOException)21 BytesArray (org.elasticsearch.common.bytes.BytesArray)21 BytesReference (org.elasticsearch.common.bytes.BytesReference)18 Version (org.elasticsearch.Version)15 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)14 ArrayList (java.util.ArrayList)12 BytesRef (org.apache.lucene.util.BytesRef)11 Map (java.util.Map)10 UUID (java.util.UUID)9 Symbol (io.crate.analyze.symbol.Symbol)8 HashMap (java.util.HashMap)8 NamedWriteableRegistry (org.elasticsearch.common.io.stream.NamedWriteableRegistry)8 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)7 StreamOutput (org.elasticsearch.common.io.stream.StreamOutput)7 List (java.util.List)6