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;
}
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());
}
}
Aggregations