/** * Combine a list of updates into a single update. * * @param updates the list of updates * @return a combined update */ public static Bson combine(final Bson... updates) { return combine(asList(updates)); }
@Override public void save(SessionData sessionData) { String sessionId = sessionData.getId(); this.sessions.updateOne( eq(SESSION_ID, sessionId), combine( set(SESSION_ID, sessionId), set(SESSION_TTL, new Date()), set(SESSION_DATA, transcoder.encode(sessionData))), new UpdateOptions().upsert(true)); }
/** * Combine a list of updates into a single update. * * @param updates the list of updates * @return a combined update */ public static Bson combine(final Bson... updates) { return combine(asList(updates)); }
public long update() { return MongoKit.INSTANCE.update(collectionName, and(query), Updates.combine(data)); }
public boolean updateOne() { return MongoKit.INSTANCE.updateOne(collectionName, and(query), Updates.combine(data)) > 0; }
public long update() { return MongoKit.INSTANCE.update(collectionName, and(query), Updates.combine(data)); }
public boolean updateOne() { return MongoKit.INSTANCE.updateOne(collectionName, and(query), Updates.combine(data)) > 0; }
@Override public QueryResult updateProjectMetadata(ProjectMetadata projectMetadata, boolean updateCounters) { Document mongo = new GenericDocumentComplexConverter<>(ProjectMetadata.class).convertToStorageType(projectMetadata); // Update field by field, instead of replacing the whole object to preserve existing fields like "_lock" List<Bson> updates = new ArrayList<>(mongo.size()); mongo.forEach((s, o) -> { // Do not update counters if (updateCounters || !s.equals(COUNTERS_FIELD)) { updates.add(new Document("$set", new Document(s, o))); } }); return collection.update(QUERY, Updates.combine(updates), new QueryOptions(UPSERT, true)); }
@Override public QueryResult updateStudyConfiguration(StudyConfiguration studyConfiguration, QueryOptions options) { Document studyMongo = new DocumentToStudyConfigurationConverter().convertToStorageType(studyConfiguration); // Update field by field, instead of replacing the whole object to preserve existing fields like "_lock" Document query = new Document("_id", studyConfiguration.getStudyId()); List<Bson> updates = new ArrayList<>(studyMongo.size()); studyMongo.forEach((s, o) -> updates.add(new Document("$set", new Document(s, o)))); QueryResult<UpdateResult> queryResult = collection.update(query, Updates.combine(updates), new QueryOptions(UPSERT, true)); // studyConfigurationMap.put(studyConfiguration.getStudyId(), studyConfiguration); return queryResult; }
@Override public void unlock() { // Set lockUtil to now or lockAtLeastUntil whichever is later getCollection().findOneAndUpdate( eq(ID, lockConfiguration.getName()), combine(set(LOCK_UNTIL, Date.from(lockConfiguration.getUnlockTime()))) ); } }
private void addPrivateVariableMap(long entryId, Map<String, String> variableMap, boolean isVersioned) throws CatalogDBException { Document queryDocument = new Document(PRIVATE_UID, entryId); if (isVersioned) { queryDocument.append(LAST_OF_VERSION, true); } List<Bson> setMap = new ArrayList<>(variableMap.size()); for (Map.Entry<String, String> entry : variableMap.entrySet()) { setMap.add(Updates.set(AnnotationSetParams.PRIVATE_VARIABLE_SET_MAP.key() + "." + entry.getKey(), entry.getValue())); } QueryResult<UpdateResult> update = getCollection().update(queryDocument, Updates.combine(setMap), new QueryOptions("multi", true)); if (update.first().getModifiedCount() < 1 && update.first().getMatchedCount() == 0) { throw new CatalogDBException("Could not add new private map information"); } }
public static Bson updateModified(UserId userId, long timestamp) { return Updates.combine( Updates.set(MODIFIED_AT, TimestampSerializer.toIsoDateTime(timestamp)), Updates.set(MODIFIED_BY, userId.getUserName()) ); }
@Override public void set(String aCounterName, long aValue) { collection .findOneAndUpdate( eq("_id", aCounterName), combine( Updates.set(DSL.VALUE, aValue), setOnInsert("_id", aCounterName), setOnInsert(DSL.CREATE_TIME, new Date()) ), new FindOneAndUpdateOptions() .upsert(true) ); }
@Override public QueryResult updateCustomAnnotations(Query query, String name, AdditionalAttribute attribute, long timeStamp, QueryOptions options) { Document queryDocument = queryParser.parseQuery(query); Document updateDocument = DocumentToVariantAnnotationConverter.convertToStorageType(attribute); return variantsCollection.update(queryDocument, combine(set(DocumentToVariantConverter.CUSTOM_ANNOTATION_FIELD + '.' + name, updateDocument), getSetIndexNotSynchronized(timeStamp)), new QueryOptions(MULTI, true)); }
private QueryResult<UpdateResult> removeStudyFromVariants(int studyId, Bson query, long timestamp) { // { $pull : { files : { sid : <studyId> } } } Bson update = combine( pull(DocumentToVariantConverter.STUDIES_FIELD, eq(STUDYID_FIELD, studyId)), pull(DocumentToVariantConverter.STATS_FIELD, eq(DocumentToVariantStatsConverter.STUDY_ID, studyId)), getSetIndexNotSynchronized(timestamp) ); logger.debug("removeStudy: query = {}", query.toBsonDocument(Document.class, MongoClient.getDefaultCodecRegistry())); logger.debug("removeStudy: update = {}", update.toBsonDocument(Document.class, MongoClient.getDefaultCodecRegistry())); QueryResult<UpdateResult> result = variantsCollection.update(query, update, new QueryOptions(MULTI, true)); logger.debug("removeStudy: matched = {}", result.first().getMatchedCount()); logger.debug("removeStudy: modified = {}", result.first().getModifiedCount()); return result; }
private void removeAllAnnotationSets(long entryId, boolean isVersioned) throws CatalogDBException { Document queryDocument = new Document(PRIVATE_UID, entryId); if (isVersioned) { queryDocument.append(LAST_OF_VERSION, true); } // We empty the annotation sets list and the private map Bson bsonUpdate = Updates.combine( Updates.set(AnnotationSetParams.ANNOTATION_SETS.key(), Collections.emptyList()), Updates.set(AnnotationSetParams.PRIVATE_VARIABLE_SET_MAP.key(), Collections.emptyMap()) ); QueryResult<UpdateResult> update = getCollection().update(queryDocument, bsonUpdate, new QueryOptions()); if (update.first().getModifiedCount() < 1 && update.first().getMatchedCount() == 0) { throw new CatalogDBException("Could not remove all annotationSets"); } }
@Override public void appendMessage(final String jobId, final JobMessage jobMessage) { collectionWithWriteTimeout(mongoProperties.getDefaultWriteTimeout(), TimeUnit.MILLISECONDS).updateOne(eq(ID, jobId), combine(push(JobStructure.MESSAGES.key(), encodeJobMessage(jobMessage)), set(JobStructure.LAST_UPDATED.key(), Date.from(jobMessage.getTimestamp().toInstant())))); }
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; }
@Override public void containerFor(Id id, SingleFeatureBean container) { checkNotNull(id, "id"); checkNotNull(container, "container"); final String ownerId = idConverter.convert(id); final ContainerDocument newContainer = ContainerDocument.fromBean(container); final Bson filter = eq(ModelDocument.F_ID, ownerId); final Bson update = combine( set(concat(ModelDocument.F_CONTAINER, ContainerDocument.F_OWNER), newContainer.getOwner()), set(concat(ModelDocument.F_CONTAINER, ContainerDocument.F_ID), newContainer.getId()) ); documents.updateOne(filter, update, new UpdateOptions().upsert(true)); }
@Override public boolean metaClassFor(Id id, ClassBean metaClass) { checkNotNull(id, "id"); checkNotNull(metaClass, "metaClass"); final String ownerId = idConverter.convert(id); final ClassDocument newMetaClass = ClassDocument.fromBean(metaClass); final Bson filter = eq(ModelDocument.F_ID, ownerId); final Bson existsFilter = and(filter, exists(ModelDocument.F_METACLASS)); final boolean notExists = documents.countDocuments(existsFilter, new CountOptions().limit(1)) == 0; if (notExists) { final Bson update = combine( setOnInsert(concat(ModelDocument.F_METACLASS, ClassDocument.F_NAME), newMetaClass.getName()), setOnInsert(concat(ModelDocument.F_METACLASS, ClassDocument.F_URI), newMetaClass.getUri()) ); documents.updateOne(filter, update, new UpdateOptions().upsert(true)); } return notExists; }