List of usage examples for com.mongodb DBCollection initializeUnorderedBulkOperation
public BulkWriteOperation initializeUnorderedBulkOperation()
Creates a builder for an unordered bulk operation, consisting of an unordered collection of write requests, which can be any combination of inserts, updates, replaces, or removes.
From source file:org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentStore.java
License:Apache License
private <T extends Document> BulkUpdateResult sendBulkUpdate(Collection<T> collection, java.util.Collection<UpdateOp> updateOps, Map<String, T> oldDocs) { DBCollection dbCollection = getDBCollection(collection); BulkWriteOperation bulk = dbCollection.initializeUnorderedBulkOperation(); String[] bulkIds = new String[updateOps.size()]; int i = 0;//from www . ja va 2s . c om for (UpdateOp updateOp : updateOps) { String id = updateOp.getId(); QueryBuilder query = createQueryForUpdate(id, updateOp.getConditions()); T oldDoc = oldDocs.get(id); DBObject update; if (oldDoc == null) { query.and(Document.MOD_COUNT).exists(false); update = createUpdate(updateOp, true); } else { query.and(Document.MOD_COUNT).is(oldDoc.getModCount()); update = createUpdate(updateOp, false); } bulk.find(query.get()).upsert().updateOne(update); bulkIds[i++] = id; } BulkWriteResult bulkResult; Set<String> failedUpdates = new HashSet<String>(); Set<String> upserts = new HashSet<String>(); try { bulkResult = bulk.execute(); } catch (BulkWriteException e) { bulkResult = e.getWriteResult(); for (BulkWriteError err : e.getWriteErrors()) { failedUpdates.add(bulkIds[err.getIndex()]); } } for (BulkWriteUpsert upsert : bulkResult.getUpserts()) { upserts.add(bulkIds[upsert.getIndex()]); } return new BulkUpdateResult(failedUpdates, upserts); }
From source file:org.opencb.opencga.storage.mongodb.variant.VariantMongoDBAdaptor.java
License:Apache License
@Override public QueryResult updateStats(List<VariantStatsWrapper> variantStatsWrappers, StudyConfiguration studyConfiguration, QueryOptions options) { DBCollection coll = db.getDb().getCollection(collectionName); BulkWriteOperation builder = coll.initializeUnorderedBulkOperation(); long start = System.nanoTime(); DBObjectToVariantStatsConverter statsConverter = new DBObjectToVariantStatsConverter( studyConfigurationManager);//from ww w .j a v a2 s. c o m // VariantSource variantSource = queryOptions.get(VariantStorageManager.VARIANT_SOURCE, VariantSource.class); DBObjectToVariantConverter variantConverter = getDbObjectToVariantConverter(new Query(), options); //TODO: Use the StudyConfiguration to change names to ids // TODO make unset of 'st' if already present? for (VariantStatsWrapper wrapper : variantStatsWrappers) { Map<String, VariantStats> cohortStats = wrapper.getCohortStats(); Iterator<VariantStats> iterator = cohortStats.values().iterator(); VariantStats variantStats = iterator.hasNext() ? iterator.next() : null; List<DBObject> cohorts = statsConverter.convertCohortsToStorageType(cohortStats, studyConfiguration.getStudyId()); // TODO remove when we remove fileId // List cohorts = statsConverter.convertCohortsToStorageType(cohortStats, variantSource.getStudyId()); // TODO use when we remove fileId // add cohorts, overwriting old values if that cid, fid and sid already exists: remove and then add // db.variants.update( // {_id:<id>}, // {$pull:{st:{cid:{$in:["Cohort 1","cohort 2"]}, fid:{$in:["file 1", "file 2"]}, sid:{$in:["study 1", "study 2"]}}}} // ) // db.variants.update( // {_id:<id>}, // {$push:{st:{$each: [{cid:"Cohort 1", fid:"file 1", ... , defaultValue:3},{cid:"Cohort 2", ... , defaultValue:3}] }}} // ) if (!cohorts.isEmpty()) { String id = variantConverter.buildStorageId(wrapper.getChromosome(), wrapper.getPosition(), variantStats.getRefAllele(), variantStats.getAltAllele()); List<Integer> cohortIds = new ArrayList<>(cohorts.size()); List<Integer> studyIds = new ArrayList<>(cohorts.size()); for (DBObject cohort : cohorts) { cohortIds.add((Integer) cohort.get(DBObjectToVariantStatsConverter.COHORT_ID)); studyIds.add((Integer) cohort.get(DBObjectToVariantStatsConverter.STUDY_ID)); } DBObject find = new BasicDBObject("_id", id); DBObject update = new BasicDBObject("$pull", new BasicDBObject(DBObjectToVariantConverter.STATS_FIELD, new BasicDBObject() .append(DBObjectToVariantStatsConverter.STUDY_ID, new BasicDBObject("$in", studyIds)) // .append( // DBObjectToVariantStatsConverter.FILE_ID, // new BasicDBObject("$in", fileIds)) .append(DBObjectToVariantStatsConverter.COHORT_ID, new BasicDBObject("$in", cohortIds)))); builder.find(find).updateOne(update); DBObject push = new BasicDBObject("$push", new BasicDBObject(DBObjectToVariantConverter.STATS_FIELD, new BasicDBObject("$each", cohorts))); builder.find(find).update(push); } } // TODO handle if the variant didn't had that studyId in the files array // TODO check the substitution is done right if the stats are already present BulkWriteResult writeResult = builder.execute(); int writes = writeResult.getModifiedCount(); return new QueryResult<>("", ((int) (System.nanoTime() - start)), writes, writes, "", "", Collections.singletonList(writeResult)); }
From source file:org.opencb.opencga.storage.mongodb.variant.VariantMongoDBAdaptor.java
License:Apache License
@Override public QueryResult updateAnnotations(List<VariantAnnotation> variantAnnotations, QueryOptions queryOptions) { DBCollection coll = db.getDb().getCollection(collectionName); BulkWriteOperation builder = coll.initializeUnorderedBulkOperation(); long start = System.nanoTime(); DBObjectToVariantConverter variantConverter = getDbObjectToVariantConverter(new Query(), queryOptions); for (VariantAnnotation variantAnnotation : variantAnnotations) { String id = variantConverter.buildStorageId(variantAnnotation.getChromosome(), variantAnnotation.getStart(), variantAnnotation.getReferenceAllele(), variantAnnotation.getAlternateAllele()); DBObject find = new BasicDBObject("_id", id); DBObjectToVariantAnnotationConverter converter = new DBObjectToVariantAnnotationConverter(); DBObject convertedVariantAnnotation = converter.convertToStorageType(variantAnnotation); DBObject update = new BasicDBObject("$set", new BasicDBObject( DBObjectToVariantConverter.ANNOTATION_FIELD + ".0", convertedVariantAnnotation)); builder.find(find).updateOne(update); }/*ww w. j a v a2 s.c o m*/ BulkWriteResult writeResult = builder.execute(); return new QueryResult<>("", ((int) (System.nanoTime() - start)), 1, 1, "", "", Collections.singletonList(writeResult)); }
From source file:org.opencb.opencga.storage.mongodb.variant.VariantMongoDBAdaptor.java
License:Apache License
@Override @Deprecated/*from ww w.jav a2 s .com*/ public QueryResult updateStats(List<VariantStatsWrapper> variantStatsWrappers, int studyId, QueryOptions queryOptions) { DBCollection coll = db.getDb().getCollection(collectionName); BulkWriteOperation builder = coll.initializeUnorderedBulkOperation(); long start = System.nanoTime(); DBObjectToVariantStatsConverter statsConverter = new DBObjectToVariantStatsConverter( studyConfigurationManager); // VariantSource variantSource = queryOptions.get(VariantStorageManager.VARIANT_SOURCE, VariantSource.class); int fileId = queryOptions.getInt(VariantStorageManager.Options.FILE_ID.key()); DBObjectToVariantConverter variantConverter = getDbObjectToVariantConverter(new Query(queryOptions), queryOptions); //TODO: Use the StudyConfiguration to change names to ids // TODO make unset of 'st' if already present? for (VariantStatsWrapper wrapper : variantStatsWrappers) { Map<String, VariantStats> cohortStats = wrapper.getCohortStats(); Iterator<VariantStats> iterator = cohortStats.values().iterator(); VariantStats variantStats = iterator.hasNext() ? iterator.next() : null; List<DBObject> cohorts = statsConverter.convertCohortsToStorageType(cohortStats, studyId); // TODO remove when we remove fileId // List cohorts = statsConverter.convertCohortsToStorageType(cohortStats, variantSource.getStudyId()); // TODO use when we remove fileId // add cohorts, overwriting old values if that cid, fid and sid already exists: remove and then add // db.variants.update( // {_id:<id>}, // {$pull:{st:{cid:{$in:["Cohort 1","cohort 2"]}, fid:{$in:["file 1", "file 2"]}, sid:{$in:["study 1", "study 2"]}}}} // ) // db.variants.update( // {_id:<id>}, // {$push:{st:{$each: [{cid:"Cohort 1", fid:"file 1", ... , defaultValue:3},{cid:"Cohort 2", ... , defaultValue:3}] }}} // ) if (!cohorts.isEmpty()) { String id = variantConverter.buildStorageId(wrapper.getChromosome(), wrapper.getPosition(), variantStats.getRefAllele(), variantStats.getAltAllele()); List<String> cohortIds = new ArrayList<>(cohorts.size()); List<Integer> fileIds = new ArrayList<>(cohorts.size()); List<Integer> studyIds = new ArrayList<>(cohorts.size()); for (DBObject cohort : cohorts) { cohortIds.add((String) cohort.get(DBObjectToVariantStatsConverter.COHORT_ID)); // fileIds.add((Integer) cohort.get(DBObjectToVariantStatsConverter.FILE_ID)); studyIds.add((Integer) cohort.get(DBObjectToVariantStatsConverter.STUDY_ID)); } DBObject find = new BasicDBObject("_id", id); DBObject update = new BasicDBObject("$pull", new BasicDBObject(DBObjectToVariantConverter.STATS_FIELD, new BasicDBObject() .append(DBObjectToVariantStatsConverter.STUDY_ID, new BasicDBObject("$in", studyIds)) // .append( // DBObjectToVariantStatsConverter.FILE_ID, // new BasicDBObject("$in", fileIds)) .append(DBObjectToVariantStatsConverter.COHORT_ID, new BasicDBObject("$in", cohortIds)))); builder.find(find).updateOne(update); DBObject push = new BasicDBObject("$push", new BasicDBObject(DBObjectToVariantConverter.STATS_FIELD, new BasicDBObject("$each", cohorts))); builder.find(find).update(push); } } // TODO handle if the variant didn't had that studyId in the files array // TODO check the substitution is done right if the stats are already present BulkWriteResult writeResult = builder.execute(); int writes = writeResult.getModifiedCount(); return new QueryResult<>("", ((int) (System.nanoTime() - start)), writes, writes, "", "", Collections.singletonList(writeResult)); }
From source file:org.springframework.data.mongodb.core.DefaultBulkOperations.java
License:Apache License
private final BulkWriteOperation initBulkOperation() { DBCollection collection = mongoOperations.getCollection(collectionName); switch (bulkMode) { case ORDERED: return collection.initializeOrderedBulkOperation(); case UNORDERED: return collection.initializeUnorderedBulkOperation(); }// w w w .j a v a 2 s. c om throw new IllegalStateException("BulkMode was null!"); }