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