83
83
import org .apache .lucene .store .NoLockFactory ;
84
84
import org .apache .lucene .store .SimpleFSLockFactory ;
85
85
import org .apache .lucene .util .BytesRef ;
86
+ import org .jetbrains .annotations .NotNull ;
86
87
import org .opengrok .indexer .analysis .AbstractAnalyzer ;
87
88
import org .opengrok .indexer .analysis .AnalyzerFactory ;
88
89
import org .opengrok .indexer .analysis .AnalyzerGuru ;
@@ -725,8 +726,28 @@ private void addFile(File file, String path, Ctags ctags)
725
726
fa .setFoldingEnabled (env .isFoldingEnabled ());
726
727
727
728
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
+
729
740
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
+ }
730
751
} catch (InterruptedException e ) {
731
752
LOGGER .log (Level .WARNING , "File ''{0}'' interrupted--{1}" ,
732
753
new Object []{path , e .getMessage ()});
@@ -745,6 +766,9 @@ private void addFile(File file, String path, Ctags ctags)
745
766
return ;
746
767
} finally {
747
768
fa .setCtags (null );
769
+ if (xrefOut != null ) {
770
+ xrefOut .close ();
771
+ }
748
772
}
749
773
750
774
try {
@@ -1629,40 +1653,63 @@ public int hashCode() {
1629
1653
return hash ;
1630
1654
}
1631
1655
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 ;
1650
1659
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
+ }
1658
1665
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 ;
1662
1670
}
1663
1671
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 ))));
1666
1713
}
1667
1714
1668
1715
LockFactory pickLockFactory (RuntimeEnvironment env ) {
0 commit comments