From 6ae62295e93f631a66d47c59af1292daaa325f8b Mon Sep 17 00:00:00 2001 From: Michael McCandless Date: Sun, 8 Nov 2009 15:35:56 +0000 Subject: [PATCH] LUCENE-2045: fix false FNFE git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@833886 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES.txt | 4 ++++ .../apache/lucene/index/DocumentsWriter.java | 21 ++----------------- .../apache/lucene/index/TestIndexWriter.java | 13 ++++++++++++ 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 8c8dc193195..7681f89aa03 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -184,6 +184,10 @@ Bug fixes char (U+FFFD) during indexing, to prevent silent index corruption. (Peter Keegan, Mike McCandless) + * LUCENE-2045: Fix silly FileNotFoundException hit if you enable + infoStream on IndexWriter and then add an empty document and commit + (Shai Erera via Mike McCandless) + API Changes * Un-deprecate search(Weight weight, Filter filter, int n) from diff --git a/src/java/org/apache/lucene/index/DocumentsWriter.java b/src/java/org/apache/lucene/index/DocumentsWriter.java index 05e9cd161b9..0c2bff55c26 100644 --- a/src/java/org/apache/lucene/index/DocumentsWriter.java +++ b/src/java/org/apache/lucene/index/DocumentsWriter.java @@ -584,7 +584,8 @@ final class DocumentsWriter { consumer.flush(threads, flushState); if (infoStream != null) { - final long newSegmentSize = segmentSize(flushState.segmentName); + SegmentInfo si = new SegmentInfo(flushState.segmentName, flushState.numDocs, directory); + final long newSegmentSize = si.sizeInBytes(); String message = " oldRAMSize=" + numBytesUsed + " newFlushedSize=" + newSegmentSize + " docs/MB=" + nf.format(numDocsInRAM/(newSegmentSize/1024./1024.)) + @@ -1138,24 +1139,6 @@ final class DocumentsWriter { NumberFormat nf = NumberFormat.getInstance(); - // TODO FI: this is not flexible -- we can't hardwire - // extensions in here: - private long segmentSize(String segmentName) throws IOException { - // Used only when infoStream != null - assert infoStream != null; - - long size = directory.fileLength(segmentName + ".tii") + - directory.fileLength(segmentName + ".tis") + - directory.fileLength(segmentName + ".frq") + - directory.fileLength(segmentName + ".prx"); - - final String normFileName = segmentName + ".nrm"; - if (directory.fileExists(normFileName)) - size += directory.fileLength(normFileName); - - return size; - } - // Coarse estimates used to measure RAM usage of buffered deletes final static int OBJECT_HEADER_BYTES = 8; final static int POINTER_NUM_BYTE = Constants.JRE_IS_64BIT ? 8 : 4; diff --git a/src/test/org/apache/lucene/index/TestIndexWriter.java b/src/test/org/apache/lucene/index/TestIndexWriter.java index 3fdb7c32ce4..d8d1144db22 100644 --- a/src/test/org/apache/lucene/index/TestIndexWriter.java +++ b/src/test/org/apache/lucene/index/TestIndexWriter.java @@ -4572,4 +4572,17 @@ public class TestIndexWriter extends LuceneTestCase { _TestUtil.checkIndex(d); d.close(); } + + public void testNoDocsIndex() throws Throwable { + Directory dir = new MockRAMDirectory(); + IndexWriter writer = new IndexWriter(dir, new SimpleAnalyzer(), IndexWriter.MaxFieldLength.UNLIMITED); + writer.setUseCompoundFile(false); + ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); + writer.setInfoStream(new PrintStream(bos)); + writer.addDocument(new Document()); + writer.close(); + + _TestUtil.checkIndex(dir); + dir.close(); + } }