1 17 package org.alfresco.repo.search.impl.lucene.query; 18 19 import java.io.IOException ; 20 import java.util.Arrays ; 21 22 import org.apache.lucene.index.IndexReader; 23 import org.apache.lucene.index.MultiReader; 24 import org.apache.lucene.index.Term; 25 import org.apache.lucene.index.TermDocs; 26 import org.apache.lucene.index.TermEnum; 27 import org.apache.lucene.index.TermPositions; 28 29 public class DeltaReader extends MultiReader 30 { 31 int[][] deletions; 32 33 Boolean hasExclusions = null; 34 35 private IndexReader[] subReaders; 36 37 private int maxDoc = 0; 38 39 private int[] starts; 40 41 public DeltaReader(IndexReader[] readers, int[][] deletions) throws IOException 42 { 43 super(readers); 44 this.deletions = deletions; 45 initialize(readers); 46 } 47 48 private void initialize(IndexReader[] subReaders) throws IOException 49 { 50 this.subReaders = subReaders; 51 starts = new int[subReaders.length + 1]; for (int i = 0; i < subReaders.length; i++) 53 { 54 starts[i] = maxDoc; 55 maxDoc += subReaders[i].maxDoc(); } 57 starts[subReaders.length] = maxDoc; 58 } 59 60 protected void doCommit() throws IOException 61 { 62 throw new UnsupportedOperationException (); 64 } 65 66 protected void doDelete(int arg0) throws IOException 67 { 68 throw new UnsupportedOperationException (); 70 } 71 72 protected void doUndeleteAll() throws IOException 73 { 74 throw new UnsupportedOperationException (); 76 } 77 78 public boolean hasDeletions() 79 { 80 return super.hasDeletions() || hasSearchExclusions(); 81 } 82 83 private boolean hasSearchExclusions() 84 { 85 if (hasExclusions == null) 86 { 87 for (int i = 0; i < deletions.length; i++) 88 { 89 if (deletions[i].length > 0) 90 { 91 hasExclusions = new Boolean (true); 92 break; 93 } 94 } 95 hasExclusions = new Boolean (false); 96 } 97 return hasExclusions.booleanValue(); 98 } 99 100 public boolean isDeleted(int docNumber) 101 { 102 int i = readerIndex(docNumber); 103 return super.isDeleted(docNumber) || (Arrays.binarySearch(deletions[i], docNumber - starts[i]) != -1); 104 } 105 106 private int readerIndex(int n) 107 { int lo = 0; int hi = subReaders.length - 1; 111 while (hi >= lo) 112 { 113 int mid = (lo + hi) >> 1; 114 int midValue = starts[mid]; 115 if (n < midValue) 116 hi = mid - 1; 117 else if (n > midValue) 118 lo = mid + 1; 119 else 120 { while (mid + 1 < subReaders.length && starts[mid + 1] == midValue) 122 { 123 mid++; } 125 return mid; 126 } 127 } 128 return hi; 129 } 130 131 public TermDocs termDocs() throws IOException 132 { 133 return new DeletingTermDocs(super.termDocs()); 134 } 135 136 public TermPositions termPositions() throws IOException 137 { 138 throw new UnsupportedOperationException (); 140 } 141 142 private class DeletingTermDocs implements TermDocs 143 { 144 TermDocs delegate; 145 146 DeletingTermDocs(TermDocs delegate) 147 { 148 super(); 149 this.delegate = delegate; 150 } 151 152 public void seek(Term term) throws IOException 153 { 154 delegate.seek(term); 155 } 156 157 public void seek(TermEnum termEnum) throws IOException 158 { 159 delegate.seek(termEnum); 160 } 161 162 public int doc() 163 { 164 return delegate.doc(); 165 } 166 167 public int freq() 168 { 169 return delegate.freq(); 170 } 171 172 public boolean next() throws IOException 173 { 174 while (delegate.next()) 175 { 176 if (!isDeleted(doc())) 177 { 178 return true; 179 } 180 } 181 return false; 182 } 183 184 public int read(int[] docs, int[] freqs) throws IOException 185 { 186 int end; 187 int deletedCount; 188 do 189 { 190 end = delegate.read(docs, freqs); 191 if (end == 0) 192 { 193 return end; 194 } 195 deletedCount = 0; 196 for (int i = 0; i < end; i++) 197 { 198 if (!isDeleted(docs[i])) 199 { 200 deletedCount++; 201 } 202 } 203 } 204 while (end == deletedCount); 205 int position = 0; 207 for(int i = 0; i < end; i++) 208 { 209 if(!isDeleted(i)) 210 { 211 docs[position] = docs[i]; 212 freqs[position] = freqs[i]; 213 position++; 214 } 215 } 216 return position; 217 } 218 219 public boolean skipTo(int docNumber) throws IOException 220 { 221 delegate.skipTo(docNumber); 222 if (!isDeleted(doc())) 223 { 224 return true; 225 } 226 else 227 { 228 return next(); 229 } 230 } 231 232 public void close() throws IOException 233 { 234 delegate.close(); 235 } 236 237 } 238 } 239 | Popular Tags |