org.elasticsearch.common.io.stream.BytesStreamOutput Java Examples

The following examples show how to use org.elasticsearch.common.io.stream.BytesStreamOutput. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WriterProjectionTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void testStreaming() throws Exception {
    WriterProjection p = new WriterProjection(
        ImmutableList.<Symbol>of(new InputColumn(1)),
        Literal.of("/foo.json"),
        WriterProjection.CompressionType.GZIP,
        MapBuilder.<ColumnIdent, Symbol>newMapBuilder().put(
            new ColumnIdent("partitionColumn"), Literal.of(1)).map(),
        ImmutableList.of("foo"),
        WriterProjection.OutputFormat.JSON_OBJECT
    );

    BytesStreamOutput out = new BytesStreamOutput();
    Projection.toStream(p, out);

    StreamInput in = out.bytes().streamInput();
    WriterProjection p2 = (WriterProjection) Projection.fromStream(in);

    assertEquals(p, p2);
}
 
Example #2
Source File: ObjectTypeTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void testStreamingOfValueWithInnerTypes() throws IOException {
    ObjectType type = ObjectType.builder()
        .setInnerType("s", DataTypes.STRING)
        .setInnerType("obj_array", new ArrayType<>(ObjectType.builder()
            .setInnerType("i", DataTypes.INTEGER)
            .build()))
        .build();
    BytesStreamOutput out = new BytesStreamOutput();

    HashMap<String, Object> map = new HashMap<>();
    map.put("s", "foo");
    map.put("obj_array", List.of(Map.of("i", 0)));
    type.writeTo(out);
    type.writeValueTo(out,  map);

    StreamInput in = out.bytes().streamInput();
    ObjectType otherType = new ObjectType(in);

    Map<String, Object> v = otherType.readValueFrom(in);

    assertThat(v.get("s"), is(map.get("s")));
    assertThat((List<Map>) v.get("obj_array"), Matchers.contains((Map.of("i", 0))));
}
 
Example #3
Source File: ADStatsTests.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
@Test
public void testADStatsNodeRequest() throws IOException {
    ADStatsNodeRequest adStatsNodeRequest1 = new ADStatsNodeRequest();
    assertNull("ADStatsNodeRequest default constructor failed", adStatsNodeRequest1.getADStatsRequest());

    ADStatsRequest adStatsRequest = new ADStatsRequest(new String[0]);
    ADStatsNodeRequest adStatsNodeRequest2 = new ADStatsNodeRequest(adStatsRequest);
    assertEquals("ADStatsNodeRequest has the wrong ADStatsRequest", adStatsNodeRequest2.getADStatsRequest(), adStatsRequest);

    // Test serialization
    BytesStreamOutput output = new BytesStreamOutput();
    adStatsNodeRequest2.writeTo(output);
    StreamInput streamInput = output.bytes().streamInput();
    adStatsNodeRequest1 = new ADStatsNodeRequest(streamInput);
    assertEquals(
        "readStats failed",
        adStatsNodeRequest2.getADStatsRequest().getStatsToBeRetrieved(),
        adStatsNodeRequest1.getADStatsRequest().getStatsToBeRetrieved()
    );
}
 
Example #4
Source File: GeneratedReferenceTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void testStreaming() throws Exception {
    ReferenceIdent referenceIdent = new ReferenceIdent(t1Info.ident(), "generated_column");
    String formattedGeneratedExpression = "concat(a, 'bar')";
    GeneratedReference generatedReferenceInfo = new GeneratedReference(null,
        referenceIdent,
        RowGranularity.DOC,
        StringType.INSTANCE, ColumnPolicy.STRICT, Reference.IndexType.ANALYZED,
        formattedGeneratedExpression, false);

    generatedReferenceInfo.generatedExpression(expressions.normalize(executor.asSymbol(formattedGeneratedExpression)));
    generatedReferenceInfo.referencedReferences(ImmutableList.of(t1Info.getReference(new ColumnIdent("a"))));

    BytesStreamOutput out = new BytesStreamOutput();
    Reference.toStream(generatedReferenceInfo, out);

    StreamInput in = out.bytes().streamInput();
    GeneratedReference generatedReferenceInfo2 = Reference.fromStream(in);

    assertThat(generatedReferenceInfo2, is(generatedReferenceInfo));
}
 
