Избежать исключения lucence QueryParser Parse? - PullRequest
6 голосов
/ 24 декабря 2010

В 3-й строке я получаю исключения, такие как «IOException: read past eof» и «LookaheadSuccess: Ошибка в приложении».Есть ли способ избежать этого?я ненавижу разрывы и нажимаю продолжить дважды каждый раз, когда я выполняю поиск

Заметьте, я замечаю это, только когда я говорю визуальным студиям показывать мне исключения, которые генерируются, даже если они пойманы.Я не получаю исключения, я просто вижу, что они выбрасываются, таким образом, две (или три) точки останова каждый раз, когда я ищу.Приложение работает нормально.

var searcher = new IndexSearcher (directory, true);var parser = new QueryParser (Lucene.Net.Util.Version.LUCENE_29, "all", анализатор);var query = parser.Parse (text);// здесь

Ответы [ 3 ]

3 голосов
/ 23 июля 2014

Lucene.NET (на момент версии 3.0.3) использовал исключения IOException для управления несколькими частями потока анализатора. Это плохо сказывалось на производительности (до 90 мс на моей машине для разработки).

Хорошей новостью является то, что версия, в настоящее время находящаяся в их хранилище исходного кода на http://lucenenet.apache.org/community.html, похоже, удалила конкретные исключения, которые вызывали это. Конечно, для меня это значительно улучшило производительность. Надеюсь, это поможет.

3 голосов
/ 22 апреля 2015

Патч для QueryParser в Lucene 3.0.3, чтобы избежать выброса исключения LookaheadSuccess:

--- a/src/core/QueryParser/QueryParser.cs
+++ b/src/core/QueryParser/QueryParser.cs
@@ -1708,16 +1708,13 @@ namespace Lucene.Net.QueryParsers
         }

         private bool Jj_2_1(int xla)
-        {
+        {
+            bool lookaheadSuccess = false;
             jj_la = xla;
             jj_lastpos = jj_scanpos = token;
             try
             {
-                return !Jj_3_1();
-            }
-            catch (LookaheadSuccess)
-            {
-                return true;
+                return !Jj_3_1(out lookaheadSuccess);
             }
             finally
             {
@@ -1725,29 +1722,31 @@ namespace Lucene.Net.QueryParsers
             }
         }

-        private bool Jj_3R_2()
-        {
-            if (jj_scan_token(TermToken)) return true;
-            if (jj_scan_token(ColonToken)) return true;
+        private bool Jj_3R_2(out bool lookaheadSuccess)
+        {
+            if (jj_scan_token(TermToken, out lookaheadSuccess)) return true;
+            if (lookaheadSuccess) return false;
+            if (jj_scan_token(ColonToken, out lookaheadSuccess)) return true;
             return false;
         }

-        private bool Jj_3_1()
+        private bool Jj_3_1(out bool lookaheadSuccess)
         {
             Token xsp;
             xsp = jj_scanpos;
-            if (Jj_3R_2())
+            if (Jj_3R_2(out lookaheadSuccess))
             {
                 jj_scanpos = xsp;
-                if (Jj_3R_3()) return true;
+                if (Jj_3R_3(out lookaheadSuccess)) return true;
             }
             return false;
         }

-        private bool Jj_3R_3()
-        {
-            if (jj_scan_token(StarToken)) return true;
-            if (jj_scan_token(ColonToken)) return true;
+        private bool Jj_3R_3(out bool lookaheadSuccess)
+        {
+            if (jj_scan_token(StarToken, out lookaheadSuccess)) return true;
+            if (lookaheadSuccess) return false;
+            if (jj_scan_token(ColonToken, out lookaheadSuccess)) return true;
             return false;
         }

@@ -1861,14 +1860,9 @@ namespace Lucene.Net.QueryParsers
             throw GenerateParseException();
         }

-        [Serializable]
-        private sealed class LookaheadSuccess : System.Exception
-        {
-        }
-
-        private LookaheadSuccess jj_ls = new LookaheadSuccess();
-        private bool jj_scan_token(int kind)
-        {
+        private bool jj_scan_token(int kind, out bool lookaheadSuccess)
+        {
+            lookaheadSuccess = false;
             if (jj_scanpos == jj_lastpos)
             {
                 jj_la--;
@@ -1896,8 +1890,8 @@ namespace Lucene.Net.QueryParsers
                 }
                 if (tok != null) Jj_add_error_token(kind, i);
             }
-            if (jj_scanpos.kind != kind) return true;
-            if (jj_la == 0 && jj_scanpos == jj_lastpos) throw jj_ls;
+            if (jj_scanpos.kind != kind) return true;
+            if (jj_la == 0 && jj_scanpos == jj_lastpos) lookaheadSuccess = true;
             return false;
         }

@@ -2029,32 +2023,34 @@ namespace Lucene.Net.QueryParsers
         }

         private void Jj_rescan_token()
-        {
+        {
+            bool lookaheadSuccess = false;
             jj_rescan = true;
             for (int i = 0; i < 1; i++)
             {
-                try
+                JJCalls p = jj_2_rtns[i];
+                do
                 {
-                    JJCalls p = jj_2_rtns[i];
-                    do
+                    if (p.gen > jj_gen)
                     {
-                        if (p.gen > jj_gen)
+                        jj_la = p.arg;
+                        jj_lastpos = jj_scanpos = p.first;
+                        switch (i)
                         {
-                            jj_la = p.arg;
-                            jj_lastpos = jj_scanpos = p.first;
-                            switch (i)
-                            {
-                                case 0:
-                                    Jj_3_1();
-                                    break;
-                            }
+                            case 0:
+                                Jj_3_1(out lookaheadSuccess);
+                                if (lookaheadSuccess)
+                                {
+                                    goto Jj_rescan_token_after_while_label;
+                                }
+                                break;
                         }
-                        p = p.next;
-                    } while (p != null);
-                }
-                catch (LookaheadSuccess)
-                {
-                }
+                    }
+                    p = p.next;
+                } while (p != null);
+
+            Jj_rescan_token_after_while_label:
+                lookaheadSuccess = false;
             }
             jj_rescan = false;
         }
