Search in sources :

Example 11 with StopWatch

use of core.framework.util.StopWatch in project core-ng-project by neowu.

the class MongoCollectionImpl method bulkDelete.

@Override
public long bulkDelete(List<?> ids) {
    StopWatch watch = new StopWatch();
    int deletedRows = 0;
    try {
        List<DeleteOneModel<T>> models = new ArrayList<>(ids.size());
        for (Object id : ids) {
            models.add(new DeleteOneModel<>(Filters.eq("_id", id)));
        }
        BulkWriteResult result = collection().bulkWrite(models, new BulkWriteOptions().ordered(false));
        deletedRows = result.getDeletedCount();
        return deletedRows;
    } finally {
        long elapsedTime = watch.elapsedTime();
        ActionLogContext.track("mongoDB", elapsedTime, 0, deletedRows);
        logger.debug("bulkDelete, collection={}, size={}, elapsedTime={}", collectionName, ids.size(), elapsedTime);
        checkSlowOperation(elapsedTime);
    }
}
Also used : BulkWriteOptions(com.mongodb.client.model.BulkWriteOptions) ArrayList(java.util.ArrayList) DeleteOneModel(com.mongodb.client.model.DeleteOneModel) BulkWriteResult(com.mongodb.bulk.BulkWriteResult) StopWatch(core.framework.util.StopWatch)

Example 12 with StopWatch

use of core.framework.util.StopWatch in project core-ng-project by neowu.

the class MongoCollectionImpl method aggregate.

@Override
public <V> List<V> aggregate(Aggregate<V> aggregate) {
    if (aggregate.pipeline == null || aggregate.pipeline.isEmpty())
        throw new Error("aggregate.pipeline must not be empty");
    if (aggregate.resultClass == null)
        throw new Error("aggregate.resultClass must not be null");
    StopWatch watch = new StopWatch();
    List<V> results = Lists.newArrayList();
    try {
        AggregateIterable<V> query = collection(aggregate.readPreference).aggregate(aggregate.pipeline, aggregate.resultClass).maxTime(mongo.timeoutInMs, TimeUnit.MILLISECONDS);
        fetch(query, results);
        checkTooManyRowsReturned(results.size());
        return results;
    } finally {
        long elapsedTime = watch.elapsedTime();
        ActionLogContext.track("mongoDB", elapsedTime, results.size(), 0);
        logger.debug("aggregate, collection={}, pipeline={}, readPref={}, returnedRows={}, elapsedTime={}", collectionName, aggregate.pipeline.stream().map(stage -> new BsonParam(stage, mongo.registry)).toArray(), aggregate.readPreference == null ? null : aggregate.readPreference.getName(), results.size(), elapsedTime);
    }
}
Also used : StopWatch(core.framework.util.StopWatch)

Example 13 with StopWatch

use of core.framework.util.StopWatch in project core-ng-project by neowu.

the class MongoCollectionImpl method bulkInsert.

@Override
public void bulkInsert(List<T> entities) {
    if (entities == null || entities.isEmpty())
        throw Exceptions.error("entities must not be empty");
    StopWatch watch = new StopWatch();
    for (T entity : entities) {
        validator.validate(entity);
    }
    try {
        collection().insertMany(entities, new InsertManyOptions().ordered(false));
    } finally {
        long elapsedTime = watch.elapsedTime();
        ActionLogContext.track("mongoDB", elapsedTime, 0, entities.size());
        logger.debug("bulkInsert, collection={}, size={}, elapsedTime={}", collectionName, entities.size(), elapsedTime);
        checkSlowOperation(elapsedTime);
    }
}
Also used : StopWatch(core.framework.util.StopWatch) InsertManyOptions(com.mongodb.client.model.InsertManyOptions)

Example 14 with StopWatch

use of core.framework.util.StopWatch in project core-ng-project by neowu.

the class MongoCollectionImpl method get.

@Override
public Optional<T> get(Get get) {
    if (get.id == null)
        throw new Error("get.id must not be null");
    StopWatch watch = new StopWatch();
    int returnedRows = 0;
    try {
        T result = collection(get.readPreference).find(Filters.eq("_id", get.id)).first();
        if (result != null)
            returnedRows = 1;
        return Optional.ofNullable(result);
    } finally {
        long elapsedTime = watch.elapsedTime();
        ActionLogContext.track("mongoDB", elapsedTime, returnedRows, 0);
        logger.debug("get, collection={}, id={}, readPref={}, elapsedTime={}", collectionName, get.id, get.readPreference == null ? null : get.readPreference.getName(), elapsedTime);
        checkSlowOperation(elapsedTime);
    }
}
Also used : StopWatch(core.framework.util.StopWatch)

Example 15 with StopWatch

use of core.framework.util.StopWatch in project core-ng-project by neowu.

the class RedisSetImpl method add.

@Override
public boolean add(String key, String value) {
    StopWatch watch = new StopWatch();
    PoolItem<RedisConnection> item = redis.pool.borrowItem();
    try {
        RedisConnection connection = item.resource;
        connection.write(SADD, encode(key), encode(value));
        return connection.readLong() == 1;
    } catch (IOException e) {
        item.broken = true;
        throw new UncheckedIOException(e);
    } finally {
        redis.pool.returnItem(item);
        long elapsedTime = watch.elapsedTime();
        ActionLogContext.track("redis", elapsedTime, 0, 1);
        logger.debug("sadd, key={}, value={}, elapsedTime={}", key, value, elapsedTime);
        redis.checkSlowOperation(elapsedTime);
    }
}
Also used : UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) StopWatch(core.framework.util.StopWatch)

Aggregations

StopWatch (core.framework.util.StopWatch)79 IOException (java.io.IOException)21 UncheckedIOException (java.io.UncheckedIOException)21 SearchException (core.framework.search.SearchException)10 ElasticsearchException (org.elasticsearch.ElasticsearchException)10 ArrayList (java.util.ArrayList)6 HTMLTemplate (core.framework.impl.template.HTMLTemplate)4 BytesParam (core.framework.impl.log.filter.BytesParam)3 Map (java.util.Map)3 BsonDocument (org.bson.BsonDocument)3 BulkWriteOptions (com.mongodb.client.model.BulkWriteOptions)2 UpdateOptions (com.mongodb.client.model.UpdateOptions)2 DeleteResult (com.mongodb.client.result.DeleteResult)2 TemplateContext (core.framework.impl.template.TemplateContext)2 Headers (org.apache.kafka.common.header.Headers)2 Bson (org.bson.conversions.Bson)2 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)2 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)2 Settings (org.elasticsearch.common.settings.Settings)2 MongoClient (com.mongodb.MongoClient)1