Search in sources :

Example 11 with RedisCommand

use of org.redisson.client.protocol.RedisCommand in project redisson by redisson.

the class RedissonLiveObjectService method findIds.

@Override
public <K> Iterable<K> findIds(Class<?> entityClass, int count) {
    NamingScheme namingScheme = commandExecutor.getObjectBuilder().getNamingScheme(entityClass);
    String pattern = namingScheme.getNamePattern(entityClass);
    RedissonKeys keys = new RedissonKeys(commandExecutor);
    RedisCommand<ListScanResult<String>> command = new RedisCommand<>("SCAN", new ListMultiDecoder2(new ListScanResultReplayDecoder(), new ObjectListReplayDecoder<Object>()), new Convertor<Object>() {

        @Override
        public Object convert(Object obj) {
            if (!(obj instanceof String)) {
                return obj;
            }
            return namingScheme.resolveId(obj.toString());
        }
    });
    return keys.getKeysByPattern(command, pattern, 0, count);
}
Also used : ListScanResult(org.redisson.client.protocol.decoder.ListScanResult) NamingScheme(org.redisson.liveobject.resolver.NamingScheme) ListMultiDecoder2(org.redisson.client.protocol.decoder.ListMultiDecoder2) RedisCommand(org.redisson.client.protocol.RedisCommand) ListScanResultReplayDecoder(org.redisson.client.protocol.decoder.ListScanResultReplayDecoder) ObjectListReplayDecoder(org.redisson.client.protocol.decoder.ObjectListReplayDecoder)

Example 12 with RedisCommand

use of org.redisson.client.protocol.RedisCommand in project redisson by redisson.

the class RedissonGeo method storeSearchToAsync.

@Override
public RFuture<Long> storeSearchToAsync(String destName, GeoSearchArgs args) {
    GeoSearchNode node = (GeoSearchNode) args;
    Map<GeoSearchNode.Params, Object> params = node.getParams();
    List<Object> commandParams = new ArrayList<>();
    if (params.get(GeoSearchNode.Params.HEIGHT) != null) {
        commandParams.add(destName);
    }
    commandParams.add(getRawName());
    RedisCommand command = null;
    if (params.get(GeoSearchNode.Params.LATITUDE) != null && params.get(GeoSearchNode.Params.LONGITUDE) != null) {
        command = RedisCommands.GEORADIUS_STORE;
        if (params.get(GeoSearchNode.Params.HEIGHT) != null) {
            command = RedisCommands.GEOSEARCHSTORE_STORE;
            commandParams.add("FROMLONLAT");
        }
        commandParams.add(convert((double) params.get(GeoSearchNode.Params.LONGITUDE)));
        commandParams.add(convert((double) params.get(GeoSearchNode.Params.LATITUDE)));
    }
    if (params.get(GeoSearchNode.Params.MEMBER) != null) {
        command = RedisCommands.GEORADIUSBYMEMBER_STORE;
        if (params.get(GeoSearchNode.Params.HEIGHT) != null) {
            command = RedisCommands.GEOSEARCHSTORE_STORE;
            commandParams.add("FROMMEMBER");
        }
        commandParams.add(encode(params.get(GeoSearchNode.Params.MEMBER)));
    }
    if (params.get(GeoSearchNode.Params.RADIUS) != null && params.get(GeoSearchNode.Params.UNIT) != null) {
        commandParams.add(params.get(GeoSearchNode.Params.RADIUS));
        commandParams.add(params.get(GeoSearchNode.Params.UNIT));
    }
    if (params.get(GeoSearchNode.Params.HEIGHT) != null && params.get(GeoSearchNode.Params.UNIT) != null) {
        commandParams.add("BYBOX");
        commandParams.add(params.get(GeoSearchNode.Params.WIDTH));
        commandParams.add(params.get(GeoSearchNode.Params.HEIGHT));
        commandParams.add(params.get(GeoSearchNode.Params.UNIT));
        if (params.get(GeoSearchNode.Params.ORDER) != null) {
            commandParams.add(params.get(GeoSearchNode.Params.ORDER));
        }
    }
    if (params.get(GeoSearchNode.Params.COUNT) != null) {
        commandParams.add("COUNT");
        commandParams.add(params.get(GeoSearchNode.Params.COUNT));
        if (params.get(GeoSearchNode.Params.COUNT_ANY) != null) {
            commandParams.add("ANY");
        }
    }
    if (params.get(GeoSearchNode.Params.HEIGHT) == null && params.get(GeoSearchNode.Params.ORDER) != null) {
        commandParams.add(params.get(GeoSearchNode.Params.ORDER));
    }
    if (params.get(GeoSearchNode.Params.HEIGHT) == null) {
        commandParams.add("STORE");
        commandParams.add(destName);
    }
    return commandExecutor.writeAsync(getRawName(), LongCodec.INSTANCE, command, commandParams.toArray());
}
Also used : GeoSearchNode(org.redisson.api.geo.GeoSearchNode) RedisCommand(org.redisson.client.protocol.RedisCommand)

