This is an automated email from the ASF dual-hosted git repository.

andy pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/jena.git

commit 24a767551f0ba4ba0603322d1a4dd49ce7a6ba8a
Author: Andy Seaborne <[email protected]>
AuthorDate: Fri Nov 8 10:26:43 2024 +0000

    SPARQL_QueryGeneral: Convert dataset description handling to work on graphs
---
 .../jena/fuseki/servlets/SPARQL_QueryGeneral.java  | 116 ++++++++++-----------
 .../apache/jena/fuseki/system/GraphLoadUtils.java  |  15 ---
 2 files changed, 58 insertions(+), 73 deletions(-)

diff --git 
a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/SPARQL_QueryGeneral.java
 
b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/SPARQL_QueryGeneral.java
index ff1bf63100..eacc2f641c 100644
--- 
a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/SPARQL_QueryGeneral.java
+++ 
b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/SPARQL_QueryGeneral.java
@@ -28,14 +28,14 @@ import org.apache.jena.fuseki.Fuseki;
 import org.apache.jena.fuseki.server.DataAccessPoint;
 import org.apache.jena.fuseki.server.DataService;
 import org.apache.jena.fuseki.system.GraphLoadUtils;
-import org.apache.jena.query.Dataset;
-import org.apache.jena.query.DatasetFactory;
+import org.apache.jena.graph.Graph;
+import org.apache.jena.graph.Node;
+import org.apache.jena.graph.NodeFactory;
 import org.apache.jena.query.Query;
-import org.apache.jena.rdf.model.Model;
-import org.apache.jena.rdf.model.ModelFactory;
 import org.apache.jena.riot.RiotException;
 import org.apache.jena.sparql.core.DatasetDescription;
 import org.apache.jena.sparql.core.DatasetGraph;
