Search in sources :

Example 1 with GridRedisGenericException

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

the class GridRedisMGetCommandHandler method asRestRequest.

/** {@inheritDoc} */
@Override
public GridRestRequest asRestRequest(GridRedisMessage msg) throws IgniteCheckedException {
    assert msg != null;
    if (msg.messageSize() < 2)
        throw new GridRedisGenericException("Wrong number of arguments");
    GridRestCacheRequest restReq = new GridRestCacheRequest();
    restReq.clientId(msg.clientId());
    restReq.key(msg.key());
    restReq.command(CACHE_GET_ALL);
    restReq.cacheName(CACHE_NAME);
    List<String> keys = msg.auxMKeys();
    Map<Object, Object> mget = U.newHashMap(keys.size());
    for (String key : keys) mget.put(key, null);
    restReq.values(mget);
    return restReq;
}
Also used : GridRestCacheRequest(org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest) GridRedisGenericException(org.apache.ignite.internal.processors.rest.handlers.redis.exception.GridRedisGenericException)

Example 2 with GridRedisGenericException

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

the class GridRedisSetCommandHandler method asRestRequest.

/** {@inheritDoc} */
@Override
public GridRestRequest asRestRequest(GridRedisMessage msg) throws IgniteCheckedException {
    assert msg != null;
    if (msg.messageSize() < 3)
        throw new GridRedisGenericException("Wrong number of arguments");
    // check if an atomic long with the key exists (related to incr/decr).
    IgniteAtomicLong l = ctx.grid().atomicLong(msg.key(), 0, false);
    if (l != null) {
        try {
            l.close();
        } catch (IgniteException ignored) {
            U.warn(log, "Failed to remove atomic long for key: " + msg.key());
        }
    }
    GridRestCacheRequest restReq = new GridRestCacheRequest();
    restReq.clientId(msg.clientId());
    restReq.key(msg.key());
    restReq.command(CACHE_PUT);
    restReq.cacheName(CACHE_NAME);
    restReq.value(msg.aux(VAL_POS));
    if (msg.messageSize() >= 4) {
        List<String> params = msg.aux();
        // get rid of SET value.
        params.remove(0);
        if (U.containsStringCollection(params, "nx", true))
            restReq.command(CACHE_PUT_IF_ABSENT);
        else if (U.containsStringCollection(params, "xx", true))
            restReq.command(CACHE_REPLACE);
        setExpire(restReq, params);
    }
    return restReq;
}
Also used : GridRestCacheRequest(org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest) GridRedisGenericException(org.apache.ignite.internal.processors.rest.handlers.redis.exception.GridRedisGenericException) IgniteException(org.apache.ignite.IgniteException) IgniteAtomicLong(org.apache.ignite.IgniteAtomicLong)

Example 3 with GridRedisGenericException

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

the class GridRedisGetSetCommandHandler method asRestRequest.

/** {@inheritDoc} */
@Override
public GridRestRequest asRestRequest(GridRedisMessage msg) throws IgniteCheckedException {
    assert msg != null;
    if (msg.messageSize() < 3)
        throw new GridRedisGenericException("Wrong syntax");
    GridRestCacheRequest restReq = new GridRestCacheRequest();
    restReq.clientId(msg.clientId());
    restReq.key(msg.key());
    restReq.value(msg.aux(VAL_POS));
    restReq.command(CACHE_GET_AND_PUT);
    restReq.cacheName(CACHE_NAME);
    return restReq;
}
Also used : GridRestCacheRequest(org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest) GridRedisGenericException(org.apache.ignite.internal.processors.rest.handlers.redis.exception.GridRedisGenericException)

Example 4 with GridRedisGenericException

use of org.apache.ignite.internal.processors.rest.handlers.redis.exception.GridRedisGenericException 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(CACHE_NAME);
    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(CACHE_NAME);
        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 : GridRestCacheRequest(org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest) GridRedisGenericException(org.apache.ignite.internal.processors.rest.handlers.redis.exception.GridRedisGenericException) 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) 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)

Example 5 with GridRedisGenericException

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

the class GridRedisDelCommandHandler method asRestRequest.

/** {@inheritDoc} */
@Override
public GridRestRequest asRestRequest(GridRedisMessage msg) throws IgniteCheckedException {
    assert msg != null;
    if (msg.messageSize() < 2)
        throw new GridRedisGenericException("Wrong number of arguments");
    GridRestCacheRequest restReq = new GridRestCacheRequest();
    restReq.clientId(msg.clientId());
    restReq.key(msg.key());
    restReq.command(CACHE_REMOVE_ALL);
    restReq.cacheName(CACHE_NAME);
    List<String> keys = msg.auxMKeys();
    Map<Object, Object> mget = U.newHashMap(keys.size());
    for (String key : keys) mget.put(key, null);
    restReq.values(mget);
    return restReq;
}
Also used : GridRestCacheRequest(org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest) GridRedisGenericException(org.apache.ignite.internal.processors.rest.handlers.redis.exception.GridRedisGenericException)

Aggregations

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