1 17 package org.alfresco.repo.search.impl.lucene.query; 18 19 import java.io.IOException ; 20 21 import org.apache.lucene.index.Term; 22 import org.apache.lucene.index.TermEnum; 23 import org.apache.lucene.index.TermPositions; 24 25 31 public class CachingTermPositions implements TermPositions 32 { 33 int[] results; 34 35 int position = -1; 36 37 int last = -1; 38 39 TermPositions delegate; 40 41 CachingTermPositions(TermPositions delegate) 42 { 43 this.delegate = delegate; 44 } 45 46 51 public int nextPosition() throws IOException 52 { 53 if (results == null) 54 { 55 results = new int[freq()]; 56 } 57 position++; 58 if (last < position) 59 { 60 results[position] = delegate.nextPosition(); 61 last = position; 62 } 63 return results[position]; 64 65 } 66 67 public void reset() 68 { 69 position = -1; 70 } 71 72 private void clear() 73 { 74 position = -1; 75 last = -1; 76 results = null; 77 } 78 79 84 public void seek(Term term) throws IOException 85 { 86 delegate.seek(term); 87 clear(); 88 } 89 90 95 public void seek(TermEnum termEnum) throws IOException 96 { 97 delegate.seek(termEnum); 98 clear(); 99 } 100 101 106 public int doc() 107 { 108 return delegate.doc(); 109 } 110 111 116 public int freq() 117 { 118 return delegate.freq(); 119 } 120 121 126 public boolean next() throws IOException 127 { 128 if (delegate.next()) 129 { 130 clear(); 131 return true; 132 } 133 else 134 { 135 return false; 136 } 137 } 138 139 144 public int read(int[] docs, int[] freqs) throws IOException 145 { 146 int answer = delegate.read(docs, freqs); 147 clear(); 148 return answer; 149 } 150 151 156 public boolean skipTo(int target) throws IOException 157 { 158 if (delegate.skipTo(target)) 159 { 160 clear(); 161 return true; 162 } 163 else 164 { 165 return false; 166 } 167 } 168 169 174 public void close() throws IOException 175 { 176 delegate.close(); 177 clear(); 178 } 179 180 } | Popular Tags |