Search in sources :

Example 21 with BytesReference

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

the class Request method bulk.

static Request bulk(BulkRequest bulkRequest) throws IOException {
    Params parameters = Params.builder();
    parameters.withTimeout(bulkRequest.timeout());
    parameters.withRefreshPolicy(bulkRequest.getRefreshPolicy());
    // Bulk API only supports newline delimited JSON or Smile. Before executing
    // the bulk, we need to check that all requests have the same content-type
    // and this content-type is supported by the Bulk API.
    XContentType bulkContentType = null;
    for (int i = 0; i < bulkRequest.numberOfActions(); i++) {
        DocWriteRequest<?> request = bulkRequest.requests().get(i);
        DocWriteRequest.OpType opType = request.opType();
        if (opType == DocWriteRequest.OpType.INDEX || opType == DocWriteRequest.OpType.CREATE) {
            bulkContentType = enforceSameContentType((IndexRequest) request, bulkContentType);
        } else if (opType == DocWriteRequest.OpType.UPDATE) {
            UpdateRequest updateRequest = (UpdateRequest) request;
            if (updateRequest.doc() != null) {
                bulkContentType = enforceSameContentType(updateRequest.doc(), bulkContentType);
            }
            if (updateRequest.upsertRequest() != null) {
                bulkContentType = enforceSameContentType(updateRequest.upsertRequest(), bulkContentType);
            }
        }
    }
    if (bulkContentType == null) {
        bulkContentType = XContentType.JSON;
    }
    byte separator = bulkContentType.xContent().streamSeparator();
    ContentType requestContentType = ContentType.create(bulkContentType.mediaType());
    ByteArrayOutputStream content = new ByteArrayOutputStream();
    for (DocWriteRequest<?> request : bulkRequest.requests()) {
        DocWriteRequest.OpType opType = request.opType();
        try (XContentBuilder metadata = XContentBuilder.builder(bulkContentType.xContent())) {
            metadata.startObject();
            {
                metadata.startObject(opType.getLowercase());
                if (Strings.hasLength(request.index())) {
                    metadata.field("_index", request.index());
                }
                if (Strings.hasLength(request.type())) {
                    metadata.field("_type", request.type());
                }
                if (Strings.hasLength(request.id())) {
                    metadata.field("_id", request.id());
                }
                if (Strings.hasLength(request.routing())) {
                    metadata.field("_routing", request.routing());
                }
                if (Strings.hasLength(request.parent())) {
                    metadata.field("_parent", request.parent());
                }
                if (request.version() != Versions.MATCH_ANY) {
                    metadata.field("_version", request.version());
                }
                VersionType versionType = request.versionType();
                if (versionType != VersionType.INTERNAL) {
                    if (versionType == VersionType.EXTERNAL) {
                        metadata.field("_version_type", "external");
                    } else if (versionType == VersionType.EXTERNAL_GTE) {
                        metadata.field("_version_type", "external_gte");
                    } else if (versionType == VersionType.FORCE) {
                        metadata.field("_version_type", "force");
                    }
                }
                if (opType == DocWriteRequest.OpType.INDEX || opType == DocWriteRequest.OpType.CREATE) {
                    IndexRequest indexRequest = (IndexRequest) request;
                    if (Strings.hasLength(indexRequest.getPipeline())) {
                        metadata.field("pipeline", indexRequest.getPipeline());
                    }
                } else if (opType == DocWriteRequest.OpType.UPDATE) {
                    UpdateRequest updateRequest = (UpdateRequest) request;
                    if (updateRequest.retryOnConflict() > 0) {
                        metadata.field("_retry_on_conflict", updateRequest.retryOnConflict());
                    }
                    if (updateRequest.fetchSource() != null) {
                        metadata.field("_source", updateRequest.fetchSource());
                    }
                }
                metadata.endObject();
            }
            metadata.endObject();
            BytesRef metadataSource = metadata.bytes().toBytesRef();
            content.write(metadataSource.bytes, metadataSource.offset, metadataSource.length);
            content.write(separator);
        }
        BytesRef source = null;
        if (opType == DocWriteRequest.OpType.INDEX || opType == DocWriteRequest.OpType.CREATE) {
            IndexRequest indexRequest = (IndexRequest) request;
            BytesReference indexSource = indexRequest.source();
            XContentType indexXContentType = indexRequest.getContentType();
            try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, indexSource, indexXContentType)) {
                try (XContentBuilder builder = XContentBuilder.builder(bulkContentType.xContent())) {
                    builder.copyCurrentStructure(parser);
                    source = builder.bytes().toBytesRef();
                }
            }
        } else if (opType == DocWriteRequest.OpType.UPDATE) {
            source = XContentHelper.toXContent((UpdateRequest) request, bulkContentType, false).toBytesRef();
        }
        if (source != null) {
            content.write(source.bytes, source.offset, source.length);
            content.write(separator);
        }
    }
    HttpEntity entity = new ByteArrayEntity(content.toByteArray(), 0, content.size(), requestContentType);
    return new Request(HttpPost.METHOD_NAME, "/_bulk", parameters.getParams(), entity);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) ContentType(org.apache.http.entity.ContentType) HttpEntity(org.apache.http.HttpEntity) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) WriteRequest(org.elasticsearch.action.support.WriteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) GetRequest(org.elasticsearch.action.get.GetRequest) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IndexRequest(org.elasticsearch.action.index.IndexRequest) VersionType(org.elasticsearch.index.VersionType) XContentType(org.elasticsearch.common.xcontent.XContentType) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) BytesRef(org.apache.lucene.util.BytesRef) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 22 with BytesReference

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

