Search in sources :

Example 1 with GridRestCacheRequest

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

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

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

the class GridRedisStrlenCommandHandler 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_GET);
    restReq.cacheName(CACHE_NAME);
    return restReq;
}
Also used : GridRestCacheRequest(org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest)

Example 4 with GridRestCacheRequest

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

the class GridRestProcessor method interceptRequest.

/**
     * Applies {@link ConnectorMessageInterceptor}
     * from {@link ConnectorConfiguration#getMessageInterceptor()} ()}
     * to all user parameters in the request.
     *
     * @param req Client request.
     */
private void interceptRequest(GridRestRequest req) {
    ConnectorMessageInterceptor interceptor = config().getMessageInterceptor();
    if (interceptor == null)
        return;
    if (req instanceof GridRestCacheRequest) {
        GridRestCacheRequest req0 = (GridRestCacheRequest) req;
        req0.key(interceptor.onReceive(req0.key()));
        req0.value(interceptor.onReceive(req0.value()));
        req0.value2(interceptor.onReceive(req0.value2()));
        Map<Object, Object> oldVals = req0.values();
        if (oldVals != null) {
            Map<Object, Object> newVals = U.newHashMap(oldVals.size());
            for (Map.Entry<Object, Object> e : oldVals.entrySet()) newVals.put(interceptor.onReceive(e.getKey()), interceptor.onReceive(e.getValue()));
            req0.values(U.sealMap(newVals));
        }
    } else if (req instanceof GridRestTaskRequest) {
        GridRestTaskRequest req0 = (GridRestTaskRequest) req;
        List<Object> oldParams = req0.params();
        if (oldParams != null) {
            Collection<Object> newParams = new ArrayList<>(oldParams.size());
            for (Object o : oldParams) newParams.add(interceptor.onReceive(o));
            req0.params(U.sealList(newParams));
        }
    }
}
Also used : GridRestCacheRequest(org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest) GridRestTaskRequest(org.apache.ignite.internal.processors.rest.request.GridRestTaskRequest) ConnectorMessageInterceptor(org.apache.ignite.configuration.ConnectorMessageInterceptor) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) EnumMap(java.util.EnumMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 5 with GridRestCacheRequest

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

the class GridCacheCommandHandler method handleAsync.

/** {@inheritDoc} */
@Override
public IgniteInternalFuture<GridRestResponse> handleAsync(final GridRestRequest req) {
    assert req instanceof GridRestCacheRequest : "Invalid command for topology handler: " + req;
    assert SUPPORTED_COMMANDS.contains(req.command());
    if (log.isDebugEnabled())
        log.debug("Handling cache REST request: " + req);
    GridRestCacheRequest req0 = (GridRestCacheRequest) req;
    final String cacheName = req0.cacheName() == null ? DFLT_CACHE_NAME : req0.cacheName();
    final Object key = req0.key();
    final boolean skipStore = parseCacheFlags(req0.cacheFlags());
    try {
        GridRestCommand cmd = req0.command();
        if (key == null && KEY_REQUIRED_REQUESTS.contains(cmd))
            throw new IgniteCheckedException(GridRestCommandHandlerAdapter.missingParameter("key"));
        final Long ttl = req0.ttl();
        IgniteInternalFuture<GridRestResponse> fut;
        switch(cmd) {
            case DESTROY_CACHE:
                {
                    // Do not check thread tx here since there can be active system cache txs.
                    fut = ((IgniteKernal) ctx.grid()).destroyCacheAsync(cacheName, false, false).chain(new CX1<IgniteInternalFuture<?>, GridRestResponse>() {

                        @Override
                        public GridRestResponse applyx(IgniteInternalFuture<?> f) throws IgniteCheckedException {
                            f.get();
                            return new GridRestResponse(null);
                        }
                    });
                    break;
                }
            case GET_OR_CREATE_CACHE:
                {
                    // Do not check thread tx here since there can be active system cache txs.
                    fut = ((IgniteKernal) ctx.grid()).getOrCreateCacheAsync(cacheName, false).chain(new CX1<IgniteInternalFuture<?>, GridRestResponse>() {

                        @Override
                        public GridRestResponse applyx(IgniteInternalFuture<?> f) throws IgniteCheckedException {
                            f.get();
                            return new GridRestResponse(null);
                        }
                    });
                    break;
                }
            case CACHE_METADATA:
                {
                    fut = ctx.task().execute(MetadataTask.class, null);
                    break;
                }
            case CACHE_CONTAINS_KEYS:
                {
                    fut = executeCommand(req.destinationId(), req.clientId(), cacheName, skipStore, key, new ContainsKeysCommand(getKeys(req0)));
                    break;
                }
            case CACHE_CONTAINS_KEY:
                {
                    fut = executeCommand(req.destinationId(), req.clientId(), cacheName, skipStore, key, new ContainsKeyCommand(key));
                    break;
                }
            case CACHE_GET:
                {
                    fut = executeCommand(req.destinationId(), req.clientId(), cacheName, skipStore, key, new GetCommand(key));
                    break;
                }
            case CACHE_GET_AND_PUT:
                {
                    fut = executeCommand(req.destinationId(), req.clientId(), cacheName, skipStore, key, new GetAndPutCommand(key, getValue(req0)));
                    break;
                }
            case CACHE_GET_AND_REPLACE:
                {
                    fut = executeCommand(req.destinationId(), req.clientId(), cacheName, skipStore, key, new GetAndReplaceCommand(key, getValue(req0)));
                    break;
                }
            case CACHE_GET_AND_PUT_IF_ABSENT:
                {
                    fut = executeCommand(req.destinationId(), req.clientId(), cacheName, skipStore, key, new GetAndPutIfAbsentCommand(key, getValue(req0)));
                    break;
                }
            case CACHE_PUT_IF_ABSENT:
                {
                    fut = executeCommand(req.destinationId(), req.clientId(), cacheName, skipStore, key, new PutIfAbsentCommand(key, ttl, getValue(req0)));
                    break;
                }
            case CACHE_GET_ALL:
                {
                    fut = executeCommand(req.destinationId(), req.clientId(), cacheName, skipStore, key, new GetAllCommand(getKeys(req0)));
                    break;
                }
            case CACHE_PUT:
                {
                    fut = executeCommand(req.destinationId(), req.clientId(), cacheName, skipStore, key, new PutCommand(key, ttl, getValue(req0)));
                    break;
                }
            case CACHE_ADD:
                {
                    fut = executeCommand(req.destinationId(), req.clientId(), cacheName, skipStore, key, new AddCommand(key, ttl, getValue(req0)));
                    break;
                }
            case CACHE_PUT_ALL:
                {
                    Map<Object, Object> map = req0.values();
                    if (F.isEmpty(map))
                        throw new IgniteCheckedException(GridRestCommandHandlerAdapter.missingParameter("values"));
                    for (Map.Entry<Object, Object> e : map.entrySet()) {
                        if (e.getKey() == null)
                            throw new IgniteCheckedException("Failing putAll operation (null keys are not allowed).");
                        if (e.getValue() == null)
                            throw new IgniteCheckedException("Failing putAll operation (null values are not allowed).");
                    }
                    // HashMap wrapping for correct serialization
                    map = new HashMap<>(map);
                    fut = executeCommand(req.destinationId(), req.clientId(), cacheName, skipStore, key, new PutAllCommand(map));
                    break;
                }
            case CACHE_REMOVE:
                {
                    fut = executeCommand(req.destinationId(), req.clientId(), cacheName, skipStore, key, new RemoveCommand(key));
                    break;
                }
            case CACHE_REMOVE_VALUE:
                {
                    fut = executeCommand(req.destinationId(), req.clientId(), cacheName, skipStore, key, new RemoveValueCommand(key, getValue(req0)));
                    break;
                }
            case CACHE_REPLACE_VALUE:
                {
                    fut = executeCommand(req.destinationId(), req.clientId(), cacheName, skipStore, key, new ReplaceValueCommand(key, getValue(req0), req0.value2()));
                    break;
                }
            case CACHE_GET_AND_REMOVE:
                {
                    fut = executeCommand(req.destinationId(), req.clientId(), cacheName, skipStore, key, new GetAndRemoveCommand(key));
                    break;
                }
            case CACHE_REMOVE_ALL:
                {
                    Map<Object, Object> map = req0.values();
                    // HashSet wrapping for correct serialization
                    Set<Object> keys = map == null ? null : new HashSet<>(map.keySet());
                    fut = executeCommand(req.destinationId(), req.clientId(), cacheName, skipStore, key, new RemoveAllCommand(keys));
                    break;
                }
            case CACHE_REPLACE:
                {
                    final Object val = req0.value();
                    if (val == null)
                        throw new IgniteCheckedException(GridRestCommandHandlerAdapter.missingParameter("val"));
                    fut = executeCommand(req.destinationId(), req.clientId(), cacheName, skipStore, key, new ReplaceCommand(key, ttl, val));
                    break;
                }
            case CACHE_CAS:
                {
                    final Object val1 = req0.value();
                    final Object val2 = req0.value2();
                    fut = executeCommand(req.destinationId(), req.clientId(), cacheName, skipStore, key, new CasCommand(val2, val1, key));
                    break;
                }
            case CACHE_APPEND:
                {
                    fut = executeCommand(req.destinationId(), req.clientId(), cacheName, skipStore, key, new AppendCommand(key, req0));
                    break;
                }
            case CACHE_PREPEND:
                {
                    fut = executeCommand(req.destinationId(), req.clientId(), cacheName, skipStore, key, new PrependCommand(key, req0));
                    break;
                }
            case CACHE_METRICS:
                {
                    fut = executeCommand(req.destinationId(), req.clientId(), cacheName, key, new MetricsCommand());
                    break;
                }
            case CACHE_SIZE:
                {
                    fut = executeCommand(req.destinationId(), req.clientId(), cacheName, key, new SizeCommand());
                    break;
                }
            default:
                throw new IllegalArgumentException("Invalid command for cache handler: " + req);
        }
        return fut;
    } catch (IgniteException e) {
        U.error(log, "Failed to execute cache command: " + req, e);
        return new GridFinishedFuture<>(e);
    } catch (IgniteCheckedException e) {
        U.error(log, "Failed to execute cache command: " + req, e);
        return new GridFinishedFuture<>(e);
    } finally {
        if (log.isDebugEnabled())
            log.debug("Handled cache REST request: " + req);
    }
}
Also used : EnumSet(java.util.EnumSet) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) GridRestCacheRequest(org.apache.ignite.internal.processors.rest.request.GridRestCacheRequest) MutableEntry(javax.cache.processor.MutableEntry) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) HashSet(java.util.HashSet) GridRestCommand(org.apache.ignite.internal.processors.rest.GridRestCommand) GridRestResponse(org.apache.ignite.internal.processors.rest.GridRestResponse) Map(java.util.Map) HashMap(java.util.HashMap)

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