1 package org.apache.lucene.index; 2 3 18 19 import java.io.IOException ; 20 import java.util.Arrays ; 21 import java.util.Iterator ; 22 import java.util.LinkedList ; 23 import java.util.List ; 24 25 import org.apache.lucene.util.PriorityQueue; 26 27 33 public class MultipleTermPositions implements TermPositions { 34 35 private static final class TermPositionsQueue extends PriorityQueue { 36 TermPositionsQueue(List termPositions) throws IOException { 37 initialize(termPositions.size()); 38 39 Iterator i = termPositions.iterator(); 40 while (i.hasNext()) { 41 TermPositions tp = (TermPositions) i.next(); 42 if (tp.next()) 43 put(tp); 44 } 45 } 46 47 final TermPositions peek() { 48 return (TermPositions) top(); 49 } 50 51 public final boolean lessThan(Object a, Object b) { 52 return ((TermPositions) a).doc() < ((TermPositions) b).doc(); 53 } 54 } 55 56 private static final class IntQueue { 57 private int _arraySize = 16; 58 private int _index = 0; 59 private int _lastIndex = 0; 60 private int[] _array = new int[_arraySize]; 61 62 final void add(int i) { 63 if (_lastIndex == _arraySize) 64 growArray(); 65 66 _array[_lastIndex++] = i; 67 } 68 69 final int next() { 70 return _array[_index++]; 71 } 72 73 final void sort() { 74 Arrays.sort(_array, _index, _lastIndex); 75 } 76 77 final void clear() { 78 _index = 0; 79 _lastIndex = 0; 80 } 81 82 final int size() { 83 return (_lastIndex - _index); 84 } 85 86 private void growArray() { 87 int[] newArray = new int[_arraySize * 2]; 88 System.arraycopy(_array, 0, newArray, 0, _arraySize); 89 _array = newArray; 90 _arraySize *= 2; 91 } 92 } 93 94 private int _doc; 95 private int _freq; 96 private TermPositionsQueue _termPositionsQueue; 97 private IntQueue _posList; 98 99 104 public MultipleTermPositions(IndexReader indexReader, Term[] terms) throws IOException { 105 List termPositions = new LinkedList (); 106 107 for (int i = 0; i < terms.length; i++) 108 termPositions.add(indexReader.termPositions(terms[i])); 109 110 _termPositionsQueue = new TermPositionsQueue(termPositions); 111 _posList = new IntQueue(); 112 } 113 114 public final boolean next() throws IOException { 115 if (_termPositionsQueue.size() == 0) 116 return false; 117 118 _posList.clear(); 119 _doc = _termPositionsQueue.peek().doc(); 120 121 TermPositions tp; 122 do { 123 tp = _termPositionsQueue.peek(); 124 125 for (int i = 0; i < tp.freq(); i++) 126 _posList.add(tp.nextPosition()); 127 128 if (tp.next()) 129 _termPositionsQueue.adjustTop(); 130 else { 131 _termPositionsQueue.pop(); 132 tp.close(); 133 } 134 } while (_termPositionsQueue.size() > 0 && _termPositionsQueue.peek().doc() == _doc); 135 136 _posList.sort(); 137 _freq = _posList.size(); 138 139 return true; 140 } 141 142 public final int nextPosition() { 143 return _posList.next(); 144 } 145 146 public final boolean skipTo(int target) throws IOException { 147 while (_termPositionsQueue.peek() != null && target > _termPositionsQueue.peek().doc()) { 148 TermPositions tp = (TermPositions) _termPositionsQueue.pop(); 149 if (tp.skipTo(target)) 150 _termPositionsQueue.put(tp); 151 else 152 tp.close(); 153 } 154 return next(); 155 } 156 157 public final int doc() { 158 return _doc; 159 } 160 161 public final int freq() { 162 return _freq; 163 } 164 165 public final void close() throws IOException { 166 while (_termPositionsQueue.size() > 0) 167 ((TermPositions) _termPositionsQueue.pop()).close(); 168 } 169 170 174 public void seek(Term arg0) throws IOException { 175 throw new UnsupportedOperationException (); 176 } 177 178 182 public void seek(TermEnum termEnum) throws IOException { 183 throw new UnsupportedOperationException (); 184 } 185 186 190 public int read(int[] arg0, int[] arg1) throws IOException { 191 throw new UnsupportedOperationException (); 192 } 193 194 } 195 | Popular Tags |