Example usage for com.mongodb.bulk BulkWriteResult getDeletedCount

List of usage examples for com.mongodb.bulk BulkWriteResult getDeletedCount

Introduction

In this page you can find the example usage for com.mongodb.bulk BulkWriteResult getDeletedCount.

Prototype

public abstract int getDeletedCount();

Source Link

Document

Returns the number of documents deleted by the write operation.

Usage

From source file:com.streamsets.pipeline.stage.destination.mongodb.MongoDBTarget.java

License:Apache License

@Override
public void write(Batch batch) throws StageException {
    Iterator<Record> records = batch.getRecords();
    List<WriteModel<Document>> documentList = new ArrayList<>();
    List<Record> recordList = new ArrayList<>();
    while (records.hasNext()) {
        Record record = records.next();/*from   w w  w.ja  va 2s. c  o  m*/
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream(DEFAULT_CAPACITY);
            DataGenerator generator = generatorFactory.getGenerator(baos);
            generator.write(record);
            generator.close();
            Document document = Document.parse(new String(baos.toByteArray()));

            //create a write model based on record header
            if (!record.getHeader().getAttributeNames().contains(OPERATION_KEY)) {
                LOG.error(Errors.MONGODB_15.getMessage(), record.getHeader().getSourceId());
                throw new OnRecordErrorException(Errors.MONGODB_15, record.getHeader().getSourceId());
            }

            String operation = record.getHeader().getAttribute(OPERATION_KEY);
            switch (operation) {
            case INSERT:
                documentList.add(new InsertOneModel<>(document));
                recordList.add(record);
                break;
            case UPSERT:
                validateUniqueKey(operation, record);
                recordList.add(record);
                documentList.add(new ReplaceOneModel<>(
                        new Document(removeLeadingSlash(mongoTargetConfigBean.uniqueKeyField),
                                record.get(mongoTargetConfigBean.uniqueKeyField).getValueAsString()),
                        document, new UpdateOptions().upsert(true)));
                break;
            case DELETE:
                recordList.add(record);
                documentList.add(new DeleteOneModel<Document>(document));
                break;
            default:
                LOG.error(Errors.MONGODB_14.getMessage(), operation, record.getHeader().getSourceId());
                throw new StageException(Errors.MONGODB_14, operation, record.getHeader().getSourceId());
            }
        } catch (IOException | StageException e) {
            errorRecordHandler.onError(new OnRecordErrorException(record, Errors.MONGODB_13, e.toString(), e));
        }
    }

    if (!documentList.isEmpty()) {
        try {
            BulkWriteResult bulkWriteResult = coll.bulkWrite(documentList);
            if (bulkWriteResult.wasAcknowledged()) {
                LOG.trace("Wrote batch with {} inserts, {} updates and {} deletes",
                        bulkWriteResult.getInsertedCount(), bulkWriteResult.getModifiedCount(),
                        bulkWriteResult.getDeletedCount());
            }
        } catch (MongoException e) {
            for (Record record : recordList) {
                errorRecordHandler
                        .onError(new OnRecordErrorException(record, Errors.MONGODB_17, e.toString(), e));
            }
        }
    }
}

From source file:org.eclipse.ditto.services.thingsearch.persistence.write.streaming.SearchUpdaterStream.java

License:Open Source License

private static String logResult(final BulkWriteResult bulkWriteResult) {
    return String.format("BulkWriteResult[matched=%d,upserts=%d,inserted=%d,modified=%d,deleted=%d]",
            bulkWriteResult.getMatchedCount(), bulkWriteResult.getUpserts().size(),
            bulkWriteResult.getInsertedCount(), bulkWriteResult.getModifiedCount(),
            bulkWriteResult.getDeletedCount());
}

From source file:org.restheart.handlers.bulk.BulkResultRepresentationFactory.java

License:Open Source License

private void addBulkResult(final BulkOperationResult result, final RequestContext context,
        final Representation rep, final String requestPath) {
    Representation nrep = new Representation();

    BulkWriteResult wr = result.getBulkResult();

    if (wr.wasAcknowledged()) {
        if (wr.getUpserts() != null) {
            nrep.addProperty("inserted", new BsonInt32(wr.getUpserts().size()));

            // add links to new, upserted documents
            wr.getUpserts().stream().forEach(update -> {
                nrep.addLink(/*from   w w w . j  a v  a2  s. com*/
                        new Link("rh:newdoc", URLUtils.getReferenceLink(context, requestPath, update.getId())),
                        true);
            });
        }

        nrep.addProperty("deleted", new BsonInt32(wr.getDeletedCount()));

        if (wr.isModifiedCountAvailable()) {
            nrep.addProperty("modified", new BsonInt32(wr.getModifiedCount()));
        }

        nrep.addProperty("matched", new BsonInt32(wr.getMatchedCount()));

        rep.addRepresentation("rh:result", nrep);
    }
}

From source file:org.restheart.handlers.bulk.BulkResultRepresentationFactory.java

License:Open Source License

private void addWriteResult(final BulkWriteResult wr, final Representation rep, final String requestPath) {
    Representation nrep = new Representation();

    if (wr.wasAcknowledged()) {
        if (wr.getUpserts() != null) {
            nrep.addProperty("inserted", new BsonInt32(wr.getUpserts().size()));

            // add links to new, upserted documents
            wr.getUpserts().stream().forEach(update -> {
                nrep.addLink(new Link("rh:newdoc", URLUtils.getReferenceLink(requestPath, update.getId())),
                        true);// w w  w .j a v a 2  s.c o m
            });
        }

        nrep.addProperty("deleted", new BsonInt32(wr.getDeletedCount()));

        if (wr.isModifiedCountAvailable()) {
            nrep.addProperty("modified", new BsonInt32(wr.getModifiedCount()));
        }

        nrep.addProperty("matched", new BsonInt32(wr.getMatchedCount()));

        rep.addRepresentation("rh:result", nrep);
    }
}