KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > search > impl > lucene > query > DeltaReader


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.search.impl.lucene.query;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Arrays JavaDoc;
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 JavaDoc 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 JavaDoc
42     {
43         super(readers);
44         this.deletions = deletions;
45         initialize(readers);
46     }
47
48     private void initialize(IndexReader[] subReaders) throws IOException JavaDoc
49     {
50         this.subReaders = subReaders;
51         starts = new int[subReaders.length + 1]; // build starts array
52
for (int i = 0; i < subReaders.length; i++)
53         {
54             starts[i] = maxDoc;
55             maxDoc += subReaders[i].maxDoc(); // compute maxDocs
56
}
57         starts[subReaders.length] = maxDoc;
58     }
59
60     protected void doCommit() throws IOException JavaDoc
61     {
62         // TODO Auto-generated method stub
63
throw new UnsupportedOperationException JavaDoc();
64     }
65
66     protected void doDelete(int arg0) throws IOException JavaDoc
67     {
68         // TODO Auto-generated method stub
69
throw new UnsupportedOperationException JavaDoc();
70     }
71
72     protected void doUndeleteAll() throws IOException JavaDoc
73     {
74         // TODO Auto-generated method stub
75
throw new UnsupportedOperationException JavaDoc();
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 JavaDoc(true);
92                     break;
93                 }
94             }
95             hasExclusions = new Boolean JavaDoc(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     { // find reader for doc n:
108
int lo = 0; // search starts array
109
int hi = subReaders.length - 1; // for first element less
110

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             { // found a match
121
while (mid + 1 < subReaders.length && starts[mid + 1] == midValue)
122                 {
123                     mid++; // scan to last match
124
}
125                 return mid;
126             }
127         }
128         return hi;
129     }
130
131     public TermDocs termDocs() throws IOException JavaDoc
132     {
133         return new DeletingTermDocs(super.termDocs());
134     }
135
136     public TermPositions termPositions() throws IOException JavaDoc
137     {
138         // TODO Auto-generated method stub
139
throw new UnsupportedOperationException JavaDoc();
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 JavaDoc
153         {
154             delegate.seek(term);
155         }
156
157         public void seek(TermEnum termEnum) throws IOException JavaDoc
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 JavaDoc
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 JavaDoc
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             // fix up for deleted
206
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 JavaDoc
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 JavaDoc
233         {
234             delegate.close();
235         }
236
237     }
238 }
239
Popular Tags