Search in sources :

Example 1 with FastLRUCacheManager

use of org.apache.stanbol.enhancer.engines.lucenefstlinking.cache.FastLRUCacheManager in project stanbol by apache.

the class FstLinkingEngineComponent method updateEngineRegistration.

/**
     * This will be called on each <ul>
     * <li>update to the Component configuration (activate, deactivate)
     * <li>updates on the SolrCore
     * </ul>
     * on any detected change it will update the registered EnhancementEngine.<p>
     * This also initialises the FST configuration.
     * @param reference the ServiceRefernece for the SolrServer or <code>null</code>
     * in case the service is no longer available.
     * @param server the SolrServer (or <code>null</code>
     */
protected void updateEngineRegistration(ServiceReference reference, SolrServer server) {
    log.info(" ... updateEngineRegistration for {}: {}", getClass().getSimpleName(), engineName);
    if (reference != null && server == null) {
        server = solrServerTracker.getService(reference);
    }
    if (reference == null && this.indexReference == null) {
        //and return
        return;
    }
    BundleContext bundleContext = this.bundleContext;
    //We need to keep the old configuration vars for unregistering the
    //current engine (see #unregisterEngine(..) method)
    final ServiceRegistration<?> oldEngineRegistration = this.engineRegistration;
    final SolrCore oldSolrCore = this.solrCore;
    final IndexConfiguration oldIndexConfig = this.indexConfig;
    SolrCore core;
    // the indexConfig build by this call
    IndexConfiguration indexConfig;
    synchronized (this) {
        //init one after the other in case of multiple calls
        try {
            //try to init - finally unregisterEngine
            //reset the old field values
            this.engineRegistration = null;
            this.indexConfig = null;
            this.solrCore = null;
            //now we can update the engines configuration
            if (bundleContext == null) {
                //NOTE: unregistering is done in finally block
                return;
            }
            core = getSolrCore(server);
            if (core == null) {
                //no SolrCore
                log.info("   - SolrCore {} present", oldSolrCore == null ? "not yet" : "no longer");
                //NOTE: unregistering is done in finally block
                return;
            } else {
                //- we do have a SolrCore
                log.info("    - solrCore (name: {} | indexDir: {}", core.getName(), core.getIndexDir());
            }
            //File fstDir = new File(dataDir,"fst");
            //now collect the FST configuration
            indexConfig = new IndexConfiguration(fstConfig, core, fieldEncoding, entityLinkerConfig.getDefaultLanguage());
            indexConfig.setTypeField(solrTypeField);
            indexConfig.setRankingField(solrRankingField);
            //set fields parsed in the activate method
            indexConfig.setExecutorService(fstCreatorService);
            //TODO add support
            indexConfig.setRedirectField(null);
            indexConfig.setOrigin(origin);
            //NOTE: the FST cofnig is processed even if the SolrCore has not changed
            //      because their might be config changes and/or new FST files in the
            //      FST directory of the SolrCore.
            indexConfig.setFstDirectory(getFstDirectory(core, fstFolder));
            //set the DocumentCacheFactory
            if (entityCacheSize > 0) {
                indexConfig.setEntityCacheManager(new FastLRUCacheManager(entityCacheSize));
            }
            //else no entityCache is used
            if (skipAltTokensConfig != null) {
                indexConfig.setSkipAltTokens(skipAltTokensConfig);
            }
            //activate the index configuration
            try {
                //this will init the FST directory if necessary so we might run
                //into IOExceptions
                indexConfig.activate();
            } catch (IOException e) {
                throw new RuntimeException("Unable to activate Index for FST Linking Engine '" + engineName + "' (solrCore: " + core.getName() + ", instanceDir: " + core.getCoreDescriptor().getInstanceDir() + ")!", e);
            }
            if (log.isInfoEnabled()) {
                //log the initialised languages
                Set<String> langSet = new HashSet<String>(indexConfig.getCorpusLanguages());
                if (langSet.remove(null)) {
                    //replace the null for the default language
                    //with an empty string
                    langSet.add("");
                }
                String[] langArray = langSet.toArray(new String[langSet.size()]);
                Arrays.sort(langArray, String.CASE_INSENSITIVE_ORDER);
                log.info(" ... initialised FST corpora for languages {}", Arrays.toString(langArray));
            }
            //check if we need to create some FST files
            for (CorpusInfo fstInfo : indexConfig.getCorpora()) {
                //check if the fst does not exist and the fstInfo allows creation
                if (!fstInfo.fst.exists() && fstInfo.allowCreation) {
                    //create a task on the FST corpus creation service
                    fstInfo.corpusLock.writeLock().lock();
                    try {
                        Future<TaggerFstCorpus> enqueued = fstCreatorService.submit(new CorpusCreationTask(indexConfig, fstInfo));
                        fstInfo.enqueued(enqueued);
                    } finally {
                        fstInfo.corpusLock.writeLock().unlock();
                    }
                }
            }
            //set the newly configured instances to the fields
            this.indexConfig = indexConfig;
            this.solrServerReference = reference;
            this.solrCore = core;
            //create the new FST linking engine instance
            FstLinkingEngine engine = new FstLinkingEngine(engineName, linkingMode, indexConfig, textProcessingConfig, entityLinkerConfig, nerTypeMappings);
            //register it as a service
            String[] services = new String[] { EnhancementEngine.class.getName(), ServiceProperties.class.getName() };
            log.info(" ... register {}: {}", engine.getClass().getSimpleName(), engineName);
            this.engineRegistration = bundleContext.registerService(services, engine, engineMetadata);
        } finally {
            //in any case (even an Exception) ensure that the current
            //engine registration is unregistered and the currently used
            //SolrCore is unregistered!
            unregisterEngine(oldEngineRegistration, oldIndexConfig, oldSolrCore);
        }
    }
}
Also used : SolrCore(org.apache.solr.core.SolrCore) TaggerFstCorpus(org.opensextant.solrtexttagger.TaggerFstCorpus) IOException(java.io.IOException) FastLRUCacheManager(org.apache.stanbol.enhancer.engines.lucenefstlinking.cache.FastLRUCacheManager) BundleContext(org.osgi.framework.BundleContext) HashSet(java.util.HashSet)

Aggregations

IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 SolrCore (org.apache.solr.core.SolrCore)1 FastLRUCacheManager (org.apache.stanbol.enhancer.engines.lucenefstlinking.cache.FastLRUCacheManager)1 TaggerFstCorpus (org.opensextant.solrtexttagger.TaggerFstCorpus)1 BundleContext (org.osgi.framework.BundleContext)1