clerezza-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From m...@apache.org
Subject [1/2] git commit: CLEREZZA-939: Caching and exposing as service made optional
Date Tue, 12 Aug 2014 16:13:50 GMT
Repository: clerezza
Updated Branches:
  refs/heads/master 70b63d940 -> 68a9b1ec6


CLEREZZA-939: Caching and exposing as service made optional


Project: http://git-wip-us.apache.org/repos/asf/clerezza/repo
Commit: http://git-wip-us.apache.org/repos/asf/clerezza/commit/ba3004ac
Tree: http://git-wip-us.apache.org/repos/asf/clerezza/tree/ba3004ac
Diff: http://git-wip-us.apache.org/repos/asf/clerezza/diff/ba3004ac

Branch: refs/heads/master
Commit: ba3004ac6c3a1a2e867a92448724d62b84852108
Parents: 70b63d9
Author: Minto van der Sluis <misl@apache.org>
Authored: Tue Aug 12 18:11:53 2014 +0200
Committer: Minto van der Sluis <misl@apache.org>
Committed: Tue Aug 12 18:11:53 2014 +0200

----------------------------------------------------------------------
 rdf.core/pom.xml                                |   7 +-
 .../clerezza/rdf/core/access/TcManager.java     |  88 +++++++++++-----
 .../rdf/core/access/TcProviderMultiplexer.java  | 105 ++++++++++++-------
 3 files changed, 132 insertions(+), 68 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/clerezza/blob/ba3004ac/rdf.core/pom.xml
----------------------------------------------------------------------
diff --git a/rdf.core/pom.xml b/rdf.core/pom.xml
index cd6fcae..6c43334 100644
--- a/rdf.core/pom.xml
+++ b/rdf.core/pom.xml
@@ -22,7 +22,7 @@
     <parent>
         <artifactId>clerezza</artifactId>
         <groupId>org.apache.clerezza</groupId>
-        <version>0.5</version>
+        <version>1.0.0-SNAPSHOT</version>
         <relativePath>../parent</relativePath>
     </parent>
     <groupId>org.apache.clerezza</groupId>
@@ -40,11 +40,10 @@
             <groupId>org.osgi</groupId>
             <artifactId>org.osgi.compendium</artifactId>
         </dependency>
-        <!-- <dependency>
+        <dependency>
             <groupId>org.apache.felix</groupId>
             <artifactId>org.apache.felix.scr.annotations</artifactId>
-            <version>1.9.6</version>
-        </dependency> -->
+        </dependency>
         <dependency>
             <groupId>org.apache.felix</groupId>
             <artifactId>org.apache.felix.scr.ds-annotations</artifactId>

http://git-wip-us.apache.org/repos/asf/clerezza/blob/ba3004ac/rdf.core/src/main/java/org/apache/clerezza/rdf/core/access/TcManager.java
----------------------------------------------------------------------
diff --git a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/access/TcManager.java b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/access/TcManager.java
index c59b048..48a5c9a 100644
--- a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/access/TcManager.java
+++ b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/access/TcManager.java
@@ -24,11 +24,13 @@ import java.util.Collections;
 import java.util.Dictionary;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.Map;
-import java.util.Properties;
 import java.util.ServiceLoader;
 import java.util.Set;
+import java.util.SortedSet;
+import java.util.TreeSet;
 
 import org.apache.clerezza.rdf.core.Graph;
 import org.apache.clerezza.rdf.core.MGraph;
@@ -48,7 +50,11 @@ import org.apache.clerezza.rdf.core.sparql.query.ConstructQuery;
 import org.apache.clerezza.rdf.core.sparql.query.DescribeQuery;
 import org.apache.clerezza.rdf.core.sparql.query.Query;
 import org.apache.clerezza.rdf.core.sparql.query.SelectQuery;
+import org.apache.felix.scr.annotations.Properties;
+import org.apache.felix.scr.annotations.Property;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.component.ComponentContext;
 import org.osgi.service.component.annotations.Component;
@@ -99,9 +105,15 @@ import org.osgi.service.component.annotations.ReferencePolicy;
  */
 //immedia is set to true as this should register the graph services (even if manager service
is not required)
 @Component(service = TcManager.class, immediate = true)
