Search in sources :

Example 1 with CommandArgs

use of io.lettuce.core.protocol.CommandArgs 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 CommandArgs

use of io.lettuce.core.protocol.CommandArgs in project turms by turms-im.

the class TurmsRedisCommandBuilder method georadiusbymember.

// Geo
public <T> Command<ByteBuf, ByteBuf, List<GeoWithin<T>>> georadiusbymember(CommandType commandType, ByteBuf key, ByteBuf member, double distance, String unit, GeoArgs geoArgs) {
    CommandArgs<ByteBuf, ByteBuf> args = new CommandArgs<>(memberCodec).addKey(key).addValue(member).add(distance).add(unit);
    geoArgs.build(args);
    GeoWithinListOutput output = new GeoWithinListOutput<>(memberCodec, geoArgs.isWithDistance(), geoArgs.isWithHash(), geoArgs.isWithCoordinates());
    return createCommand(commandType, output, args);
}
Also used : CommandArgs(io.lettuce.core.protocol.CommandArgs) GeoWithinListOutput(io.lettuce.core.output.GeoWithinListOutput) ByteBuf(io.netty.buffer.ByteBuf)

Example 3 with CommandArgs

use of io.lettuce.core.protocol.CommandArgs 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)

Example 4 with CommandArgs

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

the class CommandSegmentCommandFactory method createCommand.

@Override
public RedisCommand<Object, Object, Object> createCommand(Object[] parameters) {
    MethodParametersAccessor parametersAccessor = new CodecAwareMethodParametersAccessor(new DefaultMethodParametersAccessor(commandMethod.getParameters(), parameters), typeContext);
    CommandArgs<Object, Object> args = new CommandArgs<>(redisCodec);
    CommandOutput<Object, Object, ?> output = outputFactory.create(redisCodec);
    Command<Object, Object, ?> command = new Command<>(this.segments.getCommandType(), output, args);
    parameterBinder.bind(args, redisCodec, segments, parametersAccessor);
    return (Command) command;
}
Also used : MethodParametersAccessor(io.lettuce.core.dynamic.parameter.MethodParametersAccessor) CommandArgs(io.lettuce.core.protocol.CommandArgs) RedisCommand(io.lettuce.core.protocol.RedisCommand) Command(io.lettuce.core.protocol.Command)

Example 5 with CommandArgs

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

the class ParameterBinderUnitTests method bind.

private CommandArgs<String, String> bind(CommandMethod commandMethod, Object object) {
    DefaultMethodParametersAccessor parametersAccessor = new DefaultMethodParametersAccessor(commandMethod.getParameters(), object);
    CommandArgs<String, String> args = new CommandArgs<>(new StringCodec());
    binder.bind(args, StringCodec.UTF8, segments, parametersAccessor);
    return args;
}
Also used : CommandArgs(io.lettuce.core.protocol.CommandArgs) StringCodec(io.lettuce.core.codec.StringCodec)

Aggregations

CommandArgs (io.lettuce.core.protocol.CommandArgs)5 Command (io.lettuce.core.protocol.Command)2 ProtocolKeyword (io.lettuce.core.protocol.ProtocolKeyword)2 RedisFuture (io.lettuce.core.RedisFuture)1 ByteArrayCodec (io.lettuce.core.codec.ByteArrayCodec)1 StringCodec (io.lettuce.core.codec.StringCodec)1 MethodParametersAccessor (io.lettuce.core.dynamic.parameter.MethodParametersAccessor)1 CommandOutput (io.lettuce.core.output.CommandOutput)1 GeoWithinListOutput (io.lettuce.core.output.GeoWithinListOutput)1 RedisCommand (io.lettuce.core.protocol.RedisCommand)1 ByteBuf (io.netty.buffer.ByteBuf)1 Arrays (java.util.Arrays)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