Search in sources :

Example 91 with BytesArray

use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.

the class BytesStreamsTests method testVLong.

public void testVLong() throws IOException {
    final long value = randomLong();
    {
        // Read works for positive and negative numbers
        BytesStreamOutput output = new BytesStreamOutput();
        // Use NoCheck variant so we can write negative numbers
        output.writeVLongNoCheck(value);
        StreamInput input = output.bytes().streamInput();
        assertEquals(value, input.readVLong());
    }
    if (value < 0) {
        // Write doesn't work for negative numbers
        BytesStreamOutput output = new BytesStreamOutput();
        Exception e = expectThrows(IllegalStateException.class, () -> output.writeVLong(value));
        assertEquals("Negative longs unsupported, use writeLong or writeZLong for negative numbers [" + value + "]", e.getMessage());
    }
    assertTrue("If we're not compatible with 5.1.1 we can drop the assertion below", Version.CURRENT.minimumCompatibilityVersion().onOrBefore(Version.V_5_1_1_UNRELEASED));
    /* Read -1 as serialized by a version of Elasticsearch that supported writing negative numbers with writeVLong. Note that this
         * should be the same test as the first case (when value is negative) but we've kept some bytes so no matter what we do to
         * writeVLong in the future we can be sure we can read bytes as written by Elasticsearch before 5.1.2 */
    StreamInput in = new BytesArray(Base64.getDecoder().decode("////////////AQAAAAAAAA==")).streamInput();
    assertEquals(-1, in.readVLong());
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) IOException(java.io.IOException) EOFException(java.io.EOFException)

Example 92 with BytesArray

use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.

the class StreamTests method testSpecificVLongSerialization.

public void testSpecificVLongSerialization() throws IOException {
    List<Tuple<Long, byte[]>> values = Arrays.asList(new Tuple<>(0L, new byte[] { 0 }), new Tuple<>(-1L, new byte[] { 1 }), new Tuple<>(1L, new byte[] { 2 }), new Tuple<>(-2L, new byte[] { 3 }), new Tuple<>(2L, new byte[] { 4 }), new Tuple<>(Long.MIN_VALUE, new byte[] { -1, -1, -1, -1, -1, -1, -1, -1, -1, 1 }), new Tuple<>(Long.MAX_VALUE, new byte[] { -2, -1, -1, -1, -1, -1, -1, -1, -1, 1 }));
    for (Tuple<Long, byte[]> value : values) {
        BytesStreamOutput out = new BytesStreamOutput();
        out.writeZLong(value.v1());
        assertArrayEquals(Long.toString(value.v1()), value.v2(), BytesReference.toBytes(out.bytes()));
        BytesReference bytes = new BytesArray(value.v2());
        assertEquals(Arrays.toString(value.v2()), (long) value.v1(), bytes.streamInput().readZLong());
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) BytesArray(org.elasticsearch.common.bytes.BytesArray) Tuple(org.elasticsearch.common.collect.Tuple)

Example 93 with BytesArray

use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.

the class StreamTests method testBooleanSerialization.

public void testBooleanSerialization() throws IOException {
    final BytesStreamOutput output = new BytesStreamOutput();
    output.writeBoolean(false);
    output.writeBoolean(true);
    final BytesReference bytesReference = output.bytes();
    final BytesRef bytesRef = bytesReference.toBytesRef();
    assertThat(bytesRef.length, equalTo(2));
    final byte[] bytes = bytesRef.bytes;
    assertThat(bytes[0], equalTo((byte) 0));
    assertThat(bytes[1], equalTo((byte) 1));
    final StreamInput input = bytesReference.streamInput();
    assertFalse(input.readBoolean());
    assertTrue(input.readBoolean());
    final Set<Byte> set = IntStream.range(Byte.MIN_VALUE, Byte.MAX_VALUE).mapToObj(v -> (byte) v).collect(Collectors.toSet());
    set.remove((byte) 0);
    set.remove((byte) 1);
    final byte[] corruptBytes = new byte[] { randomFrom(set) };
    final BytesReference corrupt = new BytesArray(corruptBytes);
    final IllegalStateException e = expectThrows(IllegalStateException.class, () -> corrupt.streamInput().readBoolean());
    final String message = String.format(Locale.ROOT, "unexpected byte [0x%02x]", corruptBytes[0]);
    assertThat(e, hasToString(containsString(message)));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) IntStream(java.util.stream.IntStream) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Arrays(java.util.Arrays) BytesRef(org.apache.lucene.util.BytesRef) Set(java.util.Set) IOException(java.io.IOException) BytesReference(org.elasticsearch.common.bytes.BytesReference) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) BytesArray(org.elasticsearch.common.bytes.BytesArray) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) ByteArrayInputStream(java.io.ByteArrayInputStream) Locale(java.util.Locale) Map(java.util.Map) Matchers.equalTo(org.hamcrest.Matchers.equalTo) ESTestCase(org.elasticsearch.test.ESTestCase) Tuple(org.elasticsearch.common.collect.Tuple) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) BytesArray(org.elasticsearch.common.bytes.BytesArray) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) BytesRef(org.apache.lucene.util.BytesRef)

