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

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

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

import java.util.function.Function;
import java.util.function.Predicate;

import org.springframework.batch.item.Chunk;

import com.redis.lettucemod.api.async.RediSearchAsyncCommands;
import com.redis.lettucemod.search.Suggestion;
import com.redis.spring.batch.util.Predicates;

import io.lettuce.core.RedisFuture;
import io.lettuce.core.api.async.BaseRedisAsyncCommands;

public class Sugadd extends AbstractKeyWriteOperation {

	private final Function> suggestionFunction;

	private Predicate incrPredicate = Predicates.isFalse();

	public Sugadd(Function keyFunction, Function> suggestionFunction) {
		super(keyFunction);
		this.suggestionFunction = suggestionFunction;
	}

	public void setIncr(boolean incr) {
		this.incrPredicate = t -> incr;
	}

	public void setIncrPredicate(Predicate predicate) {
		this.incrPredicate = predicate;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	protected void execute(BaseRedisAsyncCommands commands, T item, K key, Chunk> outputs) {
		outputs.add((RedisFuture) execute((RediSearchAsyncCommands) commands, item, key));
	}

	private RedisFuture execute(RediSearchAsyncCommands commands, T item, K key) {
		Suggestion suggestion = suggestionFunction.apply(item);
		if (incrPredicate.test(item)) {
			return commands.ftSugaddIncr(key, suggestion);
		}
		return commands.ftSugadd(key, suggestion);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy