Search in sources :

Example 1 with ProtocolKeyword

use of io.lettuce.core.protocol.ProtocolKeyword in project lettuce-core by lettuce-io.

the class CliParser method parse.

/**
 * Parse a CLI command string into a {@link Command}.
 *
 * @param command
 * @return
 */
public static Command<String, String, List<Object>> parse(String command) {
    String[] parts = command.split(" ");
    boolean quoted = false;
    ProtocolKeyword type = null;
    CommandArgs<String, String> args = new CommandArgs<>(StringCodec.UTF8);
    StringBuilder buffer = new StringBuilder();
    for (int i = 0; i < parts.length; i++) {
        String part = parts[i];
        if (quoted && part.endsWith("\"")) {
            buffer.append(part, 0, part.length() - 1);
        } else if (part.startsWith("\"")) {
            quoted = true;
            buffer.append(buffer.append(part.substring(1)));
        } else {
            buffer.append(part);
        }
        if (quoted) {
            continue;
        }
        if (type == null) {
            String typeName = buffer.toString();
            type = new ProtocolKeyword() {

                @Override
                public byte[] getBytes() {
                    return name().getBytes(StandardCharsets.UTF_8);
                }

                @Override
                public String name() {
                    return typeName;
                }
            };
        } else {
            args.addKey(buffer.toString());
        }
        buffer.setLength(0);
    }
    return new Command<>(type, new ArrayOutput<>(StringCodec.UTF8), args);
}
Also used : CommandArgs(io.lettuce.core.protocol.CommandArgs) Command(io.lettuce.core.protocol.Command) ProtocolKeyword(io.lettuce.core.protocol.ProtocolKeyword)

Example 2 with ProtocolKeyword

use of io.lettuce.core.protocol.ProtocolKeyword in project uavstack by uavorg.

the class Lettuce5CommandHandlerIT method doWriteStart.

@SuppressWarnings({ "unchecked", "rawtypes" })
public Object doWriteStart(Object[] args) {
    RedisCommand cmdRc = (RedisCommand) args[0];
    ProtocolKeyword cmd = (ProtocolKeyword) cmdRc.getType();
    String host = (String) args[1];
    Integer port = (Integer) args[2];
    String targetURL = "redis://" + host + ":" + port;
    String redisAction = cmd.toString();
    if (logger.isDebugable()) {
        logger.debug("REDIS INVOKE START: " + targetURL + " action: " + redisAction, null);
    }
    Map<String, Object> params = new HashMap<String, Object>();
    params.put(CaptureConstants.INFO_CLIENT_REQUEST_URL, targetURL);
    params.put(CaptureConstants.INFO_CLIENT_REQUEST_ACTION, redisAction);
    params.put(CaptureConstants.INFO_CLIENT_APPID, appid);
    params.put(CaptureConstants.INFO_CLIENT_TYPE, "redis.client.Lettuce");
    // register adapter
    UAVServer.instance().runSupporter("com.creditease.uav.apm.supporters.InvokeChainSupporter", "registerAdapter", Lettuce5ClientAdapter.class);
    ivcContextParams = (Map<String, Object>) UAVServer.instance().runSupporter("com.creditease.uav.apm.supporters.InvokeChainSupporter", "runCap", InvokeChainConstants.CHAIN_APP_CLIENT, InvokeChainConstants.CapturePhase.PRECAP, params, Lettuce5ClientAdapter.class, args);
    UAVServer.instance().runMonitorCaptureOnServerCapPoint(CaptureConstants.CAPPOINT_APP_CLIENT, Monitor.CapturePhase.PRECAP, params);
    return null;
}
Also used : HashMap(java.util.HashMap) RedisCommand(io.lettuce.core.protocol.RedisCommand) ProtocolKeyword(io.lettuce.core.protocol.ProtocolKeyword)

Example 3 with ProtocolKeyword

use of io.lettuce.core.protocol.ProtocolKeyword in project ballcat by ballcat-projects.

the class AbstractRedisModuleHelper method execute.

@SuppressWarnings("unchecked")
protected <T> Optional<T> execute(String key, ProtocolKeyword type, CommandOutput<byte[], byte[], T> output, String... args) {
    List<byte[]> extraArgs = Arrays.stream(args).filter(StringUtils::hasLength).map(arg -> valueSerializer.serialize(arg)).collect(Collectors.toList());
    CommandArgs<byte[], byte[]> commandArgs = new CommandArgs<>(codec).addKey(keySerializer.serialize(key)).addValues(extraArgs);
    try (LettuceConnection connection = (LettuceConnection) connectionFactory.getConnection()) {
        RedisFuture<T> future = connection.getNativeConnection().dispatch(type, output, commandArgs);
        return Optional.ofNullable(future.get());
    } catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
    } catch (Exception e) {
        log.error("[execute] 执行异常, KEY: [{}], type: [{}], args: [{}]", key, type.name(), args, e);
    }
    return Optional.empty();
}
Also used : Setter(lombok.Setter) Arrays(java.util.Arrays) ByteArrayCodec(io.lettuce.core.codec.ByteArrayCodec) LettuceConnection(org.springframework.data.redis.connection.lettuce.LettuceConnection) Getter(lombok.Getter) RequiredArgsConstructor(lombok.RequiredArgsConstructor) LettuceConnectionFactory(org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory) ProtocolKeyword(io.lettuce.core.protocol.ProtocolKeyword) Collectors(java.util.stream.Collectors) CommandOutput(io.lettuce.core.output.CommandOutput) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) CommandArgs(io.lettuce.core.protocol.CommandArgs) RedisFuture(io.lettuce.core.RedisFuture) Optional(java.util.Optional) RedisSerializer(org.springframework.data.redis.serializer.RedisSerializer) StringUtils(org.springframework.util.StringUtils) CommandArgs(io.lettuce.core.protocol.CommandArgs) StringUtils(org.springframework.util.StringUtils) LettuceConnection(org.springframework.data.redis.connection.lettuce.LettuceConnection)

Aggregations

ProtocolKeyword (io.lettuce.core.protocol.ProtocolKeyword)3 CommandArgs (io.lettuce.core.protocol.CommandArgs)2 RedisFuture (io.lettuce.core.RedisFuture)1 ByteArrayCodec (io.lettuce.core.codec.ByteArrayCodec)1 CommandOutput (io.lettuce.core.output.CommandOutput)1 Command (io.lettuce.core.protocol.Command)1 RedisCommand (io.lettuce.core.protocol.RedisCommand)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Getter (lombok.Getter)1 RequiredArgsConstructor (lombok.RequiredArgsConstructor)1 Setter (lombok.Setter)1 Slf4j (lombok.extern.slf4j.Slf4j)1 LettuceConnection (org.springframework.data.redis.connection.lettuce.LettuceConnection)1 LettuceConnectionFactory (org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory)1 RedisSerializer (org.springframework.data.redis.serializer.RedisSerializer)1 StringUtils (org.springframework.util.StringUtils)1