/* A Possible future optimization could skip entire segments */ public boolean skipTo(int target) throws IOException { for (; ; ) { if (current != null && current.skipTo(target - base)) { return true; } else if (pointer < readers.length) { if (tenum != null) { SegmentMergeInfo smi = tenum.matchingSegments[matchingSegmentPos++]; if (smi == null) { pointer = readers.length; return false; } pointer = smi.ord; } base = starts[pointer]; current = termDocs(pointer++); } else return false; } }
public void testSkipTo(int indexDivisor) throws IOException { Directory dir = new RAMDirectory(); IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED); Term ta = new Term("content", "aaa"); for (int i = 0; i < 10; i++) addDoc(writer, "aaa aaa aaa aaa"); Term tb = new Term("content", "bbb"); for (int i = 0; i < 16; i++) addDoc(writer, "bbb bbb bbb bbb"); Term tc = new Term("content", "ccc"); for (int i = 0; i < 50; i++) addDoc(writer, "ccc ccc ccc ccc"); // assure that we deal with a single segment writer.optimize(); writer.close(); IndexReader reader = IndexReader.open(dir); reader.setTermInfosIndexDivisor(indexDivisor); assertEquals(indexDivisor, reader.getTermInfosIndexDivisor()); TermDocs tdocs = reader.termDocs(); // without optimization (assumption skipInterval == 16) // with next tdocs.seek(ta); assertTrue(tdocs.next()); assertEquals(0, tdocs.doc()); assertEquals(4, tdocs.freq()); assertTrue(tdocs.next()); assertEquals(1, tdocs.doc()); assertEquals(4, tdocs.freq()); assertTrue(tdocs.skipTo(0)); assertEquals(2, tdocs.doc()); assertTrue(tdocs.skipTo(4)); assertEquals(4, tdocs.doc()); assertTrue(tdocs.skipTo(9)); assertEquals(9, tdocs.doc()); assertFalse(tdocs.skipTo(10)); // without next tdocs.seek(ta); assertTrue(tdocs.skipTo(0)); assertEquals(0, tdocs.doc()); assertTrue(tdocs.skipTo(4)); assertEquals(4, tdocs.doc()); assertTrue(tdocs.skipTo(9)); assertEquals(9, tdocs.doc()); assertFalse(tdocs.skipTo(10)); // exactly skipInterval documents and therefore with optimization // with next tdocs.seek(tb); assertTrue(tdocs.next()); assertEquals(10, tdocs.doc()); assertEquals(4, tdocs.freq()); assertTrue(tdocs.next()); assertEquals(11, tdocs.doc()); assertEquals(4, tdocs.freq()); assertTrue(tdocs.skipTo(5)); assertEquals(12, tdocs.doc()); assertTrue(tdocs.skipTo(15)); assertEquals(15, tdocs.doc()); assertTrue(tdocs.skipTo(24)); assertEquals(24, tdocs.doc()); assertTrue(tdocs.skipTo(25)); assertEquals(25, tdocs.doc()); assertFalse(tdocs.skipTo(26)); // without next tdocs.seek(tb); assertTrue(tdocs.skipTo(5)); assertEquals(10, tdocs.doc()); assertTrue(tdocs.skipTo(15)); assertEquals(15, tdocs.doc()); assertTrue(tdocs.skipTo(24)); assertEquals(24, tdocs.doc()); assertTrue(tdocs.skipTo(25)); assertEquals(25, tdocs.doc()); assertFalse(tdocs.skipTo(26)); // much more than skipInterval documents and therefore with optimization // with next tdocs.seek(tc); assertTrue(tdocs.next()); assertEquals(26, tdocs.doc()); assertEquals(4, tdocs.freq()); assertTrue(tdocs.next()); assertEquals(27, tdocs.doc()); assertEquals(4, tdocs.freq()); assertTrue(tdocs.skipTo(5)); assertEquals(28, tdocs.doc()); assertTrue(tdocs.skipTo(40)); assertEquals(40, tdocs.doc()); assertTrue(tdocs.skipTo(57)); assertEquals(57, tdocs.doc()); assertTrue(tdocs.skipTo(74)); assertEquals(74, tdocs.doc()); assertTrue(tdocs.skipTo(75)); assertEquals(75, tdocs.doc()); assertFalse(tdocs.skipTo(76)); // without next tdocs.seek(tc); assertTrue(tdocs.skipTo(5)); assertEquals(26, tdocs.doc()); assertTrue(tdocs.skipTo(40)); assertEquals(40, tdocs.doc()); assertTrue(tdocs.skipTo(57)); assertEquals(57, tdocs.doc()); assertTrue(tdocs.skipTo(74)); assertEquals(74, tdocs.doc()); assertTrue(tdocs.skipTo(75)); assertEquals(75, tdocs.doc()); assertFalse(tdocs.skipTo(76)); tdocs.close(); reader.close(); dir.close(); }