Example 13 with RedisCommand

use of org.redisson.client.protocol.RedisCommand in project redisson by redisson.

the class RedissonGeo method searchWithDistanceAsync.

@Override
public RFuture<Map<V, Double>> searchWithDistanceAsync(GeoSearchArgs args) {
    GeoSearchNode node = (GeoSearchNode) args;
    Map<GeoSearchNode.Params, Object> params = node.getParams();
    List<Object> commandParams = new ArrayList<>();
    commandParams.add(getRawName());
    RedisCommand command = null;
    if (params.get(GeoSearchNode.Params.LATITUDE) != null && params.get(GeoSearchNode.Params.LONGITUDE) != null) {
        command = GEORADIUS_RO_DISTANCE;
        if (params.get(GeoSearchNode.Params.HEIGHT) != null) {
            command = GEOSEARCH_DISTANCE;
            commandParams.add("FROMLONLAT");
        }
        commandParams.add(convert((double) params.get(GeoSearchNode.Params.LONGITUDE)));
        commandParams.add(convert((double) params.get(GeoSearchNode.Params.LATITUDE)));
    }
    if (params.get(GeoSearchNode.Params.MEMBER) != null) {
        command = GEORADIUSBYMEMBER_RO_DISTANCE;
        if (params.get(GeoSearchNode.Params.HEIGHT) != null) {
            command = GEOSEARCH_DISTANCE;
            commandParams.add("FROMMEMBER");
        }
        commandParams.add(encode(params.get(GeoSearchNode.Params.MEMBER)));
    }
    if (params.get(GeoSearchNode.Params.RADIUS) != null && params.get(GeoSearchNode.Params.UNIT) != null) {
        commandParams.add(params.get(GeoSearchNode.Params.RADIUS));
        commandParams.add(params.get(GeoSearchNode.Params.UNIT));
    }
    if (params.get(GeoSearchNode.Params.HEIGHT) != null && params.get(GeoSearchNode.Params.UNIT) != null) {
        commandParams.add("BYBOX");
        commandParams.add(params.get(GeoSearchNode.Params.WIDTH));
        commandParams.add(params.get(GeoSearchNode.Params.HEIGHT));
        commandParams.add(params.get(GeoSearchNode.Params.UNIT));
        if (params.get(GeoSearchNode.Params.ORDER) != null) {
            commandParams.add(params.get(GeoSearchNode.Params.ORDER));
        }
    }
    if (params.get(GeoSearchNode.Params.HEIGHT) == null) {
        commandParams.add("WITHDIST");
    }
    if (params.get(GeoSearchNode.Params.COUNT) != null) {
        commandParams.add("COUNT");
        commandParams.add(params.get(GeoSearchNode.Params.COUNT));
        if (params.get(GeoSearchNode.Params.COUNT_ANY) != null) {
            commandParams.add("ANY");
        }
    }
    if (params.get(GeoSearchNode.Params.HEIGHT) == null && params.get(GeoSearchNode.Params.ORDER) != null) {
        commandParams.add(params.get(GeoSearchNode.Params.ORDER));
    }
    if (params.get(GeoSearchNode.Params.HEIGHT) != null) {
        commandParams.add("WITHDIST");
    }
    return commandExecutor.readAsync(getRawName(), codec, command, commandParams.toArray());
}
Also used : GeoSearchNode(org.redisson.api.geo.GeoSearchNode) RedisCommand(org.redisson.client.protocol.RedisCommand)

