Search in sources :

Example 11 with BytesReference

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

the class AllocationIdTests method testSerialization.

public void testSerialization() throws IOException {
    AllocationId allocationId = AllocationId.newInitializing();
    if (randomBoolean()) {
        allocationId = AllocationId.newRelocation(allocationId);
    }
    BytesReference bytes = allocationId.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS).bytes();
    AllocationId parsedAllocationId = AllocationId.fromXContent(createParser(JsonXContent.jsonXContent, bytes));
    assertEquals(allocationId, parsedAllocationId);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference)

Example 12 with BytesReference

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

the class MetaDataTests method testUnknownFieldIndexMetaData.

public void testUnknownFieldIndexMetaData() throws IOException {
    BytesReference metadata = JsonXContent.contentBuilder().startObject().startObject("index_name").field("random", "value").endObject().endObject().bytes();
    XContentParser parser = createParser(JsonXContent.jsonXContent, metadata);
    try {
        IndexMetaData.Builder.fromXContent(parser);
        fail();
    } catch (IllegalArgumentException e) {
        assertEquals("Unexpected field [random]", e.getMessage());
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 13 with BytesReference

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

the class CompressorFactory method uncompressIfNeeded.

/**
     * Uncompress the provided data, data can be detected as compressed using {@link #isCompressed(BytesReference)}.
     * @throws NullPointerException a NullPointerException will be thrown when bytes is null
     */
public static BytesReference uncompressIfNeeded(BytesReference bytes) throws IOException {
    Compressor compressor = compressor(Objects.requireNonNull(bytes, "the BytesReference must not be null"));
    BytesReference uncompressed;
    if (compressor != null) {
        uncompressed = uncompress(bytes, compressor);
    } else {
        uncompressed = bytes;
    }
    return uncompressed;
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference)

Example 14 with BytesReference

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

the class DecayFunctionParser method fromXContent.

/**
     * Parses bodies of the kind
     *
     * <pre>
     * <code>
     * {
     *      "fieldname1" : {
     *          "origin" : "someValue",
     *          "scale" : "someValue"
     *      },
     *      "multi_value_mode" : "min"
     * }
     * </code>
     * </pre>
     */
@Override
public DFB fromXContent(QueryParseContext context) throws IOException, ParsingException {
    XContentParser parser = context.parser();
    String currentFieldName;
    XContentParser.Token token;
    MultiValueMode multiValueMode = DecayFunctionBuilder.DEFAULT_MULTI_VALUE_MODE;
    String fieldName = null;
    BytesReference functionBytes = null;
    while ((token = parser.nextToken()) == XContentParser.Token.FIELD_NAME) {
        currentFieldName = parser.currentName();
        token = parser.nextToken();
        if (token == XContentParser.Token.START_OBJECT) {
            fieldName = currentFieldName;
            XContentBuilder builder = XContentFactory.jsonBuilder();
            builder.copyCurrentStructure(parser);
            functionBytes = builder.bytes();
        } else if (MULTI_VALUE_MODE.match(currentFieldName)) {
            multiValueMode = MultiValueMode.fromString(parser.text());
        } else {
            throw new ParsingException(parser.getTokenLocation(), "malformed score function score parameters.");
        }
    }
    if (fieldName == null || functionBytes == null) {
        throw new ParsingException(parser.getTokenLocation(), "malformed score function score parameters.");
    }
    DFB functionBuilder = createFromBytes.apply(fieldName, functionBytes);
    functionBuilder.setMultiValueMode(multiValueMode);
    return functionBuilder;
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser) MultiValueMode(org.elasticsearch.search.MultiValueMode) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 15 with BytesReference

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

the class TcpTransport method sendRequestToChannel.

private void sendRequestToChannel(DiscoveryNode node, final Channel targetChannel, final long requestId, final String action, final TransportRequest request, TransportRequestOptions options, Version channelVersion, byte status) throws IOException, TransportException {
    if (compress) {
        options = TransportRequestOptions.builder(options).withCompress(true).build();
    }
    status = TransportStatus.setRequest(status);
    ReleasableBytesStreamOutput bStream = new ReleasableBytesStreamOutput(bigArrays);
    // we wrap this in a release once since if the onRequestSent callback throws an exception
    // we might release things twice and this should be prevented
    final Releasable toRelease = Releasables.releaseOnce(() -> Releasables.close(bStream.bytes()));
    boolean addedReleaseListener = false;
    StreamOutput stream = bStream;
    try {
        // the header part is compressed, and the "body" can't be extracted as compressed
        if (options.compress() && canCompress(request)) {
            status = TransportStatus.setCompress(status);
            stream = CompressorFactory.COMPRESSOR.streamOutput(stream);
        }
        // we pick the smallest of the 2, to support both backward and forward compatibility
        // note, this is the only place we need to do this, since from here on, we use the serialized version
        // as the version to use also when the node receiving this request will send the response with
        Version version = Version.min(getCurrentVersion(), channelVersion);
        stream.setVersion(version);
        threadPool.getThreadContext().writeTo(stream);
        stream.writeString(action);
        BytesReference message = buildMessage(requestId, status, node.getVersion(), request, stream, bStream);
        final TransportRequestOptions finalOptions = options;
        Runnable onRequestSent = () -> {
            // this might be called in a different thread
            try {
                toRelease.close();
            } finally {
                transportServiceAdapter.onRequestSent(node, requestId, action, request, finalOptions);
            }
        };
        addedReleaseListener = internalSendMessage(targetChannel, message, onRequestSent);
    } finally {
        IOUtils.close(stream);
        if (!addedReleaseListener) {
            toRelease.close();
        }
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) CompositeBytesReference(org.elasticsearch.common.bytes.CompositeBytesReference) Version(org.elasticsearch.Version) AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) AbstractLifecycleRunnable(org.elasticsearch.common.util.concurrent.AbstractLifecycleRunnable) ReleasableBytesStreamOutput(org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput) Releasable(org.elasticsearch.common.lease.Releasable) StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) ReleasableBytesStreamOutput(org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Aggregations

BytesReference (org.elasticsearch.common.bytes.BytesReference)318 Matchers.containsString (org.hamcrest.Matchers.containsString)72 XContentParser (org.elasticsearch.common.xcontent.XContentParser)63 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)61 IOException (java.io.IOException)58 XContentType (org.elasticsearch.common.xcontent.XContentType)50 BytesArray (org.elasticsearch.common.bytes.BytesArray)47 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)37 ArrayList (java.util.ArrayList)30 HashMap (java.util.HashMap)30 Map (java.util.Map)26 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)26 Test (org.junit.Test)25 List (java.util.List)24 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)24 Version (org.elasticsearch.Version)22 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)20 ReleasableBytesReference (org.elasticsearch.common.bytes.ReleasableBytesReference)19 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)18 BytesRef (org.apache.lucene.util.BytesRef)18