+import org.apache.jena.sparql.core.DatasetGraphFactory;
 import org.apache.jena.sparql.core.DatasetGraphZero;
 
 public class SPARQL_QueryGeneral extends ServletAction {
@@ -109,62 +109,16 @@ public class SPARQL_QueryGeneral extends ServletAction {
                 if ( datasetDesc.isEmpty() )
                     return null;
 
-                List<String> graphURLs = datasetDesc.getDefaultGraphURIs();
-                List<String> namedGraphs = datasetDesc.getNamedGraphURIs();
-
-                if ( graphURLs.size() == 0 && namedGraphs.size() == 0 )
+                if ( datasetDesc.isEmpty() )
                     return null;
 
-                Dataset dataset = DatasetFactory.create();
-                // Look in cache for loaded graphs!!
-
-                // ---- Default graph
-                {
-                    Model model = ModelFactory.createDefaultModel();
-                    for ( String uri : graphURLs ) {
-                        if ( uri == null || uri.equals("") )
-                            throw new InternalErrorException("Default graph 
URI is null or the empty string");
-
-                        try {
-                            GraphLoadUtils.loadModel(model, uri, MaxTriples);
-                            action.log.info(format("[%d] Load (default graph) 
%s", action.id, uri));
-                        }
-                        catch (RiotException ex) {
-                            action.log.info(format("[%d] Parsing error loading 
%s: %s", action.id, uri, ex.getMessage()));
-                            ServletOps.errorBadRequest("Failed to load URL 
(parse error) " + uri + " : " + ex.getMessage());
-                        }
-                        catch (Exception ex) {
-                            action.log.info(format("[%d] Failed to load 
(default) %s: %s", action.id, uri, ex.getMessage()));
-                            ServletOps.errorBadRequest("Failed to load URL " + 
uri);
-                        }
-                    }
-                    dataset.setDefaultModel(model);
-                }
-                // ---- Named graphs
-                if ( namedGraphs != null ) {
-                    for ( String uri : namedGraphs ) {
-                        if ( uri == null || uri.equals("") )
-                            throw new InternalErrorException("Named graph URI 
is null or the empty string");
-
-                        try {
-                            Model model = ModelFactory.createDefaultModel();
-                            GraphLoadUtils.loadModel(model, uri, MaxTriples);
-                            action.log.info(format("[%d] Load (named graph) 
%s", action.id, uri));
-                            dataset.addNamedModel(uri, model);
-                        }
-                        catch (RiotException ex) {
-                            action.log.info(format("[%d] Parsing error loading 
%s: %s", action.id, uri, ex.getMessage()));
-                            ServletOps.errorBadRequest("Failed to load URL 
(parse error) " + uri + " : " + ex.getMessage());
-                        }
-                        catch (Exception ex) {
-                            action.log.info(format("[%d] Failed to load (named 
graph) %s: %s", action.id, uri, ex.getMessage()));
-                            ServletOps.errorBadRequest("Failed to load URL " + 
uri);
-                        }
-                    }
-                }
-
-                return dataset.asDatasetGraph();
+                DatasetGraph dsg = DatasetGraphFactory.create();
+                dsg.executeWrite(()-> {
+                    datasetDescDefaultGraph(action, dsg, datasetDesc);
+                    datasetDescNamedGraphs(action, dsg, datasetDesc);
+                });
 
+                return dsg;
             }
             catch (ActionErrorException ex) {
                 throw ex;
@@ -175,6 +129,52 @@ public class SPARQL_QueryGeneral extends ServletAction {
                 return null;
             }
         }
-    }
 
+        // ---- Default graph
+        private static void datasetDescDefaultGraph(HttpAction action, 
DatasetGraph dsg, DatasetDescription datasetDesc) {
+            List<String> graphURLs = datasetDesc.getDefaultGraphURIs();
+            Graph graph = dsg.getDefaultGraph();
+
+            for ( String uri : graphURLs ) {
+                if ( uri == null || uri.equals("") )
+                    throw new InternalErrorException("Default graph URI is 
null or the empty string");
+                try {
+                    GraphLoadUtils.loadGraph(graph, uri, MaxTriples);
+                    action.log.info(format("[%d] Load (default graph) %s", 
action.id, uri));
+                }
+                catch (RiotException ex) {
+                    action.log.info(format("[%d] Parsing error loading %s: 
%s", action.id, uri, ex.getMessage()));
+                    ServletOps.errorBadRequest("Failed to load URL (parse 
error) " + uri + " : " + ex.getMessage());
+                }
+                catch (Exception ex) {
+                    action.log.info(format("[%d] Failed to load (default) %s: 
%s", action.id, uri, ex.getMessage()));
+                    ServletOps.errorBadRequest("Failed to load URL " + uri);
+                }
+            }
+        }
+
+        // ---- Named graphs
+        private static void datasetDescNamedGraphs(HttpAction action, 
DatasetGraph dsg, DatasetDescription datasetDesc) {
+            List<String> namedGraphs = datasetDesc.getNamedGraphURIs();
+            for ( String uri : namedGraphs ) {
+                if ( uri == null || uri.equals("") )
+                    throw new InternalErrorException("Named graph URI is null 
or the empty string");
+
+                try {
+                    Node graphName = NodeFactory.createURI(uri);
+                    Graph graph = dsg.getGraph(graphName);
+                    GraphLoadUtils.loadGraph(graph, uri, MaxTriples);
+                    action.log.info(format("[%d] Load (named graph) %s", 
action.id, uri));
+                }
+                catch (RiotException ex) {
+                    action.log.info(format("[%d] Parsing error loading %s: 
%s", action.id, uri, ex.getMessage()));
+                    ServletOps.errorBadRequest("Failed to load URL (parse 
error) " + uri + " : " + ex.getMessage());
+                }
+                catch (Exception ex) {
+                    action.log.info(format("[%d] Failed to load (named graph) 
%s: %s", action.id, uri, ex.getMessage()));
+                    ServletOps.errorBadRequest("Failed to load URL " + uri);
+                }
+            }
+        }
+    }
 }
diff --git 
a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/system/GraphLoadUtils.java
 
b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/system/GraphLoadUtils.java
index f097501376..a4a5c95013 100644
--- 
a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/system/GraphLoadUtils.java
+++ 
b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/system/GraphLoadUtils.java
@@ -22,8 +22,6 @@ package org.apache.jena.fuseki.system;
 import org.apache.jena.fuseki.Fuseki;
 import org.apache.jena.graph.GraphMemFactory;
 import org.apache.jena.graph.Graph;
-import org.apache.jena.rdf.model.Model;
-import org.apache.jena.rdf.model.ModelFactory;
 import org.apache.jena.riot.RDFParser;
 import org.apache.jena.riot.system.StreamRDF;
 import org.apache.jena.riot.system.StreamRDFLib;
@@ -32,19 +30,6 @@ import org.apache.jena.riot.system.StreamRDFLib;
 
 public class GraphLoadUtils
 {
-    // ---- Model level
-
-    public static Model readModel(String uri, int limit) {
-        Graph g = GraphMemFactory.createDefaultGraphSameTerm();
-        readUtil(g, uri, limit);
-        return ModelFactory.createModelForGraph(g);
-    }
-
-    public static void loadModel(Model model, String uri, int limit) {
-        Graph g = model.getGraph();
-        readUtil(g, uri, limit);
-    }
-
     // ---- Graph level
 
     public static Graph readGraph(String uri, int limit) {

Reply via email to