use of org.apache.lucene.store.BaseDirectoryWrapper in project elasticsearch by elastic.
the class StoreTests method testCanOpenIndex.
public void testCanOpenIndex() throws IOException {
final ShardId shardId = new ShardId("index", "_na_", 1);
IndexWriterConfig iwc = newIndexWriterConfig();
Path tempDir = createTempDir();
final BaseDirectoryWrapper dir = newFSDirectory(tempDir);
assertFalse(Store.canOpenIndex(logger, tempDir, shardId, (id, l) -> new DummyShardLock(id)));
IndexWriter writer = new IndexWriter(dir, iwc);
Document doc = new Document();
doc.add(new StringField("id", "1", random().nextBoolean() ? Field.Store.YES : Field.Store.NO));
writer.addDocument(doc);
writer.commit();
writer.close();
assertTrue(Store.canOpenIndex(logger, tempDir, shardId, (id, l) -> new DummyShardLock(id)));
DirectoryService directoryService = new DirectoryService(shardId, INDEX_SETTINGS) {
@Override
public Directory newDirectory() throws IOException {
return dir;
}
};
Store store = new Store(shardId, INDEX_SETTINGS, directoryService, new DummyShardLock(shardId));
store.markStoreCorrupted(new CorruptIndexException("foo", "bar"));
assertFalse(Store.canOpenIndex(logger, tempDir, shardId, (id, l) -> new DummyShardLock(id)));
store.close();
}
use of org.apache.lucene.store.BaseDirectoryWrapper in project elasticsearch by elastic.
the class ShardUtilsTests method testExtractShardId.
public void testExtractShardId() throws IOException {
BaseDirectoryWrapper dir = newDirectory();
IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig());
writer.commit();
ShardId id = new ShardId("foo", "_na_", random().nextInt());
try (DirectoryReader reader = DirectoryReader.open(writer)) {
ElasticsearchDirectoryReader wrap = ElasticsearchDirectoryReader.wrap(reader, id);
assertEquals(id, ShardUtils.extractShardId(wrap));
}
final int numDocs = 1 + random().nextInt(5);
for (int i = 0; i < numDocs; i++) {
Document d = new Document();
d.add(newField("name", "foobar", StringField.TYPE_STORED));
writer.addDocument(d);
if (random().nextBoolean()) {
writer.commit();
}
}
try (DirectoryReader reader = DirectoryReader.open(writer)) {
ElasticsearchDirectoryReader wrap = ElasticsearchDirectoryReader.wrap(reader, id);
assertEquals(id, ShardUtils.extractShardId(wrap));
CompositeReaderContext context = wrap.getContext();
for (LeafReaderContext leaf : context.leaves()) {
assertEquals(id, ShardUtils.extractShardId(leaf.reader()));
}
}
IOUtils.close(writer, dir);
}
use of org.apache.lucene.store.BaseDirectoryWrapper in project lucene-solr by apache.
the class TestBackwardsCompatibility method testUnsupportedOldIndexes.
/** This test checks that *only* IndexFormatTooOldExceptions are thrown when you open and operate on too old indexes! */
public void testUnsupportedOldIndexes() throws Exception {
for (int i = 0; i < unsupportedNames.length; i++) {
if (VERBOSE) {
System.out.println("TEST: index " + unsupportedNames[i]);
}
Path oldIndexDir = createTempDir(unsupportedNames[i]);
TestUtil.unzip(getDataInputStream("unsupported." + unsupportedNames[i] + ".zip"), oldIndexDir);
BaseDirectoryWrapper dir = newFSDirectory(oldIndexDir);
// don't checkindex, these are intentionally not supported
dir.setCheckIndexOnClose(false);
IndexReader reader = null;
IndexWriter writer = null;
try {
reader = DirectoryReader.open(dir);
fail("DirectoryReader.open should not pass for " + unsupportedNames[i]);
} catch (IndexFormatTooOldException e) {
if (e.getReason() != null) {
assertNull(e.getVersion());
assertNull(e.getMinVersion());
assertNull(e.getMaxVersion());
assertEquals(e.getMessage(), new IndexFormatTooOldException(e.getResourceDescription(), e.getReason()).getMessage());
} else {
assertNotNull(e.getVersion());
assertNotNull(e.getMinVersion());
assertNotNull(e.getMaxVersion());
assertTrue(e.getMessage(), e.getMaxVersion() >= e.getMinVersion());
assertTrue(e.getMessage(), e.getMaxVersion() < e.getVersion() || e.getVersion() < e.getMinVersion());
assertEquals(e.getMessage(), new IndexFormatTooOldException(e.getResourceDescription(), e.getVersion(), e.getMinVersion(), e.getMaxVersion()).getMessage());
}
// pass
if (VERBOSE) {
System.out.println("TEST: got expected exc:");
e.printStackTrace(System.out);
}
} finally {
if (reader != null)
reader.close();
reader = null;
}
try {
writer = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())).setCommitOnClose(false));
fail("IndexWriter creation should not pass for " + unsupportedNames[i]);
} catch (IndexFormatTooOldException e) {
if (e.getReason() != null) {
assertNull(e.getVersion());
assertNull(e.getMinVersion());
assertNull(e.getMaxVersion());
assertEquals(e.getMessage(), new IndexFormatTooOldException(e.getResourceDescription(), e.getReason()).getMessage());
} else {
assertNotNull(e.getVersion());
assertNotNull(e.getMinVersion());
assertNotNull(e.getMaxVersion());
assertTrue(e.getMessage(), e.getMaxVersion() >= e.getMinVersion());
assertTrue(e.getMessage(), e.getMaxVersion() < e.getVersion() || e.getVersion() < e.getMinVersion());
assertEquals(e.getMessage(), new IndexFormatTooOldException(e.getResourceDescription(), e.getVersion(), e.getMinVersion(), e.getMaxVersion()).getMessage());
}
// pass
if (VERBOSE) {
System.out.println("TEST: got expected exc:");
e.printStackTrace(System.out);
}
// Make sure exc message includes a path=
assertTrue("got exc message: " + e.getMessage(), e.getMessage().indexOf("path=\"") != -1);
} finally {
// above, so close without waiting for merges.
if (writer != null) {
try {
writer.commit();
} finally {
writer.close();
}
}
writer = null;
}
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
CheckIndex checker = new CheckIndex(dir);
checker.setInfoStream(new PrintStream(bos, false, IOUtils.UTF_8));
CheckIndex.Status indexStatus = checker.checkIndex();
assertFalse(indexStatus.clean);
assertTrue(bos.toString(IOUtils.UTF_8).contains(IndexFormatTooOldException.class.getName()));
checker.close();
dir.close();
}
}
use of org.apache.lucene.store.BaseDirectoryWrapper in project lucene-solr by apache.
the class TestManyPointsInOldIndex method testCheckOldIndex.
public void testCheckOldIndex() throws IOException {
Path path = createTempDir("manypointsindex");
InputStream resource = getClass().getResourceAsStream("manypointsindex.zip");
assertNotNull("manypointsindex not found", resource);
TestUtil.unzip(resource, path);
BaseDirectoryWrapper dir = newFSDirectory(path);
// disable default checking...
dir.setCheckIndexOnClose(false);
// ... because we check ourselves here:
TestUtil.checkIndex(dir, false, true, null);
dir.close();
}
use of org.apache.lucene.store.BaseDirectoryWrapper in project lucene-solr by apache.
the class TestSegmentInfos method testVersionsTwoSegments.
// LUCENE-5954
public void testVersionsTwoSegments() throws IOException {
BaseDirectoryWrapper dir = newDirectory();
dir.setCheckIndexOnClose(false);
byte[] id = StringHelper.randomId();
Codec codec = Codec.getDefault();
SegmentInfos sis = new SegmentInfos(Version.LATEST.major);
SegmentInfo info = new SegmentInfo(dir, Version.LUCENE_7_0_0, Version.LUCENE_7_0_0, "_0", 1, false, Codec.getDefault(), Collections.<String, String>emptyMap(), id, Collections.<String, String>emptyMap(), null);
info.setFiles(Collections.<String>emptySet());
codec.segmentInfoFormat().write(dir, info, IOContext.DEFAULT);
SegmentCommitInfo commitInfo = new SegmentCommitInfo(info, 0, -1, -1, -1);
sis.add(commitInfo);
info = new SegmentInfo(dir, Version.LUCENE_7_0_0, Version.LUCENE_7_0_0, "_1", 1, false, Codec.getDefault(), Collections.<String, String>emptyMap(), id, Collections.<String, String>emptyMap(), null);
info.setFiles(Collections.<String>emptySet());
codec.segmentInfoFormat().write(dir, info, IOContext.DEFAULT);
commitInfo = new SegmentCommitInfo(info, 0, -1, -1, -1);
sis.add(commitInfo);
sis.commit(dir);
sis = SegmentInfos.readLatestCommit(dir);
assertEquals(Version.LUCENE_7_0_0, sis.getMinSegmentLuceneVersion());
assertEquals(Version.LATEST, sis.getCommitLuceneVersion());
dir.close();
}
Aggregations