当前位置: 首页>>代码示例>>Java>>正文


Java BytesStreamOutput类代码示例

本文整理汇总了Java中org.elasticsearch.common.io.stream.BytesStreamOutput的典型用法代码示例。如果您正苦于以下问题:Java BytesStreamOutput类的具体用法?Java BytesStreamOutput怎么用?Java BytesStreamOutput使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


BytesStreamOutput类属于org.elasticsearch.common.io.stream包,在下文中一共展示了BytesStreamOutput类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testCompareParsingTokens

import org.elasticsearch.common.io.stream.BytesStreamOutput; //导入依赖的package包/类
public void testCompareParsingTokens() throws IOException {
    BytesStreamOutput xsonOs = new BytesStreamOutput();
    XContentGenerator xsonGen = XContentFactory.xContent(XContentType.CBOR).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(CborXContent.cborXContent, xsonOs.bytes()));
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:33,代码来源:JsonVsCborTests.java

示例2: testSerializationBwc

import org.elasticsearch.common.io.stream.BytesStreamOutput; //导入依赖的package包/类
public void testSerializationBwc() throws IOException {
    final byte[] data = Base64.getDecoder().decode("ADwDAANmb28APAMBB215X3R5cGULeyJ0eXBlIjp7fX0AAAD////+AA==");
    final Version version = randomFrom(Version.V_5_0_0, Version.V_5_0_1, Version.V_5_0_2,
        Version.V_5_0_3_UNRELEASED, Version.V_5_1_1_UNRELEASED, Version.V_5_1_2_UNRELEASED, Version.V_5_2_0_UNRELEASED);
    try (StreamInput in = StreamInput.wrap(data)) {
        in.setVersion(version);
        CreateIndexRequest serialized = new CreateIndexRequest();
        serialized.readFrom(in);
        assertEquals("foo", serialized.index());
        BytesReference bytesReference = JsonXContent.contentBuilder().startObject().startObject("type").endObject().endObject().bytes();
        assertEquals(bytesReference.utf8ToString(), serialized.mappings().get("my_type"));

        try (BytesStreamOutput out = new BytesStreamOutput()) {
            out.setVersion(version);
            serialized.writeTo(out);
            out.flush();
            assertArrayEquals(data, out.bytes().toBytesRef().bytes);
        }
    }
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:21,代码来源:CreateIndexRequestTests.java

示例3: run

import org.elasticsearch.common.io.stream.BytesStreamOutput; //导入依赖的package包/类
@Override
public Object run() {
    final BytesStreamOutput result = new BytesStreamOutput();
    try (UTF8StreamWriter writer = utf8StreamWriter().setOutput(result)) {
        // crazy reflection here
        SpecialPermission.check();
        AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
            ((Mustache) template.compiled()).execute(writer, vars);
            return null;
        });
    } catch (Exception e) {
        logger.error((Supplier<?>) () -> new ParameterizedMessage("Error running {}", template), e);
        throw new GeneralScriptException("Error running " + template, e);
    }
    return result.bytes();
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:17,代码来源:MustacheScriptEngineService.java

示例4: testDiscoveryNodeSerializationKeepsHost

