Skip to content

Commit bcd862f

Browse files
author
Vladimir Kotal
committed
avoid empty xref files
1 parent fcd1459 commit bcd862f

File tree

1 file changed

+78
-31
lines changed

1 file changed

+78
-31
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/index/IndexDatabase.java

Lines changed: 78 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
import org.apache.lucene.store.NoLockFactory;
8484
import org.apache.lucene.store.SimpleFSLockFactory;
8585
import org.apache.lucene.util.BytesRef;
86+
import org.jetbrains.annotations.NotNull;
8687
import org.opengrok.indexer.analysis.AbstractAnalyzer;
8788
import org.opengrok.indexer.analysis.AnalyzerFactory;
8889
import org.opengrok.indexer.analysis.AnalyzerGuru;
@@ -725,8 +726,28 @@ private void addFile(File file, String path, Ctags ctags)
725726
fa.setFoldingEnabled(env.isFoldingEnabled());
726727

727728
Document doc = new Document();
728-
try (Writer xrefOut = newXrefWriter(fa, path)) {
729+
CountingWriter xrefOut = null;
730+
try {
731+
String xrefAbs = null;
732+
File transientXref = null;
733+
if (env.isGenerateHtml()) {
734+
xrefAbs = getXrefPath(path);
735+
transientXref = new File(TandemPath.join(xrefAbs,
736+
PendingFileCompleter.PENDING_EXTENSION));
737+
xrefOut = newXrefWriter(path, transientXref, env.isCompressXref());
738+
}
739+
729740
analyzerGuru.populateDocument(doc, file, path, fa, xrefOut);
741+
742+
// Avoid producing empty xref files.
743+
if (xrefOut != null && xrefOut.getCount() > 0) {
744+
PendingFileRenaming ren = new PendingFileRenaming(xrefAbs,
745+
transientXref.getAbsolutePath());
746+
completer.add(ren);
747+
} else if (xrefOut != null) {
748+
LOGGER.log(Level.FINER, "xref for {0} would be empty, removing", path);
749+
transientXref.delete();
750+
}
730751
} catch (InterruptedException e) {
731752
LOGGER.log(Level.WARNING, "File ''{0}'' interrupted--{1}",
732753
new Object[]{path, e.getMessage()});
@@ -745,6 +766,9 @@ private void addFile(File file, String path, Ctags ctags)
745766
return;
746767
} finally {
747768
fa.setCtags(null);
769+
if (xrefOut != null) {
770+
xrefOut.close();
771+
}
748772
}
749773

750774
try {
@@ -1629,40 +1653,63 @@ public int hashCode() {
16291653
return hash;
16301654
}
16311655

1632-
/**
1633-
* Get a writer to which the xref can be written, or null if no xref
1634-
* should be produced for files of this type.
1635-
*/
1636-
private Writer newXrefWriter(AbstractAnalyzer fa, String path) throws IOException {
1637-
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
1638-
if (env.isGenerateHtml() && fa != null) {
1639-
boolean compressed = env.isCompressXref();
1640-
File xrefFile = whatXrefFile(path, compressed);
1641-
File parentFile = xrefFile.getParentFile();
1642-
1643-
// If mkdirs() returns false, the failure is most likely
1644-
// because the file already exists. But to check for the
1645-
// file first and only add it if it doesn't exists would
1646-
// only increase the file IO...
1647-
if (!parentFile.mkdirs()) {
1648-
assert parentFile.exists();
1649-
}
1656+
private class CountingWriter extends Writer {
1657+
private long count;
1658+
private Writer out;
16501659

1651-
// Write to a pending file for later renaming.
1652-
String xrefAbs = xrefFile.getAbsolutePath();
1653-
File transientXref = new File(TandemPath.join(xrefAbs,
1654-
PendingFileCompleter.PENDING_EXTENSION));
1655-
PendingFileRenaming ren = new PendingFileRenaming(xrefAbs,
1656-
transientXref.getAbsolutePath());
1657-
completer.add(ren);
1660+
CountingWriter(Writer out) {
1661+
super(out);
1662+
this.out = out;
1663+
this.count = 0;
1664+
}
16581665

1659-
return new BufferedWriter(new OutputStreamWriter(compressed ?
1660-
new GZIPOutputStream(new FileOutputStream(transientXref)) :
1661-
new FileOutputStream(transientXref)));
1666+
@Override
1667+
public void write(@NotNull char[] chars, int off, int len) throws IOException {
1668+
out.write(chars, off, len);
1669+
count += len;
16621670
}
16631671

1664-
// no Xref for this analyzer
1665-
return null;
1672+
@Override
1673+
public void flush() throws IOException {
1674+
out.flush();
1675+
}
1676+
1677+
@Override
1678+
public void close() throws IOException {
1679+
out.close();
1680+
}
1681+
1682+
public long getCount() {
1683+
return count;
1684+
}
1685+
}
1686+
1687+
private String getXrefPath(String path) {
1688+
boolean compressed = RuntimeEnvironment.getInstance().isCompressXref();
1689+
File xrefFile = whatXrefFile(path, compressed);
1690+
File parentFile = xrefFile.getParentFile();
1691+
1692+
// If mkdirs() returns false, the failure is most likely
1693+
// because the file already exists. But to check for the
1694+
// file first and only add it if it doesn't exists would
1695+
// only increase the file IO...
1696+
if (!parentFile.mkdirs()) {
1697+
assert parentFile.exists();
1698+
}
1699+
1700+
// Write to a pending file for later renaming.
1701+
String xrefAbs = xrefFile.getAbsolutePath();
1702+
return xrefAbs;
1703+
}
1704+
1705+
/**
1706+
* Get a writer to which the xref can be written, or null if no xref
1707+
* should be produced for files of this type.
1708+
*/
1709+
private CountingWriter newXrefWriter(String path, File transientXref, boolean compressed) throws IOException {
1710+
return new CountingWriter(new BufferedWriter(new OutputStreamWriter(compressed ?
1711+
new GZIPOutputStream(new FileOutputStream(transientXref)) :
1712+
new FileOutputStream(transientXref))));
16661713
}
16671714

16681715
LockFactory pickLockFactory(RuntimeEnvironment env) {

0 commit comments

Comments
 (0)