Search in sources :

Example 16 with IndexOutput

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

the class BaseCompoundFormatTestCase method testManySubFiles.

// Make sure we don't somehow use more than 1 descriptor
// when reading a CFS with many subs:
public void testManySubFiles() throws IOException {
    final MockDirectoryWrapper dir = newMockFSDirectory(createTempDir("CFSManySubFiles"));
    final int FILE_COUNT = atLeast(500);
    List<String> files = new ArrayList<>();
    SegmentInfo si = newSegmentInfo(dir, "_123");
    for (int fileIdx = 0; fileIdx < FILE_COUNT; fileIdx++) {
        String file = "_123." + fileIdx;
        files.add(file);
        try (IndexOutput out = dir.createOutput(file, newIOContext(random()))) {
            CodecUtil.writeIndexHeader(out, "Foo", 0, si.getId(), "suffix");
            out.writeByte((byte) fileIdx);
            CodecUtil.writeFooter(out);
        }
    }
    assertEquals(0, dir.getFileHandleCount());
    si.setFiles(files);
    si.getCodec().compoundFormat().write(dir, si, IOContext.DEFAULT);
    Directory cfs = si.getCodec().compoundFormat().getCompoundReader(dir, si, IOContext.DEFAULT);
    final IndexInput[] ins = new IndexInput[FILE_COUNT];
    for (int fileIdx = 0; fileIdx < FILE_COUNT; fileIdx++) {
        ins[fileIdx] = cfs.openInput("_123." + fileIdx, newIOContext(random()));
        CodecUtil.checkIndexHeader(ins[fileIdx], "Foo", 0, 0, si.getId(), "suffix");
    }
    assertEquals(1, dir.getFileHandleCount());
    for (int fileIdx = 0; fileIdx < FILE_COUNT; fileIdx++) {
        assertEquals((byte) fileIdx, ins[fileIdx].readByte());
    }
    assertEquals(1, dir.getFileHandleCount());
    for (int fileIdx = 0; fileIdx < FILE_COUNT; fileIdx++) {
        ins[fileIdx].close();
    }
    cfs.close();
    dir.close();
}
Also used : MockDirectoryWrapper(org.apache.lucene.store.MockDirectoryWrapper) ArrayList(java.util.ArrayList) IndexInput(org.apache.lucene.store.IndexInput) IndexOutput(org.apache.lucene.store.IndexOutput) FilterDirectory(org.apache.lucene.store.FilterDirectory) Directory(org.apache.lucene.store.Directory) NRTCachingDirectory(org.apache.lucene.store.NRTCachingDirectory)

Example 17 with IndexOutput

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

the class BaseCompoundFormatTestCase method testMissingCodecHeadersAreCaught.

public void testMissingCodecHeadersAreCaught() throws Exception {
    Directory dir = newDirectory();
    String subFile = "_123.xyz";
    // missing codec header
    try (IndexOutput os = dir.createOutput(subFile, newIOContext(random()))) {
        for (int i = 0; i < 1024; i++) {
            os.writeByte((byte) i);
        }
    }
    SegmentInfo si = newSegmentInfo(dir, "_123");
    si.setFiles(Collections.singletonList(subFile));
    Exception e = expectThrows(CorruptIndexException.class, () -> si.getCodec().compoundFormat().write(dir, si, IOContext.DEFAULT));
    assertTrue(e.getMessage().contains("codec header mismatch"));
    dir.close();
}
Also used : IndexOutput(org.apache.lucene.store.IndexOutput) IOException(java.io.IOException) FilterDirectory(org.apache.lucene.store.FilterDirectory) Directory(org.apache.lucene.store.Directory) NRTCachingDirectory(org.apache.lucene.store.NRTCachingDirectory)

Example 18 with IndexOutput

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

the class BaseCompoundFormatTestCase method testCorruptFilesAreCaught.

