use of java.nio.file.NoSuchFileException in project lucene-solr by apache.
the class MockDirectoryWrapper method maybeThrowIOExceptionOnOpen.
void maybeThrowIOExceptionOnOpen(String name) throws IOException {
if (randomState.nextDouble() < randomIOExceptionRateOnOpen) {
if (LuceneTestCase.VERBOSE) {
System.out.println(Thread.currentThread().getName() + ": MockDirectoryWrapper: now throw random exception during open file=" + name);
new Throwable().printStackTrace(System.out);
}
if (allowRandomFileNotFoundException == false || randomState.nextBoolean()) {
throw new IOException("a random IOException (" + name + ")");
} else {
throw randomState.nextBoolean() ? new FileNotFoundException("a random IOException (" + name + ")") : new NoSuchFileException("a random IOException (" + name + ")");
}
}
}
use of java.nio.file.NoSuchFileException in project lucene-solr by apache.
the class TestMockDirectoryWrapper method testCorruptOnCloseIsWorking.
private void testCorruptOnCloseIsWorking(Directory dir) throws Exception {
dir = new PreventCloseDirectoryWrapper(dir);
try (MockDirectoryWrapper wrapped = new MockDirectoryWrapper(random(), dir)) {
// otherwise MDW sometimes randomly leaves the file intact and we'll see false test failures:
wrapped.alwaysCorrupt = true;
// MDW will only try to corrupt things if it sees an index:
RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
iw.addDocument(new Document());
iw.close();
// not sync'd!
try (IndexOutput out = wrapped.createOutput("foo", IOContext.DEFAULT)) {
for (int i = 0; i < 100; i++) {
out.writeInt(i);
}
}
// MDW.close now corrupts our unsync'd file (foo):
}
boolean changed = false;
IndexInput in = null;
try {
in = dir.openInput("foo", IOContext.DEFAULT);
} catch (NoSuchFileException | FileNotFoundException fnfe) {
// ok
changed = true;
}
if (in != null) {
for (int i = 0; i < 100; i++) {
int x;
try {
x = in.readInt();
} catch (EOFException eofe) {
changed = true;
break;
}
if (x != i) {
changed = true;
break;
}
}
in.close();
}
assertTrue("MockDirectoryWrapper on dir=" + dir + " failed to corrupt an unsync'd file", changed);
}
use of java.nio.file.NoSuchFileException in project lucene-solr by apache.
the class Node method readLocalFileMetaData.
/** Opens the specified file, reads its identifying information, including file length, full index header (includes the unique segment
* ID) and the full footer (includes checksum), and returns the resulting {@link FileMetaData}.
*
* <p>This returns null, logging a message, if there are any problems (the file does not exist, is corrupt, truncated, etc.).</p> */
public FileMetaData readLocalFileMetaData(String fileName) throws IOException {
Map<String, FileMetaData> cache = lastFileMetaData;
FileMetaData result;
if (cache != null) {
// We may already have this file cached from the last NRT point:
result = cache.get(fileName);
} else {
result = null;
}
if (result == null) {
// Pull from the filesystem
long checksum;
long length;
byte[] header;
byte[] footer;
try (IndexInput in = dir.openInput(fileName, IOContext.DEFAULT)) {
try {
length = in.length();
header = CodecUtil.readIndexHeader(in);
footer = CodecUtil.readFooter(in);
checksum = CodecUtil.retrieveChecksum(in);
} catch (EOFException | CorruptIndexException cie) {
// to delete such unreferenced files, but virus checker can block that, leaving this bad file.
if (VERBOSE_FILES) {
message("file " + fileName + ": will copy [existing file is corrupt]");
}
return null;
}
if (VERBOSE_FILES) {
message("file " + fileName + " has length=" + bytesToString(length));
}
} catch (FileNotFoundException | NoSuchFileException e) {
if (VERBOSE_FILES) {
message("file " + fileName + ": will copy [file does not exist]");
}
return null;
}
// NOTE: checksum is redundant w/ footer, but we break it out separately because when the bits cross the wire we need direct access to
// checksum when copying to catch bit flips:
result = new FileMetaData(header, footer, length, checksum);
}
return result;
}
use of java.nio.file.NoSuchFileException in project lucene-solr by apache.
the class FileSwitchDirectory method listAll.
@Override
public String[] listAll() throws IOException {
Set<String> files = new HashSet<>();
// LUCENE-3380: either or both of our dirs could be FSDirs,
// 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 NoSuchFileException
NoSuchFileException exc = null;
try {
for (String f : primaryDir.listAll()) {
files.add(f);
}
} catch (NoSuchFileException e) {
exc = e;
}
try {
for (String f : secondaryDir.listAll()) {
files.add(f);
}
} catch (NoSuchFileException e) {
// rethrow the first.
if (exc != null) {
throw exc;
}
// and the primary is empty.
if (files.isEmpty()) {
throw e;
}
}
// and the secondary is empty.
if (exc != null && files.isEmpty()) {
throw exc;
}
String[] result = files.toArray(new String[files.size()]);
Arrays.sort(result);
return result;
}
use of java.nio.file.NoSuchFileException in project lucene-solr by apache.
the class TestIndexWriter method testLeftoverTempFiles.
public void testLeftoverTempFiles() throws Exception {
Directory dir = newDirectory();
IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
IndexWriter w = new IndexWriter(dir, iwc);
w.close();
IndexOutput out = dir.createTempOutput("_0", "bkd", IOContext.DEFAULT);
String tempName = out.getName();
out.close();
iwc = new IndexWriterConfig(new MockAnalyzer(random()));
w = new IndexWriter(dir, iwc);
// Make sure IW deleted the unref'd file:
try {
dir.openInput(tempName, IOContext.DEFAULT);
fail("did not hit exception");
} catch (FileNotFoundException | NoSuchFileException e) {
// expected
}
w.close();
dir.close();
}
Aggregations