Search in sources :

Example 1 with Watch

use of sirius.kernel.commons.Watch in project sirius-db by scireum.

the class Mongo method createIndices.

private void createIndices(String database, MongoDatabase db) {
    for (IndexDescription idx : indexDescriptions) {
        Watch w = Watch.start();
        try {
            LOG.INFO("Creating indices in Mongo DB: %s", idx.getClass().getName());
            idx.createIndices(database, db);
            LOG.INFO("Completed indices for: %s (%s)", idx.getClass().getName(), w.duration());
        } catch (Exception t) {
            Exceptions.handle().to(LOG).error(t).withSystemErrorMessage("Error while creating indices for '%s': %s (%s)", idx.getClass().getName()).handle();
        }
    }
}
Also used : Watch(sirius.kernel.commons.Watch)

Example 2 with Watch

use of sirius.kernel.commons.Watch in project sirius-db by scireum.

the class Deleter method singleFrom.

/**
 * Executes the delete statement on the given collection.
 *
 * @param collection the name of the collection to delete documents from
 * @return the result of the delete operation
 */
public DeleteResult singleFrom(String collection) {
    Watch w = Watch.start();
    try {
        if (Mongo.LOG.isFINE()) {
            Mongo.LOG.FINE("DELETE: %s\nFilter: %s", collection, filterObject);
        }
        return mongo.db(database).getCollection(collection).deleteOne(filterObject);
    } finally {
        mongo.callDuration.addValue(w.elapsedMillis());
        if (Microtiming.isEnabled()) {
            w.submitMicroTiming("mongo", "DELETE - " + collection + ": " + filterObject.keySet());
        }
        traceIfRequired(collection, w);
    }
}
Also used : Watch(sirius.kernel.commons.Watch)

Example 3 with Watch

use of sirius.kernel.commons.Watch in project sirius-db by scireum.

the class Deleter method manyFrom.

/**
 * Executes the delete statement on the given collection.
 *
 * @param collection the name of the collection to delete documents from
 * @return the result of the delete operation
 */
public DeleteResult manyFrom(String collection) {
    Watch w = Watch.start();
    try {
        if (Mongo.LOG.isFINE()) {
            Mongo.LOG.FINE("DELETE: %s\nFilter: %s", collection, filterObject);
        }
        return mongo.db(database).getCollection(collection).deleteMany(filterObject);
    } finally {
        mongo.callDuration.addValue(w.elapsedMillis());
        if (Microtiming.isEnabled()) {
            w.submitMicroTiming("mongo", "DELETE - " + collection + ": " + filterObject.keySet());
        }
        traceIfRequired(collection, w);
    }
}
Also used : Watch(sirius.kernel.commons.Watch)

Example 4 with Watch

use of sirius.kernel.commons.Watch in project sirius-db by scireum.

the class Finder method countIn.

/**
 * Counts the number of documents in the result of the given query.
 * <p>
 * Note that limits are ignored for this query.
 * If there are no filters in this query and forceAccurate is false, a pre-counted estimate is returned instead.
 *
 * @param collection    the collection to search in
 * @param forceAccurate if set to <tt>true</tt> we'll never use <b>estimatedDocumentCount</b> which is way more efficient but might return wrong values in case a cluster is active which had experienced an unclean shutdown.
 * @param maxTimeMS     the maximum process time for this cursor in milliseconds, 0 for unlimited
 * @return the number of documents found, wrapped in an Optional, or an empty Optional if the query timed out
 */
