Search in sources :

Example 1 with CheckedConsumer

use of org.elasticsearch.common.CheckedConsumer in project elasticsearch by elastic.

the class BulkItemResponse method fromXContent.

/**
     * Reads a {@link BulkItemResponse} from a {@link XContentParser}.
     *
     * @param parser the {@link XContentParser}
     * @param id the id to assign to the parsed {@link BulkItemResponse}. It is usually the index of
     *           the item in the {@link BulkResponse#getItems} array.
     */
public static BulkItemResponse fromXContent(XContentParser parser, int id) throws IOException {
    ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser::getTokenLocation);
    XContentParser.Token token = parser.nextToken();
    ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser::getTokenLocation);
    String currentFieldName = parser.currentName();
    token = parser.nextToken();
    final OpType opType = OpType.fromString(currentFieldName);
    ensureExpectedToken(XContentParser.Token.START_OBJECT, token, parser::getTokenLocation);
    DocWriteResponse.Builder builder = null;
    CheckedConsumer<XContentParser, IOException> itemParser = null;
    if (opType == OpType.INDEX || opType == OpType.CREATE) {
        final IndexResponse.Builder indexResponseBuilder = new IndexResponse.Builder();
        builder = indexResponseBuilder;
        itemParser = (indexParser) -> IndexResponse.parseXContentFields(indexParser, indexResponseBuilder);
    } else if (opType == OpType.UPDATE) {
        final UpdateResponse.Builder updateResponseBuilder = new UpdateResponse.Builder();
        builder = updateResponseBuilder;
        itemParser = (updateParser) -> UpdateResponse.parseXContentFields(updateParser, updateResponseBuilder);
    } else if (opType == OpType.DELETE) {
        final DeleteResponse.Builder deleteResponseBuilder = new DeleteResponse.Builder();
        builder = deleteResponseBuilder;
        itemParser = (deleteParser) -> DeleteResponse.parseXContentFields(deleteParser, deleteResponseBuilder);
    } else {
        throwUnknownField(currentFieldName, parser.getTokenLocation());
    }
    RestStatus status = null;
    ElasticsearchException exception = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        }
        if (ERROR.equals(currentFieldName)) {
            if (token == XContentParser.Token.START_OBJECT) {
                exception = ElasticsearchException.fromXContent(parser);
            }
        } else if (STATUS.equals(currentFieldName)) {
            if (token == XContentParser.Token.VALUE_NUMBER) {
                status = RestStatus.fromCode(parser.intValue());
            }
        } else {
            itemParser.accept(parser);
        }
    }
    ensureExpectedToken(XContentParser.Token.END_OBJECT, token, parser::getTokenLocation);
    token = parser.nextToken();
    ensureExpectedToken(XContentParser.Token.END_OBJECT, token, parser::getTokenLocation);
    BulkItemResponse bulkItemResponse;
    if (exception != null) {
        Failure failure = new Failure(builder.getShardId().getIndexName(), builder.getType(), builder.getId(), exception, status);
        bulkItemResponse = new BulkItemResponse(id, opType, failure);
    } else {
        bulkItemResponse = new BulkItemResponse(id, opType, builder.build());
    }
    return bulkItemResponse;
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) XContentParserUtils.throwUnknownField(org.elasticsearch.common.xcontent.XContentParserUtils.throwUnknownField) Streamable(org.elasticsearch.common.io.stream.Streamable) ToXContent(org.elasticsearch.common.xcontent.ToXContent) CheckedConsumer(org.elasticsearch.common.CheckedConsumer) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) DocWriteResponse(org.elasticsearch.action.DocWriteResponse) XContentParserUtils.ensureExpectedToken(org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken) StatusToXContentObject(org.elasticsearch.common.xcontent.StatusToXContentObject) Strings(org.elasticsearch.common.Strings) XContentParser(org.elasticsearch.common.xcontent.XContentParser) ExceptionsHelper(org.elasticsearch.ExceptionsHelper) OpType(org.elasticsearch.action.DocWriteRequest.OpType) Version(org.elasticsearch.Version) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) StreamInput(org.elasticsearch.common.io.stream.StreamInput) RestStatus(org.elasticsearch.rest.RestStatus) IndexResponse(org.elasticsearch.action.index.IndexResponse) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) Writeable(org.elasticsearch.common.io.stream.Writeable) DocWriteResponse(org.elasticsearch.action.DocWriteResponse) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) RestStatus(org.elasticsearch.rest.RestStatus) IndexResponse(org.elasticsearch.action.index.IndexResponse) OpType(org.elasticsearch.action.DocWriteRequest.OpType) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 2 with CheckedConsumer

