clerezza-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From enrid...@apache.org
Subject [1/3] git commit: CLEREZZA-878 Active graphs are now stored in a dedicated meta-graph in virtuoso, and restored on component activation.
Date Sat, 08 Mar 2014 00:03:28 GMT
Repository: clerezza
Updated Branches:
  refs/heads/master 9da2007d6 -> 659ad22ab


CLEREZZA-878 Active graphs are now stored in a dedicated meta-graph in virtuoso, and restored
on component activation.


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

Branch: refs/heads/master
Commit: ec85afb73cc0abad2040712e6b677fb42a70696b
Parents: 47b78a9
Author: enridaga <enridaga@apache.org>
Authored: Tue Feb 18 22:44:25 2014 +0000
Committer: enridaga <enridaga@apache.org>
Committed: Tue Feb 18 22:44:25 2014 +0000

----------------------------------------------------------------------
 .../rdf/virtuoso/storage/VirtuosoMGraph.java    |   2 +
 .../access/VirtuosoWeightedProvider.java        | 184 ++++++++++++++++++-
 2 files changed, 182 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/clerezza/blob/ec85afb7/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/VirtuosoMGraph.java
----------------------------------------------------------------------
diff --git a/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/VirtuosoMGraph.java
b/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/VirtuosoMGraph.java
index 709e377..0adad7b 100644
--- a/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/VirtuosoMGraph.java
+++ b/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/VirtuosoMGraph.java
@@ -431,6 +431,8 @@ public class VirtuosoMGraph extends AbstractMGraph implements MGraph,
 		try {
 			connection = provider.getConnection();
 			st = connection.prepareStatement(sql);
+			String s = b.toString();
+			logger.trace(" TTL is \n{}\n", s);
 			st.setNString(1, b.toString());
 			st.execute();
 			connection.commit();

http://git-wip-us.apache.org/repos/asf/clerezza/blob/ec85afb7/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/access/VirtuosoWeightedProvider.java
----------------------------------------------------------------------
diff --git a/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/access/VirtuosoWeightedProvider.java
b/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/access/VirtuosoWeightedProvider.java
index db073b1..1b7fa62 100644
--- a/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/access/VirtuosoWeightedProvider.java
+++ b/clerezza-virtuoso/rdf.virtuoso.storage/src/main/java/rdf/virtuoso/storage/access/VirtuosoWeightedProvider.java
@@ -52,9 +52,11 @@ import org.osgi.service.component.ComponentException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import rdf.virtuoso.storage.VirtuosoGraph;
 import rdf.virtuoso.storage.VirtuosoMGraph;
 import virtuoso.jdbc4.VirtuosoConnection;
 import virtuoso.jdbc4.VirtuosoException;
+import virtuoso.jdbc4.VirtuosoPreparedStatement;
 import virtuoso.jdbc4.VirtuosoResultSet;
 import virtuoso.jdbc4.VirtuosoStatement;
 
@@ -98,7 +100,8 @@ public class VirtuosoWeightedProvider implements WeightedTcProvider {
 	 * MAP OF LOADED GRAPHS
 	 */
 	private Map<UriRef, VirtuosoMGraph> graphs = new HashMap<UriRef, VirtuosoMGraph>();
-
+	private String ACTIVE_GRAPHS_GRAPH = "urn:x-virtuoso:active-graphs";
+	
 	/**
 	 * Weight
 	 */
@@ -233,9 +236,179 @@ public class VirtuosoWeightedProvider implements WeightedTcProvider
{
 				throw new ComponentException(e.getLocalizedMessage());
 			}
 		}
+		// Load remembered graphs
+		Set<UriRef> remembered = readRememberedGraphs();
+		for(UriRef name : remembered){
+			if (canModify(name)) {
+				graphs.put(name, new VirtuosoMGraph(name.getUnicodeString(),
+							this));
+			} else {
+				graphs.put(name,  new VirtuosoGraph(name.getUnicodeString(), this));
+			}
+		}
 		logger.info("Activated VirtuosoWeightedProvider.");
 	}
 
+	private Set<UriRef> readRememberedGraphs(){
+		logger.trace(" readRememberedGraphs()");
+		String SQL = "SPARQL SELECT DISTINCT ?G FROM <" + ACTIVE_GRAPHS_GRAPH + "> WHERE
{ ?G a <urn:x-virtuoso/active-graph> }";
+		VirtuosoConnection connection = null;
+		Exception e = null;
+		VirtuosoStatement st = null;
+		VirtuosoResultSet rs = null;
+		Set<UriRef> remembered = new HashSet<UriRef>();
+		try {
+			connection = getConnection();
+			st = (VirtuosoStatement) connection
+					.createStatement();
+			logger.debug("Executing SQL: {}", SQL);
+			rs = (VirtuosoResultSet) st.executeQuery(SQL);
+			while (rs.next()) {
+				UriRef name = new UriRef(rs.getString(1));
+				logger.debug(" > Graph {}", name);
+				remembered.add(name);
+			}
+		} catch (VirtuosoException e1) {
+			logger.error("Error while executing query/connection.", e1);
+			e = e1;
+		} catch (SQLException e1) {
+			logger.error("Error while executing query/connection.", e1);
+			e = e1;
+		} catch (ClassNotFoundException e1) {
+			logger.error("Error while executing query/connection.", e1);
+			e = e1;
+		}finally{
+
+			try { if (rs != null) rs.close(); } catch (Exception ex) {};
+		    try { if (st != null) st.close(); } catch (Exception ex) {};
+			if(connection!=null){
+				try {
+					connection.close();
+				} catch (VirtuosoException e1) {
+					logger.error("Cannot close connection", e1);
+				}
+			}
+		}
+		if(e!=null){
+			throw new RuntimeException(e);
+		}
+		return remembered;
+	}
+
+	private void rememberGraphs(UriRef... graphs){
+		logger.trace(" saveActiveGraphs()");
+		if (graphs.length>0) {
+			// Returns the list of graphs in the virtuoso quad store
+			String SQL = "SPARQL INSERT INTO <" + ACTIVE_GRAPHS_GRAPH + "> { `iri(??)` a <urn:x-virtuoso/active-graph>
}";
+			VirtuosoConnection connection = null;
+			Exception e = null;
+			VirtuosoPreparedStatement st = null;
+			VirtuosoResultSet rs = null;
+			try {
+				try {
+					connection = getConnection();
+					connection.setAutoCommit(false);
+					st = (VirtuosoPreparedStatement) connection
+							.prepareStatement(SQL);
+					logger.debug("Executing SQL: {}", SQL);
+					for (UriRef u : graphs) {
+						logger.trace(" > remembering {}", u);
+						st.setString(1, u.getUnicodeString());
+						st.executeUpdate();
+					}
+					connection.commit();
+				} catch (Exception e1) {
+					logger.error("Error while executing query/connection.", e1);
+					e = e1;
+					connection.rollback();
+				}
+			} catch (SQLException e1) {
+				logger.error("Error while executing query/connection.", e1);
+				e = e1;
+			} finally {
+				try {
+					if (rs != null)
+						rs.close();
+				} catch (Exception ex) {
+				}
+				;
+				try {
+					if (st != null)
+						st.close();
+				} catch (Exception ex) {
+				}
+				;
+				if (connection != null) {
+					try {
+						connection.close();
+					} catch (VirtuosoException e1) {
+						logger.error("Cannot close connection", e1);
+					}
+				}
+			}
+			if (e != null) {
+				throw new RuntimeException(e);
+			}
+		}
+	}
+	
+	private void forgetGraphs(UriRef... graphs){
+		logger.trace(" forgetGraphs()");
+		if (graphs.length>0) {
+			// Returns the list of graphs in the virtuoso quad store
+			String SQL = "SPARQL WITH <" + ACTIVE_GRAPHS_GRAPH + "> DELETE { ?s ?p ?v } WHERE
{ ?s ?p ?v . FILTER( ?s = iri(??) ) }";
+			VirtuosoConnection connection = null;
+			Exception e = null;
+			VirtuosoPreparedStatement st = null;
+			VirtuosoResultSet rs = null;
+			try {
+				try {
+					connection = getConnection();
+					connection.setAutoCommit(false);
+					st = (VirtuosoPreparedStatement) connection
+							.prepareStatement(SQL);
+					logger.debug("Executing SQL: {}", SQL);
+					for (UriRef u : graphs) {
+						logger.trace(" > remembering {}", u);
+						st.setString(1, u.getUnicodeString());
+						st.executeUpdate();
+					}
+					connection.commit();
+				} catch (Exception e1) {
+					logger.error("Error while executing query/connection.", e1);
+					e = e1;
+					connection.rollback();
+				}
+			} catch (SQLException e1) {
+				logger.error("Error while executing query/connection.", e1);
+				e = e1;
+			} finally {
+				try {
+					if (rs != null)
+						rs.close();
+				} catch (Exception ex) {
+				}
+				;
+				try {
+					if (st != null)
+						st.close();
+				} catch (Exception ex) {
+				}
+				;
+				if (connection != null) {
+					try {
+						connection.close();
+					} catch (VirtuosoException e1) {
+						logger.error("Cannot close connection", e1);
+					}
+				}
+			}
+			if (e != null) {
+				throw new RuntimeException(e);
+			}
+		}
+	}
+
 	/**
 	 * Deactivates this component.
 	 * 
@@ -245,7 +418,8 @@ public class VirtuosoWeightedProvider implements WeightedTcProvider {
 	@Deactivate
 	public void deactivate(ComponentContext cCtx) {
 		logger.debug("deactivate(ComponentContext {})", cCtx);
-		// XXX Anything to do here?
+		// Save active (possibly empty) graphs to a dedicated graph
+		rememberGraphs();
 		logger.info("Shutdown complete.");
 	}
 	
@@ -334,7 +508,7 @@ public class VirtuosoWeightedProvider implements WeightedTcProvider {
 			VirtuosoMGraph graph = null;
 			logger.debug("Attempt to load {}", name);
 			// Let's create the graph object
-			String SQL = "SPARQL SELECT ?G WHERE { GRAPH ?G {?A ?B ?C} . FILTER(?G = "
+			String SQL = "SPARQL SELECT ?G WHERE { GRAPH ?G {[] [] []} . FILTER(?G = "
 					+ name + ")} LIMIT 1";
 	
 			Statement st = null;
@@ -422,7 +596,7 @@ public class VirtuosoWeightedProvider implements WeightedTcProvider {
 		// XXX Add the active (possibly empty) mgraphs
 		graphs.addAll(this.graphs.keySet());
 		// Returns the list of graphs in the virtuoso quad store
-		String SQL = "SPARQL SELECT DISTINCT ?G WHERE {GRAPH ?G {?S ?P ?O} }";
+		String SQL = "SPARQL SELECT DISTINCT ?G WHERE {GRAPH ?G {[] [] []} }";
 		VirtuosoConnection connection = null;
 		Exception e = null;
 		VirtuosoStatement st = null;
@@ -567,6 +741,7 @@ public class VirtuosoWeightedProvider implements WeightedTcProvider {
 			if (canModify(name)) {
 				graphs.put(name, new VirtuosoMGraph(name.getUnicodeString(),
 							this));
+				rememberGraphs(name);
 				return graphs.get(name);
 			} else {
 				logger.error("Cannot create MGraph {}", name);
@@ -616,6 +791,7 @@ public class VirtuosoWeightedProvider implements WeightedTcProvider {
 		} else {
 			((MGraph) g).clear();
 			graphs.remove(name);
+			forgetGraphs(name);
 		}
 	}
 


Mime
View raw message