[exo-jcr-commits] exo-jcr SVN: r2642 - jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/spell.

do-not-reply at jboss.org do-not-reply at jboss.org
Wed Jun 16 10:48:31 EDT 2010


Author: nzamosenchuk
Date: 2010-06-16 10:48:30 -0400 (Wed, 16 Jun 2010)
New Revision: 2642

Modified:
   jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/spell/LuceneSpellChecker.java
Log:
EXOJCR-758: indexer updated

Modified: jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/spell/LuceneSpellChecker.java
===================================================================
--- jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/spell/LuceneSpellChecker.java	2010-06-16 14:39:20 UTC (rev 2641)
+++ jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/query/lucene/spell/LuceneSpellChecker.java	2010-06-16 14:48:30 UTC (rev 2642)
@@ -32,12 +32,14 @@
 import org.exoplatform.services.jcr.impl.core.query.TraversingQueryNodeVisitor;
 import org.exoplatform.services.jcr.impl.core.query.lucene.FieldNames;
 import org.exoplatform.services.jcr.impl.core.query.lucene.SearchIndex;
+import org.exoplatform.services.jcr.impl.util.SecurityHelper;
 import org.exoplatform.services.log.ExoLogger;
 import org.exoplatform.services.log.Log;
 
 import java.io.File;
 import java.io.IOException;
 import java.io.StringReader;
+import java.security.PrivilegedExceptionAction;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -194,6 +196,7 @@
       final String[] stmt = new String[1];
       aqt.accept(new TraversingQueryNodeVisitor()
       {
+         @Override
          public Object visit(RelationQueryNode node, Object o) throws RepositoryException
          {
             if (stmt[0] == null && node.getOperation() == RelationQueryNode.OPERATION_SPELLCHECK)
@@ -227,7 +230,7 @@
       /**
        * The directory where the spell index is stored.
        */
-      private final Directory spellIndexDirectory;
+      private Directory spellIndexDirectory;
 
       /**
        * The underlying spell checker.
@@ -249,12 +252,20 @@
       InternalSpellChecker(SearchIndex handler, float minDistance, boolean morePopular) throws IOException
       {
          this.handler = handler;
-         String path = handler.getContext().getIndexDirectory() + File.separatorChar + "spellchecker";
-         this.spellIndexDirectory = FSDirectory.getDirectory(path, new NativeFSLockFactory(path));
-         if (IndexReader.indexExists(spellIndexDirectory))
+         final String path = handler.getContext().getIndexDirectory() + File.separatorChar + "spellchecker";
+         spellIndexDirectory = null;
+         SecurityHelper.doPriviledgedIOExceptionAction(new PrivilegedExceptionAction<Object>()
          {
-            this.lastRefresh = System.currentTimeMillis();
-         }
+            public Object run() throws Exception
+            {
+               spellIndexDirectory = FSDirectory.getDirectory(path, new NativeFSLockFactory(path));
+               if (IndexReader.indexExists(spellIndexDirectory))
+               {
+                  lastRefresh = System.currentTimeMillis();
+               }
+               return null;
+            }
+         });
          this.spellChecker = new SpellChecker(spellIndexDirectory);
          this.spellChecker.setAccuracy(minDistance);
          this.morePopular = morePopular;
@@ -313,7 +324,14 @@
       {
          try
          {
-            spellIndexDirectory.close();
+            SecurityHelper.doPriviledgedIOExceptionAction(new PrivilegedExceptionAction<Object>()
+            {
+               public Object run() throws Exception
+               {
+                  spellIndexDirectory.close();
+                  return null;
+               }
+            });
          }
          catch (IOException e)
          {
@@ -385,11 +403,11 @@
        * @throws IOException
        *             if an error occurs while spell checking.
        */
-      private String[] check(String words[]) throws IOException
+      private String[] check(final String words[]) throws IOException
       {
          refreshSpellChecker();
          boolean hasSuggestion = false;
-         IndexReader reader = handler.getIndexReader();
+         final IndexReader reader = handler.getIndexReader();
          try
          {
             for (int retries = 0; retries < 100; retries++)
@@ -399,8 +417,16 @@
                   String[] suggestion = new String[words.length];
                   for (int i = 0; i < words.length; i++)
                   {
+                     final int currentIndex = i;
                      String[] similar =
-                        spellChecker.suggestSimilar(words[i], 5, reader, FieldNames.FULLTEXT, morePopular);
+                        SecurityHelper.doPriviledgedIOExceptionAction(new PrivilegedExceptionAction<String[]>()
+                        {
+                           public String[] run() throws Exception
+                           {
+                              return spellChecker.suggestSimilar(words[currentIndex], 5, reader, FieldNames.FULLTEXT,
+                                 morePopular);
+                           }
+                        });
 
                      if (similar.length > 0)
                      {
@@ -435,7 +461,14 @@
          }
          finally
          {
-            reader.close();
+            SecurityHelper.doPriviledgedIOExceptionAction(new PrivilegedExceptionAction<Object>()
+            {
+               public Object run() throws Exception
+               {
+                  reader.close();
+                  return null;
+               }
+            });
          }
       }
 
@@ -462,27 +495,35 @@
                   {
                      public void run()
                      {
+
                         try
                         {
-                           IndexReader reader = handler.getIndexReader();
-                           try
+                           SecurityHelper.doPriviledgedIOExceptionAction(new PrivilegedExceptionAction<Object>()
                            {
-                              long time = System.currentTimeMillis();
-                              Dictionary dict = new LuceneDictionary(reader, FieldNames.FULLTEXT);
-                              log.debug("Starting spell checker index refresh");
-                              spellChecker.indexDictionary(dict);
-                              time = System.currentTimeMillis() - time;
-                              time = time / 1000;
-                              log.info("Spell checker index refreshed in: " + new Long(time) + " s.");
-                           }
-                           finally
-                           {
-                              reader.close();
-                              synchronized (InternalSpellChecker.this)
+                              public Object run() throws Exception
                               {
-                                 refreshing = false;
+                                 IndexReader reader = handler.getIndexReader();
+                                 try
+                                 {
+                                    long time = System.currentTimeMillis();
+                                    Dictionary dict = new LuceneDictionary(reader, FieldNames.FULLTEXT);
+                                    log.debug("Starting spell checker index refresh");
+                                    spellChecker.indexDictionary(dict);
+                                    time = System.currentTimeMillis() - time;
+                                    time = time / 1000;
+                                    log.info("Spell checker index refreshed in: " + new Long(time) + " s.");
+                                 }
+                                 finally
+                                 {
+                                    reader.close();
+                                    synchronized (InternalSpellChecker.this)
+                                    {
+                                       refreshing = false;
+                                    }
+                                 }
+                                 return null;
                               }
-                           }
+                           });
                         }
                         catch (IOException e)
                         {



More information about the exo-jcr-commits mailing list