Example #5
Source File: GroupProjectionTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void testStreaming() throws Exception {
    ImmutableList<Symbol> keys = ImmutableList.of(
        new InputColumn(0, DataTypes.STRING),
        new InputColumn(1, DataTypes.SHORT)
    );
    ImmutableList<Aggregation> aggregations = ImmutableList.of();
    GroupProjection p = new GroupProjection(keys, aggregations, AggregateMode.ITER_FINAL, RowGranularity.CLUSTER);

    BytesStreamOutput out = new BytesStreamOutput();
    Projection.toStream(p, out);

    StreamInput in = out.bytes().streamInput();
    GroupProjection p2 = (GroupProjection) Projection.fromStream(in);

    assertEquals(p, p2);
}
 
Example #6
Source File: ADStatsTests.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
@Test
public void testADStatsNodeResponse() throws IOException, JsonPathNotFoundException {
    Map<String, Object> stats = new HashMap<String, Object>() {
        {
            put("testKey", "testValue");
        }
    };

    // Test serialization
    ADStatsNodeResponse adStatsNodeResponse = new ADStatsNodeResponse(discoveryNode1, stats);
    BytesStreamOutput output = new BytesStreamOutput();
    adStatsNodeResponse.writeTo(output);
    StreamInput streamInput = output.bytes().streamInput();
    ADStatsNodeResponse readResponse = ADStatsNodeResponse.readStats(streamInput);
    assertEquals("readStats failed", readResponse.getStatsMap(), adStatsNodeResponse.getStatsMap());

    // Test toXContent
    XContentBuilder builder = jsonBuilder();
    adStatsNodeResponse.toXContent(builder.startObject(), ToXContent.EMPTY_PARAMS).endObject();
    String json = Strings.toString(builder);

    for (Map.Entry<String, Object> stat : stats.entrySet()) {
        assertEquals("toXContent does not work", JsonDeserializer.getTextValue(json, stat.getKey()), stat.getValue());
    }
}
 
Example #7
Source File: UpdateProjectionTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void test_serialization() throws Exception {

    UpdateProjection expected = new UpdateProjection(
        Literal.of(1),
        new String[]{"foo"},
        new Symbol[]{Literal.of(1)},
        new Symbol[]{new InputColumn(0, DataTypes.STRING)},
        new Symbol[]{Literal.of(1)},
        null);

    BytesStreamOutput out = new BytesStreamOutput();
    expected.writeTo(out);

    StreamInput in = out.bytes().streamInput();
    UpdateProjection result = new UpdateProjection(in);

    assertThat(result, equalTo(expected));
}
 
Example #8
Source File: OrderedTopNProjectionTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void testStreaming() throws Exception {
    Projection p = new OrderedTopNProjection(
        10,
        20,
        Collections.singletonList(Literal.of("foobar")),
        Collections.singletonList(new InputColumn(0, DataTypes.STRING)),
        new boolean[] { true },
        new boolean[] { true }
    );

    BytesStreamOutput out = new BytesStreamOutput();
    Projection.toStream(p, out);

    StreamInput in = out.bytes().streamInput();
    Projection projection = Projection.fromStream(in);

    assertThat(projection, equalTo(p));
}
 
Example #9
Source File: AggregationTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void test_serialization_with_filter() throws Exception {
    Aggregation actual = new Aggregation(
        CountAggregation.COUNT_STAR_SIGNATURE,
        CountAggregation.COUNT_STAR_SIGNATURE.getReturnType().createType(),
        CountAggregation.COUNT_STAR_SIGNATURE.getReturnType().createType(),
        List.of(),
        Literal.BOOLEAN_FALSE
    );
    BytesStreamOutput output = new BytesStreamOutput();
    Symbols.toStream(actual, output);

    StreamInput input = output.bytes().streamInput();
    Aggregation expected = (Aggregation) Symbols.fromStream(input);

    assertThat(expected.filter(), is(Literal.BOOLEAN_FALSE));
    assertThat(expected, is(actual));
}
 
Example #10
Source File: DistributedResultRequestTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void testStreaming() throws Exception {
    Streamer<?>[] streamers = new Streamer[]{DataTypes.STRING.streamer()};

    UUID uuid = UUID.randomUUID();
    StreamBucket.Builder builder = new StreamBucket.Builder(streamers, RamAccounting.NO_ACCOUNTING);
    builder.add(new RowN(new Object[] {"ab"}));
    builder.add(new RowN(new Object[] {null}));
    builder.add(new RowN(new Object[] {"cd"}));
    DistributedResultRequest r1 = new DistributedResultRequest(uuid, 1, (byte) 3, 1, builder.build(), false);

    BytesStreamOutput out = new BytesStreamOutput();
    r1.writeTo(out);
    StreamInput in = out.bytes().streamInput();
    DistributedResultRequest r2 = new DistributedResultRequest(in);

    assertEquals(r1.readRows(streamers).size(), r2.readRows(streamers).size());
    assertThat(r1.isLast(), is(r2.isLast()));
    assertThat(r1.executionPhaseInputId(), is(r2.executionPhaseInputId()));

    assertThat(r2.readRows(streamers), contains(isRow("ab"), isNullRow(), isRow("cd")));
}
 
Example #11
Source File: StoredLtrModelParserTests.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
public void testSerializationModelDef() throws IOException {
    String modelDefnJson = "{\n" +
            "   \"type\": \"model/dummy\",\n" +
            "   \"definition\": \"completely ignored\",\n"+
            "   \"feature_normalizers\": {\n"+
            "     \"feature_2\": { \"min_max\":" +
            "           {\"minimum\": 1.0," +
            "            \"maximum\": 1.25}}}}";

    XContentParser xContent = jsonXContent.createParser(EMPTY,
            LoggingDeprecationHandler.INSTANCE, modelDefnJson);
    StoredLtrModel.LtrModelDefinition modelDef = StoredLtrModel.LtrModelDefinition.parse(xContent, null);

    BytesStreamOutput out = new BytesStreamOutput();
    modelDef.writeTo(out);
    out.close();

    BytesRef ref = out.bytes().toBytesRef();
    StreamInput input = ByteBufferStreamInput.wrap(ref.bytes, ref.offset, ref.length);

    StoredLtrModel.LtrModelDefinition modelUnserialized = new StoredLtrModel.LtrModelDefinition(input);
    assertEquals(modelUnserialized.getDefinition(), modelDef.getDefinition());
    assertEquals(modelUnserialized.getType(), modelDef.getType());
    assertEquals(modelUnserialized.getFtrNorms(), modelDef.getFtrNorms());

}
 