public Optional<Long> countIn(String collection, boolean forceAccurate, long maxTimeMS) {
    Watch watch = Watch.start();
    try {
        if (filterObject.isEmpty() && !forceAccurate) {
            return Optional.of(getMongoCollection(collection).estimatedDocumentCount(new EstimatedDocumentCountOptions().maxTime(maxTimeMS, TimeUnit.MILLISECONDS)));
        }
        return Optional.of(getMongoCollection(collection).countDocuments(filterObject, new CountOptions().maxTime(maxTimeMS, TimeUnit.MILLISECONDS)));
    } catch (MongoExecutionTimeoutException e) {
        Exceptions.ignore(e);
        return Optional.empty();
    } finally {
        long callDuration = watch.elapsedMillis();
        mongo.callDuration.addValue(callDuration);
        if (readPreference != null && readPreference.isSlaveOk()) {
            mongo.secondaryCallDuration.addValue(callDuration);
        }
        if (Microtiming.isEnabled()) {
            watch.submitMicroTiming(KEY_MONGO, "COUNT - " + collection + ": " + filterObject.keySet());
        }
        traceIfRequired(collection, watch);
    }
}
Also used : MongoExecutionTimeoutException(com.mongodb.MongoExecutionTimeoutException) Watch(sirius.kernel.commons.Watch) CountOptions(com.mongodb.client.model.CountOptions) EstimatedDocumentCountOptions(com.mongodb.client.model.EstimatedDocumentCountOptions) EstimatedDocumentCountOptions(com.mongodb.client.model.EstimatedDocumentCountOptions)

Example 5 with Watch

use of sirius.kernel.commons.Watch in project sirius-db by scireum.

the class Finder method aggregateIn.

/**
 * Aggregates the documents in the result of the given query with an accumulator operator.
 * <p>
 * Note that limits are ignored for this query.
 *
 * @param collection the collection to search in
 * @param field      the field to aggregate
 * @param operator   the accumulation operator to aggregate with
 * @return the result of the accumulation (usually Integer, Double or List)
 * @see <a href="https://docs.mongodb.com/manual/reference/operator/aggregation/group/#accumulator-operator">MongoDB Reference</a>
 */
public Value aggregateIn(@Nonnull String collection, @Nonnull Mapping field, @Nonnull String operator) {
    Watch watch = Watch.start();
    try {
        BasicDBObject groupStage = new BasicDBObject().append(Mango.ID_FIELD, null).append("result", new BasicDBObject(operator, "$" + field));
        MongoCursor<Document> queryResult = getMongoCollection(collection).aggregate(Arrays.asList(new BasicDBObject(OPERATOR_MATCH, filterObject), new BasicDBObject("$group", groupStage))).iterator();
        if (queryResult.hasNext()) {
            return Value.of(queryResult.next().get("result"));
        } else {
            return Value.EMPTY;
        }
    } finally {
        long callDuration = watch.elapsedMillis();
        mongo.callDuration.addValue(callDuration);
        if (readPreference != null && readPreference.isSlaveOk()) {
            mongo.secondaryCallDuration.addValue(callDuration);
        }
        if (Microtiming.isEnabled()) {
            watch.submitMicroTiming(KEY_MONGO, "AGGREGATE - " + collection + "." + field + " (" + operator + "): " + filterObject.keySet());
        }
        traceIfRequired("aggregate-" + collection, watch);
    }
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) Watch(sirius.kernel.commons.Watch) Document(org.bson.Document)

Aggregations

Watch (sirius.kernel.commons.Watch)53 PreparedStatement (java.sql.PreparedStatement)8 SQLException (java.sql.SQLException)8 Connection (java.sql.Connection)6 HandledException (sirius.kernel.health.HandledException)6 Document (org.bson.Document)5 ResultSet (java.sql.ResultSet)4 TaskContext (sirius.kernel.async.TaskContext)4 ArrayList (java.util.ArrayList)3 OMA (sirius.db.jdbc.OMA)3 Property (sirius.db.mixing.Property)3 BasicDBObject (com.mongodb.BasicDBObject)2 ProcessContext (sirius.biz.process.ProcessContext)2 ObjectStorageSpace (sirius.biz.storage.layer1.ObjectStorageSpace)2 Operator (sirius.db.jdbc.Operator)2 Operation (sirius.kernel.async.Operation)2 Context (sirius.kernel.commons.Context)2 Limit (sirius.kernel.commons.Limit)2 AS400 (com.ibm.as400.access.AS400)1 ProgramCall (com.ibm.as400.access.ProgramCall)1