-- 

Патч для QueryParser в Lucene 3.0.3, чтобы избежать большого количества исключений System.IO.IOException:

CharStream.cs:

--- CharStream.cs
+++ CharStream.cs
@@ -44,6 +44,7 @@
        /// implementing this interface.  Can throw any java.io.IOException.
        /// </summary>
        char ReadChar();
+       char ReadChar(ref bool? systemIoException);

        /// <summary> Returns the column position of the character last read.</summary>
        /// <deprecated>
@@ -93,6 +94,7 @@
        /// to this method to implement backup correctly.
        /// </summary>
        char BeginToken();
+       char BeginToken(ref bool? systemIoException);

        /// <summary> Returns a string made up of characters from the marked token beginning
        /// to the current buffer position. Implementations have the choice of returning

FastCharStream.cs:

--- FastCharStream.cs
+++ FastCharStream.cs
@@ -48,12 +48,35 @@

        public char ReadChar()
        {
+           bool? systemIoException = null;
            if (bufferPosition >= bufferLength)
-               Refill();
+           {
+               Refill(ref systemIoException);
+           }
+           return buffer[bufferPosition++];
+       }
+       
+       public char ReadChar(ref bool? systemIoException)
+       {
+           if (bufferPosition >= bufferLength)
+           {
+               Refill(ref systemIoException);
+               // If using this Nullable as System.IO.IOException signal and is signaled.
+               if (systemIoException.HasValue && systemIoException.Value == true)
+               {
+                   return '\0';
+               }
+           }
            return buffer[bufferPosition++];
        }

-       private void  Refill()
+       // You may ask to be signaled of a System.IO.IOException through the systemIoException parameter.
+       // Set it to false if you are interested, it will be set to true to signal a System.IO.IOException.
+       // Set it to null if you are not interested.
+       // This is used to avoid having a lot of System.IO.IOExceptions thrown while running the code under a debugger.
+       // Having a lot of exceptions thrown under a debugger causes the code to execute a lot more slowly.
+       // So use this if you are experimenting a lot of slow parsing at runtime under a debugger.
+       private void Refill(ref bool? systemIoException)
        {
            int newPosition = bufferLength - tokenStart;

@@ -86,7 +109,18 @@

            int charsRead = input.Read(buffer, newPosition, buffer.Length - newPosition);
            if (charsRead <= 0)
-               throw new System.IO.IOException("read past eof");
+           {
+               // If interested in using this Nullable to signal a System.IO.IOException
+               if (systemIoException.HasValue)
+               {
+                   systemIoException = true;
+                   return;
+               }
+               else
+               {
+                   throw new System.IO.IOException("read past eof");
+               }
+           }
            else
                bufferLength += charsRead;
        }
@@ -96,6 +130,12 @@
            tokenStart = bufferPosition;
            return ReadChar();
        }
+
+       public char BeginToken(ref bool? systemIoException)
+       {
+           tokenStart = bufferPosition;
+           return ReadChar(ref systemIoException);
+       }

        public void  Backup(int amount)
        {
@@ -156,4 +196,4 @@
            get { return 1; }
        }
    }
-}
\ No newline at end of file
+}

QueryParserTokenManager.cs:

--- QueryParserTokenManager.cs
+++ QueryParserTokenManager.cs
@@ -1341,9 +1341,16 @@

            for (; ; )
            {
+               bool? systemIoException = false;
                try
                {
-                   curChar = input_stream.BeginToken();
+                   curChar = input_stream.BeginToken(ref systemIoException);
+                   if (systemIoException != null && systemIoException.HasValue && systemIoException.Value == true)
+                   {
+                       jjmatchedKind = 0;
+                       matchedToken = JjFillToken();
+                       return matchedToken;
+                   }
                }
                catch (System.IO.IOException)
                {
@@ -1459,4 +1466,4 @@
            while (start++ != end);
        }
    }
-}
\ No newline at end of file
+}

Вы также можете использовать мою версию GitHub https://github.com/franckspike/lucenenet.git

3 голосов
/ 25 декабря 2010

Это исключения первого шанса, которые происходят и обнаруживаются в Lucene.Вы настроили Visual Studio для работы со всеми исключениями, а не только с необработанными.Используйте диалоговое окно «Исключения» (ctrl-alt-e iirc) и измените настройки.

...