KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > index > FieldInfos


1 package org.apache.lucene.index;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.util.*;
20 import java.io.IOException JavaDoc;
21
22 import org.apache.lucene.document.Document;
23 import org.apache.lucene.document.Field;
24
25 import org.apache.lucene.store.Directory;
26 import org.apache.lucene.store.IndexOutput;
27 import org.apache.lucene.store.IndexInput;
28
29 /** Access to the Field Info file that describes document fields and whether or
30  * not they are indexed. Each segment has a separate Field Info file. Objects
31  * of this class are thread-safe for multiple readers, but only one thread can
32  * be adding documents at a time, with no other reader or writer threads
33  * accessing this object.
34  */

35 final class FieldInfos {
36   
37   static final byte IS_INDEXED = 0x1;
38   static final byte STORE_TERMVECTOR = 0x2;
39   static final byte STORE_POSITIONS_WITH_TERMVECTOR = 0x4;
40   static final byte STORE_OFFSET_WITH_TERMVECTOR = 0x8;
41   static final byte OMIT_NORMS = 0x10;
42   
43   private ArrayList byNumber = new ArrayList();
44   private HashMap byName = new HashMap();
45
46   FieldInfos() { }
47
48   /**
49    * Construct a FieldInfos object using the directory and the name of the file
50    * IndexInput
51    * @param d The directory to open the IndexInput from
52    * @param name The name of the file to open the IndexInput from in the Directory
53    * @throws IOException
54    */

55   FieldInfos(Directory d, String JavaDoc name) throws IOException JavaDoc {
56     IndexInput input = d.openInput(name);
57     try {
58       read(input);
59     } finally {
60       input.close();
61     }
62   }
63
64   /** Adds field info for a Document. */
65   public void add(Document doc) {
66     Enumeration fields = doc.fields();
67     while (fields.hasMoreElements()) {
68       Field field = (Field) fields.nextElement();
69       add(field.name(), field.isIndexed(), field.isTermVectorStored(), field.isStorePositionWithTermVector(),
70               field.isStoreOffsetWithTermVector(), field.getOmitNorms());
71     }
72   }
73   
74   /**
75    * Add fields that are indexed. Whether they have termvectors has to be specified.
76    *
77    * @param names The names of the fields
78    * @param storeTermVectors Whether the fields store term vectors or not
79    * @param storePositionWithTermVector treu if positions should be stored.
80    * @param storeOffsetWithTermVector true if offsets should be stored
81    */

82   public void addIndexed(Collection names, boolean storeTermVectors, boolean storePositionWithTermVector,
83                          boolean storeOffsetWithTermVector) {
84     Iterator i = names.iterator();
85     while (i.hasNext()) {
86       add((String JavaDoc)i.next(), true, storeTermVectors, storePositionWithTermVector, storeOffsetWithTermVector);
87     }
88   }
89
90   /**
91    * Assumes the fields are not storing term vectors.
92    *
93    * @param names The names of the fields
94    * @param isIndexed Whether the fields are indexed or not
95    *
96    * @see #add(String, boolean)
97    */

98   public void add(Collection names, boolean isIndexed) {
99     Iterator i = names.iterator();
100     while (i.hasNext()) {
101       add((String JavaDoc)i.next(), isIndexed);
102     }
103   }
104
105   /**
106    * Calls 5 parameter add with false for all TermVector parameters.
107    *
108    * @param name The name of the Field
109    * @param isIndexed true if the field is indexed
110    * @see #add(String, boolean, boolean, boolean, boolean)
111    */

112   public void add(String JavaDoc name, boolean isIndexed) {
113     add(name, isIndexed, false, false, false, false);
114   }
115
116   /**
117    * Calls 5 parameter add with false for term vector positions and offsets.
118    *
119    * @param name The name of the field
120    * @param isIndexed true if the field is indexed
121    * @param storeTermVector true if the term vector should be stored
122    */

123   public void add(String JavaDoc name, boolean isIndexed, boolean storeTermVector){
124     add(name, isIndexed, storeTermVector, false, false, false);
125   }
126   
127   /** If the field is not yet known, adds it. If it is known, checks to make
128    * sure that the isIndexed flag is the same as was given previously for this
129    * field. If not - marks it as being indexed. Same goes for the TermVector
130    * parameters.
131    *
132    * @param name The name of the field
133    * @param isIndexed true if the field is indexed
134    * @param storeTermVector true if the term vector should be stored
135    * @param storePositionWithTermVector true if the term vector with positions should be stored
136    * @param storeOffsetWithTermVector true if the term vector with offsets should be stored
137    */

138   public void add(String JavaDoc name, boolean isIndexed, boolean storeTermVector,
139                   boolean storePositionWithTermVector, boolean storeOffsetWithTermVector) {
140
141     add(name, isIndexed, storeTermVector, storePositionWithTermVector, storeOffsetWithTermVector, false);
142   }
143
144     /** If the field is not yet known, adds it. If it is known, checks to make
145    * sure that the isIndexed flag is the same as was given previously for this
146    * field. If not - marks it as being indexed. Same goes for the TermVector
147    * parameters.
148    *
149    * @param name The name of the field
150    * @param isIndexed true if the field is indexed
151    * @param storeTermVector true if the term vector should be stored
152    * @param storePositionWithTermVector true if the term vector with positions should be stored
153    * @param storeOffsetWithTermVector true if the term vector with offsets should be stored
154    * @param omitNorms true if the norms for the indexed field should be omitted
155    */

156   public void add(String JavaDoc name, boolean isIndexed, boolean storeTermVector,
157                   boolean storePositionWithTermVector, boolean storeOffsetWithTermVector, boolean omitNorms) {
158     FieldInfo fi = fieldInfo(name);
159     if (fi == null) {
160       addInternal(name, isIndexed, storeTermVector, storePositionWithTermVector, storeOffsetWithTermVector, omitNorms);
161     } else {
162       if (fi.isIndexed != isIndexed) {
163         fi.isIndexed = true; // once indexed, always index
164
}
165       if (fi.storeTermVector != storeTermVector) {
166         fi.storeTermVector = true; // once vector, always vector
167
}
168       if (fi.storePositionWithTermVector != storePositionWithTermVector) {
169         fi.storePositionWithTermVector = true; // once vector, always vector
170
}
171       if (fi.storeOffsetWithTermVector != storeOffsetWithTermVector) {
172         fi.storeOffsetWithTermVector = true; // once vector, always vector
173
}
174       if (fi.omitNorms != omitNorms) {
175         fi.omitNorms = false; // once norms are stored, always store
176
}
177
178     }
179   }
180
181
182   private void addInternal(String JavaDoc name, boolean isIndexed,
183                            boolean storeTermVector, boolean storePositionWithTermVector,
184                            boolean storeOffsetWithTermVector, boolean omitNorms) {
185     FieldInfo fi =
186       new FieldInfo(name, isIndexed, byNumber.size(), storeTermVector, storePositionWithTermVector,
187               storeOffsetWithTermVector, omitNorms);
188     byNumber.add(fi);
189     byName.put(name, fi);
190   }
191
192   public int fieldNumber(String JavaDoc fieldName) {
193     try {
194       FieldInfo fi = fieldInfo(fieldName);
195       if (fi != null)
196         return fi.number;
197     }
198     catch (IndexOutOfBoundsException JavaDoc ioobe) {
199       return -1;
200     }
201     return -1;
202   }
203
204   public FieldInfo fieldInfo(String JavaDoc fieldName) {
205     return (FieldInfo) byName.get(fieldName);
206   }
207
208   /**
209    * Return the fieldName identified by its number.
210    *
211    * @param fieldNumber
212    * @return the fieldName or an empty string when the field
213    * with the given number doesn't exist.
214    */

215   public String JavaDoc fieldName(int fieldNumber) {
216     try {
217       return fieldInfo(fieldNumber).name;
218     }
219     catch (NullPointerException JavaDoc npe) {
220       return "";
221     }
222   }
223
224   /**
225    * Return the fieldinfo object referenced by the fieldNumber.
226    * @param fieldNumber
227    * @return the FieldInfo object or null when the given fieldNumber
228    * doesn't exist.
229    */

230   public FieldInfo fieldInfo(int fieldNumber) {
231     try {
232       return (FieldInfo) byNumber.get(fieldNumber);
233     }
234     catch (IndexOutOfBoundsException JavaDoc ioobe) {
235       return null;
236     }
237   }
238
239   public int size() {
240     return byNumber.size();
241   }
242
243   public boolean hasVectors() {
244     boolean hasVectors = false;
245     for (int i = 0; i < size(); i++) {
246       if (fieldInfo(i).storeTermVector) {
247         hasVectors = true;
248         break;
249       }
250     }
251     return hasVectors;
252   }
253
254   public void write(Directory d, String JavaDoc name) throws IOException JavaDoc {
255     IndexOutput output = d.createOutput(name);
256     try {
257       write(output);
258     } finally {
259       output.close();
260     }
261   }
262
263   public void write(IndexOutput output) throws IOException JavaDoc {
264     output.writeVInt(size());
265     for (int i = 0; i < size(); i++) {
266       FieldInfo fi = fieldInfo(i);
267       byte bits = 0x0;
268       if (fi.isIndexed) bits |= IS_INDEXED;
269       if (fi.storeTermVector) bits |= STORE_TERMVECTOR;
270       if (fi.storePositionWithTermVector) bits |= STORE_POSITIONS_WITH_TERMVECTOR;
271       if (fi.storeOffsetWithTermVector) bits |= STORE_OFFSET_WITH_TERMVECTOR;
272       if (fi.omitNorms) bits |= OMIT_NORMS;
273       output.writeString(fi.name);
274       output.writeByte(bits);
275     }
276   }
277
278   private void read(IndexInput input) throws IOException JavaDoc {
279     int size = input.readVInt();//read in the size
280
for (int i = 0; i < size; i++) {
281       String JavaDoc name = input.readString().intern();
282       byte bits = input.readByte();
283       boolean isIndexed = (bits & IS_INDEXED) != 0;
284       boolean storeTermVector = (bits & STORE_TERMVECTOR) != 0;
285       boolean storePositionsWithTermVector = (bits & STORE_POSITIONS_WITH_TERMVECTOR) != 0;
286       boolean storeOffsetWithTermVector = (bits & STORE_OFFSET_WITH_TERMVECTOR) != 0;
287       boolean omitNorms = (bits & OMIT_NORMS) != 0;
288
289       addInternal(name, isIndexed, storeTermVector, storePositionsWithTermVector, storeOffsetWithTermVector, omitNorms);
290     }
291   }
292
293 }
294
Popular Tags