Example 94 with BytesArray

use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.

the class StreamsTests method testBytesStreamInput.

public void testBytesStreamInput() throws IOException {
    byte[] stuff = new byte[] { 0, 1, 2, 3 };
    BytesRef stuffRef = new BytesRef(stuff, 2, 2);
    BytesArray stuffArray = new BytesArray(stuffRef);
    StreamInput input = stuffArray.streamInput();
    assertEquals(2, input.read());
    assertEquals(3, input.read());
    assertEquals(-1, input.read());
    input.close();
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesRef(org.apache.lucene.util.BytesRef)

Example 95 with BytesArray

use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.

the class BaseXContentTestCase method doTestRawValue.

void doTestRawValue(XContent source) throws Exception {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try (XContentGenerator generator = source.createGenerator(os)) {
        generator.writeStartObject();
        generator.writeFieldName("foo");
        generator.writeNull();
        generator.writeEndObject();
    }
    final byte[] rawData = os.toByteArray();
    os = new ByteArrayOutputStream();
    try (XContentGenerator generator = xcontentType().xContent().createGenerator(os)) {
        generator.writeRawValue(new BytesArray(rawData));
    }
    XContentParser parser = xcontentType().xContent().createParser(NamedXContentRegistry.EMPTY, os.toByteArray());
    assertEquals(Token.START_OBJECT, parser.nextToken());
    assertEquals(Token.FIELD_NAME, parser.nextToken());
    assertEquals("foo", parser.currentName());
    assertEquals(Token.VALUE_NULL, parser.nextToken());
    assertEquals(Token.END_OBJECT, parser.nextToken());
    assertNull(parser.nextToken());
    os = new ByteArrayOutputStream();
    try (XContentGenerator generator = xcontentType().xContent().createGenerator(os)) {
        generator.writeStartObject();
        generator.writeFieldName("test");
        generator.writeRawValue(new BytesArray(rawData));
        generator.writeEndObject();
    }
    parser = xcontentType().xContent().createParser(NamedXContentRegistry.EMPTY, os.toByteArray());
    assertEquals(Token.START_OBJECT, parser.nextToken());
    assertEquals(Token.FIELD_NAME, parser.nextToken());
    assertEquals("test", parser.currentName());
    assertEquals(Token.START_OBJECT, parser.nextToken());
    assertEquals(Token.FIELD_NAME, parser.nextToken());
    assertEquals("foo", parser.currentName());
    assertEquals(Token.VALUE_NULL, parser.nextToken());
    assertEquals(Token.END_OBJECT, parser.nextToken());
    assertEquals(Token.END_OBJECT, parser.nextToken());
    assertNull(parser.nextToken());
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

BytesArray (org.elasticsearch.common.bytes.BytesArray)203 BytesReference (org.elasticsearch.common.bytes.BytesReference)36 Matchers.containsString (org.hamcrest.Matchers.containsString)31 IOException (java.io.IOException)29 StreamInput (org.elasticsearch.common.io.stream.StreamInput)24 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)24 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)21 HashMap (java.util.HashMap)17 BytesRef (org.apache.lucene.util.BytesRef)17 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)14 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)14 FakeRestRequest (org.elasticsearch.test.rest.FakeRestRequest)13 ArrayList (java.util.ArrayList)12 TopDocs (org.apache.lucene.search.TopDocs)12 SearchResponse (org.elasticsearch.action.search.SearchResponse)12 Document (org.elasticsearch.index.mapper.ParseContext.Document)12 Index (org.elasticsearch.index.Index)11 Map (java.util.Map)10 IndexableField (org.apache.lucene.index.IndexableField)10 IndexService (org.elasticsearch.index.IndexService)10