Search in sources :

Example 86 with BytesStreamOutput

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

the class TimeValueTests method testSerialize.

public void testSerialize() throws Exception {
    assertEqualityAfterSerialize(new TimeValue(100, TimeUnit.DAYS), 3);
    assertEqualityAfterSerialize(timeValueNanos(-1), 2);
    assertEqualityAfterSerialize(timeValueNanos(1), 2);
    assertEqualityAfterSerialize(timeValueSeconds(30), 2);
    final TimeValue timeValue = new TimeValue(randomIntBetween(0, 1024), randomFrom(TimeUnit.values()));
    BytesStreamOutput out = new BytesStreamOutput();
    out.writeZLong(timeValue.duration());
    assertEqualityAfterSerialize(timeValue, 1 + out.bytes().length());
}
Also used : BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 87 with BytesStreamOutput

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

the class JsonVsSmileTests method testCompareParsingTokens.

public void testCompareParsingTokens() throws IOException {
    BytesStreamOutput xsonOs = new BytesStreamOutput();
    XContentGenerator xsonGen = XContentFactory.xContent(XContentType.SMILE).createGenerator(xsonOs);
    BytesStreamOutput jsonOs = new BytesStreamOutput();
    XContentGenerator jsonGen = XContentFactory.xContent(XContentType.JSON).createGenerator(jsonOs);
    xsonGen.writeStartObject();
    jsonGen.writeStartObject();
    xsonGen.writeStringField("test", "value");
    jsonGen.writeStringField("test", "value");
    xsonGen.writeFieldName("arr");
    xsonGen.writeStartArray();
    jsonGen.writeFieldName("arr");
    jsonGen.writeStartArray();
    xsonGen.writeNumber(1);
    jsonGen.writeNumber(1);
    xsonGen.writeNull();
    jsonGen.writeNull();
    xsonGen.writeEndArray();
    jsonGen.writeEndArray();
    xsonGen.writeEndObject();
    jsonGen.writeEndObject();
    xsonGen.close();
    jsonGen.close();
    verifySameTokens(createParser(JsonXContent.jsonXContent, jsonOs.bytes()), createParser(SmileXContent.smileXContent, xsonOs.bytes()));
}
Also used : XContentGenerator(org.elasticsearch.common.xcontent.XContentGenerator) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 88 with BytesStreamOutput

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

the class TDigestStateTest method testStreaming.

@Test
public void testStreaming() throws Exception {
    TDigestState digestState1 = new TDigestState(250, new double[] { 0.5, 0.8 });
    BytesStreamOutput out = new BytesStreamOutput();
    TDigestStateType digestStateType = TDigestStateType.INSTANCE;
    Streamer streamer = digestStateType.create().streamer();
    streamer.writeValueTo(out, digestState1);
    StreamInput in = StreamInput.wrap(out.bytes());
    TDigestState digestState2 = (TDigestState) streamer.readValueFrom(in);
    assertEquals(digestState1.compression(), digestState2.compression(), 0.001d);
    assertEquals(digestState1.fractions()[0], digestState2.fractions()[0], 0.001d);
    assertEquals(digestState1.fractions()[1], digestState2.fractions()[1], 0.001d);
}
Also used : Streamer(io.crate.Streamer) StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) Test(org.junit.Test)

Example 89 with BytesStreamOutput

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

the class FulltextAnalyzerResolver method encodeSettings.

public static BytesReference encodeSettings(Settings settings) {
    try {
        BytesStreamOutput bso = new BytesStreamOutput();
        XContentBuilder builder = XContentFactory.jsonBuilder(bso);
        builder.startObject();
        for (Map.Entry<String, String> entry : settings.getAsMap().entrySet()) {
            builder.field(entry.getKey(), entry.getValue());
        }
        builder.endObject();
        builder.flush();
        return bso.bytes();
    } catch (IOException e) {
        // this is a memory stream so no real I/O happens and a IOException can't really happen at runtime
        throw Throwables.propagate(e);
    }
}
Also used : IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 90 with BytesStreamOutput

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

the class PartitionName method encodeIdent.

@Nullable
public static String encodeIdent(Collection<? extends BytesRef> values) {
    if (values.size() == 0) {
        return null;
    }
    BytesStreamOutput streamOutput = new BytesStreamOutput(estimateSize(values));
    try {
        streamOutput.writeVInt(values.size());
        for (BytesRef value : values) {
            StringType.INSTANCE.streamer().writeValueTo(streamOutput, value);
        }
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
    String identBase32 = BASE32.encodeAsString(streamOutput.bytes().toBytes()).toLowerCase(Locale.ROOT);
    // decode doesn't need padding, remove it
    int idx = identBase32.indexOf('=');
    if (idx > -1) {
        return identBase32.substring(0, idx);
    }
    return identBase32;
}
Also used : IOException(java.io.IOException) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) BytesRef(org.apache.lucene.util.BytesRef) Nullable(org.elasticsearch.common.Nullable)

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