Search in sources :

Example 46 with IndexInput

use of org.apache.lucene.store.IndexInput in project lucene-solr by apache.

the class HdfsDirectoryTest method testEof.

private void testEof(String name, Directory directory, long length) throws IOException {
    IndexInput input = directory.openInput(name, new IOContext());
    input.seek(length);
    try {
        input.readByte();
        fail("should throw eof");
    } catch (IOException e) {
    }
}
Also used : IndexInput(org.apache.lucene.store.IndexInput) IOContext(org.apache.lucene.store.IOContext) IOException(java.io.IOException)

Example 47 with IndexInput

use of org.apache.lucene.store.IndexInput in project lucene-solr by apache.

the class HdfsDirectoryTest method testRename.

public void testRename() throws IOException {
    String[] listAll = directory.listAll();
    for (String file : listAll) {
        directory.deleteFile(file);
    }
    IndexOutput output = directory.createOutput("testing.test", new IOContext());
    output.writeInt(12345);
    output.close();
    directory.rename("testing.test", "testing.test.renamed");
    assertFalse(slowFileExists(directory, "testing.test"));
    assertTrue(slowFileExists(directory, "testing.test.renamed"));
    IndexInput input = directory.openInput("testing.test.renamed", new IOContext());
    assertEquals(12345, input.readInt());
    assertEquals(input.getFilePointer(), input.length());
    input.close();
    directory.deleteFile("testing.test.renamed");
    assertFalse(slowFileExists(directory, "testing.test.renamed"));
}
Also used : IOContext(org.apache.lucene.store.IOContext) IndexInput(org.apache.lucene.store.IndexInput) IndexOutput(org.apache.lucene.store.IndexOutput)

Example 48 with IndexInput

use of org.apache.lucene.store.IndexInput in project lucene-solr by apache.

the class BlockDirectoryTest method testEof.

private void testEof(String name, Directory directory, long length) throws IOException {
    IndexInput input = directory.openInput(name, new IOContext());
    try {
        input.seek(length);
        try {
            input.readByte();
            fail("should throw eof");
        } catch (IOException e) {
        }
    } finally {
        input.close();
    }
}
Also used : IndexInput(org.apache.lucene.store.IndexInput) IOContext(org.apache.lucene.store.IOContext) IOException(java.io.IOException)

Example 49 with IndexInput

use of org.apache.lucene.store.IndexInput in project lucene-solr by apache.

the class TestAllFilesDetectTruncation method truncateOneFile.

private void truncateOneFile(Directory dir, String victim) throws IOException {
    try (BaseDirectoryWrapper dirCopy = newDirectory()) {
        dirCopy.setCheckIndexOnClose(false);
        long victimLength = dir.fileLength(victim);
        int lostBytes = TestUtil.nextInt(random(), 1, (int) Math.min(100, victimLength));
        assert victimLength > 0;
        if (VERBOSE) {
            System.out.println("TEST: now truncate file " + victim + " by removing " + lostBytes + " of " + victimLength + " bytes");
        }
        for (String name : dir.listAll()) {
            if (name.equals(victim) == false) {
                dirCopy.copyFrom(dir, name, name, IOContext.DEFAULT);
            } else {
                try (IndexOutput out = dirCopy.createOutput(name, IOContext.DEFAULT);
                    IndexInput in = dir.openInput(name, IOContext.DEFAULT)) {
                    out.copyBytes(in, victimLength - lostBytes);
                }
            }
            dirCopy.sync(Collections.singleton(name));
        }
        try {
            // NOTE: we .close so that if the test fails (truncation not detected) we don't also get all these confusing errors about open files:
            DirectoryReader.open(dirCopy).close();
            fail("truncation not detected after removing " + lostBytes + " bytes out of " + victimLength + " for file " + victim);
        } catch (CorruptIndexException | EOFException e) {
        // expected
        }
        // CheckIndex should also fail:
        try {
            TestUtil.checkIndex(dirCopy, true, true, null);
            fail("truncation not detected after removing " + lostBytes + " bytes out of " + victimLength + " for file " + victim);
        } catch (CorruptIndexException | EOFException e) {
        // expected
        }
    }
}
Also used : EOFException(java.io.EOFException) BaseDirectoryWrapper(org.apache.lucene.store.BaseDirectoryWrapper) IndexInput(org.apache.lucene.store.IndexInput) IndexOutput(org.apache.lucene.store.IndexOutput)

Example 50 with IndexInput

use of org.apache.lucene.store.IndexInput in project lucene-solr by apache.

the class TestIndexWriterExceptions method testSegmentsChecksumError.

// LUCENE-1044: Simulate checksum error in segments_N
public void testSegmentsChecksumError() throws IOException {
    BaseDirectoryWrapper dir = newDirectory();
    // we corrupt the index
    dir.setCheckIndexOnClose(false);
    IndexWriter writer = null;
    writer = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())));
    // add 100 documents
    for (int i = 0; i < 100; i++) {
        addDoc(writer);
    }
    // close
    writer.close();
    long gen = SegmentInfos.getLastCommitGeneration(dir);
    assertTrue("segment generation should be > 0 but got " + gen, gen > 0);
    final String segmentsFileName = SegmentInfos.getLastCommitSegmentsFileName(dir);
    IndexInput in = dir.openInput(segmentsFileName, newIOContext(random()));
    IndexOutput out = dir.createOutput(IndexFileNames.fileNameFromGeneration(IndexFileNames.SEGMENTS, "", 1 + gen), newIOContext(random()));
    out.copyBytes(in, in.length() - 1);
    byte b = in.readByte();
    out.writeByte((byte) (1 + b));
    out.close();
    in.close();
    expectThrows(CorruptIndexException.class, () -> {
        DirectoryReader.open(dir);
    });
    dir.close();
}
Also used : MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) BaseDirectoryWrapper(org.apache.lucene.store.BaseDirectoryWrapper) IndexInput(org.apache.lucene.store.IndexInput) IndexOutput(org.apache.lucene.store.IndexOutput)

Aggregations

IndexInput (org.apache.lucene.store.IndexInput)150 IndexOutput (org.apache.lucene.store.IndexOutput)69 Directory (org.apache.lucene.store.Directory)62 ChecksumIndexInput (org.apache.lucene.store.ChecksumIndexInput)41 IOException (java.io.IOException)21 RAMDirectory (org.apache.lucene.store.RAMDirectory)21 FilterDirectory (org.apache.lucene.store.FilterDirectory)19 BufferedChecksumIndexInput (org.apache.lucene.store.BufferedChecksumIndexInput)17 CorruptIndexException (org.apache.lucene.index.CorruptIndexException)14 ArrayList (java.util.ArrayList)13 BytesRef (org.apache.lucene.util.BytesRef)13 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)13 CorruptingIndexOutput (org.apache.lucene.store.CorruptingIndexOutput)10 IOContext (org.apache.lucene.store.IOContext)10 NRTCachingDirectory (org.apache.lucene.store.NRTCachingDirectory)10 IntersectVisitor (org.apache.lucene.index.PointValues.IntersectVisitor)9 Relation (org.apache.lucene.index.PointValues.Relation)9 Test (org.junit.Test)8 FileNotFoundException (java.io.FileNotFoundException)7 BaseDirectoryWrapper (org.apache.lucene.store.BaseDirectoryWrapper)7