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

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

package com.redis.spring.batch.reader;

import java.util.ArrayList;
import java.util.List;

import org.springframework.util.Assert;

import com.redis.lettucemod.api.StatefulRedisModulesConnection;
import com.redis.spring.batch.common.KeyValue;
import com.redis.spring.batch.common.OperationExecutor;

import io.lettuce.core.AbstractRedisClient;
import io.lettuce.core.ReadFrom;
import io.lettuce.core.RedisFuture;
import io.lettuce.core.api.async.BaseRedisAsyncCommands;
import io.lettuce.core.codec.RedisCodec;

public class KeyComparisonRead implements InitializingOperation> {

	private final MemKeyValueRead source;
	private final MemKeyValueRead target;

	private OperationExecutor> targetOperationExecutor;
	private AbstractRedisClient targetClient;
	private int targetPoolSize;
	private ReadFrom targetReadFrom;
	private KeyComparator comparator = new KeyComparator<>();
	private RedisCodec codec;

	public KeyComparisonRead(RedisCodec codec, MemKeyValueRead source,
			MemKeyValueRead target) {
		this.codec = codec;
		this.source = source;
		this.target = target;
	}

	public void setTargetClient(AbstractRedisClient targetClient) {
		this.targetClient = targetClient;
	}

	public void setTargetPoolSize(int targetPoolSize) {
		this.targetPoolSize = targetPoolSize;
	}

	public void setTargetReadFrom(ReadFrom targetReadFrom) {
		this.targetReadFrom = targetReadFrom;
	}

	public void setComparator(KeyComparator comparator) {
		this.comparator = comparator;
	}

	@Override
	public void afterPropertiesSet(StatefulRedisModulesConnection connection) throws Exception {
		Assert.notNull(targetClient, "Target Redis client not set");
		Assert.isTrue(targetPoolSize > 0, "Target pool size must be strictly positive");
		source.afterPropertiesSet(connection);
		targetOperationExecutor = new OperationExecutor<>(codec, target);
		targetOperationExecutor.setClient(targetClient);
		targetOperationExecutor.setPoolSize(targetPoolSize);
		targetOperationExecutor.setReadFrom(targetReadFrom);
		targetOperationExecutor.afterPropertiesSet();
	}

	@Override
	public void execute(BaseRedisAsyncCommands commands, Iterable inputs,
			List>> outputs) {
		List>> sourceOutputs = new ArrayList<>();
		source.execute(commands, inputs, sourceOutputs);
		List> targetItems = targetOperationExecutor.apply(inputs);
		for (int index = 0; index < sourceOutputs.size(); index++) {
			RedisFuture> sourceOutput = sourceOutputs.get(index);
			KeyValue targetItem = targetItems.get(index);
			outputs.add(new MappingRedisFuture<>(sourceOutput, v -> comparator.compare(v, targetItem)));
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy