All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.redis.spring.batch.reader.Evalsha Maven / Gradle / Ivy

package com.redis.spring.batch.reader;

import java.util.List;
import java.util.function.Function;

import com.redis.spring.batch.common.BatchUtils;
import com.redis.spring.batch.common.Operation;

import io.lettuce.core.RedisFuture;
import io.lettuce.core.ScriptOutputType;
import io.lettuce.core.api.async.BaseRedisAsyncCommands;
import io.lettuce.core.api.async.RedisScriptingAsyncCommands;
import io.lettuce.core.codec.RedisCodec;

@SuppressWarnings("unchecked")
public class Evalsha implements Operation> {

	private static final Object[] EMPTY_ARRAY = new Object[0];

	private final String digest;
	private final Function keyFunction;
	private final Function stringValueFunction;

	private Function argsFunction = t -> (V[]) EMPTY_ARRAY;

	public Evalsha(String digest, RedisCodec codec, Function key) {
		this.digest = digest;
		this.stringValueFunction = BatchUtils.stringValueFunction(codec);
		this.keyFunction = key;
	}

	@Override
	public void execute(BaseRedisAsyncCommands commands, Iterable inputs,
			List>> outputs) {
		RedisScriptingAsyncCommands scriptingCommands = (RedisScriptingAsyncCommands) commands;
		for (I item : inputs) {
			K[] keys = (K[]) new Object[] { keyFunction.apply(item) };
			V[] args = argsFunction.apply(item);
			outputs.add(scriptingCommands.evalsha(digest, ScriptOutputType.MULTI, keys, args));
		}
	}

	public void setArgs(Function function) {
		this.argsFunction = function;
	}

	public void setArgs(Object... args) {
		V[] encodedArgs = (V[]) new Object[args.length];
		for (int index = 0; index < args.length; index++) {
			encodedArgs[index] = stringValueFunction.apply(String.valueOf(args[index]));
		}
		this.argsFunction = t -> encodedArgs;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy