Index: lucene/src/test/org/apache/lucene/search/TestCachingCollector.java =================================================================== --- lucene/src/test/org/apache/lucene/search/TestCachingCollector.java (revision 1104680) +++ lucene/src/test/org/apache/lucene/search/TestCachingCollector.java (working copy) @@ -171,5 +171,18 @@ assertFalse(cc.isCached()); } } + + public void testNoWrappedCollector() throws Exception { + for (boolean cacheScores : new boolean[] { false, true }) { + // create w/ null wrapped collector, and test that the methods work + CachingCollector cc = CachingCollector.create(true, cacheScores, 50 * ONE_BYTE); + cc.setNextReader(null, 0); + cc.setScorer(new MockScorer()); + cc.collect(0); + + assertTrue(cc.isCached()); + cc.replay(new NoOpCollector(true)); + } + } } Index: lucene/src/java/org/apache/lucene/search/CachingCollector.java =================================================================== --- lucene/src/java/org/apache/lucene/search/CachingCollector.java (revision 1104680) +++ lucene/src/java/org/apache/lucene/search/CachingCollector.java (working copy) @@ -310,6 +310,48 @@ protected int base; protected int lastDocBase; + /** + * Creates a {@link CachingCollector} which does not wrap another collector. + * The cached documents and scores can later be {@link #replay(Collector) + * replayed}. + * + * @param acceptDocsOutOfOrder + * whether documents are allowed to be collected out-of-order + */ + public static CachingCollector create(final boolean acceptDocsOutOfOrder, boolean cacheScores, double maxRAMMB) { + Collector other = new Collector() { + @Override + public boolean acceptsDocsOutOfOrder() { + return acceptDocsOutOfOrder; + } + + @Override + public void setScorer(Scorer scorer) throws IOException {} + + @Override + public void collect(int doc) throws IOException {} + + @Override + public void setNextReader(IndexReader reader, int docBase) throws IOException {} + + }; + return create(other, cacheScores, maxRAMMB); + } + + /** + * Create a new {@link CachingCollector} that wraps the given collector and + * caches documents and scores up to the specified RAM threshold. + * + * @param other + * the Collector to wrap and delegate calls to. + * @param cacheScores + * whether to cache scores in addition to document IDs. Note that + * this increases the RAM consumed per doc + * @param maxRAMMB + * the maximum RAM in MB to consume for caching the documents and + * scores. If the collector exceeds the threshold, no documents and + * scores are cached. + */ public static CachingCollector create(Collector other, boolean cacheScores, double maxRAMMB) { return cacheScores ? new ScoreCachingCollector(other, maxRAMMB) : new NoScoreCachingCollector(other, maxRAMMB); }