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

com.redis.spring.batch.writer.operation.TsAddAll 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 com.redis.lettucemod.api.async.RedisTimeSeriesAsyncCommands;
import com.redis.lettucemod.timeseries.AddOptions;
import com.redis.lettucemod.timeseries.Sample;
import com.redis.spring.batch.common.Operation;

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

public class TsAddAll implements Operation {

	private Function keyFunction;

	private Function> samplesFunction;

	private Function> optionsFunction = t -> null;

	public void setKey(K key) {
		this.keyFunction = k -> key;
	}

	public void setKeyFunction(Function keyFunction) {
		this.keyFunction = keyFunction;
	}

	public void setSamplesFunction(Function> function) {
		this.samplesFunction = function;
	}

	public void setOptions(AddOptions options) {
		this.optionsFunction = t -> options;
	}

	public void setOptionsFunction(Function> function) {
		this.optionsFunction = function;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void execute(BaseRedisAsyncCommands commands, Chunk items,
			Chunk> futures) {
		RedisTimeSeriesAsyncCommands timeseriesCommands = (RedisTimeSeriesAsyncCommands) commands;
		for (T item : items) {
			K key = keyFunction.apply(item);
			AddOptions options = optionsFunction.apply(item);
			Collection samples = samplesFunction.apply(item);
			if (CollectionUtils.isEmpty(samples)) {
				continue;
			}
			for (Sample sample : samples) {
				futures.add((RedisFuture) timeseriesCommands.tsAdd(key, sample, options));
			}
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy