Search in sources :

Example 1 with GridRedisTypeException

use of org.apache.ignite.internal.processors.rest.handlers.redis.exception.GridRedisTypeException in project ignite by apache.

the class GridRedisIncrDecrCommandHandler method asRestRequest.

/**
 * {@inheritDoc}
 */
@Override
public GridRestRequest asRestRequest(GridRedisMessage msg) throws IgniteCheckedException {
    assert msg != null;
    DataStructuresRequest restReq = new DataStructuresRequest();
    GridRestCacheRequest getReq = new GridRestCacheRequest();
    getReq.clientId(msg.clientId());
    getReq.key(msg.key());
    getReq.command(CACHE_GET);
    getReq.cacheName(msg.cacheName());
    GridRestResponse getResp = hnd.handle(getReq);
    if (getResp.getResponse() == null)
        restReq.initial(0L);
    else {
        if (getResp.getResponse() instanceof String) {
            long init;
            try {
                init = Long.parseLong((String) getResp.getResponse());
                restReq.initial(init);
            } catch (Exception e) {
                U.error(log, "An initial value must be numeric and in range", e);
                throw new GridRedisGenericException("An initial value must be numeric and in range");
            }
            if ((init == Long.MAX_VALUE && (msg.command() == INCR || msg.command() == INCRBY)) || (init == Long.MIN_VALUE && (msg.command() == DECR || msg.command() == DECRBY)))
                throw new GridRedisGenericException("Increment or decrement would overflow");
        } else
            throw new GridRedisTypeException("Operation against a key holding the wrong kind of value");
        // remove from cache.
        GridRestCacheRequest rmReq = new GridRestCacheRequest();
        rmReq.clientId(msg.clientId());
        rmReq.key(msg.key());
        rmReq.command(CACHE_REMOVE);
        rmReq.cacheName(msg.cacheName());
        Object rmResp = hnd.handle(rmReq).getResponse();
        if (rmResp == null || !(boolean) rmResp)
            throw new GridRedisGenericException("Cannot incr/decr on the non-atomiclong key");
    }
    restReq.clientId(msg.clientId());
    restReq.key(msg.key());
    restReq.delta(1L);
    if (msg.messageSize() > 2) {
        try {
            Long delta = Long.valueOf(msg.aux(DELTA_POS));
            // check if it can be safely added.
            safeAdd(restReq.initial(), delta);
            restReq.delta(delta);
        } catch (NumberFormatException | ArithmeticException e) {
            U.error(log, "An increment value must be numeric and in range", e);
            throw new GridRedisGenericException("An increment value must be numeric and in range");
        }
    }
    switch(msg.command()) {
        case INCR:
        case INCRBY:
            restReq.command(ATOMIC_INCREMENT);
            break;
        case DECR:
        case DECRBY:
            restReq.command(ATOMIC_DECREMENT);
            break;
        default:
            assert false : "Unexpected command received";
    }
    return restReq;
}
Also used : GridRedisGenericException(org.apache.ignite.internal.processors.rest.handlers.redis.exception.GridRedisGenericException) GridRedisGenericException(org.apache.ignite.internal.processors.rest.handlers.redis.exception.GridRedisGenericException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridRedisTypeException(org.apache.ignite.internal.processors.rest.handlers.redis.exception.GridRedisTypeException) GridRestCacheRequest(org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest) GridRestResponse(org.apache.ignite.internal.processors.rest.GridRestResponse) DataStructuresRequest(org.apache.ignite.internal.processors.rest.request.DataStructuresRequest) GridRedisTypeException(org.apache.ignite.internal.processors.rest.handlers.redis.exception.GridRedisTypeException)

Aggregations

IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 GridRestResponse (org.apache.ignite.internal.processors.rest.GridRestResponse)1 GridRedisGenericException (org.apache.ignite.internal.processors.rest.handlers.redis.exception.GridRedisGenericException)1 GridRedisTypeException (org.apache.ignite.internal.processors.rest.handlers.redis.exception.GridRedisTypeException)1 DataStructuresRequest (org.apache.ignite.internal.processors.rest.request.DataStructuresRequest)1 GridRestCacheRequest (org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest)1