Indeed, getReader is an expensive way to get the segment count (it flushes the current RAM buffer to disk as a new segment).
Since SegmentInfos is now public, you could use SegmentInfos.read to read the current segments_N file, and then call its .size() method? But, this will only count as of the last commit... which is probably not sufficient for SOLR-1559? We could simply make getSegmentCount public / expert / not only for tests? Mike On Tue, Jan 12, 2010 at 8:42 PM, Chris Hostetter <hossman_luc...@fucit.org> wrote: > > A conversation with someone earlier today got me thinking about cranking out > a patch for SOLR-1559 (in which the goal is to allow for rules do dermine > the iput to optimize(maxNumSegments) instead of requiring a fixed integer > value as input) when i realized that i wasn't certain what "approved" > methods there might be for deterrmining hte current number of segments from > an IndexWriter. > > I see IndexWriter.getSegmentCount() but it's package protected (with a > comment that it exists for tests). So my best guess using only public APIs > would be something like... > > int numCurrentSegments = -1; > IndexReader r = writer.getReader(); > try { > IndexReader[]tmp = r.getSequentialSubReaders(); > numCurrentSegments = null==tmp ? 1 : tmp.length; > } finally { > r.close(); > } > > Is there a better way? > > (My main concern about this approach being that my intuition (which seems > supported by the javadocs) is that getReader might be a little > expensive/excesive just to count the segments) > > -Hoss > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: java-user-unsubscr...@lucene.apache.org > For additional commands, e-mail: java-user-h...@lucene.apache.org > > --------------------------------------------------------------------- To unsubscribe, e-mail: java-user-unsubscr...@lucene.apache.org For additional commands, e-mail: java-user-h...@lucene.apache.org