Skip to content

Commit

Permalink
big refactor to remove redundant data in sources and targets
Browse files Browse the repository at this point in the history
  • Loading branch information
mdavis95 committed Sep 5, 2024
1 parent f377636 commit a79ebe8
Show file tree
Hide file tree
Showing 30 changed files with 297 additions and 308 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,30 @@
import java.io.IOException;
import java.util.Iterator;

public class JsonArrayDataSource implements DataSource<JsonDataSourceRecord>, AutoCloseable {
private final JsonArrayDataSourceConfig jsonArrayDataSourceConfig;
public class JsonArraySource implements DataSource<JsonSourceRecord>, AutoCloseable {
private final JsonArraySourceConfig jsonArraySourceConfig;

private String next;
private JsonParser parser;

private final ObjectMapper mapper = new ObjectMapper();

public static JsonArrayDataSource withConfig(JsonArrayDataSourceConfig jsonArrayDataSourceConfig) throws IOException {
return new JsonArrayDataSource(jsonArrayDataSourceConfig);
public static JsonArraySource withConfig(JsonArraySourceConfig jsonArraySourceConfig) throws IOException {
return new JsonArraySource(jsonArraySourceConfig);
}

public static JsonArrayDataSource withDefaults(DataInputStream dataInputStream) throws IOException {
return withConfig(JsonArrayDataSourceConfig.from(dataInputStream));
public static JsonArraySource withDefaults(DataInputStream dataInputStream) throws IOException {
return withConfig(JsonArraySourceConfig.from(dataInputStream));
}

protected JsonArrayDataSource(JsonArrayDataSourceConfig jsonArrayDataSourceConfig) throws IOException {
this.jsonArrayDataSourceConfig = jsonArrayDataSourceConfig;
protected JsonArraySource(JsonArraySourceConfig jsonArraySourceConfig) throws IOException {
this.jsonArraySourceConfig = jsonArraySourceConfig;
open();
}

protected void open() throws IOException {

parser = mapper.getFactory().createParser(jsonArrayDataSourceConfig.getDataInputStream().openInputStream());
parser = mapper.getFactory().createParser(jsonArraySourceConfig.getDataInputStream().openInputStream());
if (parser.nextToken() != JsonToken.START_ARRAY) {
throw new IllegalStateException("Expected an array");
}
Expand All @@ -48,7 +48,7 @@ public void reset() throws IOException {
}

@Override
public Iterator<JsonDataSourceRecord> iterator() {
public Iterator<JsonSourceRecord> iterator() {

if (next == null) {
try {
Expand All @@ -68,8 +68,8 @@ public boolean hasNext() {
}

@Override
public JsonDataSourceRecord next() {
JsonDataSourceRecord jsonDataSourceRecord = new JsonDataSourceRecord(next);
public JsonSourceRecord next() {
JsonSourceRecord jsonSourceRecord = new JsonSourceRecord(next);
try {
if (parser.nextToken() == JsonToken.START_OBJECT) {
next = mapper.readTree(parser).toString();
Expand All @@ -82,7 +82,7 @@ public JsonDataSourceRecord next() {
System.out.println(e.getMessage());
//throw new RuntimeException(e);
}
return jsonDataSourceRecord;
return jsonSourceRecord;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class JsonArrayDataSourceConfig {
public class JsonArraySourceConfig {

private final DataInputStream dataInputStream;

private Charset charset = StandardCharsets.UTF_8;

public static JsonArrayDataSourceConfig from(DataInputStream dataInputStream) {
return new JsonArrayDataSourceConfig(dataInputStream);
public static JsonArraySourceConfig from(DataInputStream dataInputStream) {
return new JsonArraySourceConfig(dataInputStream);
}

private JsonArrayDataSourceConfig(DataInputStream dataInputStream) {
private JsonArraySourceConfig(DataInputStream dataInputStream) {
this.dataInputStream = dataInputStream;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@
import java.io.InputStreamReader;
import java.util.Iterator;

public class JsonLineDataSource implements DataSource<JsonDataSourceRecord>, AutoCloseable {
private final JsonLineDataSourceConfig jsonLineDataSourceConfig;
public class JsonLineDataSource implements DataSource<JsonSourceRecord>, AutoCloseable {
private final JsonLineSourceConfig jsonLineSourceConfig;
private BufferedReader reader;

private String next;

public static JsonLineDataSource withConfig(JsonLineDataSourceConfig jsonLineDataSourceConfig) throws IOException {
return new JsonLineDataSource(jsonLineDataSourceConfig);
public static JsonLineDataSource withConfig(JsonLineSourceConfig jsonLineSourceConfig) throws IOException {
return new JsonLineDataSource(jsonLineSourceConfig);
}

public static JsonLineDataSource withDefaults(DataInputStream dataInputStream) throws IOException {
return withConfig(JsonLineDataSourceConfig.from(dataInputStream));
return withConfig(JsonLineSourceConfig.from(dataInputStream));
}

protected JsonLineDataSource(JsonLineDataSourceConfig jsonLineDataSourceConfig) throws IOException {
this.jsonLineDataSourceConfig = jsonLineDataSourceConfig;
protected JsonLineDataSource(JsonLineSourceConfig jsonLineSourceConfig) throws IOException {
this.jsonLineSourceConfig = jsonLineSourceConfig;
open();
}

protected void open() throws IOException {
InputStream inputStream = jsonLineDataSourceConfig.getDataInputStream().openInputStream();
reader = new BufferedReader(new InputStreamReader(inputStream, jsonLineDataSourceConfig.getCharset().newDecoder()));
InputStream inputStream = jsonLineSourceConfig.getDataInputStream().openInputStream();
reader = new BufferedReader(new InputStreamReader(inputStream, jsonLineSourceConfig.getCharset().newDecoder()));
next = reader.readLine();
}

Expand All @@ -40,7 +40,7 @@ public void reset() throws IOException {
}

@Override
public Iterator<JsonDataSourceRecord> iterator() {
public Iterator<JsonSourceRecord> iterator() {

if (next == null) {
try {
Expand All @@ -59,14 +59,14 @@ public boolean hasNext() {
}

@Override
public JsonDataSourceRecord next() {
public JsonSourceRecord next() {
try {
JsonDataSourceRecord jsonDataSourceRecord = new JsonDataSourceRecord(next);
JsonSourceRecord jsonSourceRecord = new JsonSourceRecord(next);
next = reader.readLine();
return jsonDataSourceRecord;
return jsonSourceRecord;
}
catch (Exception e) {
return jsonLineDataSourceConfig.getExceptionHandler().handleException(e);
return jsonLineSourceConfig.getExceptionHandler().handleException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.zulia.data.source.json;

public interface JsonLineParseExceptionHandler {

JsonDataSourceRecord handleException(Exception e);
JsonSourceRecord handleException(Exception e);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class JsonLineDataSourceConfig {
public class JsonLineSourceConfig {

private final DataInputStream dataInputStream;

private Charset charset = StandardCharsets.UTF_8;

private JsonLineParseExceptionHandler exceptionHandler = new ThrowingJsonLineParseExceptionHandler();

public static JsonLineDataSourceConfig from(DataInputStream dataInputStream) {
return new JsonLineDataSourceConfig(dataInputStream);
public static JsonLineSourceConfig from(DataInputStream dataInputStream) {
return new JsonLineSourceConfig(dataInputStream);
}

private JsonLineDataSourceConfig(DataInputStream dataInputStream) {
private JsonLineSourceConfig(DataInputStream dataInputStream) {
this.dataInputStream = dataInputStream;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import java.util.Date;
import java.util.List;

public class JsonDataSourceRecord implements DataSourceRecord {
public class JsonSourceRecord implements DataSourceRecord {

private final Document document;

public JsonDataSourceRecord(String json) {
public JsonSourceRecord(String json) {
document = Document.parse(json);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public LoggingJsonLineParseExceptionHandler(Logger log) {
}

@Override
public JsonDataSourceRecord handleException(Exception e) {
public JsonSourceRecord handleException(Exception e) {
log.error(e.getMessage(), e);
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public class ThrowingJsonLineParseExceptionHandler implements JsonLineParseExceptionHandler {
@Override
public JsonDataSourceRecord handleException(Exception e) {
public JsonSourceRecord handleException(Exception e) {
throw new RuntimeException(e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.io.IOException;
import java.util.SequencedSet;

public interface SpreadsheetDataSource<T extends SpreadsheetRecord> extends DataSource<T> {
public interface SpreadsheetSource<T extends SpreadsheetRecord> extends DataSource<T> {

boolean hasHeader(String field);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,54 @@
import io.zulia.data.common.SpreadsheetType;
import io.zulia.data.input.DataInputStream;
import io.zulia.data.input.FileDataInputStream;
import io.zulia.data.source.spreadsheet.csv.CSVDataSource;
import io.zulia.data.source.spreadsheet.csv.CSVDataSourceConfig;
import io.zulia.data.source.spreadsheet.excel.ExcelDataSource;
import io.zulia.data.source.spreadsheet.excel.ExcelDataSourceConfig;
import io.zulia.data.source.spreadsheet.csv.CSVSource;
import io.zulia.data.source.spreadsheet.csv.CSVSourceConfig;
import io.zulia.data.source.spreadsheet.excel.ExcelSource;
import io.zulia.data.source.spreadsheet.excel.ExcelSourceConfig;

import java.io.IOException;

public class SpreadsheetSourceFactory {

public static SpreadsheetDataSource<?> fromFileWithoutHeaders(String filePath) throws IOException {
public static SpreadsheetSource<?> fromFileWithoutHeaders(String filePath) throws IOException {
return fromStream(FileDataInputStream.from(filePath), false);
}

public static SpreadsheetDataSource<?> fromFileWithHeaders(String filePath) throws IOException {
public static SpreadsheetSource<?> fromFileWithHeaders(String filePath) throws IOException {
return fromStream(FileDataInputStream.from(filePath), true);
}

public static SpreadsheetDataSource<?> fromFile(String filePath, boolean hasHeaders) throws IOException {
public static SpreadsheetSource<?> fromFile(String filePath, boolean hasHeaders) throws IOException {
return fromStream(FileDataInputStream.from(filePath), hasHeaders);
}

public static SpreadsheetDataSource<?> fromStreamWithoutHeaders(DataInputStream dataInputStream) throws IOException {
public static SpreadsheetSource<?> fromStreamWithoutHeaders(DataInputStream dataInputStream) throws IOException {
return fromStream(dataInputStream, false);
}

public static SpreadsheetDataSource<?> fromStreamWithHeaders(DataInputStream dataInputStream) throws IOException {
public static SpreadsheetSource<?> fromStreamWithHeaders(DataInputStream dataInputStream) throws IOException {
return fromStream(dataInputStream, true);
}

public static SpreadsheetDataSource<?> fromStream(DataInputStream dataInputStream, boolean hasHeaders) throws IOException {
public static SpreadsheetSource<?> fromStream(DataInputStream dataInputStream, boolean hasHeaders) throws IOException {
SpreadsheetType spreadsheetType = SpreadsheetType.getSpreadsheetType(dataInputStream.getMeta());

if (SpreadsheetType.CSV.equals(spreadsheetType) || SpreadsheetType.TSV.equals(spreadsheetType)) {
CSVDataSourceConfig csvDataSourceConfig = CSVDataSourceConfig.from(dataInputStream);
CSVSourceConfig csvSourceConfig = CSVSourceConfig.from(dataInputStream);
if (hasHeaders) {
csvDataSourceConfig.withHeaders();
csvSourceConfig.withHeaders();
}
if (SpreadsheetType.TSV.equals(spreadsheetType)) {
csvDataSourceConfig.withDelimiter('\t');
csvSourceConfig.withDelimiter('\t');
}
return CSVDataSource.withConfig(csvDataSourceConfig);
return CSVSource.withConfig(csvSourceConfig);
}
else if (SpreadsheetType.XLSX.equals(spreadsheetType) || SpreadsheetType.XLS.equals(spreadsheetType)) {
ExcelDataSourceConfig excelDataSourceConfig = ExcelDataSourceConfig.from(dataInputStream);
ExcelSourceConfig excelSourceConfig = ExcelSourceConfig.from(dataInputStream);
if (hasHeaders) {
excelDataSourceConfig.withHeaders();
excelSourceConfig.withHeaders();
}
return ExcelDataSource.withConfig(excelDataSourceConfig);
return ExcelSource.withConfig(excelSourceConfig);
}
else {
throw new IllegalArgumentException("Failed to determine file type from content type <" + dataInputStream.getMeta()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@
import java.util.List;
import java.util.SequencedSet;

public class CSVDataSourceRecord implements SpreadsheetRecord {
public class CSVRecord implements SpreadsheetRecord {

private final String[] row;
private final HeaderMapping headerMapping;

private final CSVDataSourceConfig csvDataSourceConfig;
private final CSVSourceConfig csvSourceConfig;

public CSVDataSourceRecord(String[] row, HeaderMapping headerMapping, CSVDataSourceConfig csvDataSourceConfig) {
public CSVRecord(String[] row, HeaderMapping headerMapping, CSVSourceConfig csvSourceConfig) {
this.row = row;
this.headerMapping = headerMapping;
this.csvDataSourceConfig = csvDataSourceConfig;
this.csvSourceConfig = csvSourceConfig;

}

public int getIndexFromField(String field) {
if (headerMapping == null) {
throw new IllegalStateException("Use csvDataSourceConfig.withHeaders() use field names");
throw new IllegalStateException("Use csvSourceConfig.withHeaders() use field names");
}
if (headerMapping.hasHeader(field)) {
return headerMapping.getHeaderIndex(field);
Expand All @@ -34,7 +34,7 @@ public int getIndexFromField(String field) {
@Override
public <T> List<T> getList(String key, Class<T> clazz) {
String cellValue = getString(key);
return csvDataSourceConfig.getDelimitedListHandler().cellValueToList(clazz, cellValue);
return csvSourceConfig.getDelimitedListHandler().cellValueToList(clazz, cellValue);
}

@Override
Expand All @@ -44,7 +44,7 @@ public String getString(String field) {

@Override
public Boolean getBoolean(String field) {
return parseFromString(field, csvDataSourceConfig.getBooleanParser(), null);
return parseFromString(field, csvSourceConfig.getBooleanParser(), null);
}

@Override
Expand All @@ -69,13 +69,13 @@ public Long getLong(String field) {

@Override
public Date getDate(String field) {
return parseFromString(field, csvDataSourceConfig.getDateParser(), null);
return parseFromString(field, csvSourceConfig.getDateParser(), null);
}

@Override
public <T> List<T> getList(int index, Class<T> clazz) {
String cellValue = getString(index);
return csvDataSourceConfig.getDelimitedListHandler().cellValueToList(clazz, cellValue);
return csvSourceConfig.getDelimitedListHandler().cellValueToList(clazz, cellValue);
}

@Override
Expand All @@ -85,7 +85,7 @@ public String getString(int index) {

@Override
public Boolean getBoolean(int index) {
return parseFromString(index, csvDataSourceConfig.getBooleanParser(), null);
return parseFromString(index, csvSourceConfig.getBooleanParser(), null);
}

@Override
Expand All @@ -110,7 +110,7 @@ public Long getLong(int index) {

@Override
public Date getDate(int index) {
return parseFromString(index, csvDataSourceConfig.getDateParser(), null);
return parseFromString(index, csvSourceConfig.getDateParser(), null);
}

public String[] getRow() {
Expand Down
Loading

0 comments on commit a79ebe8

Please sign in to comment.