[Mulgara-svn] r1310 - trunk/src/jar/resolver-lucene/java/org/mulgara/resolver/lucene

ronald at mulgara.org ronald at mulgara.org
Tue Oct 14 13:53:03 UTC 2008


Author: ronald
Date: 2008-10-14 06:53:01 -0700 (Tue, 14 Oct 2008)
New Revision: 1310

Modified:
   trunk/src/jar/resolver-lucene/java/org/mulgara/resolver/lucene/FullTextStringIndex.java
   trunk/src/jar/resolver-lucene/java/org/mulgara/resolver/lucene/FullTextStringIndexUnitTest.java
   trunk/src/jar/resolver-lucene/java/org/mulgara/resolver/lucene/LuceneResolver.java
Log:
Implement removeModel operation. Note that this can be very slow because it
iterates over all the documents and removes each one individually; but this
the only to implement this without disrupting existing readers.

Modified: trunk/src/jar/resolver-lucene/java/org/mulgara/resolver/lucene/FullTextStringIndex.java
===================================================================
--- trunk/src/jar/resolver-lucene/java/org/mulgara/resolver/lucene/FullTextStringIndex.java	2008-10-14 13:52:55 UTC (rev 1309)
+++ trunk/src/jar/resolver-lucene/java/org/mulgara/resolver/lucene/FullTextStringIndex.java	2008-10-14 13:53:01 UTC (rev 1310)
@@ -48,6 +48,7 @@
 
 // Lucene text indexer
 import org.apache.lucene.queryParser.QueryParser;
+import org.apache.lucene.queryParser.ParseException;
 import org.apache.lucene.search.BooleanClause;
 import org.apache.lucene.search.BooleanQuery;
 import org.apache.lucene.search.HitCollector;
@@ -483,13 +484,14 @@
 
   /**
    * Remove all index files from the current initialised directory WARNING : All
-   * files are removed in the specified directory.
+   * files are removed in the specified directory. See {@link #removeAll} for an
+   * alternate solution.
    *
    * @return return true if successful at removing all index files
    * @throws FullTextStringIndexException Exception occurs when attempting to
    *      close the indexes
    */
