![JAR search and dependency download from the Maven repository](/logo.png)
org.infinispan.server.resp.commands.hash.HINCRBY Maven / Gradle / Ivy
Show all versions of infinispan-server-resp Show documentation
package org.infinispan.server.resp.commands.hash;
import java.util.List;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.atomic.AtomicBoolean;
import org.infinispan.multimap.impl.EmbeddedMultimapPairCache;
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.commands.ArgumentUtils;
import org.infinispan.server.resp.commands.Resp3Command;
import org.infinispan.server.resp.serialization.Resp3Response;
import io.netty.channel.ChannelHandlerContext;
/**
* `HINCRBY key field increment
` command.
*
* Increments the long value at the field
in the hash map stored at key
by increment
.
* The command returns an error if the stored value is not a numeric long.
*
* @since 15.0
* @see Redis Documentation
* @author José Bolina
*/
public class HINCRBY extends RespCommand implements Resp3Command {
public HINCRBY() {
super(4, 1, 1, 1);
}
@Override
public CompletionStage perform(Resp3Handler handler, ChannelHandlerContext ctx, List arguments) {
EmbeddedMultimapPairCache multimap = handler.getHashMapMultimap();
long delta = ArgumentUtils.toLong(arguments.get(2));
AtomicBoolean failed = new AtomicBoolean(false);
CompletionStage cs = multimap.compute(arguments.get(0), arguments.get(1), (ignore, prev) -> {
if (prev == null) return arguments.get(2);
// This is supposed to throw when the `prev` is not a long.
long prevLong = ArgumentUtils.toLong(prev);
long result = prevLong + delta;
// Same check as Math#addExact(long, long).
// We bring it here to avoid throwing an exception.
if (((prevLong ^ result) & (delta ^ result)) < 0) {
failed.set(true);
return prev;
}
return ArgumentUtils.toByteArray(result);
});
return handler.stageToReturn(cs, ctx, (res, alloc) -> {
if (failed.get()) {
RespErrorUtil.customError("increment or decrement would overflow", alloc);
} else {
Resp3Response.integers(ArgumentUtils.toLong(res), alloc);
}
});
}
}