use of org.elasticsearch.common.CheckedConsumer in project elasticsearch by elastic.

the class ActionListenerTests method testOnResponse.

public void testOnResponse() {
    final int numListeners = randomIntBetween(1, 20);
    List<AtomicReference<Boolean>> refList = new ArrayList<>();
    List<AtomicReference<Exception>> excList = new ArrayList<>();
    List<ActionListener<Boolean>> listeners = new ArrayList<>();
    List<Boolean> failOnTrue = new ArrayList<>();
    AtomicInteger exceptionCounter = new AtomicInteger(0);
    for (int i = 0; i < numListeners; i++) {
        boolean doFailOnTrue = rarely();
        failOnTrue.add(doFailOnTrue);
        AtomicReference<Boolean> reference = new AtomicReference<>();
        AtomicReference<Exception> exReference = new AtomicReference<>();
        refList.add(reference);
        excList.add(exReference);
        CheckedConsumer<Boolean, ? extends Exception> handler = (o) -> {
            if (Boolean.FALSE.equals(o)) {
                throw new IllegalArgumentException("must not be false " + exceptionCounter.getAndIncrement());
            }
            if (doFailOnTrue) {
                throw new IllegalStateException("must not be true");
            }
            reference.set(o);
        };
        listeners.add(ActionListener.wrap(handler, exReference::set));
    }
    ActionListener.onResponse(listeners, Boolean.TRUE);
    for (int i = 0; i < numListeners; i++) {
        if (failOnTrue.get(i) == false) {
            assertTrue("listener index " + i, refList.get(i).get());
            refList.get(i).set(null);
        } else {
            assertNull("listener index " + i, refList.get(i).get());
        }
    }
    for (int i = 0; i < numListeners; i++) {
        if (failOnTrue.get(i) == false) {
            assertNull("listener index " + i, excList.get(i).get());
        } else {
            assertEquals("listener index " + i, "must not be true", excList.get(i).get().getMessage());
        }
    }
    ActionListener.onResponse(listeners, Boolean.FALSE);
    for (int i = 0; i < numListeners; i++) {
        assertNull("listener index " + i, refList.get(i).get());
    }
    assertEquals(numListeners, exceptionCounter.get());
    for (int i = 0; i < numListeners; i++) {
        assertNotNull(excList.get(i).get());
        assertEquals("listener index " + i, "must not be false " + i, excList.get(i).get().getMessage());
    }
}
Also used : List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CheckedConsumer(org.elasticsearch.common.CheckedConsumer) ESTestCase(org.elasticsearch.test.ESTestCase) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Aggregations

CheckedConsumer (org.elasticsearch.common.CheckedConsumer)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 ExceptionsHelper (org.elasticsearch.ExceptionsHelper)1 Version (org.elasticsearch.Version)1 OpType (org.elasticsearch.action.DocWriteRequest.OpType)1 DocWriteResponse (org.elasticsearch.action.DocWriteResponse)1 DeleteResponse (org.elasticsearch.action.delete.DeleteResponse)1 IndexResponse (org.elasticsearch.action.index.IndexResponse)1 UpdateResponse (org.elasticsearch.action.update.UpdateResponse)1 Strings (org.elasticsearch.common.Strings)1 StreamInput (org.elasticsearch.common.io.stream.StreamInput)1 StreamOutput (org.elasticsearch.common.io.stream.StreamOutput)1 Streamable (org.elasticsearch.common.io.stream.Streamable)1 Writeable (org.elasticsearch.common.io.stream.Writeable)1 StatusToXContentObject (org.elasticsearch.common.xcontent.StatusToXContentObject)1