-  public boolean removeAll() throws FullTextStringIndexException {
+  public boolean removeAllIndexes() throws FullTextStringIndexException {
     // debug logging
     if (logger.isDebugEnabled()) {
       logger.debug("Removing all indexes from " + luceneIndexDirectory.toString());
@@ -587,6 +589,37 @@
   }
 
   /**
+   * Remove all entries in the string pool. Unlike {@link removeAllIndexes}, this may be
+   * called while readers are active. However, this method may be very slow.
+   *
+   * @throws FullTextStringIndexException Exception occurs when attempting to remove the documents
+   */
+  public void removeAll() throws FullTextStringIndexException {
+    // debug logging
+    if (logger.isDebugEnabled()) {
+      logger.debug("Removing all documents from " + luceneIndexDirectory.toString());
+    }
+
+    try {
+      QueryParser parser = new QueryParser(ID_KEY, analyzer);
+      parser.setAllowLeadingWildcard(true);
+      synchronized (indexLock) {
+        //check the status of the index
+        indexer.deleteDocuments(parser.parse("*"));
+
+        //set the index status to modified
+        indexLock.setStatus(indexLock.MODIFIED);
+      }
+    } catch (ParseException ex) {
+      logger.error("Unexpected internal error", ex);
+      throw new FullTextStringIndexException("Unexpected internal error", ex);
+    } catch (IOException ex) {
+      logger.error("Unable to delete all documents", ex);
+      throw new FullTextStringIndexException("Unable to delete all documents", ex);
+    }
+  }
+
+  /**
    * Close the indexes on disk.
    *
    * @throws FullTextStringIndexException if there is an error whilst saving the
@@ -725,7 +758,7 @@
     } catch (IOException ex) {
       logger.error("Unable to read results for query '" + bQuery.toString(LITERAL_KEY) + "'", ex);
       throw new FullTextStringIndexException("Unable to read results for query '" + bQuery.toString(LITERAL_KEY) + "'", ex);
-    } catch (org.apache.lucene.queryParser.ParseException ex) {
+    } catch (ParseException ex) {
       logger.error("Unable to parse query '" + bQuery.toString(LITERAL_KEY) + "'", ex);
       throw new FullTextStringIndexException("Unable to parse query '" + bQuery.toString(LITERAL_KEY) + "'", ex);
     }

Modified: trunk/src/jar/resolver-lucene/java/org/mulgara/resolver/lucene/FullTextStringIndexUnitTest.java
===================================================================
--- trunk/src/jar/resolver-lucene/java/org/mulgara/resolver/lucene/FullTextStringIndexUnitTest.java	2008-10-14 13:52:55 UTC (rev 1309)
+++ trunk/src/jar/resolver-lucene/java/org/mulgara/resolver/lucene/FullTextStringIndexUnitTest.java	2008-10-14 13:53:01 UTC (rev 1310)
@@ -214,7 +214,7 @@
       String has = "http://mulgara.org/mulgara/document#has";
 
       //Clean any existing indexes.
-      index.removeAll();
+      index.removeAllIndexes();
 
       //re-create the index
       index = new FullTextStringIndex(indexDirectory, "fulltextsp", true);
@@ -289,10 +289,19 @@
 
       returned = index.find(document, has, "+*hrombosis").length();
       assertEquals("Reverse lookup was expecting 1 document returned", 1, returned);
+
+      // test removing all documents
+      index.removeAll();
+
+      returned = index.find(document, has, "European").length();
+      assertEquals("Got unexpected documents after removeAll:", 0, returned);
+
+      returned = index.find(document, has, "+study +*roup").length();
+      assertEquals("Got unexpected documents after removeAll:", 0, returned);
     } finally {
       if (index != null) {
         index.close();
-        assertTrue("Unable to remove all index files", index.removeAll());
+        assertTrue("Unable to remove all index files", index.removeAllIndexes());
       }
     }
   }
@@ -310,7 +319,7 @@
     try {
       // make sure the index directory is empty
       index.close();
-      assertTrue("Unable to remove all index files", index.removeAll());
+      assertTrue("Unable to remove all index files", index.removeAllIndexes());
 
       // create a new index
       index = new FullTextStringIndex(indexDirectory, "fulltextsp", true);
@@ -384,7 +393,7 @@
       // close the fulltextstringpool
       if (index != null) {
         index.close();
-        assertTrue("Unable to remove all index files", index.removeAll());
+        assertTrue("Unable to remove all index files", index.removeAllIndexes());
       }
     }
   }

Modified: trunk/src/jar/resolver-lucene/java/org/mulgara/resolver/lucene/LuceneResolver.java
===================================================================
--- trunk/src/jar/resolver-lucene/java/org/mulgara/resolver/lucene/LuceneResolver.java	2008-10-14 13:52:55 UTC (rev 1309)
+++ trunk/src/jar/resolver-lucene/java/org/mulgara/resolver/lucene/LuceneResolver.java	2008-10-14 13:53:01 UTC (rev 1310)
@@ -329,8 +329,17 @@
    */
   public void removeModel(long model) throws ResolverException {
     if (logger.isDebugEnabled()) {
-      logger.debug("Remove memory model " + model);
+      logger.debug("Removing full-text model " + model);
     }
+
+    try {
+      FullTextStringIndex stringIndex = openFullTextStringIndex(model);
+      stringIndex.removeAll();
+      stringIndex.optimize();
+      stringIndex.close();
+    } catch (FullTextStringIndexException ef) {
+      throw new ResolverException("Query failed against string index", ef);
+    }
   }
 
   /**




More information about the Mulgara-svn mailing list