Search in sources :

Example 6 with StreamOutput

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

the class ScriptExceptionTests method testRoundTrip.

/** ensure we can round trip in serialization */
public void testRoundTrip() throws IOException {
    ScriptException e = new ScriptException("messageData", new Exception("causeData"), Arrays.asList("stack1", "stack2"), "sourceData", "langData");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    StreamOutput output = new DataOutputStreamOutput(new DataOutputStream(bytes));
    e.writeTo(output);
    output.close();
    StreamInput input = new InputStreamStreamInput(new ByteArrayInputStream(bytes.toByteArray()));
    ScriptException e2 = new ScriptException(input);
    input.close();
    assertEquals(e.getMessage(), e2.getMessage());
    assertEquals(e.getScriptStack(), e2.getScriptStack());
    assertEquals(e.getScript(), e2.getScript());
    assertEquals(e.getLang(), e2.getLang());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) InputStreamStreamInput(org.elasticsearch.common.io.stream.InputStreamStreamInput) StreamInput(org.elasticsearch.common.io.stream.StreamInput) DataOutputStreamOutput(org.elasticsearch.common.io.stream.DataOutputStreamOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) DataOutputStreamOutput(org.elasticsearch.common.io.stream.DataOutputStreamOutput) IOException(java.io.IOException) InputStreamStreamInput(org.elasticsearch.common.io.stream.InputStreamStreamInput)

Example 7 with StreamOutput

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

the class DeflateCompressTests method doTest.

private void doTest(byte[] bytes) throws IOException {
    ByteBuffer bb = ByteBuffer.wrap(bytes);
    StreamInput rawIn = new ByteBufferStreamInput(bb);
    Compressor c = compressor;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    OutputStreamStreamOutput rawOs = new OutputStreamStreamOutput(bos);
    StreamOutput os = c.streamOutput(rawOs);
    Random r = random();
    int bufferSize = r.nextBoolean() ? 65535 : TestUtil.nextInt(random(), 1, 70000);
    int prepadding = r.nextInt(70000);
    int postpadding = r.nextInt(70000);
    byte[] buffer = new byte[prepadding + bufferSize + postpadding];
    // fill block completely with junk
    r.nextBytes(buffer);
    int len;
    while ((len = rawIn.read(buffer, prepadding, bufferSize)) != -1) {
        os.write(buffer, prepadding, len);
    }
    os.close();
    rawIn.close();
    // now we have compressed byte array
    byte[] compressed = bos.toByteArray();
    ByteBuffer bb2 = ByteBuffer.wrap(compressed);
    StreamInput compressedIn = new ByteBufferStreamInput(bb2);
    StreamInput in = c.streamInput(compressedIn);
    // randomize constants again
    bufferSize = r.nextBoolean() ? 65535 : TestUtil.nextInt(random(), 1, 70000);
    prepadding = r.nextInt(70000);
    postpadding = r.nextInt(70000);
    buffer = new byte[prepadding + bufferSize + postpadding];
    // fill block completely with junk
    r.nextBytes(buffer);
    ByteArrayOutputStream uncompressedOut = new ByteArrayOutputStream();
    while ((len = in.read(buffer, prepadding, bufferSize)) != -1) {
        uncompressedOut.write(buffer, prepadding, len);
    }
    uncompressedOut.close();
    assertArrayEquals(bytes, uncompressedOut.toByteArray());
}
Also used : ByteBufferStreamInput(org.elasticsearch.common.io.stream.ByteBufferStreamInput) OutputStreamStreamOutput(org.elasticsearch.common.io.stream.OutputStreamStreamOutput) Random(java.util.Random) ByteBufferStreamInput(org.elasticsearch.common.io.stream.ByteBufferStreamInput) StreamInput(org.elasticsearch.common.io.stream.StreamInput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) OutputStreamStreamOutput(org.elasticsearch.common.io.stream.OutputStreamStreamOutput) ByteBuffer(java.nio.ByteBuffer)

Example 8 with StreamOutput

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

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

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

the class SpanMultiTermQueryBuilderTests method testUnsupportedInnerQueryType.

/**
     * test checks that we throw an {@link UnsupportedOperationException} if the query wrapped
     * by {@link SpanMultiTermQueryBuilder} does not generate a lucene {@link MultiTermQuery}.
     * This is currently the case for {@link RangeQueryBuilder} when the target field is mapped
     * to a date.
     */
public void testUnsupportedInnerQueryType() throws IOException {
    MultiTermQueryBuilder query = new MultiTermQueryBuilder() {

        @Override
        public Query toQuery(QueryShardContext context) throws IOException {
            return new TermQuery(new Term("foo", "bar"));
        }

        @Override
        public Query toFilter(QueryShardContext context) throws IOException {
            return toQuery(context);
        }

        @Override
        public QueryBuilder queryName(String queryName) {
            return this;
        }

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

        @Override
        public float boost() {
            return 1f;
        }

        @Override
        public QueryBuilder boost(float boost) {
            return this;
        }

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

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

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

        @Override
        public void writeTo(StreamOutput out) throws IOException {
        }
    };
    SpanMultiTermQueryBuilder spamMultiTermQuery = new SpanMultiTermQueryBuilder(query);
    UnsupportedOperationException e = expectThrows(UnsupportedOperationException.class, () -> spamMultiTermQuery.toQuery(createShardContext()));
    assertThat(e.getMessage(), containsString("unsupported inner query, should be " + MultiTermQuery.class.getName()));
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) Term(org.apache.lucene.index.Term) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Aggregations

StreamOutput (org.elasticsearch.common.io.stream.StreamOutput)18 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)8 OutputStreamStreamOutput (org.elasticsearch.common.io.stream.OutputStreamStreamOutput)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 CrateUnitTest (io.crate.test.integration.CrateUnitTest)6 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InputStreamStreamInput (org.elasticsearch.common.io.stream.InputStreamStreamInput)6 Test (org.junit.Test)6 BytesReference (org.elasticsearch.common.bytes.BytesReference)5 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)4 IOException (java.io.IOException)3 StreamInput (org.elasticsearch.common.io.stream.StreamInput)3 ThreadPools (io.crate.monitor.ThreadPools)2 CompositeBytesReference (org.elasticsearch.common.bytes.CompositeBytesReference)2 ReleasableBytesStreamOutput (org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput)2 Releasable (org.elasticsearch.common.lease.Releasable)2 AbstractLifecycleRunnable (org.elasticsearch.common.util.concurrent.AbstractLifecycleRunnable)2 AbstractRunnable (org.elasticsearch.common.util.concurrent.AbstractRunnable)2 DataOutputStream (java.io.DataOutputStream)1 ByteBuffer (java.nio.ByteBuffer)1