Lucene.Net.Core.Store: Removed NoSuchDirectoryException and replaced with System.IO.DirectoryNotFoundException.
Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/0e2de164 Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/0e2de164 Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/0e2de164 Branch: refs/heads/api-work Commit: 0e2de164d6659aa913a327f1858d71b7fe4e8ac8 Parents: b6dcfe8 Author: Shad Storhaug <[email protected]> Authored: Thu Mar 30 17:15:57 2017 +0700 Committer: Shad Storhaug <[email protected]> Committed: Thu Mar 30 17:20:01 2017 +0700 ---------------------------------------------------------------------- src/Lucene.Net.Core/Index/DirectoryReader.cs | 16 ++++- src/Lucene.Net.Core/Index/IndexFileDeleter.cs | 20 +++++- src/Lucene.Net.Core/Index/IndexWriter.cs | 6 ++ src/Lucene.Net.Core/Index/SegmentInfos.cs | 3 +- src/Lucene.Net.Core/Store/Directory.cs | 2 +- src/Lucene.Net.Core/Store/FSDirectory.cs | 8 +-- .../Store/FileSwitchDirectory.cs | 7 +- .../Store/NRTCachingDirectory.cs | 2 +- .../Store/NoSuchDirectoryException.cs | 76 ++++++++++---------- .../Util/LuceneTestCase.cs | 6 ++ .../Index/TestDirectoryReader.cs | 3 +- .../Index/TestIndexWriterExceptions.cs | 6 ++ .../Index/TestIndexWriterLockRelease.cs | 24 +++++++ src/Lucene.Net.Tests/Store/TestDirectory.cs | 13 +++- .../Store/TestFileSwitchDirectory.cs | 2 +- .../Store/TestNRTCachingDirectory.cs | 2 +- 16 files changed, 140 insertions(+), 56 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucenenet/blob/0e2de164/src/Lucene.Net.Core/Index/DirectoryReader.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Core/Index/DirectoryReader.cs b/src/Lucene.Net.Core/Index/DirectoryReader.cs index 3d55e8e..4a2b588 100644 --- a/src/Lucene.Net.Core/Index/DirectoryReader.cs +++ b/src/Lucene.Net.Core/Index/DirectoryReader.cs @@ -24,7 +24,6 @@ namespace Lucene.Net.Index // javadocs using Directory = Lucene.Net.Store.Directory; - using NoSuchDirectoryException = Lucene.Net.Store.NoSuchDirectoryException; /// <summary> /// DirectoryReader is an implementation of <seealso cref="CompositeReader"/> @@ -315,6 +314,19 @@ namespace Lucene.Net.Index //{ // sis = null; //} + // LUCENENET specific - since NoSuchDirectoryException subclasses FileNotFoundException + // in Lucene, we need to catch it here to be on the safe side. + catch (System.IO.DirectoryNotFoundException) + { + // LUCENE-948: on NFS (and maybe others), if + // you have writers switching back and forth + // between machines, it's very likely that the + // dir listing will be stale and will claim a + // file segments_X exists when in fact it + // doesn't. So, we catch this and handle it + // as if the file does not exist + sis = null; + } if (sis != null) { @@ -356,7 +368,7 @@ namespace Lucene.Net.Index files = directory.ListAll(); } #pragma warning disable 168 - catch (NoSuchDirectoryException nsde) + catch (DirectoryNotFoundException nsde) #pragma warning restore 168 { // Directory does not exist --> no index exists http://git-wip-us.apache.org/repos/asf/lucenenet/blob/0e2de164/src/Lucene.Net.Core/Index/IndexFileDeleter.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Core/Index/IndexFileDeleter.cs b/src/Lucene.Net.Core/Index/IndexFileDeleter.cs index a31b6d7..69a4ff3 100644 --- a/src/Lucene.Net.Core/Index/IndexFileDeleter.cs +++ b/src/Lucene.Net.Core/Index/IndexFileDeleter.cs @@ -29,7 +29,6 @@ namespace Lucene.Net.Index using CollectionUtil = Lucene.Net.Util.CollectionUtil; using Directory = Lucene.Net.Store.Directory; using InfoStream = Lucene.Net.Util.InfoStream; - using NoSuchDirectoryException = Lucene.Net.Store.NoSuchDirectoryException; /* * this class keeps track of each SegmentInfos instance that @@ -149,7 +148,7 @@ namespace Lucene.Net.Index files = directory.ListAll(); } #pragma warning disable 168 - catch (NoSuchDirectoryException e) + catch (DirectoryNotFoundException e) #pragma warning restore 168 { // it means the directory is empty, so ignore it. @@ -214,6 +213,23 @@ namespace Lucene.Net.Index // } // sis = null; //} + // LUCENENET specific - since NoSuchDirectoryException subclasses FileNotFoundException + // in Lucene, we need to catch it here to be on the safe side. + catch (System.IO.DirectoryNotFoundException) + { + // LUCENE-948: on NFS (and maybe others), if + // you have writers switching back and forth + // between machines, it's very likely that the + // dir listing will be stale and will claim a + // file segments_X exists when in fact it + // doesn't. So, we catch this and handle it + // as if the file does not exist + if (infoStream.IsEnabled("IFD")) + { + infoStream.Message("IFD", "init: hit FileNotFoundException when loading commit \"" + fileName + "\"; skipping this commit point"); + } + sis = null; + } catch (IOException e) { if (SegmentInfos.GenerationFromSegmentsFileName(fileName) <= currentGen && directory.FileLength(fileName) > 0) http://git-wip-us.apache.org/repos/asf/lucenenet/blob/0e2de164/src/Lucene.Net.Core/Index/IndexWriter.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Core/Index/IndexWriter.cs b/src/Lucene.Net.Core/Index/IndexWriter.cs index d7d5bd4..5032de0 100644 --- a/src/Lucene.Net.Core/Index/IndexWriter.cs +++ b/src/Lucene.Net.Core/Index/IndexWriter.cs @@ -5934,6 +5934,12 @@ namespace Lucene.Net.Index //{ // return false; //} + // LUCENENET specific - since NoSuchDirectoryException subclasses FileNotFoundException + // in Lucene, we need to catch it here to be on the safe side. + catch (System.IO.DirectoryNotFoundException) + { + return false; + } } } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/0e2de164/src/Lucene.Net.Core/Index/SegmentInfos.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Core/Index/SegmentInfos.cs b/src/Lucene.Net.Core/Index/SegmentInfos.cs index 960dedc..733d3d5 100644 --- a/src/Lucene.Net.Core/Index/SegmentInfos.cs +++ b/src/Lucene.Net.Core/Index/SegmentInfos.cs @@ -38,7 +38,6 @@ namespace Lucene.Net.Index using Lucene3xCodec = Lucene.Net.Codecs.Lucene3x.Lucene3xCodec; using Lucene3xSegmentInfoFormat = Lucene.Net.Codecs.Lucene3x.Lucene3xSegmentInfoFormat; using Lucene3xSegmentInfoReader = Lucene.Net.Codecs.Lucene3x.Lucene3xSegmentInfoReader; - using NoSuchDirectoryException = Lucene.Net.Store.NoSuchDirectoryException; using StringHelper = Lucene.Net.Util.StringHelper; /// <summary> @@ -214,7 +213,7 @@ namespace Lucene.Net.Index { return GetLastCommitGeneration(directory.ListAll()); } - catch (NoSuchDirectoryException) + catch (DirectoryNotFoundException) { return -1; } http://git-wip-us.apache.org/repos/asf/lucenenet/blob/0e2de164/src/Lucene.Net.Core/Store/Directory.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Core/Store/Directory.cs b/src/Lucene.Net.Core/Store/Directory.cs index 8f6f4a7..6fa8b6a 100644 --- a/src/Lucene.Net.Core/Store/Directory.cs +++ b/src/Lucene.Net.Core/Store/Directory.cs @@ -45,7 +45,7 @@ namespace Lucene.Net.Store /// <summary> /// Returns an array of strings, one for each file in the directory. /// </summary> - /// <exception cref="NoSuchDirectoryException"> if the directory is not prepared for any + /// <exception cref="DirectoryNotFoundException"> if the directory is not prepared for any /// write operations (such as <see cref="CreateOutput(string, IOContext)"/>). </exception> /// <exception cref="System.IO.IOException"> in case of other IO errors </exception> public abstract string[] ListAll(); http://git-wip-us.apache.org/repos/asf/lucenenet/blob/0e2de164/src/Lucene.Net.Core/Store/FSDirectory.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Core/Store/FSDirectory.cs b/src/Lucene.Net.Core/Store/FSDirectory.cs index 6c9ce60..b66e535 100644 --- a/src/Lucene.Net.Core/Store/FSDirectory.cs +++ b/src/Lucene.Net.Core/Store/FSDirectory.cs @@ -118,7 +118,7 @@ namespace Lucene.Net.Store if (File.Exists(path.FullName)) { - throw new NoSuchDirectoryException("file '" + path.FullName + "' exists but is not a directory"); + throw new DirectoryNotFoundException("file '" + path.FullName + "' exists but is not a directory"); } SetLockFactory(lockFactory); @@ -223,7 +223,7 @@ namespace Lucene.Net.Store /// directory. This method never returns <c>null</c> (throws /// <seealso cref="IOException"/> instead). /// </summary> - /// <exception cref="NoSuchDirectoryException"> if the directory + /// <exception cref="DirectoryNotFoundException"> if the directory /// does not exist, or does exist but is not a /// directory or is invalid (for example, it is on an unmapped drive). </exception> /// <exception cref="System.Security.SecurityException">The caller does not have the required permission.</exception> @@ -231,11 +231,11 @@ namespace Lucene.Net.Store { if (!System.IO.Directory.Exists(dir.FullName)) { - throw new NoSuchDirectoryException("directory '" + dir + "' does not exist"); + throw new DirectoryNotFoundException("directory '" + dir + "' does not exist"); } else if (File.Exists(dir.FullName)) { - throw new NoSuchDirectoryException("file '" + dir + "' exists but is not a directory"); + throw new DirectoryNotFoundException("file '" + dir + "' exists but is not a directory"); } // Exclude subdirs http://git-wip-us.apache.org/repos/asf/lucenenet/blob/0e2de164/src/Lucene.Net.Core/Store/FileSwitchDirectory.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Core/Store/FileSwitchDirectory.cs b/src/Lucene.Net.Core/Store/FileSwitchDirectory.cs index 4d52a93..3e40945 100644 --- a/src/Lucene.Net.Core/Store/FileSwitchDirectory.cs +++ b/src/Lucene.Net.Core/Store/FileSwitchDirectory.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; namespace Lucene.Net.Store @@ -92,7 +93,7 @@ namespace Lucene.Net.Store // but if one underlying delegate is an FSDir and mkdirs() has not // yet been called, because so far everything is written to the other, // in this case, we don't want to throw a NoSuchDirectoryException - NoSuchDirectoryException exc = null; + DirectoryNotFoundException exc = null; try { foreach (string f in primaryDir.ListAll()) @@ -100,7 +101,7 @@ namespace Lucene.Net.Store files.Add(f); } } - catch (NoSuchDirectoryException e) + catch (DirectoryNotFoundException e) { exc = e; } @@ -112,7 +113,7 @@ namespace Lucene.Net.Store files.Add(f); } } - catch (NoSuchDirectoryException e) + catch (DirectoryNotFoundException e) { // we got NoSuchDirectoryException from both dirs // rethrow the first. http://git-wip-us.apache.org/repos/asf/lucenenet/blob/0e2de164/src/Lucene.Net.Core/Store/NRTCachingDirectory.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Core/Store/NRTCachingDirectory.cs b/src/Lucene.Net.Core/Store/NRTCachingDirectory.cs index 3d42334..8a603a3 100644 --- a/src/Lucene.Net.Core/Store/NRTCachingDirectory.cs +++ b/src/Lucene.Net.Core/Store/NRTCachingDirectory.cs @@ -149,7 +149,7 @@ namespace Lucene.Net.Store files.Add(f); } } - catch (NoSuchDirectoryException ex) + catch (DirectoryNotFoundException ex) { // however, if there are no cached files, then the directory truly // does not "exist" http://git-wip-us.apache.org/repos/asf/lucenenet/blob/0e2de164/src/Lucene.Net.Core/Store/NoSuchDirectoryException.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Core/Store/NoSuchDirectoryException.cs b/src/Lucene.Net.Core/Store/NoSuchDirectoryException.cs index 2b91bb4..a75c4dc 100644 --- a/src/Lucene.Net.Core/Store/NoSuchDirectoryException.cs +++ b/src/Lucene.Net.Core/Store/NoSuchDirectoryException.cs @@ -1,38 +1,42 @@ -using System; -using System.IO; +// LUCENENET specific - excluding this class in favor of System.IO.DirectoryNotFoundException, +// although that means we need to catch System.IO.DirectoryNotFoundException everywhere that +// FileNotFoundException is being caught (because it is a superclass) to be sure we have the same behavior. -namespace Lucene.Net.Store -{ - /* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +//using System; +//using System.IO; - /// <summary> - /// This exception is thrown when you try to list a - /// non-existent directory. - /// </summary> - // LUCENENET: All exeption classes should be marked serializable -#if FEATURE_SERIALIZABLE - [Serializable] -#endif - public class NoSuchDirectoryException : FileNotFoundException // LUCENENET TODO: API Replace with System.IO.DirectoryNotFoundException ? Note that it does not subclass FileNotFoundException - { - public NoSuchDirectoryException(string message) - : base(message) - { - } - } -} \ No newline at end of file +//namespace Lucene.Net.Store +//{ +// /* +// * Licensed to the Apache Software Foundation (ASF) under one or more +// * contributor license agreements. See the NOTICE file distributed with +// * this work for additional information regarding copyright ownership. +// * The ASF licenses this file to You under the Apache License, Version 2.0 +// * (the "License"); you may not use this file except in compliance with +// * the License. You may obtain a copy of the License at +// * +// * http://www.apache.org/licenses/LICENSE-2.0 +// * +// * Unless required by applicable law or agreed to in writing, software +// * distributed under the License is distributed on an "AS IS" BASIS, +// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// * See the License for the specific language governing permissions and +// * limitations under the License. +// */ + +// /// <summary> +// /// This exception is thrown when you try to list a +// /// non-existent directory. +// /// </summary> +// // LUCENENET: All exeption classes should be marked serializable +//#if FEATURE_SERIALIZABLE +// [Serializable] +//#endif +// public class NoSuchDirectoryException : FileNotFoundException +// { +// public NoSuchDirectoryException(string message) +// : base(message) +// { +// } +// } +//} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/0e2de164/src/Lucene.Net.TestFramework/Util/LuceneTestCase.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.TestFramework/Util/LuceneTestCase.cs b/src/Lucene.Net.TestFramework/Util/LuceneTestCase.cs index 1772485..b0a1a49 100644 --- a/src/Lucene.Net.TestFramework/Util/LuceneTestCase.cs +++ b/src/Lucene.Net.TestFramework/Util/LuceneTestCase.cs @@ -2658,6 +2658,12 @@ namespace Lucene.Net.Util //{ // return false; //} + // LUCENENET specific - since NoSuchDirectoryException subclasses FileNotFoundException + // in Lucene, we need to catch it here to be on the safe side. + catch (System.IO.DirectoryNotFoundException) + { + return false; + } } /// <summary> http://git-wip-us.apache.org/repos/asf/lucenenet/blob/0e2de164/src/Lucene.Net.Tests/Index/TestDirectoryReader.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Tests/Index/TestDirectoryReader.cs b/src/Lucene.Net.Tests/Index/TestDirectoryReader.cs index 25cbad9..73f7bb4 100644 --- a/src/Lucene.Net.Tests/Index/TestDirectoryReader.cs +++ b/src/Lucene.Net.Tests/Index/TestDirectoryReader.cs @@ -38,7 +38,6 @@ namespace Lucene.Net.Index */ using MockAnalyzer = Lucene.Net.Analysis.MockAnalyzer; - using NoSuchDirectoryException = Lucene.Net.Store.NoSuchDirectoryException; using StoredField = StoredField; using StringField = StringField; using TestUtil = Lucene.Net.Util.TestUtil; @@ -771,7 +770,7 @@ namespace Lucene.Net.Index Assert.Fail("did not hit expected exception"); } #pragma warning disable 168 - catch (NoSuchDirectoryException nsde) + catch (DirectoryNotFoundException nsde) #pragma warning restore 168 { // expected http://git-wip-us.apache.org/repos/asf/lucenenet/blob/0e2de164/src/Lucene.Net.Tests/Index/TestIndexWriterExceptions.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Tests/Index/TestIndexWriterExceptions.cs b/src/Lucene.Net.Tests/Index/TestIndexWriterExceptions.cs index dd6cbb5..ed5587f 100644 --- a/src/Lucene.Net.Tests/Index/TestIndexWriterExceptions.cs +++ b/src/Lucene.Net.Tests/Index/TestIndexWriterExceptions.cs @@ -2083,6 +2083,12 @@ namespace Lucene.Net.Index { continue; } + // LUCENENET specific - since NoSuchDirectoryException subclasses FileNotFoundException + // in Lucene, we need to catch it here to be on the safe side. + catch (System.IO.DirectoryNotFoundException) + { + continue; + } failure.ClearDoFail(); iw.Dispose(); ir = DirectoryReader.Open(dir); http://git-wip-us.apache.org/repos/asf/lucenenet/blob/0e2de164/src/Lucene.Net.Tests/Index/TestIndexWriterLockRelease.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Tests/Index/TestIndexWriterLockRelease.cs b/src/Lucene.Net.Tests/Index/TestIndexWriterLockRelease.cs index 06a3e52..a228a82 100644 --- a/src/Lucene.Net.Tests/Index/TestIndexWriterLockRelease.cs +++ b/src/Lucene.Net.Tests/Index/TestIndexWriterLockRelease.cs @@ -55,6 +55,30 @@ namespace Lucene.Net.Index #pragma warning restore 168 { } + // LUCENENET specific - since NoSuchDirectoryException subclasses FileNotFoundException + // in Lucene, we need to catch it here to be on the safe side. + catch (System.IO.DirectoryNotFoundException) + { + } + } + // LUCENENET specific - since NoSuchDirectoryException subclasses FileNotFoundException + // in Lucene, we need to catch it here to be on the safe side. + catch (System.IO.DirectoryNotFoundException) + { + try + { + new IndexWriter(dir, (new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random()))).SetOpenMode(OpenMode.APPEND)); + } +#pragma warning disable 168 + catch (FileNotFoundException /*| NoSuchFileException*/ e1) +#pragma warning restore 168 + { + } + // LUCENENET specific - since NoSuchDirectoryException subclasses FileNotFoundException + // in Lucene, we need to catch it here to be on the safe side. + catch (System.IO.DirectoryNotFoundException) + { + } } finally { http://git-wip-us.apache.org/repos/asf/lucenenet/blob/0e2de164/src/Lucene.Net.Tests/Store/TestDirectory.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Tests/Store/TestDirectory.cs b/src/Lucene.Net.Tests/Store/TestDirectory.cs index c8cbb34..735a5c5 100644 --- a/src/Lucene.Net.Tests/Store/TestDirectory.cs +++ b/src/Lucene.Net.Tests/Store/TestDirectory.cs @@ -146,6 +146,11 @@ namespace Lucene.Net.Store #pragma warning restore 168 { } + // LUCENENET specific - since NoSuchDirectoryException subclasses FileNotFoundException + // in Lucene, we need to catch it here to be on the safe side. + catch (System.IO.DirectoryNotFoundException) + { + } catch (IOException e) { if (!e.Message.Contains("still open for writing")) @@ -355,7 +360,7 @@ namespace Lucene.Net.Store Assert.Fail("did not hit expected exception"); } #pragma warning disable 168 - catch (NoSuchDirectoryException nsde) + catch (DirectoryNotFoundException nsde) #pragma warning restore 168 { // Expected @@ -406,6 +411,12 @@ namespace Lucene.Net.Store { // ok } + // LUCENENET specific - since NoSuchDirectoryException subclasses FileNotFoundException + // in Lucene, we need to catch it here to be on the safe side. + catch (System.IO.DirectoryNotFoundException) + { + // ok + } // directory is still empty Assert.AreEqual(0, fsdir.ListAll().Length); http://git-wip-us.apache.org/repos/asf/lucenenet/blob/0e2de164/src/Lucene.Net.Tests/Store/TestFileSwitchDirectory.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Tests/Store/TestFileSwitchDirectory.cs b/src/Lucene.Net.Tests/Store/TestFileSwitchDirectory.cs index 3eb0de7..9aebdf4 100644 --- a/src/Lucene.Net.Tests/Store/TestFileSwitchDirectory.cs +++ b/src/Lucene.Net.Tests/Store/TestFileSwitchDirectory.cs @@ -120,7 +120,7 @@ namespace Lucene.Net.Store DirectoryReader.Open(dir); Assert.Fail("did not hit expected exception"); } - catch (NoSuchDirectoryException) + catch (DirectoryNotFoundException) { // expected } http://git-wip-us.apache.org/repos/asf/lucenenet/blob/0e2de164/src/Lucene.Net.Tests/Store/TestNRTCachingDirectory.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Tests/Store/TestNRTCachingDirectory.cs b/src/Lucene.Net.Tests/Store/TestNRTCachingDirectory.cs index 806295b..0d7aba1 100644 --- a/src/Lucene.Net.Tests/Store/TestNRTCachingDirectory.cs +++ b/src/Lucene.Net.Tests/Store/TestNRTCachingDirectory.cs @@ -151,7 +151,7 @@ namespace Lucene.Net.Store DirectoryReader.Open(dir); Assert.Fail("did not hit expected exception"); } - catch (NoSuchDirectoryException) + catch (DirectoryNotFoundException) { // expected }
