org.infinispan.server.resp.commands.hash.HRANDFIELD Maven / Gradle / Ivy
Show all versions of infinispan-server-resp Show documentation
package org.infinispan.server.resp.commands.hash;
import static org.infinispan.server.resp.RespConstants.NULL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import org.infinispan.multimap.impl.EmbeddedMultimapPairCache;
import org.infinispan.server.resp.ByteBufPool;
import org.infinispan.server.resp.ByteBufferUtils;
import org.infinispan.server.resp.Consumers;
import org.infinispan.server.resp.Resp3Handler;
import org.infinispan.server.resp.RespCommand;
import org.infinispan.server.resp.RespErrorUtil;
import org.infinispan.server.resp.RespRequestHandler;
import org.infinispan.server.resp.Util;
import org.infinispan.server.resp.commands.ArgumentUtils;
import org.infinispan.server.resp.commands.Resp3Command;
import io.netty.channel.ChannelHandlerContext;
/**
* `HRANDFIELD key [count [WITHVALUES]]
` command.
*
* Returns count
keys from the hash stored at key
. Negative count
can return
* duplicated keys.
*
* @since 15.0
* @see Redis documentation
* @author José Bolina
*/
public class HRANDFIELD extends RespCommand implements Resp3Command {
private static final byte[] WITH_VALUES = "WITHVALUES".getBytes(StandardCharsets.US_ASCII);
public HRANDFIELD() {
super(-2, 1, 1, 1);
}
@Override
public CompletionStage perform(Resp3Handler handler, ChannelHandlerContext ctx, List arguments) {
byte[] key = arguments.get(0);
int count = 1;
boolean countDefined = false;
if (arguments.size() > 1) {
count = ArgumentUtils.toInt(arguments.get(1));
countDefined = true;
}
if (count == 0) {
ByteBufferUtils.bytesToResult(Collections.emptyList(), handler.allocator());
return handler.myStage();
}
boolean withValues = false;
if (arguments.size() > 2) {
// Syntax error, only a single option acceptable.
if (!Util.isAsciiBytesEquals(WITH_VALUES, arguments.get(2))) {
RespErrorUtil.syntaxError(handler.allocator());
return handler.myStage();
}
withValues = true;
}
EmbeddedMultimapPairCache multimap = handler.getHashMapMultimap();
BiConsumer