You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2020/12/09 10:05:05 UTC

[jena] branch master updated: JENA-2010: Unwrap to find a TDB2 dataset

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 0d5a737  JENA-2010: Unwrap to find a TDB2 dataset
     new 34b2d48  Merge pull request #883 from afs/unwrap-tdb2
0d5a737 is described below

commit 0d5a7379307e2e20707c9d56f0a39a6e4f2ee982
Author: Andy Seaborne <an...@apache.org>
AuthorDate: Mon Dec 7 19:21:22 2020 +0000

    JENA-2010: Unwrap to find a TDB2 dataset
---
 .../org/apache/jena/fuseki/mgt/ActionCompact.java  | 35 ++++++++++++++++++++--
 1 file changed, 33 insertions(+), 2 deletions(-)

diff --git a/jena-fuseki2/jena-fuseki-webapp/src/main/java/org/apache/jena/fuseki/mgt/ActionCompact.java b/jena-fuseki2/jena-fuseki-webapp/src/main/java/org/apache/jena/fuseki/mgt/ActionCompact.java
index 52654ee..c646bfc 100644
--- a/jena-fuseki2/jena-fuseki-webapp/src/main/java/org/apache/jena/fuseki/mgt/ActionCompact.java
+++ b/jena-fuseki2/jena-fuseki-webapp/src/main/java/org/apache/jena/fuseki/mgt/ActionCompact.java
@@ -20,11 +20,15 @@ package org.apache.jena.fuseki.mgt;
 
 import static java.lang.String.format;
 
+import com.github.jsonldjava.shaded.com.google.common.base.Predicate;
+
 import org.apache.jena.fuseki.Fuseki;
 import org.apache.jena.fuseki.ctl.ActionAsyncTask;
 import org.apache.jena.fuseki.ctl.TaskBase;
 import org.apache.jena.fuseki.servlets.HttpAction;
 import org.apache.jena.fuseki.servlets.ServletOps;
+import org.apache.jena.sparql.core.DatasetGraph;
+import org.apache.jena.sparql.core.DatasetGraphWrapper;
 import org.apache.jena.tdb2.DatabaseMgr;
 import org.apache.jena.tdb2.sys.TDBInternal;
 import org.slf4j.Logger;
@@ -52,13 +56,39 @@ public class ActionCompact extends ActionAsyncTask
             ServletOps.errorBadRequest("Dataset not found");
             return null;
         }
-        if ( ! TDBInternal.isTDB2(task.dataset) ) {
+
+        DatasetGraph dsg = getTDB2(task.dataset);
+
+        if ( dsg == null ) {
             ServletOps.errorBadRequest("Not a TDB2 dataset: Compact only applies to TDB2");
             return null;
         }
         return task;
     }
 
+    // Unwrapping until the top of TDBS2, DatasetGraphSwitchable, is found.
+    // This include a DatasetGraphText.
+
+    /** Safety condition that stops further unwrapping */
+    private static Predicate<DatasetGraph> notTDB2 =
+        (dsg) -> org.apache.jena.tdb.sys.TDBInternal.isTDB1(dsg);
+
+    private static DatasetGraph getTDB2(DatasetGraph dsg) {
+        return unwrap(dsg, x -> TDBInternal.isTDB2(x), notTDB2);
+    }
+
+    private static DatasetGraph unwrap(DatasetGraph dsg, Predicate<DatasetGraph> predicate, Predicate<DatasetGraph> failPredicate) {
+        for ( ;; ) {
+            if ( failPredicate.apply(dsg) )
+                return null;
+            if ( predicate.apply(dsg) )
+                return dsg;
+            if ( ! ( dsg instanceof DatasetGraphWrapper) )
+                return null;
+            dsg = ((DatasetGraphWrapper)dsg).getWrapped();
+        }
+    }
+
     static class CompactTask extends TaskBase {
         static private Logger log = Fuseki.compactLog;
 
@@ -69,8 +99,9 @@ public class ActionCompact extends ActionAsyncTask
         @Override
         public void run() {
             try {
+                DatasetGraph dsg = getTDB2(dataset);
                 log.info(format("[%d] >>>> Start compact %s", actionId, datasetName));
-                DatabaseMgr.compact(dataset);
+                DatabaseMgr.compact(dsg);
                 log.info(format("[%d] <<<< Finish compact %s", actionId, datasetName));
             } catch (Throwable ex) {
                 log.warn(format("[%d] **** Exception in compact", actionId), ex);