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();
}
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();
}
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();
}
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);
}
}
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();
}
Aggregations