Example usage for com.mongodb.bulk BulkWriteError getIndex

List of usage examples for com.mongodb.bulk BulkWriteError getIndex

Introduction

In this page you can find the example usage for com.mongodb.bulk BulkWriteError getIndex.

Prototype

public int getIndex() 

Source Link

Document

The index of the item in the bulk write operation with this error.

Usage

From source file:org.wso2.extension.siddhi.store.mongodb.MongoDBEventTable.java

License:Open Source License

/**
 * Method for doing bulk write operations on the collection.
 *
 * @param parsedRecords a List of WriteModels to be applied
 * @throws MongoTableException if the write fails
 *//*from   ww w. j  av  a 2  s . c  o m*/
private void bulkWrite(List<? extends WriteModel<Document>> parsedRecords)
        throws ConnectionUnavailableException {
    try {
        if (!parsedRecords.isEmpty()) {
            this.getCollectionObject().bulkWrite(parsedRecords);
        }
    } catch (MongoSocketOpenException e) {
        throw new ConnectionUnavailableException(e);
    } catch (MongoBulkWriteException e) {
        List<com.mongodb.bulk.BulkWriteError> writeErrors = e.getWriteErrors();
        int failedIndex;
        Object failedModel;
        for (com.mongodb.bulk.BulkWriteError bulkWriteError : writeErrors) {
            failedIndex = bulkWriteError.getIndex();
            failedModel = parsedRecords.get(failedIndex);
            if (failedModel instanceof UpdateManyModel) {
                log.error("The update filter '" + ((UpdateManyModel) failedModel).getFilter().toString()
                        + "' failed to update with event '"
                        + ((UpdateManyModel) failedModel).getUpdate().toString()
                        + "' in the MongoDB Event Table due to " + bulkWriteError.getMessage());
            } else {
                if (failedModel instanceof InsertOneModel) {
                    log.error("The event '" + ((InsertOneModel) failedModel).getDocument().toString()
                            + "' failed to insert into the Mongo Event Table due to "
                            + bulkWriteError.getMessage());
                } else {

                    log.error("The delete filter '" + ((DeleteManyModel) failedModel).getFilter().toString()
                            + "' failed to delete the events from the MongoDB Event Table due to "
                            + bulkWriteError.getMessage());
                }
            }
            if (failedIndex + 1 < parsedRecords.size()) {
                this.bulkWrite(parsedRecords.subList(failedIndex + 1, parsedRecords.size() - 1));
            }
        }
    } catch (MongoException e) {
        this.destroy();
        throw new MongoTableException(
                "Error in writing to the collection '" + this.collectionName + "' : " + e.getLocalizedMessage(),
                e);
    }
}