Example #12
Source File: Id.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private static String encode(List<BytesRef> values, int clusteredByPosition) {
    try (BytesStreamOutput out = new BytesStreamOutput(estimateSize(values))) {
        int size = values.size();
        out.writeVInt(size);
        if (clusteredByPosition >= 0) {
            out.writeBytesRef(ensureNonNull(values.get(clusteredByPosition)));
        }
        for (int i = 0; i < size; i++) {
            if (i != clusteredByPosition) {
                out.writeBytesRef(ensureNonNull(values.get(i)));
            }
        }
        return Base64.encodeBytes(out.bytes().toBytes());
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}
 
Example #13
Source File: PartitionName.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #14
Source File: NodeFetchRequestTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void testStreaming() throws Exception {

    IntObjectHashMap<IntContainer> toFetch = new IntObjectHashMap<>();
    IntHashSet docIds = new IntHashSet(3);
    toFetch.put(1, docIds);

    NodeFetchRequest orig = new NodeFetchRequest(UUID.randomUUID(), 1, true, toFetch);

    BytesStreamOutput out = new BytesStreamOutput();
    orig.writeTo(out);

    StreamInput in = out.bytes().streamInput();

    NodeFetchRequest streamed = new NodeFetchRequest(in);

    assertThat(orig.jobId(), is(streamed.jobId()));
    assertThat(orig.fetchPhaseId(), is(streamed.fetchPhaseId()));
    assertThat(orig.isCloseContext(), is(streamed.isCloseContext()));
    assertThat(orig.toFetch().toString(), is(streamed.toFetch().toString()));
}
 
Example #15
Source File: IndicesRequestCache.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public Value call() throws Exception {
    queryPhase.execute(context);

    /* BytesStreamOutput allows to pass the expected size but by default uses
     * BigArrays.PAGE_SIZE_IN_BYTES which is 16k. A common cached result ie.
     * a date histogram with 3 buckets is ~100byte so 16k might be very wasteful
     * since we don't shrink to the actual size once we are done serializing.
     * By passing 512 as the expected size we will resize the byte array in the stream
     * slowly until we hit the page size and don't waste too much memory for small query
     * results.*/
    final int expectedSizeInBytes = 512;
    try (BytesStreamOutput out = new BytesStreamOutput(expectedSizeInBytes)) {
        context.queryResult().writeToNoId(out);
        // for now, keep the paged data structure, which might have unused bytes to fill a page, but better to keep
        // the memory properly paged instead of having varied sized bytes
        final BytesReference reference = out.bytes();
        loaded = true;
        Value value = new Value(reference, out.ramBytesUsed());
        key.shard.requestCache().onCached(key, value);
        return value;
    }
}
 
Example #16
Source File: ObjectTypeTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void testStreamingWithInnerTypes() throws IOException {
    ObjectType type = ObjectType.builder()
        .setInnerType("s", DataTypes.STRING)
        .setInnerType("obj_array", new ArrayType<>(ObjectType.builder()
            .setInnerType("i", DataTypes.INTEGER)
            .build()))
        .build();
    BytesStreamOutput out = new BytesStreamOutput();
    type.writeTo(out);

    StreamInput in = out.bytes().streamInput();
    ObjectType otherType = new ObjectType(in);

    assertThat(otherType.innerTypes(), is(type.innerTypes()));
}
 
Example #17
Source File: CountPhaseTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void testStreaming() throws Exception {
    Routing routing = new Routing(
        MapBuilder.<String, Map<String, IntIndexedContainer>>treeMapBuilder()
            .put("n1", MapBuilder.<String, IntIndexedContainer>treeMapBuilder()
                .put("i1", IntArrayList.from(1, 2))
                .put("i2", IntArrayList.from(1, 2)).map())
            .put("n2", MapBuilder.<String, IntIndexedContainer>treeMapBuilder()
                .put("i1", IntArrayList.from(3)).map()).map());
    CountPhase countPhase = new CountPhase(1, routing, Literal.BOOLEAN_TRUE, DistributionInfo.DEFAULT_BROADCAST);

    BytesStreamOutput out = new BytesStreamOutput(10);
    countPhase.writeTo(out);

    StreamInput in = out.bytes().streamInput();

    CountPhase streamedNode = new CountPhase(in);

    assertThat(streamedNode.phaseId(), is(1));
    assertThat(streamedNode.nodeIds(), containsInAnyOrder("n1", "n2"));
    assertThat(streamedNode.routing(), equalTo(routing));
    assertThat(streamedNode.distributionInfo(), equalTo(DistributionInfo.DEFAULT_BROADCAST));
}
 
Example #18
Source File: BlobStoreRepository.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
/**
 * Writes snapshot index file
 * <p>
 * This file can be used by read-only repositories that are unable to list files in the repository
 *
 * @param snapshots list of snapshot ids
 * @throws IOException I/O errors
 */
protected void writeSnapshotList(List<SnapshotId> snapshots) throws IOException {
    final BytesReference bRef;
    try(BytesStreamOutput bStream = new BytesStreamOutput()) {
        try(StreamOutput stream = new OutputStreamStreamOutput(bStream)) {
            XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON, stream);
            builder.startObject();
            builder.startArray("snapshots");
            for (SnapshotId snapshot : snapshots) {
                builder.value(snapshot.getSnapshot());
            }
            builder.endArray();
            builder.endObject();
            builder.close();
        }
        bRef = bStream.bytes();
    }
    if (snapshotsBlobContainer.blobExists(SNAPSHOTS_FILE)) {
        snapshotsBlobContainer.deleteBlob(SNAPSHOTS_FILE);
    }
    snapshotsBlobContainer.writeBlob(SNAPSHOTS_FILE, bRef);
}
 
Example #19
Source File: FunctionTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void test_serialization_without_filter() throws Exception {
    Function fn = new Function(
        signature,
        List.of(createReference(randomAsciiLettersOfLength(2), DataTypes.BOOLEAN)),
        returnType
    );

    BytesStreamOutput output = new BytesStreamOutput();
    Symbols.toStream(fn, output);

    StreamInput input = output.bytes().streamInput();
    Function fn2 = (Function) Symbols.fromStream(input);

    assertThat(fn, is(fn2));
}
 
Example #20
Source File: ObjectTypeTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void testStreamingOfNullValueWithInnerTypes() throws IOException {
    ObjectType type = ObjectType.builder()
        .setInnerType("s", DataTypes.STRING)
        .setInnerType("obj_array", new ArrayType<>(ObjectType.builder()
            .setInnerType("i", DataTypes.INTEGER)
            .build()))
        .build();
    BytesStreamOutput out = new BytesStreamOutput();

    type.writeTo(out);
    type.writeValueTo(out, null);

    StreamInput in = out.bytes().streamInput();
    ObjectType otherType = new ObjectType(in);

    Object v = otherType.readValueFrom(in);

    assertThat(v, nullValue());
}
 
