use of com.mongodb.client.result.UpdateResult in project graylog2-server by Graylog2.
the class V20170110150100_FixAlertConditionsMigration method upgrade.
@Override
@SuppressWarnings("unchecked")
public void upgrade() {
if (clusterConfigService.get(MigrationCompleted.class) != null) {
LOG.debug("Migration already done.");
return;
}
final ImmutableSet.Builder<String> modifiedStreams = ImmutableSet.builder();
final ImmutableSet.Builder<String> modifiedAlertConditions = ImmutableSet.builder();
for (Document document : collection.find().sort(ascending(FIELD_CREATED_AT))) {
final String streamId = document.getObjectId(FIELD_ID).toHexString();
if (!document.containsKey(FIELD_ALERT_CONDITIONS)) {
continue;
}
final List<Document> alertConditions = (List<Document>) document.get(FIELD_ALERT_CONDITIONS);
// Need to check if the following fields are integers:
//
// FieldContentValue: grace, backlog
// FieldValue: grace, backlog, time, threshold
// MessageCount: grace, backlog, time, threshold
final Set<String> intFields = ImmutableSet.of("grace", "backlog", "time", "threshold");
for (Document alertCondition : alertConditions) {
final String alertConditionId = alertCondition.get("id", String.class);
final String alertConditionTitle = alertCondition.get("title", String.class);
final Document parameters = alertCondition.get("parameters", Document.class);
for (String field : intFields) {
final Object fieldValue = parameters.get(field);
// No need to convert anything if the field does not exist or is already an integer
if (fieldValue == null || fieldValue instanceof Integer) {
continue;
}
if (!(fieldValue instanceof String)) {
LOG.warn("Field <{}> in alert condition <{}> ({}) of stream <{}> is not a string but a <{}>, not trying to convert it!", field, alertConditionId, alertConditionTitle, streamId, fieldValue.getClass().getCanonicalName());
continue;
}
final String stringValue = parameters.get(field, String.class);
final Integer intValue = Ints.tryParse(stringValue);
LOG.info("Converting value for field <{}> from string to integer in alert condition <{}> ({}) of stream <{}>", field, alertConditionId, alertConditionTitle, streamId);
if (intValue == null) {
LOG.error("Unable to parse \"{}\" into integer!", fieldValue);
}
final UpdateResult result = collection.updateOne(eq(FIELD_ALERT_CONDITIONS_ID, alertConditionId), set(ALERT_CONDITIONS_PARAMETERS_PREFIX + field, intValue));
// Use UpdateResult#getMatchedCount() instead of #getModifiedCount() to make it work on MongoDB 2.4
if (result.getMatchedCount() > 0) {
modifiedStreams.add(streamId);
modifiedAlertConditions.add(alertConditionId);
} else {
LOG.warn("No document modified for alert condition <{}> ({})", alertConditionId, alertConditionTitle);
}
}
}
}
clusterConfigService.write(MigrationCompleted.create(modifiedStreams.build(), modifiedAlertConditions.build()));
}
use of com.mongodb.client.result.UpdateResult in project sling by apache.
the class MongoDBNoSqlAdapter method store.
@Override
public boolean store(NoSqlData data) {
Document envelope = new Document();
envelope.put(PN_PATH, data.getPath());
envelope.put(PN_DATA, new Document(data.getProperties(MultiValueMode.LISTS)));
// for list-children query efficiency store parent path as well
String parentPath = ResourceUtil.getParent(data.getPath());
if (parentPath != null) {
envelope.put(PN_PARENT_PATH, parentPath);
}
UpdateResult result = collection.replaceOne(Filters.eq(PN_PATH, data.getPath()), envelope, new UpdateOptions().upsert(true));
// return true if a new entry was inserted, false if an existing was replaced
return (result.getMatchedCount() == 0);
}
Aggregations