Search in sources :

Example 1 with BytesArray

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

the class ObjectPath method createFromResponse.

public static ObjectPath createFromResponse(Response response) throws IOException {
    byte[] bytes = EntityUtils.toByteArray(response.getEntity());
    String contentType = response.getHeader("Content-Type");
    XContentType xContentType = XContentType.fromMediaTypeOrFormat(contentType);
    return ObjectPath.createFromXContent(xContentType.xContent(), new BytesArray(bytes));
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) XContentType(org.elasticsearch.common.xcontent.XContentType)

Example 2 with BytesArray

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

the class ChecksumBlobStoreFormat method writeBlob.

/**
     * Writes blob in atomic manner without resolving the blobName using using {@link #blobName} method.
     * <p>
     * The blob will be compressed and checksum will be written if required.
     *
     * @param obj           object to be serialized
     * @param blobContainer blob container
     * @param blobName          blob name
     */
protected void writeBlob(T obj, BlobContainer blobContainer, String blobName) throws IOException {
    BytesReference bytes = write(obj);
    try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
        final String resourceDesc = "ChecksumBlobStoreFormat.writeBlob(blob=\"" + blobName + "\")";
        try (OutputStreamIndexOutput indexOutput = new OutputStreamIndexOutput(resourceDesc, blobName, byteArrayOutputStream, BUFFER_SIZE)) {
            CodecUtil.writeHeader(indexOutput, codec, VERSION);
            try (OutputStream indexOutputOutputStream = new IndexOutputOutputStream(indexOutput) {

                @Override
                public void close() throws IOException {
                // this is important since some of the XContentBuilders write bytes on close.
                // in order to write the footer we need to prevent closing the actual index input.
                }
            }) {
                bytes.writeTo(indexOutputOutputStream);
            }
            CodecUtil.writeFooter(indexOutput);
        }
        BytesArray bytesArray = new BytesArray(byteArrayOutputStream.toByteArray());
        try (InputStream stream = bytesArray.streamInput()) {
            blobContainer.writeBlob(blobName, stream, bytesArray.length());
        }
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) BytesArray(org.elasticsearch.common.bytes.BytesArray) OutputStreamIndexOutput(org.apache.lucene.store.OutputStreamIndexOutput) IndexOutputOutputStream(org.elasticsearch.common.lucene.store.IndexOutputOutputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) IndexOutputOutputStream(org.elasticsearch.common.lucene.store.IndexOutputOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 3 with BytesArray

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

the class RestRequest method contentOrSourceParam.

/**
     * Get the content of the request or the contents of the {@code source} param. Prefer {@link #contentOrSourceParamParser()} or
     * {@link #withContentOrSourceParamParserOrNull(CheckedConsumer)} if you need a parser.
     */
public final Tuple<XContentType, BytesReference> contentOrSourceParam() {
    if (hasContent()) {
        if (xContentType.get() == null) {
            throw new IllegalStateException("unknown content type");
        }
        return new Tuple<>(xContentType.get(), content());
    }
    String source = param("source");
    String typeParam = param("source_content_type");
    if (source != null && typeParam != null) {
        BytesArray bytes = new BytesArray(source);
        final XContentType xContentType = parseContentType(Collections.singletonList(typeParam));
        if (xContentType == null) {
            throw new IllegalStateException("Unknown value for source_content_type [" + typeParam + "]");
        }
        return new Tuple<>(xContentType, bytes);
    }
    return new Tuple<>(XContentType.JSON, BytesArray.EMPTY);
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) XContentType(org.elasticsearch.common.xcontent.XContentType) Tuple(org.elasticsearch.common.collect.Tuple)

Example 4 with BytesArray

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

the class ShardSearchLocalRequest method cacheKey.

@Override
public BytesReference cacheKey() throws IOException {
    BytesStreamOutput out = new BytesStreamOutput();
    this.innerWriteTo(out, true);
    // do a deep copy
    return new BytesArray(out.bytes().toBytesRef(), true);
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 5 with BytesArray

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

the class TaskTests method testTaskInfoToString.

public void testTaskInfoToString() {
    String nodeId = randomAsciiOfLength(10);
    long taskId = randomIntBetween(0, 100000);
    long startTime = randomNonNegativeLong();
    long runningTime = randomNonNegativeLong();
    boolean cancellable = randomBoolean();
    TaskInfo taskInfo = new TaskInfo(new TaskId(nodeId, taskId), "test_type", "test_action", "test_description", null, startTime, runningTime, cancellable, TaskId.EMPTY_TASK_ID);
    String taskInfoString = taskInfo.toString();
    Map<String, Object> map = XContentHelper.convertToMap(new BytesArray(taskInfoString.getBytes(StandardCharsets.UTF_8)), true).v2();
    assertEquals(((Number) map.get("id")).longValue(), taskId);
    assertEquals(map.get("type"), "test_type");
    assertEquals(map.get("action"), "test_action");
    assertEquals(map.get("description"), "test_description");
    assertEquals(((Number) map.get("start_time_in_millis")).longValue(), startTime);
    assertEquals(((Number) map.get("running_time_in_nanos")).longValue(), runningTime);
    assertEquals(map.get("cancellable"), cancellable);
}
Also used : TaskInfo(org.elasticsearch.tasks.TaskInfo) BytesArray(org.elasticsearch.common.bytes.BytesArray) TaskId(org.elasticsearch.tasks.TaskId)

Aggregations

BytesArray (org.elasticsearch.common.bytes.BytesArray)265 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)54 IOException (java.io.IOException)45 BytesReference (org.elasticsearch.common.bytes.BytesReference)45 Matchers.containsString (org.hamcrest.Matchers.containsString)39 Test (org.junit.Test)38 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)28 Searcher (org.elasticsearch.index.engine.Engine.Searcher)26 TopDocs (org.apache.lucene.search.TopDocs)25 StreamInput (org.elasticsearch.common.io.stream.StreamInput)25 ArrayList (java.util.ArrayList)23 BytesRef (org.apache.lucene.util.BytesRef)23 HashMap (java.util.HashMap)22 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)22 LongPoint (org.apache.lucene.document.LongPoint)20 Document (org.elasticsearch.index.mapper.ParseContext.Document)16 Store (org.elasticsearch.index.store.Store)16 Map (java.util.Map)15 CountDownLatch (java.util.concurrent.CountDownLatch)15 SearchResponse (org.elasticsearch.action.search.SearchResponse)15