Search in sources :

Example 1 with AhoCorasick

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;
    }
}
Also used : DataInputStream(java.io.DataInputStream) BufferedInputStream(org.apache.nifi.stream.io.BufferedInputStream) InputStream(java.io.InputStream) AhoCorasick(org.apache.nifi.util.search.ahocorasick.AhoCorasick) SearchTerm(org.apache.nifi.util.search.SearchTerm) HashSet(java.util.HashSet)

Aggregations

DataInputStream (java.io.DataInputStream)1 InputStream (java.io.InputStream)1 HashSet (java.util.HashSet)1 BufferedInputStream (org.apache.nifi.stream.io.BufferedInputStream)1 SearchTerm (org.apache.nifi.util.search.SearchTerm)1 AhoCorasick (org.apache.nifi.util.search.ahocorasick.AhoCorasick)1