Search in sources :

Example 6 with StopWatch

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

the class MongoImpl method collection.

public <T> MongoCollection<T> collection(Class<T> entityClass) {
    StopWatch watch = new StopWatch();
    try {
        new MongoClassValidator(entityClass).validateEntityClass();
        codecs.registerEntity(entityClass);
        return new MongoCollectionImpl<>(this, entityClass);
    } finally {
        logger.info("register mongo entity, entityClass={}, elapsedTime={}", entityClass.getCanonicalName(), watch.elapsedTime());
    }
}
Also used : StopWatch(core.framework.util.StopWatch)

Example 7 with StopWatch

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

the class RedisHashImpl method del.

@Override
public boolean del(String key, String... fields) {
    StopWatch watch = new StopWatch();
    PoolItem<RedisConnection> item = redis.pool.borrowItem();
    try {
        RedisConnection connection = item.resource;
        connection.write(HDEL, encode(key, fields));
        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, fields.length);
        logger.debug("hdel, key={}, fields={}, elapsedTime={}", key, fields, elapsedTime);
        redis.checkSlowOperation(elapsedTime);
    }
}
Also used : UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) StopWatch(core.framework.util.StopWatch)

Example 8 with StopWatch

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

the class RedisImpl method multiGetBytes.

public Map<String, byte[]> multiGetBytes(String... keys) {
    StopWatch watch = new StopWatch();
    PoolItem<RedisConnection> item = pool.borrowItem();
    try {
        RedisConnection connection = item.resource;
        byte[][] arguments = new byte[keys.length][];
        for (int i = 0; i < keys.length; i++) {
            arguments[i] = encode(keys[i]);
        }
        Map<String, byte[]> values = Maps.newHashMapWithExpectedSize(keys.length);
        connection.write(Protocol.Command.MGET, arguments);
        Object[] response = connection.readArray();
        for (int i = 0; i < response.length; i++) {
            byte[] value = (byte[]) response[i];
            if (value != null)
                values.put(keys[i], value);
        }
        return values;
    } catch (IOException e) {
        item.broken = true;
        throw new UncheckedIOException(e);
    } finally {
        pool.returnItem(item);
        long elapsedTime = watch.elapsedTime();
        ActionLogContext.track("redis", elapsedTime, keys.length, 0);
        logger.debug("mget, keys={}, elapsedTime={}", keys, elapsedTime);
        checkSlowOperation(elapsedTime);
    }
}
Also used : UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) StopWatch(core.framework.util.StopWatch)

Example 9 with StopWatch

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

the class RedisImpl method multiSet.

@Override
public void multiSet(Map<String, String> values) {
    StopWatch watch = new StopWatch();
    PoolItem<RedisConnection> item = pool.borrowItem();
    try {
        RedisConnection connection = item.resource;
        connection.write(Protocol.Command.MSET, encode(values));
        connection.readSimpleString();
    } catch (IOException e) {
        item.broken = true;
        throw new UncheckedIOException(e);
    } finally {
        pool.returnItem(item);
        long elapsedTime = watch.elapsedTime();
        ActionLogContext.track("redis", elapsedTime, 0, values.size());
        logger.debug("mset, values={}, elapsedTime={}", values, elapsedTime);
        checkSlowOperation(elapsedTime);
    }
}
Also used : UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) StopWatch(core.framework.util.StopWatch)

Example 10 with StopWatch

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

the class RedisImpl method setIfAbsent.

@Override
public boolean setIfAbsent(String key, String value, Duration expiration) {
    StopWatch watch = new StopWatch();
    PoolItem<RedisConnection> item = pool.borrowItem();
    try {
        RedisConnection connection = item.resource;
        connection.write(Protocol.Command.SET, encode(key), encode(value), NX, EX, encode(expiration.getSeconds()));
        String result = connection.readSimpleString();
        return "OK".equals(result);
    } catch (IOException e) {
        item.broken = true;
        throw new UncheckedIOException(e);
    } finally {
        pool.returnItem(item);
        long elapsedTime = watch.elapsedTime();
        ActionLogContext.track("redis", elapsedTime, 0, 1);
        logger.debug("setIfAbsent, key={}, value={}, expiration={}, elapsedTime={}", key, value, expiration, elapsedTime);
        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