use of org.apache.nifi.util.search.ahocorasick.AhoCorasick in project nifi by apache.
the class ScanContent method reloadDictionary.
private boolean reloadDictionary(final ProcessContext context, final boolean force, final ComponentLog logger) throws IOException {
boolean obtainedLock;
if (force) {
dictionaryUpdateLock.lock();
obtainedLock = true;
} else {
obtainedLock = dictionaryUpdateLock.tryLock();
}
if (obtainedLock) {
try {
final Search<byte[]> search = new AhoCorasick<>();
final Set<SearchTerm<byte[]>> terms = new HashSet<>();
final InputStream inStream = Files.newInputStream(Paths.get(context.getProperty(DICTIONARY).getValue()), StandardOpenOption.READ);
final TermLoader termLoader;
if (context.getProperty(DICTIONARY_ENCODING).getValue().equalsIgnoreCase(TEXT_ENCODING)) {
termLoader = new TextualTermLoader(inStream);
} else {
termLoader = new BinaryTermLoader(inStream);
}
try {
SearchTerm<byte[]> term;
while ((term = termLoader.nextTerm()) != null) {
terms.add(term);
}
search.initializeDictionary(terms);
searchRef.set(search);
logger.info("Loaded search dictionary from {}", new Object[] { context.getProperty(DICTIONARY).getValue() });
return true;
} finally {
termLoader.close();
}
} finally {
dictionaryUpdateLock.unlock();
}
} else {
return false;
}
}
Aggregations