Skip to content

Commit

Permalink
Merged #40 'en40-dryrun'
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidLeoni committed Feb 3, 2017
2 parents 5b54d7f + 6474a3a commit 9682890
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 48 deletions.
122 changes: 88 additions & 34 deletions src/main/java/eu/kidf/diversicon/core/Diversicon.java
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,7 @@ public Iterator<Synset> getConnectedSynsets(

/**
* Validates, normalizes and augments the synsetRelation graph with edges to
* speed up
* searches.
* speed up searches.
*
* @throws DivValidationException
*
Expand Down Expand Up @@ -1011,11 +1010,12 @@ public ImportJob importXml(String fileUrl) {
* </p>
*
* @throws DivValidationException
* @throws InterruptedImportException
*
* @since 0.1.0
*/
public List<ImportJob> importFiles(ImportConfig importConfig) {

public List<ImportJob> importFiles(ImportConfig importConfig) {
Date start = new Date();

List<ImportJob> ret = new ArrayList<>();
Expand All @@ -1035,7 +1035,25 @@ public List<ImportJob> importFiles(ImportConfig importConfig) {
.size()
+ " files by import author " + importConfig.getAuthor() + "...");

DbInfo oldDbInfo = prepareDbForImport();
if (importConfig.isDryRun() && importConfig.getFileUrls().size() > 1){
LOG.info("");
LOG.info("********** DRY RUN: found many files to import! ");
LOG.info("");
LOG.info("********** If second file depends on first one there might be validation errors due to ");
LOG.info("********** data not actually being written to disk!");
LOG.info("");
}

DbInfo oldDbInfo = null;

if (importConfig.isDryRun()){
LOG.info("");
LOG.info("********** DRY RUN: skipping DbInfo table update **********");
LOG.info("");
} else {
oldDbInfo = prepareDbForImport();
}


try {
for (String fileUrl : importConfig.getFileUrls()) {
Expand All @@ -1046,13 +1064,22 @@ public List<ImportJob> importFiles(ImportConfig importConfig) {
if (ret.isEmpty()) {
LOG.error("Import failed, no LexicalResource data was written to disk. "
+ "Aborting all imports.", ex);
setDbInfo(oldDbInfo);
if (importConfig.isDryRun()){
LOG.info("******** DRY RUN: skipping restoring DbInfo table (no change was done to it) *************");
} else {
setDbInfo(oldDbInfo);
}

}
throw ex;
}

if (importConfig.isSkipAugment()) {
LOG.info("Skipping graph augmentation as requested by user.");
} else if (importConfig.isDryRun()) {
LOG.info("");
LOG.info("************* DRY RUN: skipping graph augmentation ************");
LOG.info("");
} else {
try {
processGraph();
Expand All @@ -1062,6 +1089,13 @@ public List<ImportJob> importFiles(ImportConfig importConfig) {
}

logImportResult(importConfig, start, ret);

if (importConfig.isDryRun()){
LOG.info("");
LOG.info("************* DRY RUN: end of import, no data was written to disk. ************");
LOG.info("");
}

return ret;

}
Expand Down Expand Up @@ -1104,7 +1138,7 @@ private void logImportResult(ImportConfig importConfig, Date start, List<ImportJ
* Imports a single LMF XML
*
* @throws InvalidImportException
* @throws InterruptedImportException
* @throws InterruptedImportException If something goes wrong
*
* @since 0.1.0
*/
Expand Down Expand Up @@ -1134,20 +1168,31 @@ private ImportJob importFile(ImportConfig importConfig, String url) {
file,
pack);

LOG.info("");
LOG.info("Starting import...");
LOG.info("");
if (importConfig.isDryRun()){

LOG.info("");
LOG.info("************* DRY RUN: skipping actual import! *********");
LOG.info("");

} else {

LOG.info("");
LOG.info("Starting import...");
LOG.info("");

try {
DivXmlToDbTransformer trans = new DivXmlToDbTransformer(this);
trans.transform(file, null);
endImportJob(job);
try {
DivXmlToDbTransformer trans = new DivXmlToDbTransformer(this);
trans.transform(file, null);
endImportJob(job);

} catch (Exception ex) {
throw new InterruptedImportException("Error while loading lmf xml " + url, ex);
}
} catch (Exception ex) {
throw new InterruptedImportException("Error while loading lmf xml " + url, ex);
}

LOG.info("Done loading LMF : " + url + " .");
LOG.info("Done loading LMF : " + url + " .");

}


return job;
}
Expand Down Expand Up @@ -1475,14 +1520,7 @@ public ImportJob importResource(
* on it instead.
* </p>
*
* @param prefix
* The prefix to use within the database
* @param namespaces
* The namespaces used by the resource
* @param skipAugment
* if true, after the import prevents the graph froom being
* normalized
* and augmented with transitive closure.
* @param importConfig a configuration for the import. Must have only one file url.
* @param pack
* A LexicalResource doesn't contain stuff like namespaces, so we
* have to provide them by ourselves in pack.
Expand Down Expand Up @@ -1512,18 +1550,26 @@ public ImportJob importResource(
null,
pack);

prepareDbForImport();
if (importConfig.isDryRun()){
LOG.info("");
LOG.info("********** DRY RUN: skipping actual import ***********");
LOG.info("");
} else {
prepareDbForImport();

setCurrentImportJob(job);
setCurrentImportJob(job);

new JavaToDbTransformer(this, lexRes).transform();
new JavaToDbTransformer(this, lexRes).transform();

if (!importConfig.isSkipAugment()) {
processGraph();
}
if (!importConfig.isSkipAugment()) {
processGraph();
}

endImportJob(job);
endImportJob(job);

}


} catch (InvalidImportException ex) {

LOG.error("Import failed! Aborting all imports! (no LexicalResource data was written to disk) \n", ex);
Expand All @@ -1534,7 +1580,15 @@ public ImportJob importResource(
+ lexRes.getName() + " !", ex);
}

LOG.info("Done saving.");


if (importConfig.isDryRun()){
LOG.info("");
LOG.info("********** DRY RUN: end of import, no data was written to disk ! ***********");
LOG.info("");
} else {
LOG.info("Done saving.");
}

return job;
}
Expand Down
47 changes: 37 additions & 10 deletions src/main/java/eu/kidf/diversicon/core/ImportConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
import java.util.ArrayList;
import java.util.List;

import eu.kidf.diversicon.core.XmlValidationConfig.Builder;

/**
* Configuration for a whole {@link ImportJob} process.
*
* @since 0.1.0
* @see ImportJob
*/
// TODO Why is it not immutable ??
// TODO: make it immutable https://github.com/diversicon-kb/diversicon-core/issues/35
public class ImportConfig {

private List<String> fileUrls;
Expand All @@ -24,6 +23,7 @@ public class ImportConfig {
private boolean skipAugment;
private int logLimit;
private boolean force;
private boolean dryRun;

/**
* Default constructor.
Expand All @@ -39,6 +39,7 @@ public ImportConfig() {
this.skipAugment = false;
this.logLimit = Diversicons.DEFAULT_LOG_LIMIT;
this.force = false;
this.dryRun = false;
}

/**
Expand All @@ -57,9 +58,10 @@ public List<String> getFileUrls() {
*
* @since 0.1.0
*/
public void setFileUrls(List<String> fileUrls) {
public ImportConfig setFileUrls(List<String> fileUrls) {
checkNotNull(fileUrls);
this.fileUrls = fileUrls;
return this;
}

/**
Expand All @@ -76,9 +78,10 @@ public String getAuthor() {
*
* @since 0.1.0
*/
public void setAuthor(String author) {
public ImportConfig setAuthor(String author) {
checkNotNull(author);
this.author = author;
return this;
}

/**
Expand All @@ -98,9 +101,10 @@ public String getDescription() {
*
* @since 0.1.0
*/
public void setDescription(String description) {
public ImportConfig setDescription(String description) {
checkNotNull(description);
this.description = description;
return this;
}

/**
Expand All @@ -119,17 +123,19 @@ public boolean isSkipAugment() {
*
* @since 0.1.0
*/
public void setSkipAugment(boolean skipAugment) {
public ImportConfig setSkipAugment(boolean skipAugment) {
this.skipAugment = skipAugment;
return this;
}

/**
*
* @since 0.1.0
*/
public void addLexResFileUrl(String fileUrl) {
public ImportConfig addLexResFileUrl(String fileUrl) {
checkNotEmpty(fileUrl, "Invalid lexical resource file URL!");
this.fileUrls.add(fileUrl);
return this;
}

/**
Expand Down Expand Up @@ -173,9 +179,10 @@ public int getLogLimit() {
*
* @since 0.1.0
*/
public void setLogLimit(int logLimit) {
public ImportConfig setLogLimit(int logLimit) {
checkArgument(logLimit >= -1, "Log limit must be >= -1, found instead %s", logLimit);
this.logLimit = logLimit;
return this;
}

/**
Expand All @@ -192,7 +199,27 @@ public boolean isForce() {
*
* @since 0.1.0
*/
public void setForce(boolean force) {
public ImportConfig setForce(boolean force) {
this.force = force;
}
return this;
}

/**
* A dry run simply simulates the import without writing anything into the database.
*
* @since 0.1.0
*/
public boolean isDryRun() {
return dryRun;
}

/**
* See {@link #isDryRun()}
*
* @since 0.1.0
*/
public ImportConfig setDryRun(boolean dryRun) {
this.dryRun = dryRun;
return this;
}
}
6 changes: 3 additions & 3 deletions src/main/java/eu/kidf/diversicon/core/internal/Internals.java
Original file line number Diff line number Diff line change
Expand Up @@ -2041,9 +2041,9 @@ public static String longestCommonPrefix(String a, String b) {
public static ImportConfig createImportConfig(LexicalResource lexRes){
checkNotNull(lexRes);

ImportConfig importConfig = new ImportConfig();
importConfig.setAuthor(Diversicons.DEFAULT_AUTHOR);
importConfig.addLexResFileUrl(Diversicons.MEMORY_PROTOCOL + ":" + lexRes.hashCode());
ImportConfig importConfig = new ImportConfig()
.setAuthor(Diversicons.DEFAULT_AUTHOR)
.addLexResFileUrl(Diversicons.MEMORY_PROTOCOL + ":" + lexRes.hashCode());
return importConfig;

}
Expand Down
Loading

0 comments on commit 9682890

Please sign in to comment.