+@Properties({
+	@Property(name = TcManager.MGRAPH_CACHE_ENABLED, boolValue = true, description = "Enable
caching mgraphs."),
+	@Property(name = TcManager.TRIPLECOLLECTION_SERVICES_ENABLED, boolValue = true, description
= "Register triple collections as services.") })
 public class TcManager extends TcProviderMultiplexer {
 
     public final static String GENERAL_PURPOSE_TC = "general.purpose.tc";
+    public final static String TRIPLECOLLECTION_SERVICES_ENABLED = "triplecollection.services.enabled";
+    public final static String MGRAPH_CACHE_ENABLED = "mgraph.cache.enabled";
+
     private static volatile TcManager instance;
     private TcAccessController tcAccessController = new TcAccessController() {
 
@@ -115,10 +127,12 @@ public class TcManager extends TcProviderMultiplexer {
             .synchronizedMap(new HashMap<UriRef, ServiceRegistration>());
     
     protected QueryEngine queryEngine;
+    private boolean isActivated = false;
+    private boolean isTcServicesEnabled = true;
 
     private ComponentContext componentContext;
-    private Collection<UriRef> mGraphsToRegisterOnActivation = new HashSet<UriRef>();
-    private Collection<UriRef> graphsToRegisterOnActivation = new HashSet<UriRef>();
+    protected SortedSet<WeightedTcProvider> tempProviderList = new TreeSet<WeightedTcProvider>(
+            new WeightedProviderComparator());
 
     /**
      * the constructor sets the singleton instance to allow instantiation by
@@ -143,6 +157,7 @@ public class TcManager extends TcProviderMultiplexer {
             synchronized (TcManager.class) {
                 if (instance == null) {
                     instance = new TcManager();
+                    instance.isActivated = true;
                     Iterator<WeightedTcProvider> weightedProviders = ServiceLoader
                             .load(WeightedTcProvider.class).iterator();
                     while (weightedProviders.hasNext()) {
@@ -164,15 +179,26 @@ public class TcManager extends TcProviderMultiplexer {
         }
         return instance;
     }
-
+    
     protected void activate(final ComponentContext componentContext) {
         this.componentContext = componentContext;
-        for (UriRef name : mGraphsToRegisterOnActivation) {
-            registerTripleCollectionAsService(name, true);
-        }
-        for (UriRef name : graphsToRegisterOnActivation) {
-            registerTripleCollectionAsService(name, false);
+        
+        // Read configuration
+		isTcServicesEnabled = true;
+		Object configTcServicesEnabled = componentContext.getProperties().get(TRIPLECOLLECTION_SERVICES_ENABLED);
+		if ( configTcServicesEnabled != null && configTcServicesEnabled instanceof String
) {
+			isTcServicesEnabled = Boolean.valueOf((String)configTcServicesEnabled);				
+		}
+		Object configCacheEnabled = componentContext.getProperties().get(MGRAPH_CACHE_ENABLED);
+		if ( configCacheEnabled != null && configCacheEnabled instanceof String ) {
+			setCachingEnabled(Boolean.valueOf((String)configCacheEnabled));				
+		}
+		isActivated = true;
+		
+        for (WeightedTcProvider provider : tempProviderList) {
+        	addWeightedTcProvider(provider);
         }
+        tempProviderList.clear();
     }
 
     protected void deactivate(final ComponentContext componentContext) {
@@ -181,6 +207,7 @@ public class TcManager extends TcProviderMultiplexer {
         }
         serviceRegistrations.clear();
         this.componentContext = null;
+		isActivated = false;
     }
 
     @Override
@@ -476,7 +503,11 @@ public class TcManager extends TcProviderMultiplexer {
     @Reference(policy = ReferencePolicy.DYNAMIC, 
             cardinality = ReferenceCardinality.MULTIPLE)
     protected void bindWeightedTcProvider(WeightedTcProvider provider) {
-        addWeightedTcProvider(provider);
+    	if (isActivated) {
+    		addWeightedTcProvider(provider);
+    	} else {
+    		tempProviderList.add(provider);  		
+    	}
     }
 
     /**
@@ -498,7 +529,11 @@ public class TcManager extends TcProviderMultiplexer {
             cardinality = ReferenceCardinality.AT_LEAST_ONE,
             target = "("+TcManager.GENERAL_PURPOSE_TC+"=true)")
     protected void bindGpWeightedTcProvider(WeightedTcProvider provider) {
-        addWeightedTcProvider(provider);
+    	if (isActivated) {
+    		addWeightedTcProvider(provider);
+    	} else {
+    		tempProviderList.add(provider);  		
+    	}
     }
 
     /**
@@ -523,24 +558,28 @@ public class TcManager extends TcProviderMultiplexer {
 
     @Override
     protected void mGraphAppears(UriRef name) {
-        if (componentContext == null) {
-            mGraphsToRegisterOnActivation.add(name);
-        } else {
-            registerTripleCollectionAsService(name, true);
-        }
+    	if (isTcServicesEnabled()) {
+    		// Only create the service when activated. When not activated
+    		// creating will be delayed till after activation.
+	        if (componentContext != null) {
+	            registerTripleCollectionAsService(name, true);
+	        }
+    	}
     }
 
     @Override
     protected void graphAppears(UriRef name) {
-        if (componentContext == null) {
-            graphsToRegisterOnActivation.add(name);
-        } else {
-            registerTripleCollectionAsService(name, false);
-        }
+    	if (isTcServicesEnabled()) {
+    		// Only create the service when activated. When not activated
+    		// creating will be delayed till after activation.
+	        if (componentContext != null) {
+	            registerTripleCollectionAsService(name, false);
+	        }
+    	}
     }
 
     private void registerTripleCollectionAsService(UriRef name, boolean isMGraph) {
-        Dictionary props = new Properties();
+        Dictionary<String,Object> props = new Hashtable<String, Object>();
         props.put("name", name.getUnicodeString());
         String[] interfaceNames;
         Object service;
@@ -564,8 +603,6 @@ public class TcManager extends TcProviderMultiplexer {
 
     @Override
     protected void tcDisappears(UriRef name) {
-        mGraphsToRegisterOnActivation.remove(name);
-        graphsToRegisterOnActivation.remove(name);
         ServiceRegistration reg = serviceRegistrations.get(name);
         if (reg != null) {
             reg.unregister();
@@ -590,4 +627,7 @@ public class TcManager extends TcProviderMultiplexer {
         return singleTargetTcProvider;
     }
 
+    public boolean isTcServicesEnabled() {
+		return isTcServicesEnabled;
+	}
 }

http://git-wip-us.apache.org/repos/asf/clerezza/blob/ba3004ac/rdf.core/src/main/java/org/apache/clerezza/rdf/core/access/TcProviderMultiplexer.java
----------------------------------------------------------------------
diff --git a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/access/TcProviderMultiplexer.java
b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/access/TcProviderMultiplexer.java
index 8c0f832..65f663e 100644
--- a/rdf.core/src/main/java/org/apache/clerezza/rdf/core/access/TcProviderMultiplexer.java
+++ b/rdf.core/src/main/java/org/apache/clerezza/rdf/core/access/TcProviderMultiplexer.java
@@ -54,6 +54,12 @@ public class TcProviderMultiplexer implements TcProvider {
      */
     private Map<UriRef, MGraphHolder> mGraphCache = Collections.synchronizedMap(new
HashMap<UriRef, MGraphHolder>());
 
+	/**
+	 * Flag to indicate whether mgraphs should be cached for faster access. By
+	 * default caching is enabled for backward compatibility.
+	 */
+	private boolean isCachingEnabled = true;
+
     /**
      * Registers a provider
      *
@@ -142,24 +148,25 @@ public class TcProviderMultiplexer implements TcProvider {
     private void weightedProviderAdded(WeightedTcProvider newProvider,
             Set<UriRef> newProvidedUris) {
         Set<WeightedTcProvider> lowerWeightedProviderList = getLowerWeightedProvider(newProvider);
-        for (UriRef name : newProvidedUris) {
-            final MGraphHolder holder = mGraphCache.get(name);
-            if ((holder != null) && (holder.getWeightedTcProvider() != null)) {
-                if (lowerWeightedProviderList.contains(holder.getWeightedTcProvider())) {
-                    tcDisappears(name);
-                    mGraphCache.remove(name);
-                } else {
-                    continue;
-                }
-            }
-            TripleCollection triples = newProvider.getTriples(name);
-            if (triples instanceof MGraph) {
-                mGraphCache.put(name, new MGraphHolder(newProvider, ensureLockable((MGraph)triples)));
-                mGraphAppears(name);
-            } else {
-                graphAppears(name);
-            }
-
+    	if (isCachingEnabled()) {
+	        for (UriRef name : newProvidedUris) {
+	            final MGraphHolder holder = mGraphCache.get(name);
+	            if ((holder != null) && (holder.getWeightedTcProvider() != null)) {
+	                if (lowerWeightedProviderList.contains(holder.getWeightedTcProvider()))
{
+	                    tcDisappears(name);
+	                    mGraphCache.remove(name);
+	                } else {
+	                    continue;
+	                }
+	            }
+	            TripleCollection triples = newProvider.getTriples(name);
+	            if (triples instanceof MGraph) {
+	           		mGraphCache.put(name, new MGraphHolder(newProvider, ensureLockable((MGraph)triples)));
+	                mGraphAppears(name);
+	            } else {
+	                graphAppears(name);
+	            }
+	    	}
         }
     }
 
@@ -188,23 +195,24 @@ public class TcProviderMultiplexer implements TcProvider {
                 tcDisappears(name);
                 mGraphCache.remove(name);
 
-                // check if another WeightedTcProvider has the TripleCollection.
-                // And if so register as service.
-                for (WeightedTcProvider provider : providerList) {
-                    try {
-                        TripleCollection triples = provider.getTriples(name);
-                        if (triples instanceof MGraph) {
-                            mGraphCache.put(name, new MGraphHolder(provider, ensureLockable((MGraph)triples)));
-                            mGraphAppears(name);
-                        } else {
-                            graphAppears(name);
-                        }
-                        break;
-                    } catch (NoSuchEntityException e) {
-                        // continue;
-                    }
-                }
-
+            	if (isCachingEnabled()) {
+	                // check if another WeightedTcProvider has the TripleCollection.
+	                // And if so register as service.
+	                for (WeightedTcProvider provider : providerList) {
+	                    try {
+	                        TripleCollection triples = provider.getTriples(name);
+	                        if (triples instanceof MGraph) {
+	                       		mGraphCache.put(name, new MGraphHolder(provider, ensureLockable((MGraph)triples)));
+	                            mGraphAppears(name);
+	                        } else {
+	                            graphAppears(name);
+	                        }
+	                        break;
+	                    } catch (NoSuchEntityException e) {
+	                        // continue;
+	                    }
+	                }
+            	}
             }
         }
     }
@@ -253,9 +261,10 @@ public class TcProviderMultiplexer implements TcProvider {
                 MGraph providedMGraph = provider.getMGraph(name);
                 LockableMGraph result = ensureLockable(providedMGraph);
 
-                MGraphHolder holder = mGraphCache.get(name);
-                mGraphCache.put(name, new MGraphHolder(
-                        provider, result));
+                if (isCachingEnabled()) {
+	                mGraphCache.put(name, new MGraphHolder(
+	                        provider, result));
+                }
                 return result;
             } catch (NoSuchEntityException e) {
                 //we do nothing and try our luck with the next provider
@@ -305,7 +314,9 @@ public class TcProviderMultiplexer implements TcProvider {
                 // unregisters a possible Graph or MGraph service under this name
                 // provided by a WeightedTcProvider with a lower weight.
                 tcDisappears(name);
-                mGraphCache.put(name, new MGraphHolder(provider, null));
+                if (isCachingEnabled()) {
+                	mGraphCache.put(name, new MGraphHolder(provider, null));
+                }
                 mGraphAppears(name);
                 return result;
             } catch (UnsupportedOperationException e) {
@@ -327,7 +338,9 @@ public class TcProviderMultiplexer implements TcProvider {
                 // unregisters a possible Graph or MGraph service under this name
                 // provided by a WeightedTcProvider with a lower weight.
                 tcDisappears(name);
-                mGraphCache.put(name, new MGraphHolder(provider, null));
+                if (isCachingEnabled()) {
+                	mGraphCache.put(name, new MGraphHolder(provider, null));
+                }
                 graphAppears(name);
                 return result;
             } catch (UnsupportedOperationException e) {
@@ -441,4 +454,16 @@ public class TcProviderMultiplexer implements TcProvider {
     public SortedSet<WeightedTcProvider> getProviderList() {
         return providerList;
     }
+
+    public boolean isCachingEnabled() {
+		return isCachingEnabled;
+	}
+    
+    public void setCachingEnabled(boolean isCachingEnabled) {
+		this.isCachingEnabled = isCachingEnabled;
+		
+		if (!isCachingEnabled()) {
+			mGraphCache.clear();
+		}
+	}
 }


Mime
View raw message