-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added parallel handling for rows (besides XLSX)
- Loading branch information
Nikolay Meleshchenko
committed
Dec 8, 2022
1 parent
1cdfa90
commit 1ff5102
Showing
11 changed files
with
124 additions
and
79 deletions.
There are no files selected for viewing
21 changes: 12 additions & 9 deletions
21
src/main/java/com/github/boggard/universalreader/CSVReader.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,32 +1,35 @@ | ||
package com.github.boggard.universalreader; | ||
|
||
import com.github.boggard.universalreader.contenthandler.ContentHandler; | ||
import com.github.boggard.universalreader.contenthandler.RecordHandler; | ||
import com.github.boggard.universalreader.util.ReaderUtil; | ||
import lombok.experimental.UtilityClass; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.stream.Stream; | ||
|
||
import static com.github.boggard.universalreader.util.StringStreamUtil.stringStream; | ||
|
||
@UtilityClass | ||
public class CSVReader { | ||
|
||
public static <R> R readRecords(FileSource inputStreamSource, ContentHandler<R> contentsHandler, | ||
ReaderConfiguration configuration) | ||
public static <R, T> R readRecords(FileSource inputStreamSource, ContentHandler<R, T> contentsHandler, | ||
ReaderConfiguration configuration) | ||
throws IOException { | ||
try (InputStream inputStream = inputStreamSource.getInputStream(); | ||
BufferedReader reader = ReaderUtil.inputStreamToBufferedReader(inputStream)) { | ||
|
||
reader.lines().skip(configuration.getStartLineIndex()).forEach(line -> { | ||
String[] lineRecords = line.split(configuration.getRecordsSeparator()); | ||
for (String lineRecord : lineRecords) { | ||
ReaderUtil.readRecord(lineRecord, contentsHandler, configuration.getFieldsSeparator()); | ||
} | ||
}); | ||
RecordHandler<T> recordHandler = contentsHandler.recordHandler(); | ||
Stream<T> resultStream = stringStream(reader, configuration).skip(configuration.getStartLineIndex()) | ||
.map(line -> line.split(configuration.getRecordsSeparator())) | ||
.flatMap(Stream::of) | ||
.map(line -> recordHandler.handleAndReturnResult(line, configuration.getFieldsSeparator())); | ||
|
||
contentsHandler.endFile(); | ||
|
||
return contentsHandler.getResult(); | ||
return contentsHandler.getResult(resultStream); | ||
} | ||
} | ||
} |
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
15 changes: 10 additions & 5 deletions
15
src/main/java/com/github/boggard/universalreader/TXTReader.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,28 +1,33 @@ | ||
package com.github.boggard.universalreader; | ||
|
||
import com.github.boggard.universalreader.contenthandler.ContentHandler; | ||
import com.github.boggard.universalreader.contenthandler.RecordHandler; | ||
import com.github.boggard.universalreader.util.ReaderUtil; | ||
import lombok.experimental.UtilityClass; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.stream.Stream; | ||
|
||
import static com.github.boggard.universalreader.util.StringStreamUtil.stringStream; | ||
|
||
@UtilityClass | ||
public class TXTReader { | ||
|
||
public static <R> R readRecords(FileSource fileSource, ContentHandler<R> contentsHandler, | ||
ReaderConfiguration configuration) | ||
public static <R, T> R readRecords(FileSource fileSource, ContentHandler<R, T> contentsHandler, | ||
ReaderConfiguration configuration) | ||
throws IOException { | ||
try (InputStream inputStream = fileSource.getInputStream(); | ||
BufferedReader reader = ReaderUtil.inputStreamToBufferedReader(inputStream)) { | ||
|
||
reader.lines().skip(configuration.getStartLineIndex()) | ||
.forEach(line -> ReaderUtil.readRecord(line, contentsHandler, configuration.getFieldsSeparator())); | ||
RecordHandler<T> recordHandler = contentsHandler.recordHandler(); | ||
Stream<T> resultStream = stringStream(reader, configuration).skip(configuration.getStartLineIndex()) | ||
.map(line -> recordHandler.handleAndReturnResult(line, configuration.getFieldsSeparator())); | ||
|
||
contentsHandler.endFile(); | ||
|
||
return contentsHandler.getResult(); | ||
return contentsHandler.getResult(resultStream); | ||
} | ||
} | ||
} |
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
59 changes: 32 additions & 27 deletions
59
src/main/java/com/github/boggard/universalreader/XLSReader.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,51 +1,56 @@ | ||
package com.github.boggard.universalreader; | ||
|
||
import com.github.boggard.universalreader.contenthandler.ContentHandler; | ||
import com.github.boggard.universalreader.util.ReaderUtil; | ||
import com.github.boggard.universalreader.contenthandler.RecordHandler; | ||
import lombok.experimental.UtilityClass; | ||
import org.apache.poi.ss.usermodel.*; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.stream.Stream; | ||
import java.util.stream.StreamSupport; | ||
|
||
@UtilityClass | ||
public class XLSReader { | ||
|
||
private static final DataFormatter DATA_FORMATTER = new DataFormatter(); | ||
|
||
public static <R> R readRecords(File sourceFile, ContentHandler<R> contentsHandler, | ||
ReaderConfiguration configuration) | ||
public static <R, T> R readRecords(File sourceFile, ContentHandler<R, T> contentsHandler, | ||
ReaderConfiguration configuration) | ||
throws IOException { | ||
RecordHandler<T> recordHandler = contentsHandler.recordHandler(); | ||
try (Workbook workbook = WorkbookFactory.create(sourceFile)) { | ||
workbook.sheetIterator().forEachRemaining(sheet -> { | ||
for (int i = configuration.getStartLineIndex(); i < sheet.getLastRowNum() + 1; i++) { | ||
Row row = sheet.getRow(i); | ||
Stream<T> resultStream = StreamSupport.stream(workbook.spliterator(), configuration.isParallelProcessing()) | ||
.flatMap(sheet -> createRowStream(configuration, sheet)) | ||
.map(row -> handleRow(recordHandler, row)); | ||
|
||
if (row != null) { | ||
contentsHandler.startRecord(); | ||
|
||
row.cellIterator().forEachRemaining(cell -> { | ||
String value; | ||
contentsHandler.endFile(); | ||
|
||
if (cell.getCellType() == CellType.NUMERIC) { | ||
CellStyle cellStyle = cell.getCellStyle(); | ||
value = DATA_FORMATTER.formatRawCellContents(cell.getNumericCellValue(), | ||
cellStyle.getDataFormat(), cellStyle.getDataFormatString()); | ||
} else { | ||
value = cell.getStringCellValue(); | ||
} | ||
return contentsHandler.getResult(resultStream); | ||
} | ||
} | ||
|
||
contentsHandler.startField(cell.getColumnIndex(), value); | ||
}); | ||
private static Stream<Row> createRowStream(ReaderConfiguration configuration, Sheet sheet) { | ||
return StreamSupport.stream(sheet.spliterator(), configuration.isParallelProcessing()) | ||
.skip(configuration.getStartLineIndex()) | ||
.filter(row -> row != null && row.getRowNum() < sheet.getLastRowNum() + 1); | ||
} | ||
|
||
ReaderUtil.readRecord(contentsHandler); | ||
private static <T> T handleRow(RecordHandler<T> recordHandler, Row row) { | ||
T rec = recordHandler.startRecord(); | ||
row.cellIterator().forEachRemaining(cell -> { | ||
String value; | ||
if (cell.getCellType() == CellType.NUMERIC) { | ||
CellStyle cellStyle = cell.getCellStyle(); | ||
value = DATA_FORMATTER.formatRawCellContents(cell.getNumericCellValue(), | ||
cellStyle.getDataFormat(), cellStyle.getDataFormatString()); | ||
} else { | ||
value = cell.getStringCellValue(); | ||
} | ||
} | ||
}); | ||
|
||
contentsHandler.endFile(); | ||
|
||
return contentsHandler.getResult(); | ||
} | ||
recordHandler.startField(rec, cell.getColumnIndex(), value); | ||
} | ||
); | ||
return recordHandler.endRecord(rec); | ||
} | ||
} |
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
10 changes: 4 additions & 6 deletions
10
src/main/java/com/github/boggard/universalreader/contenthandler/ContentHandler.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,16 +1,14 @@ | ||
package com.github.boggard.universalreader.contenthandler; | ||
|
||
public interface ContentHandler<R> { | ||
import java.util.stream.Stream; | ||
|
||
void startRecord(); | ||
public interface ContentHandler<R, T> { | ||
|
||
void startField(int index, String field); | ||
|
||
void endRecord(); | ||
RecordHandler<T> recordHandler(); | ||
|
||
default void endFile() { | ||
//by default do nothing | ||
} | ||
|
||
R getResult(); | ||
R getResult(Stream<T> resultStream); | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/github/boggard/universalreader/contenthandler/RecordHandler.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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.github.boggard.universalreader.contenthandler; | ||
|
||
public interface RecordHandler<R> { | ||
|
||
default R handleAndReturnResult(String record, String fieldsSeparator) { | ||
R rec = startRecord(); | ||
|
||
String[] fields = record.split(fieldsSeparator); | ||
for (int i = 0; i < fields.length; i++) { | ||
startField(rec, i, fields[i]); | ||
} | ||
|
||
return endRecord(rec); | ||
} | ||
|
||
R startRecord(); | ||
|
||
void startField(R record, int index, String field); | ||
|
||
R endRecord(R record); | ||
} |
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
Oops, something went wrong.