use of org.apache.lucene.store.RAMOutputStream in project lucene-solr by apache.
the class TestCodecUtil method testCheckFooterValid.
public void testCheckFooterValid() throws Exception {
RAMFile file = new RAMFile();
IndexOutput output = new RAMOutputStream(file, true);
CodecUtil.writeHeader(output, "FooBar", 5);
output.writeString("this is the data");
CodecUtil.writeFooter(output);
output.close();
ChecksumIndexInput input = new BufferedChecksumIndexInput(new RAMInputStream("file", file));
Exception mine = new RuntimeException("fake exception");
RuntimeException expected = expectThrows(RuntimeException.class, () -> {
CodecUtil.checkFooter(input, mine);
});
assertEquals("fake exception", expected.getMessage());
Throwable[] suppressed = expected.getSuppressed();
assertEquals(1, suppressed.length);
assertTrue(suppressed[0].getMessage().contains("checksum passed"));
input.close();
}
use of org.apache.lucene.store.RAMOutputStream in project lucene-solr by apache.
the class TestCodecUtil method testWriteTooLongSuffix.
public void testWriteTooLongSuffix() throws Exception {
StringBuilder tooLong = new StringBuilder();
for (int i = 0; i < 256; i++) {
tooLong.append('a');
}
RAMFile file = new RAMFile();
IndexOutput output = new RAMOutputStream(file, true);
expectThrows(IllegalArgumentException.class, () -> {
CodecUtil.writeIndexHeader(output, "foobar", 5, StringHelper.randomId(), tooLong.toString());
});
}
use of org.apache.lucene.store.RAMOutputStream in project lucene-solr by apache.
the class PrimaryNode method setCurrentInfos.
/** Steals incoming infos refCount; returns true if there were changes. */
private synchronized boolean setCurrentInfos(Set<String> completedMergeFiles) throws IOException {
IndexSearcher searcher = null;
SegmentInfos infos;
try {
searcher = mgr.acquire();
infos = ((StandardDirectoryReader) searcher.getIndexReader()).getSegmentInfos();
// TODO: this is test code specific!!
message("setCurrentInfos: marker count: " + searcher.count(new TermQuery(new Term("marker", "marker"))) + " version=" + infos.getVersion() + " searcher=" + searcher);
} finally {
if (searcher != null) {
mgr.release(searcher);
}
}
if (curInfos != null && infos.getVersion() == curInfos.getVersion()) {
// no change
message("top: skip switch to infos: version=" + infos.getVersion() + " is unchanged: " + infos.toString());
return false;
}
SegmentInfos oldInfos = curInfos;
writer.incRefDeleter(infos);
curInfos = infos;
if (oldInfos != null) {
writer.decRefDeleter(oldInfos);
}
message("top: switch to infos=" + infos.toString() + " version=" + infos.getVersion());
// Serialize the SegmentInfos:
RAMOutputStream out = new RAMOutputStream(new RAMFile(), true);
infos.write(dir, out);
byte[] infosBytes = new byte[(int) out.getFilePointer()];
out.writeTo(infosBytes, 0);
Map<String, FileMetaData> filesMetaData = new HashMap<String, FileMetaData>();
for (SegmentCommitInfo info : infos) {
for (String fileName : info.files()) {
FileMetaData metaData = readLocalFileMetaData(fileName);
// NOTE: we hold a refCount on this infos, so this file better exist:
assert metaData != null;
assert filesMetaData.containsKey(fileName) == false;
filesMetaData.put(fileName, metaData);
}
}
lastFileMetaData = Collections.unmodifiableMap(filesMetaData);
message("top: set copyState primaryGen=" + primaryGen + " version=" + infos.getVersion() + " files=" + filesMetaData.keySet());
copyState = new CopyState(lastFileMetaData, infos.getVersion(), infos.getGeneration(), infosBytes, completedMergeFiles, primaryGen, curInfos);
return true;
}
use of org.apache.lucene.store.RAMOutputStream in project lucene-solr by apache.
the class TestCodecUtil method testWriteBogusCRC.
public void testWriteBogusCRC() throws Exception {
RAMFile file = new RAMFile();
final IndexOutput output = new RAMOutputStream(file, false);
AtomicLong fakeChecksum = new AtomicLong();
// wrap the index input where we control the checksum for mocking
IndexOutput fakeOutput = new IndexOutput("fake", "fake") {
@Override
public void close() throws IOException {
output.close();
}
@Override
public long getFilePointer() {
return output.getFilePointer();
}
@Override
public long getChecksum() throws IOException {
return fakeChecksum.get();
}
@Override
public void writeByte(byte b) throws IOException {
output.writeByte(b);
}
@Override
public void writeBytes(byte[] b, int offset, int length) throws IOException {
output.writeBytes(b, offset, length);
}
};
// bad
fakeChecksum.set(-1L);
expectThrows(IllegalStateException.class, () -> {
CodecUtil.writeCRC(fakeOutput);
});
// bad
fakeChecksum.set(1L << 32);
expectThrows(IllegalStateException.class, () -> {
CodecUtil.writeCRC(fakeOutput);
});
// bad
fakeChecksum.set(-(1L << 32));
expectThrows(IllegalStateException.class, () -> {
CodecUtil.writeCRC(fakeOutput);
});
// ok
fakeChecksum.set((1L << 32) - 1);
CodecUtil.writeCRC(fakeOutput);
}
use of org.apache.lucene.store.RAMOutputStream in project lucene-solr by apache.
the class TestCodecUtil method testHeaderLength.
public void testHeaderLength() throws Exception {
RAMFile file = new RAMFile();
IndexOutput output = new RAMOutputStream(file, true);
CodecUtil.writeHeader(output, "FooBar", 5);
output.writeString("this is the data");
output.close();
IndexInput input = new RAMInputStream("file", file);
input.seek(CodecUtil.headerLength("FooBar"));
assertEquals("this is the data", input.readString());
input.close();
}
Aggregations