Example #21
Source File: ShardDeleteRequestTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void testStreaming() throws Exception {
    ShardId shardId = new ShardId("test", UUIDs.randomBase64UUID(), 1);
    UUID jobId = UUID.randomUUID();
    ShardDeleteRequest request = new ShardDeleteRequest(shardId, jobId);

    request.add(123, new ShardDeleteRequest.Item("99"));
    request.add(5, new ShardDeleteRequest.Item("42"));

    BytesStreamOutput out = new BytesStreamOutput();
    request.writeTo(out);

    StreamInput in = out.bytes().streamInput();
    ShardDeleteRequest request2 = new ShardDeleteRequest(in);

    assertThat(request, equalTo(request2));
}
 
Example #22
Source File: CompressedXContent.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
/**
 * Create a {@link CompressedXContent} out of a serialized {@link ToXContent}
 * that may already be compressed.
 */
public CompressedXContent(BytesReference data) throws IOException {
    Compressor compressor = CompressorFactory.compressor(data);
    if (compressor != null) {
        // already compressed...
        this.bytes = data.toBytes();
        this.crc32 = crc32(new BytesArray(uncompressed()));
    } else {
        BytesStreamOutput out = new BytesStreamOutput();
        try (OutputStream compressedOutput = CompressorFactory.defaultCompressor().streamOutput(out)) {
            data.writeTo(compressedOutput);
        }
        this.bytes = out.bytes().toBytes();
        this.crc32 = crc32(data);
    }
    assertConsistent();
}
 
Example #23
Source File: UserDefinedFunctionsMetaDataTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void testUserDefinedFunctionStreaming() throws IOException {
    BytesStreamOutput out = new BytesStreamOutput();
    FUNCTION_META_DATA.writeTo(out);

    StreamInput in = out.bytes().streamInput();
    UserDefinedFunctionMetaData udfMeta2 = new UserDefinedFunctionMetaData(in);
    assertThat(FUNCTION_META_DATA, is(udfMeta2));

    assertThat(udfMeta2.schema(), is("my_schema"));
    assertThat(udfMeta2.name(), is("my_add"));
    assertThat(udfMeta2.arguments().size(), is(2));
    assertThat(udfMeta2.arguments().get(1), is(
        FunctionArgumentDefinition.of("my_named_arg", DataTypes.DOUBLE)
    ));
    assertThat(udfMeta2.argumentTypes().size(), is(2));
    assertThat(udfMeta2.argumentTypes().get(1), is(DataTypes.DOUBLE));
    assertThat(udfMeta2.returnType(), is(DataTypes.FLOAT));
    assertThat(udfMeta2.language(), is("dummy_lang"));
    assertThat(udfMeta2.definition(), is(definition));
}
 
Example #24
Source File: ArrayTypeTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void testNullValues() throws Exception {
    ArrayType<String> arrayType = new ArrayType<>(StringType.INSTANCE);
    var streamer = arrayType.streamer();

    BytesStreamOutput out = new BytesStreamOutput();

    streamer.writeValueTo(out, null);

    StreamInput in = out.bytes().streamInput();
    assertThat(streamer.readValueFrom(in), is(nullValue()));

    out.reset();
    List<String> listWithNullItem = new ArrayList<>();
    listWithNullItem.add(null);
    streamer.writeValueTo(out, listWithNullItem);

    in = out.bytes().streamInput();
    assertThat(streamer.readValueFrom(in), contains(nullValue()));
}
 
Example #25
Source File: LocalTransportChannel.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public void sendResponse(TransportResponse response, TransportResponseOptions options) throws IOException {
    try (BytesStreamOutput stream = new BytesStreamOutput()) {
        stream.setVersion(version);
        stream.writeLong(requestId);
        byte status = 0;
        status = TransportStatus.setResponse(status);
        stream.writeByte(status); // 0 for request, 1 for response.
        response.writeTo(stream);
        final byte[] data = stream.bytes().toBytes();
        targetTransport.workers().execute(new Runnable() {
            @Override
            public void run() {
                targetTransport.messageReceived(data, action, sourceTransport, version, null);
            }
        });
        sourceTransportServiceAdapter.onResponseSent(requestId, action, response, options);
    }
}
 
