@Override public void upgrade() { if (clusterConfigService.get(MigrationCompleted.class) != null) { LOG.debug("Migration already done."); return; } // Do not overwrite an existing default index config boolean defaultDone = clusterConfigService.get(DefaultIndexSetConfig.class) != null; final ImmutableSet.Builder<String> builder = ImmutableSet.builder(); final FindIterable<Document> documents = collection.find(exists(FIELD_DEFAULT)).sort(ascending(FIELD_CREATION_DATE)); for (final Document document : documents) { final ObjectId id = document.getObjectId(FIELD_ID); final String idString = id.toHexString(); final boolean isDefault = firstNonNull(document.getBoolean(FIELD_DEFAULT), false); if (!defaultDone && isDefault) { defaultDone = true; clusterConfigService.write(DefaultIndexSetConfig.create(idString)); } final long modifiedCount = collection.updateOne(eq(FIELD_ID, id), unset(FIELD_DEFAULT)).getMatchedCount(); if (modifiedCount > 0) { LOG.info("Removed <default> field from index set <{}> ({})", document.getString(FIELD_TITLE), idString); builder.add(idString); } else { LOG.error("Couldn't remove <default> field from index set <{}> ({})", document.getString(FIELD_TITLE), idString); } } clusterConfigService.write(MigrationCompleted.create(builder.build())); }
updates.add(Updates.unset(fieldName)); } else {
private void removePrivateVariableMap(long entryId, Map<String, String> privateVariableMapToSet, boolean isVersioned) throws CatalogDBException { Document queryDocument = new Document(PRIVATE_UID, entryId); if (isVersioned) { queryDocument.append(LAST_OF_VERSION, true); } for (Map.Entry<String, String> entry : privateVariableMapToSet.entrySet()) { // We only want to remove the private variable map if it is not currently in use by any annotation set queryDocument.append(AnnotationSetParams.VARIABLE_SET_ID.key(), new Document("$ne", Long.parseLong(entry.getKey()))); Bson unset = Updates.unset(AnnotationSetParams.PRIVATE_VARIABLE_SET_MAP.key() + "." + entry.getKey()); QueryResult<UpdateResult> update = getCollection().update(queryDocument, unset, new QueryOptions()); if (update.first().getModifiedCount() < 1 && update.first().getMatchedCount() == 1) { throw new CatalogDBException("Could not remove private map information"); } } }
Bson updateStage = combine( pull(StageDocumentToVariantConverter.STUDY_FILE_FIELD, studyId.toString()), unset(studyId.toString())); if (ids.size() == batchSize || !cursor.hasNext()) { updatedStageDocuments += stageCollection.update(in("_id", ids), updateStage, new QueryOptions(MULTI, true)) studyUpdate.add(unset(String.valueOf(studyId) + '.' + fileId));
@Override public void removeContainer(Id id) { checkNotNull(id, "id"); final String ownerId = idConverter.convert(id); final Bson filter = and(eq(ModelDocument.F_ID, ownerId), exists(ModelDocument.F_CONTAINER)); final Bson update = unset(ModelDocument.F_CONTAINER); documents.updateOne(filter, update); }
@Override public String setValue(final String jobType, final String key, final String value) { final Document previous; if (value != null) { previous = collection.findOneAndUpdate(eq(ID, jobType), set(key, value), UPSERT); } else { previous = collection.findOneAndUpdate(eq(ID, jobType), unset(key), UPSERT); } return previous != null ? previous.getString("key") : null; }
@Override public void removeValue(SingleFeatureBean feature) { checkNotNull(feature, "feature"); final String ownerId = idConverter.convert(feature.owner()); final String featureId = Integer.toString(feature.id()); final String fieldName = concat(ModelDocument.F_SINGLE_FEATURE, featureId); final Bson filter = and(eq(ModelDocument.F_ID, ownerId), exists(fieldName)); final Bson update = unset(fieldName); documents.updateOne(filter, update); }
@Override public String setValue(final String jobType, final String key, final String value) { final Document previous; if (value != null) { previous = collection.findOneAndUpdate(eq(ID, jobType), set(key, value), UPSERT); } else { previous = collection.findOneAndUpdate(eq(ID, jobType), unset(key), UPSERT); } return previous != null ? previous.getString("key") : null; }
List<Bson> updates = new LinkedList<>(); for (Integer fileId : fileIds) { updates.add(unset(studyId + "." + fileId));
Bson combine = combine(pull(StageDocumentToVariantConverter.STUDY_FILE_FIELD, studyId.toString()), unset(studyId.toString())); logger.debug("removeStudy: stage query = " + eq.toBsonDocument(Document.class, MongoClient.getDefaultCodecRegistry())); logger.debug("removeStudy: stage update = " + combine.toBsonDocument(Document.class, MongoClient.getDefaultCodecRegistry()));
@Override public void removeAllValues(SingleFeatureBean feature) { checkNotNull(feature, "feature"); final String ownerId = idConverter.convert(feature.owner()); final String fieldName = ModelDocument.F_MANY_FEATURE; final Bson filter = and(eq(ModelDocument.F_ID, ownerId), exists(fieldName)); final Bson update = unset(fieldName); documents.updateOne(filter, update); }
@Override public void upgrade() { if (clusterConfigService.get(MigrationCompleted.class) != null) { LOG.debug("Migration already done."); return; } // Do not overwrite an existing default index config boolean defaultDone = clusterConfigService.get(DefaultIndexSetConfig.class) != null; final ImmutableSet.Builder<String> builder = ImmutableSet.builder(); final FindIterable<Document> documents = collection.find(exists(FIELD_DEFAULT)).sort(ascending(FIELD_CREATION_DATE)); for (final Document document : documents) { final ObjectId id = document.getObjectId(FIELD_ID); final String idString = id.toHexString(); final boolean isDefault = firstNonNull(document.getBoolean(FIELD_DEFAULT), false); if (!defaultDone && isDefault) { defaultDone = true; clusterConfigService.write(DefaultIndexSetConfig.create(idString)); } final long modifiedCount = collection.updateOne(eq(FIELD_ID, id), unset(FIELD_DEFAULT)).getMatchedCount(); if (modifiedCount > 0) { LOG.info("Removed <default> field from index set <{}> ({})", document.getString(FIELD_TITLE), idString); builder.add(idString); } else { LOG.error("Couldn't remove <default> field from index set <{}> ({})", document.getString(FIELD_TITLE), idString); } } clusterConfigService.write(MigrationCompleted.create(builder.build())); }
public static long cleanStageCollection(MongoDBCollection stageCollection, int studyId, int fileId) { //Delete those studies that have duplicated variants. Those are not inserted, so they are not new variants. long modifiedCount = stageCollection.update( and(exists(studyId + "." + fileId + ".1"), exists(studyId + "." + NEW_STUDY_FIELD, false)), unset(Integer.toString(studyId)), new QueryOptions(MongoDBCollection.MULTI, true)).first().getModifiedCount(); modifiedCount += stageCollection.update( exists(studyId + "." + fileId), combine( // unset(studyId + "." + fileId), set(studyId + "." + fileId, null), set(studyId + "." + NEW_STUDY_FIELD, false) ), new QueryOptions(MongoDBCollection.MULTI, true)).first().getModifiedCount(); return modifiedCount; }
@Test public void testUpdateUnset() throws Exception { Document obj = json("_id: 1, a: 1, b: null, c: 'value'"); collection.insertOne(obj); assertMongoWriteException(() -> collection.updateOne(obj, json("$unset: {_id: ''}")), 66, "Performing an update on the path '_id' would modify the immutable field '_id'"); collection.updateOne(obj, json("$unset: {a:'', b:''}")); assertThat(collection.find().first()).isEqualTo(json("_id: 1, c: 'value'")); collection.updateOne(obj, Updates.unset("c.y")); assertThat(collection.find().first()).isEqualTo(json("_id: 1, c: 'value'")); collection.replaceOne(json("_id: 1"), json("a: {b: 'foo', c: 'bar'}")); collection.updateOne(json("_id: 1"), json("$unset: {'a.b':1}")); assertThat(collection.find().first()).isEqualTo(json("_id: 1, a: {c: 'bar'}")); }
@Override public QueryResult<Long> deleteConfig(String userId, String name) throws CatalogDBException { long startTime = startQuery(); // Insert the config Bson bsonQuery = Filters.and( Filters.eq(QueryParams.ID.key(), userId), Filters.exists(QueryParams.CONFIGS.key() + "." + name) ); Bson update = Updates.unset(QueryParams.CONFIGS.key() + "." + name); QueryResult<UpdateResult> queryResult = userCollection.update(bsonQuery, update, null); if (queryResult.first().getModifiedCount() == 0) { throw new CatalogDBException("Could not delete " + name + " configuration "); } QueryResult<User> userQueryResult = get(userId, new QueryOptions(), ""); if (userQueryResult.getNumResults() == 0) { throw new CatalogDBException("Internal error: Could not retrieve user " + userId + " information"); } return endQuery("Delete config", startTime, Arrays.asList(queryResult.first().getModifiedCount())); }