You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by ca...@apache.org on 2011/09/11 21:32:43 UTC

svn commit: r1169505 - in /incubator/jena/Jena2/LARQ/trunk/src: main/java/org/apache/jena/larq/assembler/AssemblerLARQ.java test/java/org/apache/jena/larq/TestAssemblerLARQ.java

Author: castagna
Date: Sun Sep 11 19:32:42 2011
New Revision: 1169505

URL: http://svn.apache.org/viewvc?rev=1169505&view=rev
Log:
This is to allow people specify ja:textIndex pointing to a non existing directory and have their dataset indexed (it works with Fuseki config file using multiple datasets).

Modified:
    incubator/jena/Jena2/LARQ/trunk/src/main/java/org/apache/jena/larq/assembler/AssemblerLARQ.java
    incubator/jena/Jena2/LARQ/trunk/src/test/java/org/apache/jena/larq/TestAssemblerLARQ.java

Modified: incubator/jena/Jena2/LARQ/trunk/src/main/java/org/apache/jena/larq/assembler/AssemblerLARQ.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/LARQ/trunk/src/main/java/org/apache/jena/larq/assembler/AssemblerLARQ.java?rev=1169505&r1=1169504&r2=1169505&view=diff
==============================================================================
--- incubator/jena/Jena2/LARQ/trunk/src/main/java/org/apache/jena/larq/assembler/AssemblerLARQ.java (original)
+++ incubator/jena/Jena2/LARQ/trunk/src/main/java/org/apache/jena/larq/assembler/AssemblerLARQ.java Sun Sep 11 19:32:42 2011
@@ -43,7 +43,9 @@ import com.hp.hpl.jena.assembler.Mode;
 import com.hp.hpl.jena.assembler.assemblers.AssemblerBase;
 import com.hp.hpl.jena.assembler.exceptions.AssemblerException;
 import com.hp.hpl.jena.query.Dataset;
+import com.hp.hpl.jena.rdf.model.Model;
 import com.hp.hpl.jena.rdf.model.Resource;
+import com.hp.hpl.jena.rdf.model.StmtIterator;
 import com.hp.hpl.jena.sparql.util.graph.GraphUtils;
 
 public class AssemblerLARQ extends AssemblerBase implements Assembler
@@ -79,7 +81,14 @@ public class AssemblerLARQ extends Assem
         Directory directory;
         if ( indexPath != null ) 
         {
-            directory = FSDirectory.open(new File(indexPath));
+            File path = new File(indexPath) ;
+            if ( !path.exists() ) 
+            {
+                log.debug("Directory {} does not exist, building Lucene index...") ;
+                path.mkdirs() ;
+                build ( dataset, path ) ;
+            }
+            directory = FSDirectory.open(path);
         } else {
             directory = new RAMDirectory();
         }
@@ -112,4 +121,34 @@ public class AssemblerLARQ extends Assem
         return indexLARQ ;
     }
 
+    private static void build (Dataset dataset, File path)
+    {
+        Directory directory = null ;
+        IndexWriter indexWriter = null ;
+        IndexBuilderModel larqBuilder = null ;
+        try {
+            directory = FSDirectory.open(path) ;
+            indexWriter = IndexWriterFactory.create(directory);
+            larqBuilder = new IndexBuilderString(indexWriter) ;
+            larqBuilder.setAvoidDuplicates(false) ;
+            index(larqBuilder, dataset.getDefaultModel()) ;
+            for ( Iterator<String> iter = dataset.listNames() ; iter.hasNext() ; )
+            {
+                String g = iter.next() ;
+                index(larqBuilder, dataset.getNamedModel(g)) ;
+            }
+        } catch (Exception e) {
+            log.warn("Exception building the index: {}", e.getMessage()) ;
+        } finally {
+            if ( larqBuilder != null) larqBuilder.closeWriter() ;
+            if ( directory != null ) try { directory.close() ; } catch (IOException e) { log.warn("Problems closing the Lucene directory.") ; }
+        }
+    }
+
+    private static void index(IndexBuilderModel larqBuilder, Model model)
+    {
+        StmtIterator sIter = model.listStatements() ;
+        larqBuilder.indexStatements(sIter) ;
+    }
+
 }