public void testCorruptFilesAreCaught() throws Exception {
    Directory dir = newDirectory();
    String subFile = "_123.xyz";
    // wrong checksum
    SegmentInfo si = newSegmentInfo(dir, "_123");
    try (IndexOutput os = dir.createOutput(subFile, newIOContext(random()))) {
        CodecUtil.writeIndexHeader(os, "Foo", 0, si.getId(), "suffix");
        for (int i = 0; i < 1024; i++) {
            os.writeByte((byte) i);
        }
        // write footer w/ wrong checksum
        os.writeInt(CodecUtil.FOOTER_MAGIC);
        os.writeInt(0);
        long checksum = os.getChecksum();
        os.writeLong(checksum + 1);
    }
    si.setFiles(Collections.singletonList(subFile));
    Exception e = expectThrows(CorruptIndexException.class, () -> si.getCodec().compoundFormat().write(dir, si, IOContext.DEFAULT));
    assertTrue(e.getMessage().contains("checksum failed (hardware problem?)"));
    dir.close();
}
Also used : IndexOutput(org.apache.lucene.store.IndexOutput) IOException(java.io.IOException) FilterDirectory(org.apache.lucene.store.FilterDirectory) Directory(org.apache.lucene.store.Directory) NRTCachingDirectory(org.apache.lucene.store.NRTCachingDirectory)

Example 19 with IndexOutput

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

the class BaseCompoundFormatTestCase method createSequenceFile.

/** Creates a file of the specified size with sequential data. The first
   *  byte is written as the start byte provided. All subsequent bytes are
   *  computed as start + offset where offset is the number of the byte.
   */
protected static void createSequenceFile(Directory dir, String name, byte start, int size, byte[] segID, String segSuffix) throws IOException {
    try (IndexOutput os = dir.createOutput(name, newIOContext(random()))) {
        CodecUtil.writeIndexHeader(os, "Foo", 0, segID, segSuffix);
        for (int i = 0; i < size; i++) {
            os.writeByte(start);
            start++;
        }
        CodecUtil.writeFooter(os);
    }
}
Also used : IndexOutput(org.apache.lucene.store.IndexOutput)

Example 20 with IndexOutput

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

the class BaseCompoundFormatTestCase method testSyncDisabled.

// test that cfs reader is read-only
public void testSyncDisabled() throws IOException {
    final String testfile = "_123.test";
    Directory dir = newDirectory();
    IndexOutput out = dir.createOutput(testfile, IOContext.DEFAULT);
    out.writeInt(3);
    out.close();
    SegmentInfo si = newSegmentInfo(dir, "_123");
    si.setFiles(Collections.emptyList());
    si.getCodec().compoundFormat().write(dir, si, IOContext.DEFAULT);
    Directory cfs = si.getCodec().compoundFormat().getCompoundReader(dir, si, IOContext.DEFAULT);
    expectThrows(UnsupportedOperationException.class, () -> {
        cfs.sync(Collections.singleton(testfile));
    });
    cfs.close();
    dir.close();
}
Also used : IndexOutput(org.apache.lucene.store.IndexOutput) FilterDirectory(org.apache.lucene.store.FilterDirectory) Directory(org.apache.lucene.store.Directory) NRTCachingDirectory(org.apache.lucene.store.NRTCachingDirectory)

Aggregations

IndexOutput (org.apache.lucene.store.IndexOutput)182 Directory (org.apache.lucene.store.Directory)79 IndexInput (org.apache.lucene.store.IndexInput)76 RAMDirectory (org.apache.lucene.store.RAMDirectory)36 FilterDirectory (org.apache.lucene.store.FilterDirectory)34 CorruptIndexException (org.apache.lucene.index.CorruptIndexException)27 ChecksumIndexInput (org.apache.lucene.store.ChecksumIndexInput)27 BytesRef (org.apache.lucene.util.BytesRef)26 IOException (java.io.IOException)20 CorruptingIndexOutput (org.apache.lucene.store.CorruptingIndexOutput)18 RAMFile (org.apache.lucene.store.RAMFile)16 RAMOutputStream (org.apache.lucene.store.RAMOutputStream)16 IndexFormatTooNewException (org.apache.lucene.index.IndexFormatTooNewException)14 IndexFormatTooOldException (org.apache.lucene.index.IndexFormatTooOldException)14 IOContext (org.apache.lucene.store.IOContext)13 ArrayList (java.util.ArrayList)11 BufferedChecksumIndexInput (org.apache.lucene.store.BufferedChecksumIndexInput)11 RAMInputStream (org.apache.lucene.store.RAMInputStream)11 NIOFSDirectory (org.apache.lucene.store.NIOFSDirectory)10 NRTCachingDirectory (org.apache.lucene.store.NRTCachingDirectory)10