Skip to content

Commit

Permalink
Cache dictionary databases - faster initialisation.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmay committed Oct 9, 2016
1 parent f8c8d4c commit a6b97cd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public static Dictionary unmarshal(Reader reader) {
return dict;
}

public void addEntry(Entry entry) {
void addEntry(Entry entry) {
entries.put(entry.getID().toLowerCase(), entry);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.io.InputStreamReader;
import java.io.Reader;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
Expand All @@ -44,6 +45,8 @@
*/
public class DictionaryDatabase {

private static final Map<String,Dictionary> cache = new HashMap<>();

public final static String DICTREFPROPERTYNAME = "org.openscience.cdk.dict";

private ILoggingTool logger = LoggingToolFactory
Expand Down Expand Up @@ -76,23 +79,29 @@ private Dictionary readDictionary(String databaseLocator, String type) {
databaseLocator += "." + type.substring(0, type.length() - 6);
else
databaseLocator += "." + type;
logger.info("Reading dictionary from ", databaseLocator);
try {
InputStreamReader reader = new InputStreamReader(this.getClass().getClassLoader()
.getResourceAsStream(databaseLocator));
if (type.equals("owl")) {
dictionary = OWLFile.unmarshal(reader);
} else if (type.equals("owl_React")) {
dictionary = OWLReact.unmarshal(reader);
} else { // assume XML using Castor
dictionary = Dictionary.unmarshal(reader);
if (cache.containsKey(databaseLocator)) {
return cache.get(databaseLocator);
}
else {
logger.info("Reading dictionary from ", databaseLocator);
try {
InputStreamReader reader = new InputStreamReader(this.getClass().getClassLoader()
.getResourceAsStream(databaseLocator));
if (type.equals("owl")) {
dictionary = OWLFile.unmarshal(reader);
} else if (type.equals("owl_React")) {
dictionary = OWLReact.unmarshal(reader);
} else { // assume XML using Castor
dictionary = Dictionary.unmarshal(reader);
}
} catch (Exception exception) {
dictionary = null;
logger.error("Could not read dictionary ", databaseLocator);
logger.debug(exception);
}
} catch (Exception exception) {
dictionary = null;
logger.error("Could not read dictionary ", databaseLocator);
logger.debug(exception);
cache.put(databaseLocator, dictionary);
return dictionary;
}
return dictionary;
}

/**
Expand Down

0 comments on commit a6b97cd

Please sign in to comment.