Skip to content

Commit

Permalink
SQL per input subpart, i#25 - updated DB cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
OndraZizka committed Nov 19, 2018
1 parent ab166c4 commit 9d4790a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
32 changes: 14 additions & 18 deletions src/main/java/cz/dynawest/csvcruncher/Cruncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public void crunch() throws Exception
boolean printAsArray = options.jsonExportFormat == Options.JsonExportFormat.ARRAY;

Map<String, File> tablesToFiles = new HashMap<>();
List<CruncherOutputPart> outputs = Collections.emptyList();

// Should the result have a unique incremental ID as an added 1st column?
CounterColumn counterColumn = new CounterColumn();
Expand Down Expand Up @@ -142,7 +143,7 @@ public void crunch() throws Exception


String genericSql = StringUtils.defaultString(this.options.sql, DEFAULT_SQL);
List<CruncherOutputPart> outputs = new ArrayList<>();
outputs = new ArrayList<>();


// SQL can be executed:
Expand All @@ -156,7 +157,7 @@ public void crunch() throws Exception
}
// * per input, and generate one result per execution.
else {
Set<Path> usedOutputFiles = new HashSet<Path>();
Set<Path> usedOutputFiles = new HashSet<>();

for (CruncherInputSubpart inputSubpart : inputSubparts)
{
Expand All @@ -174,10 +175,11 @@ public void crunch() throws Exception
{
File csvOutFile = output.getOutputFile().toFile();
String sql = genericSql;
String outputTableName = TABLE_NAME__OUTPUT;
//String outputTableName = TABLE_NAME__OUTPUT;
String outputTableName = output.deriveOutputTableName();
if (output.getInputTableName() != null) {
sql = sql.replace(SQL_TABLE_PLACEHOLDER, output.getInputTableName());
outputTableName = output.getInputTableName() + "_out";
//outputTableName = output.getInputTableName() + "_out";
}


Expand Down Expand Up @@ -229,28 +231,22 @@ public void crunch() throws Exception
finally
{
LOG.info(" *** SHUTDOWN CLEANUP SEQUENCE ***");
cleanUpInputOutputTables(tablesToFiles);
cleanUpInputOutputTables(tablesToFiles, outputs);
dbHelper.executeDbCommand("DROP SCHEMA PUBLIC CASCADE", "Failed to delete the database: ");
this.jdbcConn.close();
LOG.info(" *** END SHUTDOWN CLEANUP SEQUENCE ***");
}
}

private void cleanUpInputOutputTables(Map<String, File> inputTablesToFiles)
private void cleanUpInputOutputTables(Map<String, File> inputTablesToFiles, List<CruncherOutputPart> outputs)
{
//if (reachedStage.passed(ReachedCrunchStage.INPUT_TABLES_CREATED))
// I'm removing these stage checks, since the table might have been left
// from previous run. Later let's implement a cleanup at start. TODO
{
dbHelper.detachTables(inputTablesToFiles.keySet(), "Could not delete the input table: ");
}
// TODO: Implement a cleanup at start. https://github.com/OndraZizka/csv-cruncher/issues/18
dbHelper.detachTables(inputTablesToFiles.keySet(), "Could not delete the input table: ");

//if (reachedStage.passed(ReachedCrunchStage.OUTPUT_TABLE_CREATED))
{
dbHelper.detachTables(Collections.singleton(TABLE_NAME__OUTPUT), "Could not delete the output table: ");
}
//dbHelper.detachTables(Collections.singleton(TABLE_NAME__OUTPUT), "Could not delete the output table: ");

//if (reachedStage.passed(ReachedCrunchStage.OUTPUT_TABLE_FILLED))
Set<String> outputTablesNames = outputs.stream().map(x -> x.deriveOutputTableName()).collect(Collectors.toSet());
dbHelper.detachTables(outputTablesNames, "Could not delete the output table: ");
}

/**
Expand Down Expand Up @@ -282,7 +278,7 @@ private class CounterColumn
String ddl = "";
String value = "";

public CounterColumn setDdlAndVal() throws SQLException
public CounterColumn setDdlAndVal()
{
long initialNumber = getInitialNumber();

Expand Down
12 changes: 11 additions & 1 deletion src/main/java/cz/dynawest/csvcruncher/CruncherOutputPart.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,24 @@
@Data
public class CruncherOutputPart
{
public static final String OUTPUT_TABLE_SUFFIX = "_out";

@NonNull private Path outputFile;

@NonNull private String inputTableName;
private final String inputTableName;


// These are filled during processing.

private String sql;

private List<String> columnNames;

public String deriveOutputTableName()
{
if (getInputTableName() == null)
return Cruncher.TABLE_NAME__OUTPUT;
else
return getInputTableName() + OUTPUT_TABLE_SUFFIX;
}
}

0 comments on commit 9d4790a

Please sign in to comment.