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();
}
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations