Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Commit

Permalink
Expensive for creating parser instance in execute() method.
Browse files Browse the repository at this point in the history
Closes #4
  • Loading branch information
johtani committed Sep 4, 2019
1 parent e407cff commit 611d9c4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class CsvProcessor extends AbstractProcessor {
private final String field;
private final List<String> columns;
private final CsvParserSettings csvSettings;
private final CsvParser parser;

public CsvProcessor(String tag, String field, List<String> columns, char quoteChar, char separator) throws IOException {
super(tag);
Expand All @@ -46,15 +47,15 @@ public CsvProcessor(String tag, String field, List<String> columns, char quoteCh
csvSettings = new CsvParserSettings();
csvSettings.getFormat().setQuote(quoteChar);
csvSettings.getFormat().setDelimiter(separator);
parser = new CsvParser(this.csvSettings);
}

@Override
public IngestDocument execute(IngestDocument ingestDocument) throws Exception {
String content = ingestDocument.getFieldValue(field, String.class);

if (Strings.hasLength(content)) {
CsvParser parser = new CsvParser(this.csvSettings);
String[] values = parser.parseLine(content);
String[] values = parser.parseLine(content);
if (values.length != this.columns.size()) {
// TODO should be error?
throw new IllegalArgumentException("field[" + this.field + "] size ["
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,27 @@ public void testEmptyField() throws Exception {
assertThat(e.getMessage(), equalTo("field[source_field] is empty string."));
}

public void testManyTimes() throws Exception {
CsvProcessor processor = new CsvProcessor(randomAlphaOfLength(10), "source_field", defaultColumns, '\"', ',');
int times = 50000;

logger.info("start");
long startTime = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
Map<String, Object> document = new HashMap<>();
document.put("source_field", "a_value, b_value");
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);


Map<String, Object> data = processor.execute(ingestDocument).getSourceAndMetadata();

assertThat(data, hasKey("a"));
assertThat(data.get("a"), is("a_value"));
assertThat(data, hasKey("b"));
assertThat(data.get("b"), is("b_value"));
}
logger.info("end. Loop is " + times + " times. Process Time is " + String.valueOf(System.currentTimeMillis() - startTime) + " ms");
}


}
Expand Down

0 comments on commit 611d9c4

Please sign in to comment.