Search in sources :

Example 76 with BytesStreamOutput

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

the class AbstractStreamableTestCase method copyInstance.

/**
     * Round trip {@code instance} through binary serialization, setting the wire compatibility version to {@code version}.
     */
protected T copyInstance(T instance, Version version) throws IOException {
    try (BytesStreamOutput output = new BytesStreamOutput()) {
        output.setVersion(version);
        instance.writeTo(output);
        try (StreamInput in = new NamedWriteableAwareStreamInput(output.bytes().streamInput(), getNamedWriteableRegistry())) {
            in.setVersion(version);
            T newInstance = createBlankInstance();
            newInstance.readFrom(in);
            return newInstance;
        }
    }
}
Also used : NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) StreamInput(org.elasticsearch.common.io.stream.StreamInput) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 77 with BytesStreamOutput

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

the class PriorityTests method testSerialization.

public void testSerialization() throws IOException {
    for (Priority p : Priority.values()) {
        BytesStreamOutput out = new BytesStreamOutput();
        Priority.writeTo(p, out);
        Priority priority = Priority.readFrom(out.bytes().streamInput());
        assertSame(p, priority);
    }
    assertSame(Priority.IMMEDIATE, Priority.fromByte((byte) 0));
    assertSame(Priority.HIGH, Priority.fromByte((byte) 2));
    assertSame(Priority.LANGUID, Priority.fromByte((byte) 5));
    assertSame(Priority.LOW, Priority.fromByte((byte) 4));
    assertSame(Priority.NORMAL, Priority.fromByte((byte) 3));
    assertSame(Priority.URGENT, Priority.fromByte((byte) 1));
    assertEquals(6, Priority.values().length);
}
Also used : BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 78 with BytesStreamOutput

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

the class CompositeBytesReferenceTests method testCompositeBuffer.

public void testCompositeBuffer() throws IOException {
    List<BytesReference> referenceList = newRefList(randomIntBetween(1, PAGE_SIZE * 2));
    BytesReference ref = new CompositeBytesReference(referenceList.toArray(new BytesReference[0]));
    BytesRefIterator iterator = ref.iterator();
    BytesRefBuilder builder = new BytesRefBuilder();
    for (BytesReference reference : referenceList) {
        // sometimes we have a paged ref - pull an iter and walk all pages!
        BytesRefIterator innerIter = reference.iterator();
        BytesRef scratch;
        while ((scratch = innerIter.next()) != null) {
            BytesRef next = iterator.next();
            assertNotNull(next);
            assertEquals(next, scratch);
            builder.append(next);
        }
    }
    assertNull(iterator.next());
    int offset = 0;
    for (BytesReference reference : referenceList) {
        assertEquals(reference, ref.slice(offset, reference.length()));
        int probes = randomIntBetween(Math.min(10, reference.length()), reference.length());
        for (int i = 0; i < probes; i++) {
            int index = randomIntBetween(0, reference.length() - 1);
            assertEquals(ref.get(offset + index), reference.get(index));
        }
        offset += reference.length();
    }
    BytesArray array = new BytesArray(builder.toBytesRef());
    assertEquals(array, ref);
    assertEquals(array.hashCode(), ref.hashCode());
    BytesStreamOutput output = new BytesStreamOutput();
    ref.writeTo(output);
    assertEquals(array, output.bytes());
}
Also used : BytesRefIterator(org.apache.lucene.util.BytesRefIterator) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) BytesRef(org.apache.lucene.util.BytesRef) ReleasableBytesStreamOutput(org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 79 with BytesStreamOutput

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

the class DeflateCompressedXContentTests method testDifferentCompressedRepresentation.

public void testDifferentCompressedRepresentation() throws Exception {
    byte[] b = "---\nf:abcdefghijabcdefghij".getBytes("UTF-8");
    BytesStreamOutput bout = new BytesStreamOutput();
    StreamOutput out = compressor.streamOutput(bout);
    out.writeBytes(b);
    out.flush();
    out.writeBytes(b);
    out.close();
    final BytesReference b1 = bout.bytes();
    bout = new BytesStreamOutput();
    out = compressor.streamOutput(bout);
    out.writeBytes(b);
    out.writeBytes(b);
    out.close();
    final BytesReference b2 = bout.bytes();
    // because of the intermediate flush, the two compressed representations
    // are different. It can also happen for other reasons like if hash tables
    // of different size are being used
    assertFalse(b1.equals(b2));
    // we used the compressed representation directly and did not recompress
    assertArrayEquals(BytesReference.toBytes(b1), new CompressedXContent(b1).compressed());
    assertArrayEquals(BytesReference.toBytes(b2), new CompressedXContent(b2).compressed());
    // but compressedstring instances are still equal
    assertEquals(new CompressedXContent(b1), new CompressedXContent(b2));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 80 with BytesStreamOutput

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

the class GeoDistanceTests method testGeoDistanceSerialization.

public void testGeoDistanceSerialization() throws IOException {
    // make sure that ordinals don't change, because we rely on then in serialization
    assertThat(GeoDistance.PLANE.ordinal(), equalTo(0));
    assertThat(GeoDistance.ARC.ordinal(), equalTo(1));
    assertThat(GeoDistance.values().length, equalTo(2));
    GeoDistance geoDistance = randomFrom(GeoDistance.PLANE, GeoDistance.ARC);
    try (BytesStreamOutput out = new BytesStreamOutput()) {
        geoDistance.writeTo(out);
        try (StreamInput in = out.bytes().streamInput()) {
            ;
            GeoDistance copy = GeoDistance.readFromStream(in);
            assertEquals(copy.toString() + " vs. " + geoDistance.toString(), copy, geoDistance);
        }
    }
}
Also used : StreamInput(org.elasticsearch.common.io.stream.StreamInput) 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