1 package org.apache.lucene.index; 2 3 18 19 import java.io.IOException ; 20 import org.apache.lucene.util.BitVector; 21 import org.apache.lucene.store.IndexInput; 22 23 class SegmentTermDocs implements TermDocs { 24 protected SegmentReader parent; 25 protected IndexInput freqStream; 26 protected int count; 27 protected int df; 28 protected BitVector deletedDocs; 29 int doc = 0; 30 int freq; 31 32 private int skipInterval; 33 private int numSkips; 34 private int skipCount; 35 private IndexInput skipStream; 36 private int skipDoc; 37 private long freqPointer; 38 private long proxPointer; 39 private long skipPointer; 40 private boolean haveSkipped; 41 42 protected SegmentTermDocs(SegmentReader parent) { 43 this.parent = parent; 44 this.freqStream = (IndexInput) parent.freqStream.clone(); 45 this.deletedDocs = parent.deletedDocs; 46 this.skipInterval = parent.tis.getSkipInterval(); 47 } 48 49 public void seek(Term term) throws IOException { 50 TermInfo ti = parent.tis.get(term); 51 seek(ti); 52 } 53 54 public void seek(TermEnum termEnum) throws IOException { 55 TermInfo ti; 56 57 if (termEnum instanceof SegmentTermEnum && ((SegmentTermEnum) termEnum).fieldInfos == parent.fieldInfos) ti = ((SegmentTermEnum) termEnum).termInfo(); 60 else ti = parent.tis.get(termEnum.term()); 62 63 seek(ti); 64 } 65 66 void seek(TermInfo ti) throws IOException { 67 count = 0; 68 if (ti == null) { 69 df = 0; 70 } else { 71 df = ti.docFreq; 72 doc = 0; 73 skipDoc = 0; 74 skipCount = 0; 75 numSkips = df / skipInterval; 76 freqPointer = ti.freqPointer; 77 proxPointer = ti.proxPointer; 78 skipPointer = freqPointer + ti.skipOffset; 79 freqStream.seek(freqPointer); 80 haveSkipped = false; 81 } 82 } 83 84 public void close() throws IOException { 85 freqStream.close(); 86 if (skipStream != null) 87 skipStream.close(); 88 } 89 90 public final int doc() { return doc; } 91 public final int freq() { return freq; } 92 93 protected void skippingDoc() throws IOException { 94 } 95 96 public boolean next() throws IOException { 97 while (true) { 98 if (count == df) 99 return false; 100 101 int docCode = freqStream.readVInt(); 102 doc += docCode >>> 1; if ((docCode & 1) != 0) freq = 1; else 106 freq = freqStream.readVInt(); 108 count++; 109 110 if (deletedDocs == null || !deletedDocs.get(doc)) 111 break; 112 skippingDoc(); 113 } 114 return true; 115 } 116 117 118 public int read(final int[] docs, final int[] freqs) 119 throws IOException { 120 final int length = docs.length; 121 int i = 0; 122 while (i < length && count < df) { 123 124 final int docCode = freqStream.readVInt(); 126 doc += docCode >>> 1; if ((docCode & 1) != 0) freq = 1; else 130 freq = freqStream.readVInt(); count++; 132 133 if (deletedDocs == null || !deletedDocs.get(doc)) { 134 docs[i] = doc; 135 freqs[i] = freq; 136 ++i; 137 } 138 } 139 return i; 140 } 141 142 143 protected void skipProx(long proxPointer) throws IOException {} 144 145 146 public boolean skipTo(int target) throws IOException { 147 if (df >= skipInterval) { 149 if (skipStream == null) 150 skipStream = (IndexInput) freqStream.clone(); 152 if (!haveSkipped) { skipStream.seek(skipPointer); 154 haveSkipped = true; 155 } 156 157 int lastSkipDoc = skipDoc; 159 long lastFreqPointer = freqStream.getFilePointer(); 160 long lastProxPointer = -1; 161 int numSkipped = -1 - (count % skipInterval); 162 163 while (target > skipDoc) { 164 lastSkipDoc = skipDoc; 165 lastFreqPointer = freqPointer; 166 lastProxPointer = proxPointer; 167 168 if (skipDoc != 0 && skipDoc >= doc) 169 numSkipped += skipInterval; 170 171 if(skipCount >= numSkips) 172 break; 173 174 skipDoc += skipStream.readVInt(); 175 freqPointer += skipStream.readVInt(); 176 proxPointer += skipStream.readVInt(); 177 178 skipCount++; 179 } 180 181 if (lastFreqPointer > freqStream.getFilePointer()) { 183 freqStream.seek(lastFreqPointer); 184 skipProx(lastProxPointer); 185 186 doc = lastSkipDoc; 187 count += numSkipped; 188 } 189 190 } 191 192 do { 194 if (!next()) 195 return false; 196 } while (target > doc); 197 return true; 198 } 199 200 } 201 | Popular Tags |