use of com.yahoo.search.query.rewrite.RewritesConfig.FsaDict in project vespa by vespa-engine.
the class QueryRewriteSearcher method loadFSADicts.
/**
* Load the dicts specified in vespa-services.xml
*
* @param fileAcquirer Required param for retrieving file type config
* (see vespa's search container doc for more detail)
* @param config Config from vespa-services.xml (see vespa's search
* container doc for more detail)
* @param fileList pairs of file name and file handler for unit tests
* @return boolean true if loaded successfully, false otherwise
*/
private boolean loadFSADicts(FileAcquirer fileAcquirer, RewritesConfig config, HashMap<String, File> fileList) throws RuntimeException {
// Check if getRewriterName method is properly implemented
String rewriterName = getRewriterName();
if (rewriterName == null) {
RewriterUtils.error(logger, "Rewriter required method is not properly implemented: ");
return false;
}
RewriterUtils.log(logger, "Configuring rewriter: " + rewriterName);
// Check if there's no config need to be loaded
if (config == null || (fileAcquirer == null && fileList == null)) {
RewriterUtils.log(logger, "No FSA dictionary file need to be loaded");
return true;
}
// Check if config contains the FSADict param
if (config.fsaDict() == null) {
RewriterUtils.error(logger, "FSADict is not properly set in config");
return false;
}
RewriterUtils.log(logger, "Loading rewriter dictionaries");
// Retrieve FSA names and paths
ListIterator<FsaDict> fsaList = config.fsaDict().listIterator();
// Load default dictionaries if no user dictionaries is configured
if (!fsaList.hasNext()) {
RewriterUtils.log(logger, "Loading default dictionaries");
HashMap<String, String> defaultFSAs = getDefaultFSAs();
if (defaultFSAs == null) {
RewriterUtils.log(logger, "No default FSA dictionary is configured");
return true;
}
Iterator<Map.Entry<String, String>> defaultFSAList = defaultFSAs.entrySet().iterator();
while (defaultFSAList.hasNext()) {
try {
Map.Entry<String, String> currFSA = defaultFSAList.next();
String fsaName = currFSA.getKey();
String fsaPath = currFSA.getValue();
RewriterUtils.log(logger, "FSA file location for " + fsaName + ": " + fsaPath);
// Load FSA
FSA fsa = RewriterUtils.loadFSA(RewriterConstants.DEFAULT_DICT_DIR + fsaPath, null);
// Store FSA into dictionary map
rewriterDicts.put(fsaName, fsa);
} catch (IOException e) {
RewriterUtils.error(logger, "Error loading FSA dictionary: " + e.getMessage());
return false;
}
}
} else {
// Load user configured dictionaries
while (fsaList.hasNext()) {
try {
FsaDict currFSA = fsaList.next();
// fsaName and fsaPath are not null
// or else vespa config server would not have been
// able to start up
String fsaName = currFSA.name();
FileReference fsaPath = currFSA.path();
RewriterUtils.log(logger, "FSA file location for " + fsaName + ": " + fsaPath);
// Retrieve FSA File handler
File fsaFile = null;
if (fileAcquirer != null) {
fsaFile = fileAcquirer.waitFor(fsaPath, 5, TimeUnit.MINUTES);
} else if (fileList != null) {
fsaFile = fileList.get(fsaName);
}
if (fsaFile == null) {
RewriterUtils.error(logger, "Error loading FSA dictionary file handler");
return false;
}
// Load FSA
FSA fsa = RewriterUtils.loadFSA(fsaFile, null);
// Store FSA into dictionary map
rewriterDicts.put(fsaName, fsa);
} catch (InterruptedException e1) {
RewriterUtils.error(logger, "Error loading FSA dictionary file handler: " + e1.getMessage());
return false;
} catch (IOException e2) {
RewriterUtils.error(logger, "Error loading FSA dictionary: " + e2.getMessage());
return false;
}
}
}
RewriterUtils.log(logger, "Successfully loaded rewriter dictionaries");
return true;
}