-
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
- Loading branch information
Nikolay Meleshchenko
committed
Dec 9, 2022
1 parent
1cdfa90
commit 1899c2b
Showing
12 changed files
with
135 additions
and
222 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); | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/java/com/github/boggard/universalreader/ExcelReader.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,56 @@ | ||
package com.github.boggard.universalreader; | ||
|
||
import com.github.boggard.universalreader.contenthandler.ContentHandler; | ||
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 ExcelReader { | ||
|
||
private static final DataFormatter DATA_FORMATTER = new DataFormatter(); | ||
|
||
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)) { | ||
Stream<T> resultStream = StreamSupport.stream(workbook.spliterator(), configuration.isParallelProcessing()) | ||
.flatMap(sheet -> createRowStream(configuration, sheet)) | ||
.map(row -> handleRow(recordHandler, row)); | ||
|
||
contentsHandler.endFile(); | ||
|
||
return contentsHandler.getResult(resultStream); | ||
} | ||
} | ||
|
||
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); | ||
} | ||
|
||
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(); | ||
} | ||
|
||
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
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
51 changes: 0 additions & 51 deletions
51
src/main/java/com/github/boggard/universalreader/XLSReader.java
This file was deleted.
Oops, something went wrong.
120 changes: 0 additions & 120 deletions
120
src/main/java/com/github/boggard/universalreader/XLSXReader.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.