the class IndexRequest method source.

/**
     * Sets the content source to index.
     * <p>
     * <b>Note: the number of objects passed to this method as varargs must be an even
     * number. Also the first argument in each pair (the field name) must have a
     * valid String representation.</b>
     * </p>
     */
public IndexRequest source(XContentType xContentType, Object... source) {
    if (source.length % 2 != 0) {
        throw new IllegalArgumentException("The number of object passed must be even but was [" + source.length + "]");
    }
    if (source.length == 2 && source[0] instanceof BytesReference && source[1] instanceof Boolean) {
        throw new IllegalArgumentException("you are using the removed method for source with bytes and unsafe flag, the unsafe flag was removed, please just use source(BytesReference)");
    }
    try {
        XContentBuilder builder = XContentFactory.contentBuilder(xContentType);
        builder.startObject();
        for (int i = 0; i < source.length; i++) {
            builder.field(source[i++].toString(), source[i]);
        }
        builder.endObject();
        return source(builder);
    } catch (IOException e) {
        throw new ElasticsearchGenerationException("Failed to generate", e);
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ElasticsearchGenerationException(org.elasticsearch.ElasticsearchGenerationException)

Example 23 with BytesReference

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

the class PublishClusterStateAction method sendClusterStateDiff.

private void sendClusterStateDiff(ClusterState clusterState, Map<Version, BytesReference> serializedDiffs, Map<Version, BytesReference> serializedStates, DiscoveryNode node, TimeValue publishTimeout, SendingController sendingController) {
    BytesReference bytes = serializedDiffs.get(node.getVersion());
    assert bytes != null : "failed to find serialized diff for node " + node + " of version [" + node.getVersion() + "]";
    sendClusterStateToNode(clusterState, bytes, node, publishTimeout, sendingController, true, serializedStates);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference)

Example 24 with BytesReference

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

the class PublishClusterStateAction method sendFullClusterState.

private void sendFullClusterState(ClusterState clusterState, Map<Version, BytesReference> serializedStates, DiscoveryNode node, TimeValue publishTimeout, SendingController sendingController) {
    BytesReference bytes = serializedStates.get(node.getVersion());
    if (bytes == null) {
        try {
            bytes = serializeFullClusterState(clusterState, node.getVersion());
            serializedStates.put(node.getVersion(), bytes);
        } catch (Exception e) {
            logger.warn((org.apache.logging.log4j.util.Supplier<?>) () -> new ParameterizedMessage("failed to serialize cluster_state before publishing it to node {}", node), e);
            sendingController.onNodeSendFailed(node, e);
            return;
        }
    }
    sendClusterStateToNode(clusterState, bytes, node, publishTimeout, sendingController, false, serializedStates);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) Supplier(java.util.function.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ElasticsearchException(org.elasticsearch.ElasticsearchException) IncompatibleClusterStateVersionException(org.elasticsearch.cluster.IncompatibleClusterStateVersionException) IOException(java.io.IOException) TransportException(org.elasticsearch.transport.TransportException)

Example 25 with BytesReference

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

the class IndicesRequestCache method getOrCompute.

BytesReference getOrCompute(CacheEntity cacheEntity, Supplier<BytesReference> loader, DirectoryReader reader, BytesReference cacheKey) throws Exception {
    final Key key = new Key(cacheEntity, reader.getVersion(), cacheKey);
    Loader cacheLoader = new Loader(cacheEntity, loader);
    BytesReference value = cache.computeIfAbsent(key, cacheLoader);
    if (cacheLoader.isLoaded()) {
        key.entity.onMiss();
        // see if its the first time we see this reader, and make sure to register a cleanup key
        CleanupKey cleanupKey = new CleanupKey(cacheEntity, reader.getVersion());
        if (!registeredClosedListeners.containsKey(cleanupKey)) {
            Boolean previous = registeredClosedListeners.putIfAbsent(cleanupKey, Boolean.TRUE);
            if (previous == null) {
                ElasticsearchDirectoryReader.addReaderCloseListener(reader, cleanupKey);
            }
        }
    } else {
        key.entity.onHit();
    }
    return value;
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) CacheLoader(org.elasticsearch.common.cache.CacheLoader)

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