import org.elasticsearch.common.io.stream.BytesStreamOutput; //导入依赖的package包/类
public void testDiscoveryNodeSerializationKeepsHost() throws Exception {
    InetAddress inetAddress = InetAddress.getByAddress("name1", new byte[] { (byte) 192, (byte) 168, (byte) 0, (byte) 1});
    TransportAddress transportAddress = new TransportAddress(inetAddress, randomIntBetween(0, 65535));
    DiscoveryNode node = new DiscoveryNode("name1", "id1", transportAddress, emptyMap(), emptySet(), Version.CURRENT);

    BytesStreamOutput streamOutput = new BytesStreamOutput();
    streamOutput.setVersion(Version.CURRENT);
    node.writeTo(streamOutput);

    StreamInput in = StreamInput.wrap(streamOutput.bytes().toBytesRef().bytes);
    DiscoveryNode serialized = new DiscoveryNode(in);
    assertEquals(transportAddress.address().getHostString(), serialized.getHostName());
    assertEquals(transportAddress.address().getHostString(), serialized.getAddress().address().getHostString());
    assertEquals(transportAddress.getAddress(), serialized.getHostAddress());
    assertEquals(transportAddress.getAddress(), serialized.getAddress().getAddress());
    assertEquals(transportAddress.getPort(), serialized.getAddress().getPort());
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:18,代码来源:DiscoveryNodeTests.java

示例5: testSerialization

import org.elasticsearch.common.io.stream.BytesStreamOutput; //导入依赖的package包/类
public void testSerialization() throws IOException {
    SimulatePipelineRequest request = new SimulatePipelineRequest(new BytesArray(""), XContentType.JSON);
    // Sometimes we set an id
    if (randomBoolean()) {
        request.setId(randomAsciiOfLengthBetween(1, 10));
    }

    // Sometimes we explicitly set a boolean (with whatever value)
    if (randomBoolean()) {
        request.setVerbose(randomBoolean());
    }

    BytesStreamOutput out = new BytesStreamOutput();
    request.writeTo(out);
    StreamInput streamInput = out.bytes().streamInput();
    SimulatePipelineRequest otherRequest = new SimulatePipelineRequest();
    otherRequest.readFrom(streamInput);

    assertThat(otherRequest.getId(), equalTo(request.getId()));
    assertThat(otherRequest.isVerbose(), equalTo(request.isVerbose()));
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:22,代码来源:SimulatePipelineRequestTests.java

示例6: cacheShardLevelResult

import org.elasticsearch.common.io.stream.BytesStreamOutput; //导入依赖的package包/类
/**
 * Cache something calculated at the shard level.
 * @param shard the shard this item is part of
 * @param reader a reader for this shard. Used to invalidate the cache when there are changes.
 * @param cacheKey key for the thing being cached within this shard
 * @param loader loads the data into the cache if needed
 * @return the contents of the cache or the result of calling the loader
 */
private BytesReference cacheShardLevelResult(IndexShard shard, DirectoryReader reader, BytesReference cacheKey, Consumer<StreamOutput> loader)
        throws Exception {
    IndexShardCacheEntity cacheEntity = new IndexShardCacheEntity(shard);
    Supplier<BytesReference> supplier = () -> {
        /* 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)) {
            loader.accept(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
            return out.bytes();
        }
    };
    return indicesRequestCache.getOrCompute(cacheEntity, supplier, reader, cacheKey);
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:30,代码来源:IndicesService.java

示例7: testSerialization

import org.elasticsearch.common.io.stream.BytesStreamOutput; //导入依赖的package包/类
public void testSerialization() throws IOException {
    Map<String, Map<String, Map<String, FieldMappingMetaData>>> mappings = new HashMap<>();
    FieldMappingMetaData fieldMappingMetaData = new FieldMappingMetaData("my field", new BytesArray("{}"));
    mappings.put("index", Collections.singletonMap("type", Collections.singletonMap("field", fieldMappingMetaData)));
    GetFieldMappingsResponse response = new GetFieldMappingsResponse(mappings);

    try (BytesStreamOutput out = new BytesStreamOutput()) {
        response.writeTo(out);
        GetFieldMappingsResponse serialized = new GetFieldMappingsResponse();
        try (StreamInput in = StreamInput.wrap(out.bytes().toBytesRef().bytes)) {
            serialized.readFrom(in);
            FieldMappingMetaData metaData = serialized.fieldMappings("index", "type", "field");
            assertNotNull(metaData);
            assertEquals(new BytesArray("{}"), metaData.getSource());
        }
    }
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:18,代码来源:GetFieldMappingsResponseTests.java

示例8: markStoreCorrupted

import org.elasticsearch.common.io.stream.BytesStreamOutput; //导入依赖的package包/类
/**
 * Marks this store as corrupted. This method writes a <tt>corrupted_${uuid}</tt> file containing the given exception
 * message. If a store contains a <tt>corrupted_${uuid}</tt> file {@link #isMarkedCorrupted()} will return <code>true</code>.
 */
public void markStoreCorrupted(IOException exception) throws IOException {
    ensureOpen();
    if (!isMarkedCorrupted()) {
        String uuid = CORRUPTED + UUIDs.randomBase64UUID();
        try (IndexOutput output = this.directory().createOutput(uuid, IOContext.DEFAULT)) {
            CodecUtil.writeHeader(output, CODEC, VERSION);
            BytesStreamOutput out = new BytesStreamOutput();
            out.writeException(exception);
            BytesReference bytes = out.bytes();
            output.writeVInt(bytes.length());
            BytesRef ref = bytes.toBytesRef();
            output.writeBytes(ref.bytes, ref.offset, ref.length);
            CodecUtil.writeFooter(output);
        } catch (IOException ex) {
            logger.warn("Can't mark store as corrupted", ex);
        }
        directory().sync(Collections.singleton(uuid));
    }
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:24,代码来源:Store.java

示例9: prepareRequest

import org.elasticsearch.common.io.stream.BytesStreamOutput; //导入依赖的package包/类
@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    boolean helpWanted = request.paramAsBoolean("help", false);
    if (helpWanted) {
        return channel -> {
            Table table = getTableWithHeader(request);
            int[] width = buildHelpWidths(table, request);
            BytesStreamOutput bytesOutput = channel.bytesOutput();
            UTF8StreamWriter out = new UTF8StreamWriter().setOutput(bytesOutput);
            for (Table.Cell cell : table.getHeaders()) {
                // need to do left-align always, so create new cells
                pad(new Table.Cell(cell.value), width[0], request, out);
                out.append(" | ");
                pad(new Table.Cell(cell.attr.containsKey("alias") ? cell.attr.get("alias") : ""), width[1], request, out);
                out.append(" | ");
                pad(new Table.Cell(cell.attr.containsKey("desc") ? cell.attr.get("desc") : "not available"), width[2], request, out);
                out.append("\n");
            }
            out.close();
            channel.sendResponse(new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, bytesOutput.bytes()));
        };
    } else {
        return doCatRequest(request, client);
    }
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:26,代码来源:AbstractCatAction.java

示例10: sendErrorResponse

import org.elasticsearch.common.io.stream.BytesStreamOutput; //导入依赖的package包/类
/**
 * Sends back an error response to the caller via the given channel
 * @param nodeVersion the caller node version
 * @param channel the channel to send the response to
 * @param error the error to return
 * @param requestId the request ID this response replies to
 * @param action the action this response replies to
 */
public void sendErrorResponse(Version nodeVersion, Channel channel, final Exception error, final long requestId,
                              final String action) throws IOException {
    try (BytesStreamOutput stream = new BytesStreamOutput()) {
        stream.setVersion(nodeVersion);
        RemoteTransportException tx = new RemoteTransportException(
            nodeName(), new TransportAddress(getLocalAddress(channel)), action, error);
        threadPool.getThreadContext().writeTo(stream);
        stream.writeException(tx);
        byte status = 0;
        status = TransportStatus.setResponse(status);
        status = TransportStatus.setError(status);
        final BytesReference bytes = stream.bytes();
        final BytesReference header = buildHeader(requestId, status, nodeVersion, bytes.length());
        Runnable onRequestSent = () -> transportServiceAdapter.onResponseSent(requestId, action, error);
        sendMessage(channel, new CompositeBytesReference(header, bytes), onRequestSent);
    }
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:26,代码来源:TcpTransport.java

示例11: testSerialization

import org.elasticsearch.common.io.stream.BytesStreamOutput; //导入依赖的package包/类
public void testSerialization() throws IOException {
    CreateIndexRequest request = new CreateIndexRequest("foo");
    String mapping = JsonXContent.contentBuilder().startObject().startObject("type").endObject().endObject().string();
    request.mapping("my_type", mapping, XContentType.JSON);

    try (BytesStreamOutput output = new BytesStreamOutput()) {
        request.writeTo(output);

        try (StreamInput in = output.bytes().streamInput()) {
            CreateIndexRequest serialized = new CreateIndexRequest();
            serialized.readFrom(in);
            assertEquals(request.index(), serialized.index());
            assertEquals(mapping, serialized.mappings().get("my_type"));
        }
    }
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:17,代码来源:CreateIndexRequestTests.java

示例12: CompressedXContent

import org.elasticsearch.common.io.stream.BytesStreamOutput; //导入依赖的package包/类
/**
 * 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();
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:21,代码来源:CompressedXContent.java

示例13: testSerializeInDifferentContextNoDefaults

import org.elasticsearch.common.io.stream.BytesStreamOutput; //导入依赖的package包/类
public void testSerializeInDifferentContextNoDefaults() throws IOException {
    BytesStreamOutput out = new BytesStreamOutput();
    {
        ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
        threadContext.putHeader("foo", "bar");
        threadContext.putTransient("ctx.foo", 1);

        assertEquals("bar", threadContext.getHeader("foo"));
        assertNotNull(threadContext.getTransient("ctx.foo"));
        assertNull(threadContext.getHeader("default"));
        threadContext.writeTo(out);
    }
    {
        Settings otherSettings = Settings.builder().put("request.headers.default", "5").build();
        ThreadContext otherhreadContext = new ThreadContext(otherSettings);
        otherhreadContext.readHeaders(out.bytes().streamInput());

        assertEquals("bar", otherhreadContext.getHeader("foo"));
        assertNull(otherhreadContext.getTransient("ctx.foo"));
        assertEquals("5", otherhreadContext.getHeader("default"));
    }
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:23,代码来源:ThreadContextTests.java

示例14: testSerialization

import org.elasticsearch.common.io.stream.BytesStreamOutput; //导入依赖的package包/类
public void testSerialization() throws Exception {
    int iterations = randomIntBetween(5, 20);
    for (int i = 0; i < iterations; i++) {

        IndicesOptions indicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean());
        ClusterStateRequest clusterStateRequest = new ClusterStateRequest().routingTable(randomBoolean()).metaData(randomBoolean())
                .nodes(randomBoolean()).blocks(randomBoolean()).indices("testindex", "testindex2").indicesOptions(indicesOptions);

        Version testVersion = VersionUtils.randomVersionBetween(random(), Version.CURRENT.minimumCompatibilityVersion(), Version.CURRENT);
        BytesStreamOutput output = new BytesStreamOutput();
        output.setVersion(testVersion);
        clusterStateRequest.writeTo(output);

        StreamInput streamInput = output.bytes().streamInput();
        streamInput.setVersion(testVersion);
        ClusterStateRequest deserializedCSRequest = new ClusterStateRequest();
        deserializedCSRequest.readFrom(streamInput);

        assertThat(deserializedCSRequest.routingTable(), equalTo(clusterStateRequest.routingTable()));
        assertThat(deserializedCSRequest.metaData(), equalTo(clusterStateRequest.metaData()));
        assertThat(deserializedCSRequest.nodes(), equalTo(clusterStateRequest.nodes()));
        assertThat(deserializedCSRequest.blocks(), equalTo(clusterStateRequest.blocks()));
        assertThat(deserializedCSRequest.indices(), equalTo(clusterStateRequest.indices()));
        assertOptionsMatch(deserializedCSRequest.indicesOptions(), clusterStateRequest.indicesOptions());
    }
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:27,代码来源:ClusterStateRequestTests.java

示例15: testSerialization

import org.elasticsearch.common.io.stream.BytesStreamOutput; //导入依赖的package包/类
public void testSerialization() throws IOException {
    boolean isFailure = randomBoolean();
    SimulateDocumentBaseResult simulateDocumentBaseResult;
    if (isFailure) {
        simulateDocumentBaseResult = new SimulateDocumentBaseResult(new IllegalArgumentException("test"));
    } else {
        IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
        simulateDocumentBaseResult = new SimulateDocumentBaseResult(ingestDocument);
    }

    BytesStreamOutput out = new BytesStreamOutput();
    simulateDocumentBaseResult.writeTo(out);
    StreamInput streamInput = out.bytes().streamInput();
    SimulateDocumentBaseResult otherSimulateDocumentBaseResult = new SimulateDocumentBaseResult(streamInput);

    if (isFailure) {
        assertThat(otherSimulateDocumentBaseResult.getIngestDocument(), equalTo(simulateDocumentBaseResult.getIngestDocument()));
        assertThat(otherSimulateDocumentBaseResult.getFailure(), instanceOf(IllegalArgumentException.class));
        IllegalArgumentException e = (IllegalArgumentException) otherSimulateDocumentBaseResult.getFailure();
        assertThat(e.getMessage(), equalTo("test"));
    } else {
        assertIngestDocument(otherSimulateDocumentBaseResult.getIngestDocument(), simulateDocumentBaseResult.getIngestDocument());
    }
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:25,代码来源:SimulateDocumentSimpleResultTests.java


注:本文中的org.elasticsearch.common.io.stream.BytesStreamOutput类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。