You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stanbol.apache.org by rw...@apache.org on 2014/01/16 11:46:59 UTC

svn commit: r1558748 - in /stanbol/trunk/enhancement-engines: dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/ lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/ lucenefstl...

Author: rwesten
Date: Thu Jan 16 10:46:59 2014
New Revision: 1558748

URL: http://svn.apache.org/r1558748
Log:
merged fixes for STANBOL-1257 and STANBOL-1258 to trunk

Modified:
    stanbol/trunk/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/TrackingDereferencerBase.java
    stanbol/trunk/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/FstLinkingEngineComponent.java
    stanbol/trunk/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/cache/EntityCacheManager.java
    stanbol/trunk/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/cache/FastLRUCacheManager.java

Modified: stanbol/trunk/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/TrackingDereferencerBase.java
URL: http://svn.apache.org/viewvc/stanbol/trunk/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/TrackingDereferencerBase.java?rev=1558748&r1=1558747&r2=1558748&view=diff
==============================================================================
--- stanbol/trunk/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/TrackingDereferencerBase.java (original)
+++ stanbol/trunk/enhancement-engines/dereference/entityhub/src/main/java/org/apache/stanbol/enhancer/engines/dereference/entityhub/TrackingDereferencerBase.java Thu Jan 16 10:46:59 2014
@@ -19,6 +19,7 @@ package org.apache.stanbol.enhancer.engi
 import java.io.StringReader;
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashSet;
@@ -57,6 +58,7 @@ import org.apache.stanbol.entityhub.serv
 import org.apache.stanbol.entityhub.servicesapi.query.FieldQuery;
 import org.apache.stanbol.entityhub.servicesapi.query.QueryResultList;
 import org.apache.stanbol.entityhub.servicesapi.query.TextConstraint;
+import org.apache.stanbol.entityhub.servicesapi.util.ModelUtils;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Filter;
 import org.osgi.framework.InvalidSyntaxException;
@@ -158,17 +160,28 @@ public abstract class TrackingDereferenc
      */
     public void setDereferencedFields(List<String> dereferencedFields) {
         if(dereferencedFields != null && !dereferencedFields.isEmpty()){
-            fieldMapper = new DefaultFieldMapperImpl(ValueConverterFactory.getDefaultInstance());
-            log.debug(" > Initialise configured field mappings");
+            List<FieldMapping> mappings = new ArrayList<FieldMapping>(dereferencedFields.size());
+            log.debug(" > parse configured field mappings");
             for(String configuredMapping : dereferencedFields){
+            	log.trace(" - parse configure mapping '{}'",configuredMapping);
                 FieldMapping mapping = FieldMappingUtils.parseFieldMapping(configuredMapping,nsPrefixService);
                 if(mapping != null){
                     log.debug("   - add FieldMapping {}",mapping);
-                    fieldMapper.addMapping(mapping);
+                    mappings.add(mapping);
                 } else if(configuredMapping != null && !configuredMapping.isEmpty()){
                     log.warn("   - unable to parse FieldMapping '{}'", configuredMapping);
                 }
             }
+            if(!mappings.isEmpty()){
+                log.debug(" > apply {} valid mappings",mappings.size());
+                fieldMapper = new DefaultFieldMapperImpl(ValueConverterFactory.getDefaultInstance());
+                for(FieldMapping mapping : mappings){
+                	fieldMapper.addMapping(mapping);
+                }
+            } else { //no valid mapping parsed
+            	log.debug(" > no valid mapping parsed ... will dereference all fields");
+            	fieldMapper = null;
+            }
         } else {
             fieldMapper = null;
         }
@@ -331,6 +344,10 @@ public abstract class TrackingDereferenc
         } catch (EntityhubException e){
             throw new DereferenceException(uri, e);
         }