Modified: incubator/jena/Jena2/LARQ/trunk/src/test/java/org/apache/jena/larq/TestAssemblerLARQ.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/LARQ/trunk/src/test/java/org/apache/jena/larq/TestAssemblerLARQ.java?rev=1169505&r1=1169504&r2=1169505&view=diff
==============================================================================
--- incubator/jena/Jena2/LARQ/trunk/src/test/java/org/apache/jena/larq/TestAssemblerLARQ.java (original)
+++ incubator/jena/Jena2/LARQ/trunk/src/test/java/org/apache/jena/larq/TestAssemblerLARQ.java Sun Sep 11 19:32:42 2011
@@ -18,11 +18,15 @@
 
 package org.apache.jena.larq;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
+import java.io.File;
 import java.io.IOException;
+import java.util.Iterator;
 
 import org.apache.jena.larq.assembler.AssemblerLARQ;
 import org.apache.lucene.index.CorruptIndexException;
@@ -36,6 +40,7 @@ import org.junit.Test;
 import org.openjena.atlas.lib.FileOps;
 
 import com.hp.hpl.jena.query.Dataset;
+import com.hp.hpl.jena.rdf.model.ResourceFactory;
 import com.hp.hpl.jena.sparql.core.assembler.AssemblerUtils;
 import com.hp.hpl.jena.tdb.TDBFactory;
 import com.hp.hpl.jena.tdb.assembler.VocabTDB;
@@ -105,17 +110,40 @@ public class TestAssemblerLARQ {
     }
     
     @Test public void testMake3() throws CorruptIndexException, IOException {
-        IndexLARQ indexLARQ = null;
+        IndexLARQ indexLARQ1 = null ;
+        IndexLARQ indexLARQ2 = null ;
         try {
             Dataset ds = TDBFactory.createDataset() ;
-            indexLARQ = AssemblerLARQ.make(ds, tmpDir + "/lucene") ;
-            indexLARQ.writer.commit();
-            indexLARQ = AssemblerLARQ.make(ds, tmpDir + "/lucene") ;
-            Directory directory = indexLARQ.getLuceneReader().directory() ;
+            indexLARQ1 = AssemblerLARQ.make(ds, tmpDir + "/lucene") ;
+            indexLARQ1.writer.commit() ;
+            indexLARQ2 = AssemblerLARQ.make(ds, tmpDir + "/lucene") ;
+            Directory directory = indexLARQ2.getLuceneReader().directory() ;
             assertTrue ( directory instanceof FSDirectory );
         } finally {
-            if ( indexLARQ != null ) indexLARQ.close();            
+            if ( indexLARQ1 != null ) indexLARQ1.close() ;
+            if ( indexLARQ2 != null ) indexLARQ2.close() ;
+        }
+    }
+
+    // if an index directory does not exist, LARQ will create it and build the appropriate Lucene index
+    @Test public void testMake4() throws CorruptIndexException, IOException {
+        File path = new File(tmpDir + File.separator + "lucene") ;
+        IndexLARQ indexLARQ = null;
+        try {
+            FileOps.delete(path.getAbsolutePath()) ;
+            assertFalse(path.exists()) ;
+            Dataset ds = TDBFactory.createDataset() ;
+            ds.getDefaultModel().add(ResourceFactory.createResource(), ResourceFactory.createProperty("foo:p"), ResourceFactory.createPlainLiteral("bar")) ;
+            indexLARQ = AssemblerLARQ.make(ds, path.getAbsolutePath()) ;
+            Iterator<HitLARQ> iter = indexLARQ.search("bar") ;
+            assertTrue (iter.hasNext()) ;
+            assertEquals("bar", iter.next().getNode().getLiteral().getValue()) ;
+        } finally {
+            FileOps.ensureDir(path.getAbsolutePath()) ;
+            if ( indexLARQ != null ) indexLARQ.close() ;
         }
+        
+        
 
     }