Hi,
today, I had a go at adding LARQ to DatasetAssemblerTDB.

We can use reflection in TDB's DatasetAssemblerTDB so that we do not
need to add a dependency on Lucene to TDB (see patch attached).

This works, however I am not satisfied by logging and exception handling.
I did not want to pollute the console when running TDB tests, however now
when there is a problem it's not as easy to understand what's going wrong.

Also, I am not so sure how we are suppose to .close() things.

My ultimate goal was to use LARQ from Fuseki, so, I tried to use the
modified DatasetAssemblerTDB from Fuseki.

I noticed a problem with Lucene write.lock file and after some investigation
I found that the DatasetAssemblerTDB was called twice. This could be a
bug:

Index: src/main/java/org/openjena/fuseki/FusekiCmd.java
===================================================================
--- src/main/java/org/openjena/fuseki/FusekiCmd.java    (revision 8563)
+++ src/main/java/org/openjena/fuseki/FusekiCmd.java    (working copy)
@@ -183,12 +183,6 @@
                 throw new CmdException(argPort.getKeyName()+" : bad port number: 
"+portStr) ;
             }
         }
-        else
-        {
-            Dataset ds = modDataset.createDataset() ;
-            if ( ds != null )
-                dsg = ds.asDatasetGraph() ;
-        }

         if ( dsg == null )
             throw new CmdException("No dataset defined: "+argUsage) ;

After I removed these lines from FusekiCmd.java I was able to use LARQ
successfully simply adding a ja:textIndex "..." to my assembler file:

<#dataset> rdf:type tdb:DatasetTDB ;
  ...
  ja:textIndex "/path/to/lucene/index" ;
  .

I'd really like to use LARQ (the one available as a separate module)
from Fuseki and this is the best way I found to do it.

What do you think?

Paolo




Index: src/main/java/com/hp/hpl/jena/tdb/assembler/DatasetAssemblerTDB.java
===================================================================
--- src/main/java/com/hp/hpl/jena/tdb/assembler/DatasetAssemblerTDB.java	(revision 8724)
+++ src/main/java/com/hp/hpl/jena/tdb/assembler/DatasetAssemblerTDB.java	(working copy)
@@ -1,34 +1,46 @@
 /*
  * (c) Copyright 2008, 2009 Hewlett-Packard Development Company, LP
+ * (c) Copyright 2011 Talis Systems Ltd.
  * All rights reserved.
  * [See end of file]
  */
 
 package com.hp.hpl.jena.tdb.assembler;
 
-import static com.hp.hpl.jena.sparql.util.graph.GraphUtils.exactlyOneProperty ;
-import static com.hp.hpl.jena.sparql.util.graph.GraphUtils.getStringValue ;
-import static com.hp.hpl.jena.tdb.assembler.VocabTDB.pLocation ;
-import static com.hp.hpl.jena.tdb.assembler.VocabTDB.* ;
-import org.openjena.atlas.logging.Log ;
+import static com.hp.hpl.jena.sparql.util.graph.GraphUtils.exactlyOneProperty;
+import static com.hp.hpl.jena.sparql.util.graph.GraphUtils.getStringValue;
+import static com.hp.hpl.jena.tdb.assembler.VocabTDB.pLocation;
+import static com.hp.hpl.jena.tdb.assembler.VocabTDB.pUnionDefaultGraph;
+
+import java.io.File;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+import org.openjena.atlas.logging.Log;
 
-import com.hp.hpl.jena.assembler.Assembler ;
-import com.hp.hpl.jena.assembler.Mode ;
-import com.hp.hpl.jena.assembler.exceptions.AssemblerException ;
-import com.hp.hpl.jena.graph.Node ;
-import com.hp.hpl.jena.query.Dataset ;
-import com.hp.hpl.jena.rdf.model.Resource ;
-import com.hp.hpl.jena.sparql.core.assembler.DatasetAssembler ;
-import com.hp.hpl.jena.sparql.expr.NodeValue ;
-import com.hp.hpl.jena.tdb.TDB ;
-import com.hp.hpl.jena.tdb.TDBFactory ;
-import com.hp.hpl.jena.tdb.base.file.Location ;
-import com.hp.hpl.jena.tdb.store.DatasetGraphTDB ;
+import com.hp.hpl.jena.assembler.Assembler;
+import com.hp.hpl.jena.assembler.JA;
+import com.hp.hpl.jena.assembler.Mode;
+import com.hp.hpl.jena.assembler.exceptions.AssemblerException;
+import com.hp.hpl.jena.graph.Node;
+import com.hp.hpl.jena.query.Dataset;
+import com.hp.hpl.jena.rdf.model.Property;
+import com.hp.hpl.jena.rdf.model.Resource;
+import com.hp.hpl.jena.rdf.model.ResourceFactory;
+import com.hp.hpl.jena.sparql.core.assembler.DatasetAssembler;
+import com.hp.hpl.jena.sparql.expr.NodeValue;
+import com.hp.hpl.jena.sparql.util.graph.GraphUtils;
+import com.hp.hpl.jena.tdb.TDB;
+import com.hp.hpl.jena.tdb.TDBFactory;
+import com.hp.hpl.jena.tdb.base.file.Location;
+import com.hp.hpl.jena.tdb.store.DatasetGraphTDB;
 
 public class DatasetAssemblerTDB extends DatasetAssembler
 {
     static { TDB.init(); }
     
+    public static final Property pIndex = ResourceFactory.createProperty(JA.getURI(), "textIndex") ;
+
     @Override
     public Object open(Assembler a, Resource root, Mode mode)
     {
@@ -56,6 +68,26 @@
                          "Failed to recognize value for union graph setting (ignored): "+b) ;
         }
         
+        try 
+        {
+            Class<?> clazz = Class.forName("org.apache.jena.larq.assembler.AssemblerLARQ") ;
+            if ( root.hasProperty(pIndex) ) 
+            {
+                try {
+                    Log.info(DatasetAssemblerTDB.class, "Initializing LARQ") ;
+                    String index = GraphUtils.getAsStringValue(root, pIndex) ;
+                    Class<?> paramTypes[] = new Class[] { Dataset.class, Class.forName("org.apache.lucene.store.Directory") } ;
+                    Method method = clazz.getDeclaredMethod("make", paramTypes) ;
+                    Object args[] = new Object[] { dsg.toDataset(), getDirectory(index) } ;
+                    method.invoke(clazz, args) ;
+                } catch (Exception e) {
+                    Log.warn(DatasetAssemblerTDB.class, "Unable to initialize LARQ: " + e.getMessage()) ;
+                }                
+            }
+        } catch(ClassNotFoundException e) {
+            // 
+        }
+        
         /*
         <r> rdf:type tdb:DatasetTDB ;
             tdb:location "dir" ;
@@ -67,10 +99,19 @@
         return TDBFactory.createDataset(dsg) ; 
     }
     
+    private static Object getDirectory( String filename ) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
+        Class<?> clazz = Class.forName("org.apache.lucene.store.FSDirectory");
+        Class<?> paramTypes[] = new Class[] { File.class } ;
+        Method method = clazz.getMethod("open", paramTypes) ;
+        Object args[] = new Object[] { new File(filename) } ;
+        return method.invoke(clazz, args) ;
+    }
+    
 }
 
 /*
  * (c) Copyright 2008, 2009 Hewlett-Packard Development Company, LP
+ * (c) Copyright 2011 Talis Systems Ltd.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without

Reply via email to