-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Consolidated generator classes
- Loading branch information
Showing
7 changed files
with
104 additions
and
151 deletions.
There are no files selected for viewing
88 changes: 71 additions & 17 deletions
88
core/riot-core/src/main/java/com/redis/riot/core/FakerItemReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,109 @@ | ||
package com.redis.riot.core; | ||
|
||
import java.util.HashMap; | ||
import java.util.LinkedHashMap; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
|
||
import org.springframework.batch.item.ItemReader; | ||
import org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader; | ||
import org.springframework.expression.EvaluationContext; | ||
import org.springframework.expression.Expression; | ||
import org.springframework.expression.spel.standard.SpelExpressionParser; | ||
import org.springframework.expression.spel.support.SimpleEvaluationContext; | ||
import org.springframework.expression.spel.support.SimpleEvaluationContext.Builder; | ||
import org.springframework.util.Assert; | ||
import org.springframework.util.ClassUtils; | ||
|
||
import com.redis.spring.batch.common.IntRange; | ||
|
||
import net.datafaker.Faker; | ||
|
||
/** | ||
* {@link ItemReader} that generates HashMaps using Faker. | ||
* | ||
* @author Julien Ruaux | ||
*/ | ||
public class FakerItemReader extends AbstractItemCountingItemStreamItemReader<Map<String, Object>> { | ||
|
||
public static final int DEFAULT_START = 1; | ||
public static final int DEFAULT_COUNT = 1000; | ||
private static final String FIELD_THREAD = "thread"; | ||
public static final String FIELD_INDEX = "index"; | ||
public static final Locale DEFAULT_LOCALE = Locale.getDefault(); | ||
|
||
private final SpelExpressionParser parser = new SpelExpressionParser(); | ||
|
||
private final Map<String, Expression> expressions = new LinkedHashMap<>(); | ||
private IntRange indexRange = IntRange.from(1); | ||
private Locale locale = DEFAULT_LOCALE; | ||
private boolean includeMetadata; | ||
|
||
private int start = DEFAULT_START; | ||
private int count = DEFAULT_COUNT; | ||
private final Generator<Map<String, Object>> generator; | ||
private EvaluationContext context; | ||
|
||
public FakerItemReader(Generator<Map<String, Object>> generator) { | ||
public FakerItemReader() { | ||
setName(ClassUtils.getShortName(getClass())); | ||
Assert.notNull(generator, "A generator is required"); | ||
setMaxItemCount(count); | ||
this.generator = generator; | ||
} | ||
|
||
public void setStart(int start) { | ||
this.start = start; | ||
public FakerItemReader withIndexRange(IntRange range) { | ||
this.indexRange = range; | ||
return this; | ||
} | ||
|
||
public void setCount(int count) { | ||
this.count = count; | ||
setMaxItemCount(count); | ||
public FakerItemReader withLocale(Locale locale) { | ||
this.locale = locale; | ||
return this; | ||
} | ||
|
||
public FakerItemReader withIncludeMetadata(boolean include) { | ||
this.includeMetadata = include; | ||
return this; | ||
} | ||
|
||
public FakerItemReader withField(String field, String expression) { | ||
this.expressions.put(field, parser.parseExpression(expression)); | ||
return this; | ||
} | ||
|
||
public FakerItemReader withFields(String... fields) { | ||
Assert.isTrue(fields.length % 2 == 0, | ||
"fields.length must be a multiple of 2 and contain a sequence of field1, expression1, field2, expression2, fieldN, expressionN"); | ||
for (int i = 0; i < fields.length; i += 2) { | ||
withField(fields[i], fields[i + 1]); | ||
} | ||
return this; | ||
} | ||
|
||
@Override | ||
protected Map<String, Object> doRead() throws Exception { | ||
return generator.next(start + ((getCurrentItemCount() - 1) % count)); | ||
Map<String, Object> map = new HashMap<>(); | ||
int index = index(); | ||
if (includeMetadata) { | ||
map.put(FIELD_INDEX, index); | ||
map.put(FIELD_THREAD, Thread.currentThread().getId()); | ||
} | ||
context.setVariable(FIELD_INDEX, index); | ||
for (Entry<String, Expression> expression : expressions.entrySet()) { | ||
map.put(expression.getKey(), expression.getValue().getValue(context)); | ||
} | ||
return map; | ||
} | ||
|
||
private int index() { | ||
return (indexRange.getMin() + getCurrentItemCount() - 1) % indexRange.getMax(); | ||
} | ||
|
||
@Override | ||
protected void doOpen() throws Exception { | ||
// nothing to see here, move along | ||
Faker faker = new Faker(locale); | ||
Builder contextBuilder = SimpleEvaluationContext.forPropertyAccessors(new ReflectivePropertyAccessor()); | ||
contextBuilder.withInstanceMethods(); | ||
contextBuilder.withRootObject(faker); | ||
this.context = contextBuilder.build(); | ||
} | ||
|
||
@Override | ||
protected void doClose() throws Exception { | ||
// nothing to see here, move along | ||
|
||
} | ||
|
||
} |
6 changes: 0 additions & 6 deletions
6
core/riot-core/src/main/java/com/redis/riot/core/Generator.java
This file was deleted.
Oops, something went wrong.
71 changes: 0 additions & 71 deletions
71
core/riot-core/src/main/java/com/redis/riot/core/MapGenerator.java
This file was deleted.
Oops, something went wrong.
22 changes: 0 additions & 22 deletions
22
core/riot-core/src/main/java/com/redis/riot/core/MapWithMetadataGenerator.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters