Search in sources :

Example 16 with UpdateResult

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()));
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) List(java.util.List) Document(org.bson.Document) UpdateResult(com.mongodb.client.result.UpdateResult)

Example 17 with UpdateResult

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);
}
Also used : Document(org.bson.Document) UpdateResult(com.mongodb.client.result.UpdateResult) UpdateOptions(com.mongodb.client.model.UpdateOptions)

Aggregations

UpdateResult (com.mongodb.client.result.UpdateResult)17 Document (org.bson.Document)10 Test (org.junit.Test)9 Exchange (org.apache.camel.Exchange)5 Processor (org.apache.camel.Processor)5 Formatter (java.util.Formatter)4 Bson (org.bson.conversions.Bson)4 BasicDBObject (com.mongodb.BasicDBObject)3 DBObject (com.mongodb.DBObject)3 List (java.util.List)3 BsonDocument (org.bson.BsonDocument)3 UpdateOptions (com.mongodb.client.model.UpdateOptions)2 DeleteResult (com.mongodb.client.result.DeleteResult)2 ArrayList (java.util.ArrayList)2 Arrays.asList (java.util.Arrays.asList)2 HashMap (java.util.HashMap)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 Block (com.mongodb.Block)1 MongoClient (com.mongodb.MongoClient)1 MongoClientURI (com.mongodb.MongoClientURI)1