Skip to content

Commit

Permalink
Fixed KeyValueProcessor expression parsing and added test case
Browse files Browse the repository at this point in the history
  • Loading branch information
jruaux committed Aug 19, 2021
1 parent bb0fb00 commit 86de91b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ void replicate(RedisServer container) throws Throwable {
Assertions.assertEquals(sourceSize, targetSync.dbsize());
}

@ParameterizedTest
@MethodSource("containers")
void replicateKeyProcessor(RedisServer container) throws Throwable {
dataGenerator(container).build().call();
RedisServerCommands<String, String> sync = sync(container);
Long sourceSize = sync.dbsize();
Assertions.assertTrue(sourceSize > 0);
execute("replicate-key-processor", container, r -> configureReplicateCommand(r,false));
Assertions.assertEquals(sourceSize, targetSync.dbsize());
RedisStringCommands<String,String> stringCommands = sync(container);
Assertions.assertEquals(stringCommands.get("string:123"), targetSync.get("0:string:123"));
}

@ParameterizedTest
@MethodSource("containers")
public void replicateLive(RedisServer container) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
riot-redis -h source -p 6379 replicate -h target -p 6380 --batch 10 --key-process="#{#src.database}:#{key}"
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@
import org.springframework.batch.item.redis.support.KeyValue;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.common.TemplateParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import picocli.CommandLine.Option;

@Data
public class KeyValueProcessorOptions {

@Option(names = "--key-process", description = "SpEL expression to transform each key", paramLabel = "<exp>")
private Expression expression;
private String keyProcessor;

public <T extends KeyValue<?>> ItemProcessor<T, T> processor(RedisOptions sourceRedis, RedisOptions targetRedis) {
if (expression == null) {
if (keyProcessor == null) {
return null;
}
SpelExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression(keyProcessor, new TemplateParserContext());
EvaluationContext context = new StandardEvaluationContext();
context.setVariable("src", sourceRedis.uris().get(0));
context.setVariable("dest", targetRedis.uris().get(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.redis.support.KeyValue;
import org.springframework.expression.Expression;
import org.springframework.expression.common.TemplateParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

Expand All @@ -25,9 +24,7 @@ public void testKeyValueProcessor() {
@Test
public void testKeyValueProcessorOptions() throws Exception {
KeyValueProcessorOptions options = new KeyValueProcessorOptions();
SpelExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("#{#src.database}:#{key}:#{#src.host}", new TemplateParserContext());
options.setExpression(expression);
options.setKeyProcessor("#{#src.database}:#{key}:#{#src.host}");
RedisOptions src = new RedisOptions();
src.setDatabase(14);
src.setHost("srchost");
Expand Down

0 comments on commit 86de91b

Please sign in to comment.