Example 14 with RedisCommand

use of org.redisson.client.protocol.RedisCommand in project redisson by redisson.

the class RedissonGeo method hashAsync.

@Override
public RFuture<Map<V, String>> hashAsync(V... members) {
    List<Object> params = new ArrayList<Object>(members.length + 1);
    params.add(getRawName());
    for (Object member : members) {
        params.add(encode(member));
    }
    RedisCommand<Map<Object, Object>> command = new RedisCommand<Map<Object, Object>>("GEOHASH", new MapGetAllDecoder((List<Object>) Arrays.asList(members), 0));
    return commandExecutor.readAsync(getRawName(), StringCodec.INSTANCE, command, params.toArray());
}
Also used : RedisCommand(org.redisson.client.protocol.RedisCommand) MapGetAllDecoder(org.redisson.connection.decoder.MapGetAllDecoder)

Example 15 with RedisCommand

use of org.redisson.client.protocol.RedisCommand in project redisson by redisson.

the class CommandAsyncService method evalAsync.

private <T, R> RFuture<R> evalAsync(NodeSource nodeSource, boolean readOnlyMode, Codec codec, RedisCommand<T> evalCommandType, String script, List<Object> keys, boolean noRetry, Object... params) {
    if (isEvalCacheActive() && evalCommandType.getName().equals("EVAL")) {
        CompletableFuture<R> mainPromise = new CompletableFuture<>();
        Object[] pps = copy(params);
        CompletableFuture<R> promise = new CompletableFuture<>();
        String sha1 = calcSHA(script);
        RedisCommand cmd;
        if (readOnlyMode && evalShaROSupported.get()) {
            cmd = new RedisCommand(evalCommandType, "EVALSHA_RO");
        } else {
            cmd = new RedisCommand(evalCommandType, "EVALSHA");
        }
        List<Object> args = new ArrayList<Object>(2 + keys.size() + params.length);
        args.add(sha1);
        args.add(keys.size());
        args.addAll(keys);
        args.addAll(Arrays.asList(params));
        RedisExecutor<T, R> executor = new RedisExecutor<>(readOnlyMode, nodeSource, codec, cmd, args.toArray(), promise, false, connectionManager, objectBuilder, referenceType, noRetry);
        executor.execute();
        promise.whenComplete((res, e) -> {
            if (e != null) {
                if (e.getMessage().startsWith("ERR unknown command")) {
                    evalShaROSupported.set(false);
                    free(pps);
                    RFuture<R> future = evalAsync(nodeSource, readOnlyMode, codec, evalCommandType, script, keys, noRetry, params);
                    transfer(future.toCompletableFuture(), mainPromise);
                } else if (e.getMessage().startsWith("NOSCRIPT")) {
                    RFuture<String> loadFuture = loadScript(executor.getRedisClient(), script);
                    loadFuture.whenComplete((r, ex) -> {
                        if (ex != null) {
                            free(pps);
                            mainPromise.completeExceptionally(ex);
                            return;
                        }
                        List<Object> newargs = new ArrayList<Object>(2 + keys.size() + params.length);
                        newargs.add(sha1);
                        newargs.add(keys.size());
                        newargs.addAll(keys);
                        newargs.addAll(Arrays.asList(pps));
                        NodeSource ns = nodeSource;
                        if (ns.getRedisClient() == null) {
                            ns = new NodeSource(nodeSource, executor.getRedisClient());
                        }
                        RFuture<R> future = async(readOnlyMode, ns, codec, cmd, newargs.toArray(), false, noRetry);
                        transfer(future.toCompletableFuture(), mainPromise);
                    });
                } else {
                    free(pps);
                    mainPromise.completeExceptionally(e);
                }
                return;
            }
            free(pps);
            mainPromise.complete(res);
        });
        return new CompletableFutureWrapper<>(mainPromise);
    }
    List<Object> args = new ArrayList<Object>(2 + keys.size() + params.length);
    args.add(script);
    args.add(keys.size());
    args.addAll(keys);
    args.addAll(Arrays.asList(params));
    return async(readOnlyMode, nodeSource, codec, evalCommandType, args.toArray(), false, noRetry);
}
Also used : RedisException(org.redisson.client.RedisException) RPromise(org.redisson.misc.RPromise) java.util(java.util) NodeType(org.redisson.api.NodeType) Codec(org.redisson.client.codec.Codec) StringCodec(org.redisson.client.codec.StringCodec) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) MessageDigest(java.security.MessageDigest) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) RedisClient(org.redisson.client.RedisClient) AtomicReference(java.util.concurrent.atomic.AtomicReference) RFuture(org.redisson.api.RFuture) NodeSource(org.redisson.connection.NodeSource) CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) ByteBuf(io.netty.buffer.ByteBuf) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BiConsumer(java.util.function.BiConsumer) RedissonReference(org.redisson.RedissonReference) SlotCallback(org.redisson.SlotCallback) Logger(org.slf4j.Logger) LRUCacheMap(org.redisson.cache.LRUCacheMap) ConnectionManager(org.redisson.connection.ConnectionManager) RedissonPromise(org.redisson.misc.RedissonPromise) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) RedisCommands(org.redisson.client.protocol.RedisCommands) ExecutionException(java.util.concurrent.ExecutionException) AtomicLong(java.util.concurrent.atomic.AtomicLong) ByteBufUtil(io.netty.buffer.ByteBufUtil) RedisRedirectException(org.redisson.client.RedisRedirectException) CompletionStage(java.util.concurrent.CompletionStage) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) Entry(java.util.Map.Entry) RedisCommand(org.redisson.client.protocol.RedisCommand) RedissonObjectBuilder(org.redisson.liveobject.core.RedissonObjectBuilder) RFuture(org.redisson.api.RFuture) NodeSource(org.redisson.connection.NodeSource) CompletableFuture(java.util.concurrent.CompletableFuture) CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) RedisCommand(org.redisson.client.protocol.RedisCommand)

Aggregations

RedisCommand (org.redisson.client.protocol.RedisCommand)16 GeoSearchNode (org.redisson.api.geo.GeoSearchNode)5 CompletableFuture (java.util.concurrent.CompletableFuture)4 RFuture (org.redisson.api.RFuture)4 Codec (org.redisson.client.codec.Codec)4 StringCodec (org.redisson.client.codec.StringCodec)4 MapGetAllDecoder (org.redisson.connection.decoder.MapGetAllDecoder)4 CompletableFutureWrapper (org.redisson.misc.CompletableFutureWrapper)4 ByteBuf (io.netty.buffer.ByteBuf)3 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)3 ByteBufUtil (io.netty.buffer.ByteBufUtil)3 ReferenceCountUtil (io.netty.util.ReferenceCountUtil)3 IOException (java.io.IOException)3 MessageDigest (java.security.MessageDigest)3 java.util (java.util)3 Entry (java.util.Map.Entry)3 CompletionStage (java.util.concurrent.CompletionStage)3 ExecutionException (java.util.concurrent.ExecutionException)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3