use of org.apache.lucene.index.CheckIndex in project lucene-solr by apache.
the class TestUtil method checkIndex.
/** If failFast is true, then throw the first exception when index corruption is hit, instead of moving on to other fields/segments to
* look for any other corruption. */
public static CheckIndex.Status checkIndex(Directory dir, boolean crossCheckTermVectors, boolean failFast, ByteArrayOutputStream output) throws IOException {
if (output == null) {
output = new ByteArrayOutputStream(1024);
}
// some tests e.g. exception tests become much more complicated if they have to close the writer
try (CheckIndex checker = new CheckIndex(dir, NoLockFactory.INSTANCE.obtainLock(dir, "bogus"))) {
checker.setCrossCheckTermVectors(crossCheckTermVectors);
checker.setFailFast(failFast);
checker.setInfoStream(new PrintStream(output, false, IOUtils.UTF_8), false);
CheckIndex.Status indexStatus = checker.checkIndex(null);
if (indexStatus == null || indexStatus.clean == false) {
System.out.println("CheckIndex failed");
System.out.println(output.toString(IOUtils.UTF_8));
throw new RuntimeException("CheckIndex failed");
} else {
if (LuceneTestCase.INFOSTREAM) {
System.out.println(output.toString(IOUtils.UTF_8));
}
return indexStatus;
}
}
}
use of org.apache.lucene.index.CheckIndex in project zm-mailbox by Zimbra.
the class LuceneIndex method verify.
/**
* Run a sanity check for the index. Callers are responsible to make sure the index is not opened by any writer.
*
* @param out info stream where messages should go. If null, no messages are printed.
* @return true if no problems were found, otherwise false
* @throws IOException failed to verify, but it doesn't necessarily mean the index is corrupted.
*/
@Override
public boolean verify(PrintStream out) throws IOException {
if (!IndexReader.indexExists(luceneDirectory)) {
out.println("index does not exist or no segments file found: " + luceneDirectory.getDirectory());
return true;
}
CheckIndex check = new CheckIndex(luceneDirectory);
if (out != null) {
check.setInfoStream(out);
}
CheckIndex.Status status = check.checkIndex();
return status.clean;
}
use of org.apache.lucene.index.CheckIndex in project zm-mailbox by Zimbra.
the class LuceneViewer method doCheck.
private static void doCheck(CommandLine cl) throws Exception {
Console console = new Console(cl.hasOption(CLI.O_VERBOSE));
String indexDir = cl.getOptionValue(CLI.O_INPUT);
console.info("Checking index " + indexDir);
Directory dir = null;
try {
dir = LuceneDirectory.open(new File(indexDir));
} catch (Throwable t) {
console.info("ERROR: could not open directory \"" + indexDir + "\"; exiting");
t.printStackTrace(System.out);
System.exit(1);
}
CheckIndex checker = new CheckIndex(dir);
checker.setInfoStream(System.out);
Status result = checker.checkIndex();
console.info("Result:" + (result.clean ? "clean" : "not clean"));
}
use of org.apache.lucene.index.CheckIndex in project jackrabbit-oak by apache.
the class IndexConsistencyChecker method checkIndexDirectory.
private void checkIndexDirectory(DirectoryStatus dirStatus, NodeState idx, IndexDefinition defn, File workDir, String dirName, Closer closer) throws IOException {
File idxDir = createWorkDir(workDir, dirName);
Directory sourceDir = new OakDirectory(new ReadOnlyBuilder(idx), dirName, defn, true);
Directory targetDir = FSDirectory.open(idxDir);
closer.register(sourceDir);
closer.register(targetDir);
boolean clean = true;
for (String file : sourceDir.listAll()) {
log.debug("[{}][{}] Checking {}", indexPath, dirName, file);
try {
sourceDir.copy(targetDir, file, file, IOContext.DEFAULT);
} catch (FileNotFoundException ignore) {
dirStatus.missingFiles.add(file);
clean = false;
log.warn("[{}][{}] File {} missing", indexPath, dirName, file);
}
if (targetDir.fileLength(file) != sourceDir.fileLength(file)) {
FileSizeStatus fileStatus = new FileSizeStatus(file, targetDir.fileLength(file), sourceDir.fileLength(file));
dirStatus.filesWithSizeMismatch.add(fileStatus);
clean = false;
log.warn("[{}][{}] File size mismatch {}", indexPath, dirName, fileStatus);
} else {
dirStatus.size += sourceDir.fileLength(file);
log.debug("[{}][{}] File {} is consistent", indexPath, dirName, file);
}
}
if (clean) {
log.debug("[{}][{}] Directory content found to be consistent. Proceeding to IndexCheck", indexPath, dirName);
CheckIndex ci = new CheckIndex(targetDir);
if (printStream != null) {
ci.setInfoStream(printStream, verbose);
} else if (log.isDebugEnabled()) {
ci.setInfoStream(new LoggingPrintStream(log), log.isTraceEnabled());
}
dirStatus.status = ci.checkIndex();
dirStatus.clean = dirStatus.status.clean;
log.debug("[{}][{}] IndexCheck was successful. Proceeding to open DirectoryReader", indexPath, dirName);
}
if (dirStatus.clean) {
DirectoryReader dirReader = DirectoryReader.open(targetDir);
dirStatus.numDocs = dirReader.numDocs();
log.debug("[{}][{}] DirectoryReader can be opened", indexPath, dirName);
closer.register(dirReader);
}
}
use of org.apache.lucene.index.CheckIndex in project lucene-solr by apache.
the class CheckHdfsIndex method doMain.
// actual main: returns exit code instead of terminating JVM (for easy testing)
@SuppressForbidden(reason = "System.out required: command line tool")
protected static int doMain(String[] args) throws IOException, InterruptedException {
CheckIndex.Options opts;
try {
opts = CheckIndex.parseOptions(args);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
return 1;
}
if (!CheckIndex.assertsOn()) {
System.out.println("\nNOTE: testing will be more thorough if you run java with '-ea:org.apache.lucene...', so assertions are enabled");
}
if (opts.getDirImpl() != null) {
System.out.println("\nIgnoring specified -dir-impl, instead using " + HdfsDirectory.class.getSimpleName());
}
System.out.println("\nOpening index @ " + opts.getIndexPath() + "\n");
Directory directory;
try {
directory = new HdfsDirectory(new Path(opts.getIndexPath()), getConf());
} catch (IOException e) {
System.out.println("ERROR: could not open hdfs directory \"" + opts.getIndexPath() + "\"; exiting");
e.printStackTrace(System.out);
return 1;
}
try (Directory dir = directory;
CheckIndex checker = new CheckIndex(dir)) {
opts.setOut(System.out);
return checker.doCheck(opts);
}
}
Aggregations