Example #26
Source File: LocalTransportChannel.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public void sendResponse(Throwable error) throws IOException {
    BytesStreamOutput stream = new BytesStreamOutput();
    writeResponseExceptionHeader(stream);
    RemoteTransportException tx = new RemoteTransportException(targetTransport.nodeName(), targetTransport.boundAddress().boundAddresses()[0], action, error);
    stream.writeThrowable(tx);

    final byte[] data = stream.bytes().toBytes();
    targetTransport.workers().execute(new Runnable() {
        @Override
        public void run() {
            targetTransport.messageReceived(data, action, sourceTransport, version, null);
        }
    });
    sourceTransportServiceAdapter.onResponseSent(requestId, action, error);
}
 
Example #27
Source File: CompressedXContent.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Create a {@link CompressedXContent} out of a serialized {@link ToXContent}
 * that may already be compressed.
 */
public CompressedXContent(BytesReference data) throws IOException {
    Compressor compressor = CompressorFactory.compressor(data);
    if (compressor != null) {
        // already compressed...
        this.bytes = BytesReference.toBytes(data);
        this.crc32 = crc32(new BytesArray(uncompressed()));
    } else {
        BytesStreamOutput out = new BytesStreamOutput();
        try (OutputStream compressedOutput = CompressorFactory.COMPRESSOR.streamOutput(out)) {
            data.writeTo(compressedOutput);
        }
        this.bytes = BytesReference.toBytes(out.bytes());
        this.crc32 = crc32(data);
    }
    assertConsistent();
}
 
Example #28
Source File: TcpTransport.java    From crate with Apache License 2.0 5 votes vote down vote up
public TcpTransport(String transportName, Settings settings, ThreadPool threadPool, BigArrays bigArrays,
                    CircuitBreakerService circuitBreakerService, NamedWriteableRegistry namedWriteableRegistry,
                    NetworkService networkService) {
    this.settings = settings;
    this.profileSettings = getProfileSettings(settings);
    this.threadPool = threadPool;
    this.bigArrays = bigArrays;
    this.circuitBreakerService = circuitBreakerService;
    this.namedWriteableRegistry = namedWriteableRegistry;
    this.compress = Transport.TRANSPORT_TCP_COMPRESS.get(settings);
    this.networkService = networkService;
    this.transportName = transportName;
    this.nodeName = Node.NODE_NAME_SETTING.get(settings);
    final Settings defaultFeatures = DEFAULT_FEATURES_SETTING.get(settings);
    if (defaultFeatures == null) {
        this.features = new String[0];
    } else {
        defaultFeatures.names().forEach(key -> {
            if (Booleans.parseBoolean(defaultFeatures.get(key)) == false) {
                throw new IllegalArgumentException("feature settings must have default [true] value");
            }
        });
        // use a sorted set to present the features in a consistent order
        this.features = new TreeSet<>(defaultFeatures.names()).toArray(new String[defaultFeatures.names().size()]);
    }

    try (BytesStreamOutput out = new BytesStreamOutput()) {
        out.writeByte((byte) 'E');
        out.writeByte((byte) 'S');
        out.writeInt(TcpTransport.PING_DATA_SIZE);
        pingMessage = out.bytes();
    } catch (IOException e) {
        throw new AssertionError(e.getMessage(), e); // won't happen
    }
}
 
Example #29
Source File: MultiDataPathUpgrader.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Runs check-index on the target shard and throws an exception if it failed
 */
public void checkIndex(ShardPath targetPath) throws IOException {
    BytesStreamOutput os = new BytesStreamOutput();
    PrintStream out = new PrintStream(os, false, Charsets.UTF_8.name());
    try (Directory directory = new SimpleFSDirectory(targetPath.resolveIndex());
        final CheckIndex checkIndex = new CheckIndex(directory)) {
        checkIndex.setInfoStream(out);
        CheckIndex.Status status = checkIndex.checkIndex();
        out.flush();
        if (!status.clean) {
            logger.warn("check index [failure]\n{}", new String(os.bytes().toBytes(), Charsets.UTF_8));
            throw new IllegalStateException("index check failure");
        }
    }
}
 
Example #30
Source File: PartitionName.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Write utf8 bytes for bwc, with 0 as `null` indicator
 */
private static void writeValueTo(BytesStreamOutput out, @Nullable String value) throws IOException {
    // 1 is always added to the length so that
    // 0 is null
    // 1 is 0
    // ...
    if (value == null) {
        out.writeVInt(0);
    } else {
        byte[] v = value.getBytes(StandardCharsets.UTF_8);
        out.writeVInt(v.length + 1);
        out.writeBytes(v, 0, v.length);
    }
}