+       	if(log.isTraceEnabled()){
+    		log.trace("dereferenced via LDPath {}", ModelUtils.getRepresentationInfo(result));
+    	}
+
         if(!ldPathResults.isEmpty()){ //copy the results
             writeLock.lock();
             try {
@@ -387,6 +404,9 @@ public abstract class TrackingDereferenc
         try {
             RdfRepresentation clerezzaRep = valueFactory.createRdfRepresentation(uri, graph);
             fieldMapper.applyMappings(rep, clerezzaRep, valueFactory);
+           	if(log.isTraceEnabled()){
+        		log.trace("dereferenced via Mappings {}", ModelUtils.getRepresentationInfo(clerezzaRep));
+        	}
         } finally {
             writeLock.unlock();
         }
@@ -402,6 +422,9 @@ public abstract class TrackingDereferenc
     private void copyAll(UriRef uri, Representation rep, MGraph graph, Lock writeLock) {
         writeLock.lock();
         try {
+        	if(log.isTraceEnabled()){
+        		log.trace("dereferenced all of {}", ModelUtils.getRepresentationInfo(rep));
+        	}
             if(rep instanceof RdfRepresentation){
                 graph.addAll(((RdfRepresentation)rep).getRdfGraph());
             } else {

Modified: stanbol/trunk/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/FstLinkingEngineComponent.java
URL: http://svn.apache.org/viewvc/stanbol/trunk/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/FstLinkingEngineComponent.java?rev=1558748&r1=1558747&r2=1558748&view=diff
==============================================================================
--- stanbol/trunk/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/FstLinkingEngineComponent.java (original)
+++ stanbol/trunk/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/FstLinkingEngineComponent.java Thu Jan 16 10:46:59 2014
@@ -476,12 +476,13 @@ public class FstLinkingEngineComponent {
         } else {
             ecSize = -1;
         }
-        if(ecSize < 0){
-            documentCacheFactory = new FastLRUCacheManager(DEFAULT_ENTITY_CACHE_SIZE);
-        } else if(ecSize == 0){
+        if(ecSize == 0){
+            log.info(" ... EntityCache deactivated");
             documentCacheFactory = null;
         } else {
-            documentCacheFactory = new FastLRUCacheManager(ecSize);
+            int size = ecSize < 0 ? DEFAULT_ENTITY_CACHE_SIZE : ecSize;
+        	log.info(" ... create EntityCache (size: {})",size);
+            documentCacheFactory = new FastLRUCacheManager(size);
         }
         
         //(7) parse the Entity type field
@@ -776,6 +777,10 @@ public class FstLinkingEngineComponent {
         engineMetadata = null;
         textProcessingConfig = null;
         entityLinkerConfig = null;
+        if(documentCacheFactory != null){
+        	documentCacheFactory.close(); //ensure that old caches are cleared
+        }
+        documentCacheFactory = null;
         bundleContext = null;
         skipAltTokensConfig = null;
     }

Modified: stanbol/trunk/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/cache/EntityCacheManager.java
URL: http://svn.apache.org/viewvc/stanbol/trunk/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/cache/EntityCacheManager.java?rev=1558748&r1=1558747&r2=1558748&view=diff
==============================================================================
--- stanbol/trunk/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/cache/EntityCacheManager.java (original)
+++ stanbol/trunk/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/cache/EntityCacheManager.java Thu Jan 16 10:46:59 2014
@@ -49,4 +49,9 @@ public interface EntityCacheManager {
      */
     RefCounted<EntityCache> getCache(Object version);
 
+    /**
+     * Called if the EntityCacheManager is no longer used
+     */
+    void close();
+    
 }

Modified: stanbol/trunk/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/cache/FastLRUCacheManager.java
URL: http://svn.apache.org/viewvc/stanbol/trunk/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/cache/FastLRUCacheManager.java?rev=1558748&r1=1558747&r2=1558748&view=diff
==============================================================================
--- stanbol/trunk/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/cache/FastLRUCacheManager.java (original)
+++ stanbol/trunk/enhancement-engines/lucenefstlinking/src/main/java/org/apache/stanbol/enhancer/engines/lucenefstlinking/cache/FastLRUCacheManager.java Thu Jan 16 10:46:59 2014
@@ -20,13 +20,13 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
-import org.apache.felix.scr.annotations.Component;
-import org.apache.felix.scr.annotations.Service;
 import org.apache.lucene.document.Document;
 import org.apache.solr.search.CacheRegenerator;
 import org.apache.solr.search.FastLRUCache;
 import org.apache.solr.search.SolrCache;
 import org.apache.solr.util.RefCounted;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Implementation of the {@link EntityCacheManager} based on the Solr
@@ -37,6 +37,8 @@ import org.apache.solr.util.RefCounted;
  */
 public class FastLRUCacheManager implements EntityCacheManager {
 
+	private final Logger log = LoggerFactory.getLogger(getClass());
+	
     RefCounted<EntityCache> current;
     private final CacheRegenerator regenerator;
     private final Map<String,String> config;
@@ -75,21 +77,49 @@ public class FastLRUCacheManager impleme
     public RefCounted<EntityCache> getCache(Object version) {
         if(current == null || !current.get().getVersion().equals(version)){
             if(current != null){
-                //the the old one as outdated!
-                ((RefCountedImpl)current).setOutdated();
+            	log.debug(" > invalidate EntityCache for version {}", current.get().getVersion());
+            	//remove the reference to the old instance. This will allow to
+            	//destroy the old cache as soon as it is no longer used
+            	current.decref(); 
+            	log.debug("  ... {} remaining users for invalidated Cache", current.getRefcount());
+            	current = null;
             }
             //create a new cache
+            log.debug(" > create EntityCache for version {}", version);
             SolrCache<Integer,Document> cache = new FastLRUCache<Integer,Document>();
             cache.init(config, null, regenerator);
             current = new RefCountedImpl(new SolrEntityCache(version, cache));
-        }
-        current.incref();
+            //add a reference to the new cache by this class. This will be removed
+            //as soon as the instance is outdated
+            current.incref(); 
+        }
+        current.incref(); //this increase is for the holder of the returned instance
+        log.debug(" > increase RefCount for EntityCache for version {} to {}", 
+        		version, current.getRefcount());
         return current;
     }
 
+    @Override
+    public void close() {
+    	if(current != null){
+    		current.decref();
+    		current = null;
+    	}
+    }
+    
+    @Override
+    protected void finalize() {
+    	if(current != null){
+    		log.warn("[finalize] EntityCache Manager was not closed. This can "
+    				+ "cause Memory Leaks as Cached Entities will be kept in " 
+    				+ "Memory until finalization!");
+    	}
+    	close();
+    }
+    
     /**
-     * {@link RefCounted} implementation that ensures that outdated caches are
-     * cleared and closed as soon as they are no longer in use.
+     * {@link RefCounted} implementation that closes the {@link SolrEntityCache}
+     * when {@link #close()} is called by the supoer implementation.
      * 
      * @author Rupert Westenthaler
      *
@@ -100,23 +130,23 @@ public class FastLRUCacheManager impleme
             super(resource);
         }
 
-        private boolean outdated;
-
-        /**
-         * Used by the manager implementation to set the RefCounted EntityCache
-         * as outdated
-         */
-        protected void setOutdated() {
-            outdated = true;
+        @Override
+        public void decref() {
+        	super.decref();
+        	if(log.isDebugEnabled()){
+	            log.debug(" > decrease RefCount for EntityCache for version {} to {}", 
+	            		get().getVersion(), current.getRefcount());
+        	}
         }
-
         /**
-         * clears the cache if outdated
+         * closes the {@link SolrEntityCache}
          */
         protected void close(){
-            if(outdated){
-                ((SolrEntityCache)get()).close();
-            }
+        	if(log.isDebugEnabled()){
+        		log.debug(" > close EntityCache for version {}", 
+        				current.get().getVersion());
+        	}
+            ((SolrEntityCache)get()).close();
         }
 
     }