예제 #1
0
        internal static global::Lucene.Net.Index.IndexReader CreateLuceneReader(Directory directory, bool secondAttempt = false)
        {
            try
            {
                // Scan the index directory for segments files to check for corruption
                var files         = directory.ListAll();
                var foundSegments = false;
                foreach (var file in files)
                {
                    if (file.StartsWith("segments", StringComparison.InvariantCultureIgnoreCase))
                    {
                        foundSegments = true;
                        break;
                    }
                }
                if (!foundSegments)
                {
                    foreach (var file in files)
                    {
                        directory.DeleteFile(file);
                    }
                }

                var reader = global::Lucene.Net.Index.IndexReader.Open(directory, true);
                return(reader);
            }
            catch (FileNotFoundException fnf)
            {
                InitialiseBlankIndex(directory);
                if (!secondAttempt)
                {
                    return(CreateLuceneReader(directory, true));
                }
                throw new InvalidOperationException("Could not create an index reader - tried twice", fnf);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("Could not create an index reader", ex);
            }
        }
예제 #2
0
        internal static global::Lucene.Net.Index.IndexReader CreateLuceneReader(Directory directory, bool secondAttempt = false)
        {
            try
            {
                // Scan the index directory for segments files to check for corruption
                var files = directory.ListAll();
                var foundSegments = false;
                foreach (var file in files)
                {
                    if (file.StartsWith("segments", StringComparison.InvariantCultureIgnoreCase))
                    {
                        foundSegments = true;
                        break;
                    }
                }
                if (!foundSegments)
                {
                    foreach (var file in files)
                    {
                        directory.DeleteFile(file);
                    }
                }

                var reader = global::Lucene.Net.Index.IndexReader.Open(directory, true);
                return reader;
            }
            catch (FileNotFoundException fnf)
            {
                InitialiseBlankIndex(directory);
                if (!secondAttempt) return CreateLuceneReader(directory, true);
                throw new InvalidOperationException("Could not create an index reader - tried twice", fnf);
            }
            catch(Exception ex)
            {
                throw new InvalidOperationException("Could not create an index reader", ex);
            }
        }