/** * Add a result * * @param result the result * @param indexMap the index map */ public void addResult(final BulkWriteResult result, final IndexMap indexMap) { insertedCount += result.getInsertedCount(); matchedCount += result.getMatchedCount(); deletedCount += result.getDeletedCount(); if (result.isModifiedCountAvailable() && modifiedCount != null) { modifiedCount += result.getModifiedCount(); } else { modifiedCount = null; } mergeUpserts(result.getUpserts(), indexMap); }
private DeleteResult executeDelete(@Nullable final ClientSession clientSession, final Bson filter, final DeleteOptions deleteOptions, final boolean multi) { com.mongodb.bulk.BulkWriteResult result = executeSingleWriteRequest(clientSession, multi ? operations.deleteMany(filter, deleteOptions) : operations.deleteOne(filter, deleteOptions), DELETE); if (result.wasAcknowledged()) { return DeleteResult.acknowledged(result.getDeletedCount()); } else { return DeleteResult.unacknowledged(); } }
private int getCount(final BulkWriteResult bulkWriteResult) { int count = 0; if (getType() == UPDATE || getType() == REPLACE) { count = bulkWriteResult.getMatchedCount() + bulkWriteResult.getUpserts().size(); } else if (getType() == DELETE) { count = bulkWriteResult.getDeletedCount(); } return count; }
throw e; logger.info("{} object updated", bulkWriteResult.first().getUpserts().size() + bulkWriteResult.first().getModifiedCount()); Collections.singletonList((long) (bulkWriteResult.first().getUpserts().size() + bulkWriteResult.first().getModifiedCount())));
private Function<BulkWriteResult, Source<Boolean, NotUsed>> mapCombinedWritesResult( final List<WriteModel<Document>> policyWriteModels) { final BulkWriteOptions writeOrdered = new BulkWriteOptions(); writeOrdered.ordered(true); return bulkWriteResult -> { if (bulkWriteResult.getModifiedCount() > 0 || bulkWriteResult.getInsertedCount() > 0) { if (!policyWriteModels.isEmpty()) { return Source.fromPublisher(policiesCollection.bulkWrite(policyWriteModels, writeOrdered)) .map(policiesWriteResult -> Boolean.TRUE); } else { return Source.single(Boolean.TRUE); } } else { // return false if the previous bulk write did not modify anything return Source.single(Boolean.FALSE); } }; }
int icount = bwResult.getInsertedCount(); int ucount = bwResult.getMatchedCount();
protected void onUpdateError(String updateName, QueryResult<BulkWriteResult> update, List<Bson> queries, List<String> queryIds, MongoDBCollection collection) { logger.error("(Updated " + updateName + " variants = " + queries.size() + " ) != " + "(ModifiedCount = " + update.first().getModifiedCount() + "). MatchedCount:" + update.first().getMatchedCount()); logger.info("QueryIDs: {}", queryIds); List<QueryResult<Document>> queryResults = collection.find(queries, null); logger.info("Results: {}", queryResults.size()); for (QueryResult<Document> r : queryResults) { logger.info("result: '{}'", r); if (!r.getResult().isEmpty()) { String id = r.first().get("_id", String.class); boolean remove = queryIds.remove(id); logger.info("remove({}): {}", id, remove); } } StringBuilder sb = new StringBuilder("Missing Variant for update : "); for (String id : queryIds) { logger.error("Missing Variant '" + id + '\''); sb.append('\'').append(id).append("', "); } throw new RuntimeException(sb.toString()); }
private static int doInsert(final MongoDBQueryDescriptor queryDesc, final MongoCollection<Document> collection) { Document options = queryDesc.getOptions(); Boolean ordered = FALSE; WriteConcern wc = null; if ( options != null ) { ordered = (Boolean) options.get( "ordered" ); ordered = ( ordered != null ) ? ordered : FALSE; Document o = (Document) options.get( "writeConcern" ); wc = getWriteConcern( o ); } // Need to use BulkWriteOperation here rather than collection.insert(..) because the WriteResult returned // by the latter returns 0 for getN() even if the insert was successful (which is bizarre, but that's the way it // is defined...) List<InsertOneModel<Document>> operationList = null; if ( queryDesc.getUpdateOrInsertMany() != null ) { operationList = new ArrayList<>( queryDesc.getUpdateOrInsertMany().size() ); for ( Document doc : queryDesc.getUpdateOrInsertMany() ) { operationList.add( new InsertOneModel<>( doc ) ); } } else { operationList = new ArrayList<>( 1 ); operationList.add( new InsertOneModel<>( queryDesc.getUpdateOrInsertOne() ) ); } final BulkWriteResult result = collection.withWriteConcern( ( wc != null ? wc : collection.getWriteConcern() ) ).bulkWrite( operationList, new BulkWriteOptions().ordered( ordered ) ); if ( result.wasAcknowledged() ) { return result.getInsertedCount(); } return -1; // Not sure if we should throw an exception instead? }
private WriteConcernResult translateBulkWriteResult(final BulkWriteResult bulkWriteResult) { return WriteConcernResult.acknowledged(getCount(bulkWriteResult), getUpdatedExisting(bulkWriteResult), bulkWriteResult.getUpserts().isEmpty() ? null : bulkWriteResult.getUpserts().get(0).getId()); }
private boolean getUpdatedExisting(final BulkWriteResult bulkWriteResult) { if (getType() == UPDATE) { return bulkWriteResult.getMatchedCount() > 0; } return false; }
@Test public void testBulkInsert() throws Exception { List<WriteModel<Document>> inserts = new ArrayList<>(); inserts.add(new InsertOneModel<>(json("_id: 1"))); inserts.add(new InsertOneModel<>(json("_id: 2"))); inserts.add(new InsertOneModel<>(json("_id: 3"))); BulkWriteResult result = collection.bulkWrite(inserts); assertThat(result.getInsertedCount()).isEqualTo(3); }
@Override public boolean equals(final Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } BulkWriteResult that = (BulkWriteResult) o; return !that.wasAcknowledged(); }
private void removeInBulk(boolean ordered) { DeleteManyModel<Document> deleteOp = new DeleteManyModel<>(json("field: 'y'")); BulkWriteResult result = collection.bulkWrite(Collections.singletonList(deleteOp), new BulkWriteOptions().ordered(ordered)); assertThat(result.getDeletedCount()).isEqualTo(3); assertThat(collection.countDocuments()).isZero(); }
private void addBulkWriteResultToResponse(final BulkWriteResult bulkWriteResult, final BsonDocument response) { response.put("ok", new BsonInt32(1)); if (getType() == INSERT) { response.put("n", new BsonInt32(0)); } else if (getType() == DELETE) { response.put("n", new BsonInt32(bulkWriteResult.getDeletedCount())); } else if (getType() == UPDATE || getType() == REPLACE) { response.put("n", new BsonInt32(bulkWriteResult.getMatchedCount() + bulkWriteResult.getUpserts().size())); if (bulkWriteResult.getUpserts().isEmpty()) { response.put("updatedExisting", BsonBoolean.TRUE); } else { response.put("updatedExisting", BsonBoolean.FALSE); response.put("upserted", bulkWriteResult.getUpserts().get(0).getId()); } } }
throw e; logger.info("{} object updated", bulkWriteResult.first().getUpserts().size() + bulkWriteResult.first().getModifiedCount()); Collections.singletonList((long) (bulkWriteResult.first().getUpserts().size() + bulkWriteResult.first().getModifiedCount())));
private Function<BulkWriteResult, Source<Boolean, NotUsed>> mapCombinedWritesResult( final List<WriteModel<Document>> policyWriteModels) { final BulkWriteOptions writeOrdered = new BulkWriteOptions(); writeOrdered.ordered(true); return bulkWriteResult -> { if (bulkWriteResult.getModifiedCount() > 0 || bulkWriteResult.getInsertedCount() > 0) { if (!policyWriteModels.isEmpty()) { return Source.fromPublisher(policiesCollection.bulkWrite(policyWriteModels, writeOrdered)) .map(policiesWriteResult -> Boolean.TRUE); } else { return Source.single(Boolean.TRUE); } } else { // return false if the previous bulk write did not modify anything return Source.single(Boolean.FALSE); } }; }
if (writeResult.getMatchedCount() != pushQueriesBulkList.size()) { logger.warn("Could not update stats from some variants: {} != {}, {} non loaded stats", writeResult.getMatchedCount(), pushQueriesBulkList.size(), (pushQueriesBulkList.size() - writeResult.getMatchedCount())); int writes = writeResult.getModifiedCount();
private static int doInsert(final MongoDBQueryDescriptor queryDesc, final MongoCollection<Document> collection) { Document options = queryDesc.getOptions(); Boolean ordered = FALSE; WriteConcern wc = null; if ( options != null ) { ordered = (Boolean) options.get( "ordered" ); ordered = ( ordered != null ) ? ordered : FALSE; Document o = (Document) options.get( "writeConcern" ); wc = getWriteConcern( o ); } // Need to use BulkWriteOperation here rather than collection.insert(..) because the WriteResult returned // by the latter returns 0 for getN() even if the insert was successful (which is bizarre, but that's the way it // is defined...) List<InsertOneModel<Document>> operationList = null; if ( queryDesc.getUpdateOrInsertMany() != null ) { operationList = new ArrayList<>( queryDesc.getUpdateOrInsertMany().size() ); for ( Document doc : queryDesc.getUpdateOrInsertMany() ) { operationList.add( new InsertOneModel<>( doc ) ); } } else { operationList = new ArrayList<>( 1 ); operationList.add( new InsertOneModel<>( queryDesc.getUpdateOrInsertOne() ) ); } final BulkWriteResult result = collection.withWriteConcern( ( wc != null ? wc : collection.getWriteConcern() ) ).bulkWrite( operationList, new BulkWriteOptions().ordered( ordered ) ); if ( result.wasAcknowledged() ) { return result.getInsertedCount(); } return -1; // Not sure if we should throw an exception instead? }
private WriteConcernResult translateBulkWriteResult(final BulkWriteResult bulkWriteResult) { return WriteConcernResult.acknowledged(getCount(bulkWriteResult), getUpdatedExisting(bulkWriteResult), bulkWriteResult.getUpserts().isEmpty() ? null : bulkWriteResult.getUpserts().get(0).getId()); }
private boolean getUpdatedExisting(final BulkWriteResult bulkWriteResult) { if (getType() == UPDATE) { return bulkWriteResult.getMatchedCount() > 0; } return false; }