Search in sources :

Example 6 with GridRestCacheRequest

use of org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest in project ignite by apache.

the class GridRedisDbSizeCommandHandler method asRestRequest.

/** {@inheritDoc} */
@Override
public GridRestRequest asRestRequest(GridRedisMessage msg) throws IgniteCheckedException {
    assert msg != null;
    GridRestCacheRequest restReq = new GridRestCacheRequest();
    restReq.clientId(msg.clientId());
    restReq.key(msg.key());
    restReq.command(CACHE_SIZE);
    restReq.cacheName(CACHE_NAME);
    return restReq;
}
Also used : GridRestCacheRequest(org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest)

Example 7 with GridRestCacheRequest

use of org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest 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 8 with GridRestCacheRequest

use of org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest 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 9 with GridRestCacheRequest

use of org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest 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)

Example 10 with GridRestCacheRequest

use of org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest in project ignite by apache.

the class GridCacheCommandHandlerSelfTest method testCacheGetFailsSyncNotify.

/**
     * Tests the cache failure during the execution of the CACHE_GET command.
     *
     * @throws Exception If failed.
     */
public void testCacheGetFailsSyncNotify() throws Exception {
    GridRestCommandHandler hnd = new TestableCacheCommandHandler(grid().context(), "getAsync");
    GridRestCacheRequest req = new GridRestCacheRequest();
    req.cacheName(DEFAULT_CACHE_NAME);
    req.command(GridRestCommand.CACHE_GET);
    req.key("k1");
    try {
        hnd.handleAsync(req).get();
        fail("Expected exception not thrown.");
    } catch (IgniteCheckedException e) {
        info("Got expected exception: " + e);
    }
}
Also used : GridRestCommandHandler(org.apache.ignite.internal.processors.rest.handlers.GridRestCommandHandler) GridRestCacheRequest(org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest) IgniteCheckedException(org.apache.ignite.IgniteCheckedException)

Aggregations

GridRestCacheRequest (org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest)20 GridRedisGenericException (org.apache.ignite.internal.processors.rest.handlers.redis.exception.GridRedisGenericException)9 HashMap (java.util.HashMap)4 Map (java.util.Map)4 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 DataStructuresRequest (org.apache.ignite.internal.processors.rest.request.DataStructuresRequest)3 GridRestTaskRequest (org.apache.ignite.internal.processors.rest.request.GridRestTaskRequest)3 EnumMap (java.util.EnumMap)2 List (java.util.List)2 IgniteException (org.apache.ignite.IgniteException)2 GridRestResponse (org.apache.ignite.internal.processors.rest.GridRestResponse)2 GridRestCommandHandler (org.apache.ignite.internal.processors.rest.handlers.GridRestCommandHandler)2 GridRestRequest (org.apache.ignite.internal.processors.rest.request.GridRestRequest)2 GridRestTopologyRequest (org.apache.ignite.internal.processors.rest.request.GridRestTopologyRequest)2 Nullable (org.jetbrains.annotations.Nullable)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1