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

com.redis.spring.batch.writer.operation.ZaddAll Maven / Gradle / Ivy

The newest version!
package com.redis.spring.batch.writer.operation;

import java.util.Collection;
import java.util.function.Function;

import org.springframework.batch.item.Chunk;
import org.springframework.util.CollectionUtils;

import io.lettuce.core.RedisFuture;
import io.lettuce.core.ScoredValue;
import io.lettuce.core.ZAddArgs;
import io.lettuce.core.api.async.BaseRedisAsyncCommands;
import io.lettuce.core.api.async.RedisSortedSetAsyncCommands;

public class ZaddAll extends AbstractKeyWriteOperation {

	private final Function>> valuesFunction;
	private Function argsFunction = t -> null;

	public ZaddAll(Function keyFunction, Function>> valuesFunction) {
		super(keyFunction);
		this.valuesFunction = valuesFunction;
	}

	public void setArgs(ZAddArgs args) {
		this.argsFunction = t -> args;
	}

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

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	protected void execute(BaseRedisAsyncCommands commands, T item, K key, Chunk> outputs) {
		Collection> values = valuesFunction.apply(item);
		if (!CollectionUtils.isEmpty(values)) {
			ZAddArgs args = argsFunction.apply(item);
			ScoredValue[] array = values.toArray(new ScoredValue[0]);
			outputs.add((RedisFuture) ((RedisSortedSetAsyncCommands) commands).zadd(key, args, array));
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy