@Override public MongoEndpointProfile updateServerProfile(byte[] keyHash, int version, String serverProfile) { LOG.debug("Update server endpoint profile for endpoint with key hash {}, " + "schema version is {}", keyHash, version); updateFirst( query(where(EP_ENDPOINT_KEY_HASH).is(keyHash)), update( EP_SERVER_PROFILE_PROPERTY, MongoDaoUtil.encodeReservedCharacteres((DBObject) JSON.parse(serverProfile))) .set(EP_SERVER_PROFILE_VERSION_PROPERTY, version)); return findById(ByteBuffer.wrap(keyHash)); } }
/** * Creates an {@link Update} instance from the given {@link Document}. Allows to explicitly exclude fields from making * it into the created {@link Update} object. Note, that this will set attributes directly and <em>not</em> use * {@literal $set}. This means fields not given in the {@link Document} will be nulled when executing the update. To * create an only-updating {@link Update} instance of a {@link Document}, call {@link #set(String, Object)} for each * value in it. * * @param object the source {@link Document} to create the update from. * @param exclude the fields to exclude. * @return */ public static Update fromDocument(Document object, String... exclude) { Update update = new Update(); List<String> excludeList = Arrays.asList(exclude); for (String key : object.keySet()) { if (excludeList.contains(key)) { continue; } Object value = object.get(key); update.modifierOps.put(key, value); if (isKeyword(key) && value instanceof Document) { update.keysToUpdate.addAll(((Document) value).keySet()); } else { update.keysToUpdate.add(key); } } return update; }
private int getNextId() { Query query = Query.query(Criteria.where("_id").is(SEQUENCE_NAME)); query.fields().include(SEQUENCE); return (Integer) this.template.findAndModify(query, new Update().inc(SEQUENCE, 1), FindAndModifyOptions.options().returnNew(true).upsert(true), Map.class, this.collectionName).get(SEQUENCE); // NOSONAR - never returns null }
@Override public void completeGroup(Object groupId) { this.updateGroup(groupId, lastModifiedUpdate().set(GROUP_COMPLETE_KEY, true)); }
/** * If the specified key is not already associated with a value, associate it with the given value. * This is equivalent to * <pre> {@code * if (!map.containsKey(key)) * return map.put(key, value); * else * return map.get(key); * }</pre> * except that the action is performed atomically. * <p> * @param key the metadata entry key * @param value the metadata entry value to store * @return null if successful, the old value otherwise. * @see java.util.concurrent.ConcurrentMap#putIfAbsent(Object, Object) */ @Override public String putIfAbsent(String key, String value) { Assert.hasText(key, "'key' must not be empty."); Assert.hasText(value, "'value' must not be empty."); Query query = new Query(Criteria.where(ID_FIELD).is(key)); query.fields().exclude(ID_FIELD); @SuppressWarnings("unchecked") Map<String, String> result = this.template.findAndModify(query, new Update().setOnInsert(VALUE, value), new FindAndModifyOptions().upsert(true), Map.class, this.collectionName); return result == null ? null : result.get(VALUE); }
@Override public Optional<MongoCredentials> updateStatus(String applicationId, String credentialsId, CredentialsStatus status) { LOG.debug("Settings status [{}] for credentials [{}] in application [{}]", status, credentialsId, applicationId); updateFirst( Query.query(Criteria.where(MongoModelConstants.CREDENTIALS_ID) .is(credentialsId) .and(MongoModelConstants.APPLICATION_ID) .is(applicationId)), Update.update(MongoModelConstants.CREDENTIAL_STATUS, status)); return this.find(applicationId, credentialsId); }
@Override public int addTag(RecordQuery recordQuery, Tag tag) { Update update = new Update().push(F_TAGS, tag.getTag()); logger.debug("executing mongo update: {} on records selected by: {}", update.getUpdateObject(), recordQuery.getQuery()); WriteResult res = mongoTemplate.updateMulti(recordQuery.getQuery(), update, collectionName); logger.debug("added tag " + tag + " to " + res.getN() + " records"); return res.getN(); }
@Override public void inc(String key) { inc(key, 1L); }
/** * Propagates {@link #value(Object)} to {@code $push} * * @param value * @return never {@literal null}. */ public Update value(Object value) { if (this.modifiers.isEmpty()) { return Update.this.push(key, value); } this.modifiers.addModifier(new Each(Collections.singletonList(value))); return Update.this.push(key, this.modifiers); }
/** * Perform MongoDB {@code INC} operation for the document, which contains the {@link MessageDocument} * {@code sequence}, and return the new incremented value for the new {@link MessageDocument}. * The {@link #SEQUENCE_NAME} document is created on demand. * @return the next sequence value. */ protected int getNextId() { Query query = Query.query(Criteria.where("_id").is(SEQUENCE_NAME)); query.fields().include(MessageDocumentFields.SEQUENCE); return (Integer) this.mongoTemplate.findAndModify(query, new Update().inc(MessageDocumentFields.SEQUENCE, 1), FindAndModifyOptions.options().returnNew(true).upsert(true), Map.class, this.collectionName) .get(MessageDocumentFields.SEQUENCE); // NOSONAR - never returns null }
@Override public void setLastReleasedSequenceNumberForGroup(Object groupId, int sequenceNumber) { this.updateGroup(groupId, lastModifiedUpdate().set(LAST_RELEASED_SEQUENCE_NUMBER, sequenceNumber)); }
/** * Creates an {@link Update} instance from the given {@link Document}. Allows to explicitly exclude fields from making * it into the created {@link Update} object. Note, that this will set attributes directly and <em>not</em> use * {@literal $set}. This means fields not given in the {@link Document} will be nulled when executing the update. To * create an only-updating {@link Update} instance of a {@link Document}, call {@link #set(String, Object)} for each * value in it. * * @param object the source {@link Document} to create the update from. * @param exclude the fields to exclude. * @return */ public static Update fromDocument(Document object, String... exclude) { Update update = new Update(); List<String> excludeList = Arrays.asList(exclude); for (String key : object.keySet()) { if (excludeList.contains(key)) { continue; } Object value = object.get(key); update.modifierOps.put(key, value); if (isKeyword(key) && value instanceof Document) { update.keysToUpdate.addAll(((Document) value).keySet()); } else { update.keysToUpdate.add(key); } } return update; }
private static Update lastModifiedUpdate() { return Update.update(MessageDocumentFields.LAST_MODIFIED_TIME, System.currentTimeMillis()); }
@Override public void inc(String version) { delegate.inc(version); }