Options opts; try { opts = parseOptions(args); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); if (!assertsOn()) System.out.println("\nNOTE: testing will be more thorough if you run java with '-ea:org.apache.lucene...', so assertions are enabled"); CheckIndex checker = new CheckIndex(dir)) { opts.out = System.out; return checker.doCheck(opts);
setCrossCheckTermVectors(opts.doCrossCheckTermVectors); setChecksumsOnly(opts.doChecksumsOnly); setInfoStream(opts.out, opts.verbose); Status result = checkIndex(opts.onlySegments); if (result.missingSegments) { return 1; exorciseIndex(result); opts.out.println("OK"); opts.out.println("Wrote new segments file \"" + result.newSegments.getSegmentsFileName() + "\"");
ensureOpen(); long startNS = System.nanoTime(); NumberFormat nf = NumberFormat.getInstance(Locale.ROOT); msg(infoStream, "ERROR: could not read any segments file in directory"); result.missingSegments = true; if (infoStream != null) msg(infoStream, "Segments file=" + segmentsFileName + " numSegments=" + numSegments + " " + versionString + " id=" + StringHelper.idToString(sis.getId()) + userDataString); msg(infoStream, ":"); msg(infoStream, " " + (1+i) + " of " + numSegments + ": name=" + info.info.name + " maxDoc=" + info.info.maxDoc()); segInfoStat.name = info.info.name; segInfoStat.maxDoc = info.info.maxDoc(); msg(infoStream, " version=" + (version == null ? "3.0" : version)); msg(infoStream, " id=" + StringHelper.idToString(info.info.getId())); final Codec codec = info.info.getCodec(); msg(infoStream, " codec=" + codec); segInfoStat.codec = codec; msg(infoStream, " compound=" + info.info.getUseCompoundFile()); segInfoStat.compound = info.info.getUseCompoundFile(); msg(infoStream, " numFiles=" + info.files().size()); Sort indexSort = info.info.getIndexSort(); if (indexSort != null) { msg(infoStream, " sort=" + indexSort); if (previousIndexSort != null) {
public void checkClean(){ try { dir = FSDirectory.open(indexDir.toPath()); CheckIndex checkIndex = new CheckIndex(dir); CheckIndex.Status status = checkIndex.checkIndex(); assert (status.clean) : "index is not clean"; dir.close(); } catch (IOException e) { throw new RuntimeException(e); } }
public void execute(Terminal terminal, ShardPath shardPath, Directory indexDirectory, Lock writeLock, PrintStream printStream, boolean verbose) throws IOException { checkCorruptMarkerFileIsPresent(indexDirectory); final CheckIndex.Status status; try (CheckIndex checker = new CheckIndex(indexDirectory, writeLock)) { checker.setChecksumsOnly(true); checker.setInfoStream(printStream, verbose); status = checker.checkIndex(null); if (status.missingSegments == false) { if (status.clean == false) { terminal.println("Writing..."); checker.exorciseIndex(status); terminal.println("OK"); terminal.println("Wrote new segments file \"" + status.segmentsFileName + "\""); } } else { throw new ElasticsearchException("Index is unrecoverable - there are missing segments"); } } }
/** * Checks and returns the status of the existing index in this store. * * @param out where infoStream messages should go. See {@link CheckIndex#setInfoStream(PrintStream)} */ public CheckIndex.Status checkIndex(PrintStream out) throws IOException { metadataLock.writeLock().lock(); try (CheckIndex checkIndex = new CheckIndex(directory)) { checkIndex.setInfoStream(out); return checkIndex.checkIndex(); } finally { metadataLock.writeLock().unlock(); } }
if (!assertsOn()) System.out.println("\nNOTE: testing will be more thorough if you run java with '-ea:org.apache.lucene...', so assertions are enabled"); CheckIndex checker = new CheckIndex(dir); checker.setInfoStream(System.out); Status result = checker.checkIndex(onlySegments); checker.fixIndex(result); System.out.println("OK"); System.out.println("Wrote new segments file \"" + result.newSegments.getCurrentSegmentFileName() + "\"");
ensureOpen(); long startNS = System.nanoTime(); NumberFormat nf = NumberFormat.getInstance(Locale.ROOT); msg(infoStream, "ERROR: could not read any segments file in directory"); result.missingSegments = true; if (infoStream != null) msg(infoStream, "ERROR: could not open segments file in directory"); if (infoStream != null) t.printStackTrace(infoStream); msg(infoStream, "ERROR: could not read segment file version in directory"); if (infoStream != null) t.printStackTrace(infoStream); msg(infoStream, "Segments file=" + segmentsFileName + " numSegments=" + numSegments + " " + versionString + " id=" + StringHelper.idToString(sis.getId()) + " format=" + sFormat + userDataString); msg(infoStream, ":"); msg(infoStream, " " + (1+i) + " of " + numSegments + ": name=" + info.info.name + " maxDoc=" + info.info.maxDoc()); segInfoStat.name = info.info.name; segInfoStat.maxDoc = info.info.maxDoc(); msg(infoStream, " version=" + segInfoStat.version); msg(infoStream, " id=" + StringHelper.idToString(info.info.getId())); final Codec codec = info.info.getCodec(); msg(infoStream, " codec=" + codec);
/** Returns true if index is clean, else false. * @deprecated Please instantiate a CheckIndex and then use {@link #checkIndex(List)} instead */ public static boolean check(Directory dir, boolean doFix, List onlySegments) throws IOException { CheckIndex checker = new CheckIndex(dir); Status status = checker.checkIndex(onlySegments); if (doFix && !status.clean) checker.fixIndex(status); return status.clean; }
f.checkConsistency(); msg(infoStream, String.format(Locale.ROOT, "OK [%d fields] [took %.3f sec]", fieldInfos.size(), nsToSec(System.nanoTime()-startNS))); status.totFields = fieldInfos.size(); } catch (Throwable e) { throw IOUtils.rethrowAlways(e); msg(infoStream, "ERROR [" + String.valueOf(e.getMessage()) + "]"); status.error = e; if (infoStream != null) {
private void upgradeIndex(Directory dir) throws IOException { boolean doUpgrade = false; IndexWriterConfig iwc = new IndexWriterConfig(null); CheckIndex chkIndex = new CheckIndex(dir); try { for (CheckIndex.Status.SegmentInfoStatus segmentInfo : chkIndex .checkIndex().segmentInfos) { if (!segmentInfo.version.equals(Version.LATEST)) { logInfo("Found Index version %s", segmentInfo.version.toString()); doUpgrade = true; break; } } } finally { chkIndex.close(); } if (doUpgrade) { logInfo("Upgrading index to %s", Version.LATEST.toString()); new IndexUpgrader(dir, iwc, false).upgrade(); this.indexUpdateTimeMicros = Utils.getNowMicrosUtc(); } }
/** Returns a {@link Status} instance detailing * the state of the index. * * <p>As this method checks every byte in the index, on a large * index it can take quite a long time to run. * * <p><b>WARNING</b>: make sure * you only call this when the index is not opened by any * writer. */ public Status checkIndex() throws IOException { return checkIndex(null); }
checkNumericDocValues(info.name, normsReader.getNorms(info)); ++status.totFields; msg(infoStream, String.format(Locale.ROOT, "OK [%d fields] [took %.3f sec]", status.totFields, nsToSec(System.nanoTime()-startNS))); } catch (Throwable e) { if (failFast) { throw IOUtils.rethrowAlways(e); msg(infoStream, "ERROR [" + String.valueOf(e.getMessage()) + "]"); status.error = e; if (infoStream != null) {
if (fieldInfo.getDocValuesType() != DocValuesType.NONE) { status.totalValueFields++; checkDocValues(fieldInfo, dvReader, reader.maxDoc(), infoStream, status); msg(infoStream, String.format(Locale.ROOT, "OK [%d docvalues fields; %d BINARY; %d NUMERIC; %d SORTED; %d SORTED_NUMERIC; %d SORTED_SET] [took %.3f sec]", status.totalValueFields, status.totalSortedNumericFields, status.totalSortedSetFields, nsToSec(System.nanoTime()-startNS))); } catch (Throwable e) { if (failFast) { throw IOUtils.rethrowAlways(e); msg(infoStream, "ERROR [" + String.valueOf(e.getMessage()) + "]"); status.error = e; if (infoStream != null) {
checkFields(tfv, null, 1, fieldInfos, false, true, infoStream, verbose, version); msg(infoStream, String.format(Locale.ROOT, "OK [%d total term vector count; avg %.1f term/freq vector fields per doc] [took %.3f sec]", status.totVectors, vectorAvg, nsToSec(System.nanoTime() - startNS))); } catch (Throwable e) { if (failFast) { throw IOUtils.rethrowAlways(e); msg(infoStream, "ERROR [" + String.valueOf(e.getMessage()) + "]"); status.error = e; if (infoStream != null) {
/** Set infoStream where messages should go. See {@link #setInfoStream(PrintStream,boolean)}. */ public void setInfoStream(PrintStream out) { setInfoStream(out, false); }
/** * Test the term index. * @lucene.experimental */ public static Status.TermIndexStatus testPostings(CodecReader reader, PrintStream infoStream, boolean verbose, boolean failFast, Version version) throws IOException { // TODO: we should go and verify term vectors match, if // crossCheckTermVectors is on... Status.TermIndexStatus status; final int maxDoc = reader.maxDoc(); try { if (infoStream != null) { infoStream.print(" test: terms, freq, prox..."); } final Fields fields = reader.getPostingsReader().getMergeInstance(); final FieldInfos fieldInfos = reader.getFieldInfos(); status = checkFields(fields, reader.getLiveDocs(), maxDoc, fieldInfos, true, false, infoStream, verbose, version); } catch (Throwable e) { if (failFast) { throw IOUtils.rethrowAlways(e); } msg(infoStream, "ERROR: " + e); status = new Status.TermIndexStatus(); status.error = e; if (infoStream != null) { e.printStackTrace(infoStream); } } return status; }
try (CheckIndex checkIndex = new CheckIndex(directory)) { checkIndex.exorciseIndex(status); } finally {
/** * Check whether asserts are enabled or not. * @return true iff asserts are enabled */ public static boolean assertsOn() { assert testAsserts(); return assertsOn; }
private static void checkSoftDeletes(String softDeletesField, SegmentCommitInfo info, SegmentReader reader, PrintStream infoStream, boolean failFast) throws IOException { if (infoStream != null) infoStream.print(" test: check soft deletes....."); try { int softDeletes = PendingSoftDeletes.countSoftDeletes(DocValuesFieldExistsQuery.getDocValuesDocIdSetIterator(softDeletesField, reader), reader.getLiveDocs()); if (softDeletes != info.getSoftDelCount()) { throw new RuntimeException("actual soft deletes: " + softDeletes + " but expected: " +info.getSoftDelCount()); } } catch (Exception e) { if (failFast) { throw IOUtils.rethrowAlways(e); } msg(infoStream, "ERROR [" + String.valueOf(e.getMessage()) + "]"); if (infoStream != null) { e.printStackTrace(infoStream); } } }