cvsimport
[beagle.git] / beagled / Lucene.Net / Index / FieldInfos.cs
blobaa9c486c42b9de7f9d4de6426ba51d4ff6020f00
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 using System;
18 using Document = Lucene.Net.Documents.Document;
19 using Field = Lucene.Net.Documents.Field;
20 using Directory = Lucene.Net.Store.Directory;
21 using IndexInput = Lucene.Net.Store.IndexInput;
22 using IndexOutput = Lucene.Net.Store.IndexOutput;
24 namespace Lucene.Net.Index
27 /// <summary>Access to the Field Info file that describes document fields and whether or
28 /// not they are indexed. Each segment has a separate Field Info file. Objects
29 /// of this class are thread-safe for multiple readers, but only one thread can
30 /// be adding documents at a time, with no other reader or writer threads
31 /// accessing this object.
32 /// </summary>
33 public sealed class FieldInfos
36 internal const byte IS_INDEXED = (byte) (0x1);
37 internal const byte STORE_TERMVECTOR = (byte) (0x2);
38 internal const byte STORE_POSITIONS_WITH_TERMVECTOR = (byte) (0x4);
39 internal const byte STORE_OFFSET_WITH_TERMVECTOR = (byte) (0x8);
40 internal const byte OMIT_NORMS = (byte) (0x10);
42 private System.Collections.ArrayList byNumber = new System.Collections.ArrayList();
43 private System.Collections.Hashtable byName = new System.Collections.Hashtable();
45 public /*internal*/ FieldInfos()
49 /// <summary> Construct a FieldInfos object using the directory and the name of the file
50 /// IndexInput
51 /// </summary>
52 /// <param name="d">The directory to open the IndexInput from
53 /// </param>
54 /// <param name="name">The name of the file to open the IndexInput from in the Directory
55 /// </param>
56 /// <throws> IOException </throws>
57 public /*internal*/ FieldInfos(Directory d, System.String name)
59 IndexInput input = d.OpenInput(name);
60 try
62 Read(input);
64 finally
66 input.Close();
70 /// <summary>Adds field info for a Document. </summary>
71 public void Add(Document doc)
73 foreach(Field field in doc.Fields())
75 Add(field.Name(), field.IsIndexed(), field.IsTermVectorStored(), field.IsStorePositionWithTermVector(), field.IsStoreOffsetWithTermVector(), field.GetOmitNorms());
79 /// <summary> Add fields that are indexed. Whether they have termvectors has to be specified.
80 ///
81 /// </summary>
82 /// <param name="names">The names of the fields
83 /// </param>
84 /// <param name="storeTermVectors">Whether the fields store term vectors or not
85 /// </param>
86 /// <param name="storePositionWithTermVector">treu if positions should be stored.
87 /// </param>
88 /// <param name="storeOffsetWithTermVector">true if offsets should be stored
89 /// </param>
90 public void AddIndexed(System.Collections.ICollection names, bool storeTermVectors, bool storePositionWithTermVector, bool storeOffsetWithTermVector)
92 System.Collections.IEnumerator i = names.GetEnumerator();
93 while (i.MoveNext())
95 System.Collections.DictionaryEntry t = (System.Collections.DictionaryEntry) i.Current;
96 Add((System.String) t.Key, true, storeTermVectors, storePositionWithTermVector, storeOffsetWithTermVector);
100 /// <summary> Assumes the fields are not storing term vectors.
101 ///
102 /// </summary>
103 /// <param name="names">The names of the fields
104 /// </param>
105 /// <param name="isIndexed">Whether the fields are indexed or not
106 ///
107 /// </param>
108 /// <seealso cref="Add(String, boolean)">
109 /// </seealso>
110 public void Add(System.Collections.ICollection names, bool isIndexed)
112 System.Collections.IEnumerator i = names.GetEnumerator();
113 while (i.MoveNext())
115 System.Collections.DictionaryEntry t = (System.Collections.DictionaryEntry) i.Current;
116 Add((System.String) t.Key, isIndexed);
120 /// <summary> Calls 5 parameter add with false for all TermVector parameters.
121 ///
122 /// </summary>
123 /// <param name="name">The name of the Field
124 /// </param>
125 /// <param name="isIndexed">true if the field is indexed
126 /// </param>
127 /// <seealso cref="Add(String, boolean, boolean, boolean, boolean)">
128 /// </seealso>
129 public void Add(System.String name, bool isIndexed)
131 Add(name, isIndexed, false, false, false, false);
134 /// <summary> Calls 5 parameter add with false for term vector positions and offsets.
135 ///
136 /// </summary>
137 /// <param name="name">The name of the field
138 /// </param>
139 /// <param name="isIndexed"> true if the field is indexed
140 /// </param>
141 /// <param name="storeTermVector">true if the term vector should be stored
142 /// </param>
143 public void Add(System.String name, bool isIndexed, bool storeTermVector)
145 Add(name, isIndexed, storeTermVector, false, false, false);
148 /// <summary>If the field is not yet known, adds it. If it is known, checks to make
149 /// sure that the isIndexed flag is the same as was given previously for this
150 /// field. If not - marks it as being indexed. Same goes for the TermVector
151 /// parameters.
152 ///
153 /// </summary>
154 /// <param name="name">The name of the field
155 /// </param>
156 /// <param name="isIndexed">true if the field is indexed
157 /// </param>
158 /// <param name="storeTermVector">true if the term vector should be stored
159 /// </param>
160 /// <param name="storePositionWithTermVector">true if the term vector with positions should be stored
161 /// </param>
162 /// <param name="storeOffsetWithTermVector">true if the term vector with offsets should be stored
163 /// </param>
164 public void Add(System.String name, bool isIndexed, bool storeTermVector, bool storePositionWithTermVector, bool storeOffsetWithTermVector)
167 Add(name, isIndexed, storeTermVector, storePositionWithTermVector, storeOffsetWithTermVector, false);
170 /// <summary>If the field is not yet known, adds it. If it is known, checks to make
171 /// sure that the isIndexed flag is the same as was given previously for this
172 /// field. If not - marks it as being indexed. Same goes for the TermVector
173 /// parameters.
174 ///
175 /// </summary>
176 /// <param name="name">The name of the field
177 /// </param>
178 /// <param name="isIndexed">true if the field is indexed
179 /// </param>
180 /// <param name="storeTermVector">true if the term vector should be stored
181 /// </param>
182 /// <param name="storePositionWithTermVector">true if the term vector with positions should be stored
183 /// </param>
184 /// <param name="storeOffsetWithTermVector">true if the term vector with offsets should be stored
185 /// </param>
186 /// <param name="omitNorms">true if the norms for the indexed field should be omitted
187 /// </param>
188 public void Add(System.String name, bool isIndexed, bool storeTermVector, bool storePositionWithTermVector, bool storeOffsetWithTermVector, bool omitNorms)
190 FieldInfo fi = FieldInfo(name);
191 if (fi == null)
193 AddInternal(name, isIndexed, storeTermVector, storePositionWithTermVector, storeOffsetWithTermVector, omitNorms);
195 else
197 if (fi.isIndexed != isIndexed)
199 fi.isIndexed = true; // once indexed, always index
201 if (fi.storeTermVector != storeTermVector)
203 fi.storeTermVector = true; // once vector, always vector
205 if (fi.storePositionWithTermVector != storePositionWithTermVector)
207 fi.storePositionWithTermVector = true; // once vector, always vector
209 if (fi.storeOffsetWithTermVector != storeOffsetWithTermVector)
211 fi.storeOffsetWithTermVector = true; // once vector, always vector
213 if (fi.omitNorms != omitNorms)
215 fi.omitNorms = false; // once norms are stored, always store
221 private void AddInternal(System.String name, bool isIndexed, bool storeTermVector, bool storePositionWithTermVector, bool storeOffsetWithTermVector, bool omitNorms)
223 FieldInfo fi = new FieldInfo(name, isIndexed, byNumber.Count, storeTermVector, storePositionWithTermVector, storeOffsetWithTermVector, omitNorms);
224 byNumber.Add(fi);
225 byName[name] = fi;
228 public int FieldNumber(System.String fieldName)
230 FieldInfo fi = FieldInfo(fieldName);
231 if (fi != null)
232 return fi.number;
233 return - 1;
236 public FieldInfo FieldInfo(System.String fieldName)
238 return (FieldInfo) byName[fieldName];
241 /// <summary> Return the fieldName identified by its number.
242 ///
243 /// </summary>
244 /// <param name="fieldNumber">
245 /// </param>
246 /// <returns> the fieldName or an empty string when the field
247 /// with the given number doesn't exist.
248 /// </returns>
249 public System.String FieldName(int fieldNumber)
251 FieldInfo info = FieldInfo(fieldNumber);
252 if (info == null)
253 return "";
254 else
255 return info.name;
258 /// <summary> Return the fieldinfo object referenced by the fieldNumber.</summary>
259 /// <param name="fieldNumber">
260 /// </param>
261 /// <returns> the FieldInfo object or null when the given fieldNumber
262 /// doesn't exist.
263 /// </returns>
264 public FieldInfo FieldInfo(int fieldNumber)
266 if (fieldNumber < 0 || fieldNumber >= byNumber.Count)
268 return null;
270 return (FieldInfo) byNumber[fieldNumber];
273 public int Size()
275 return byNumber.Count;
278 public bool HasVectors()
280 bool hasVectors = false;
281 for (int i = 0; i < Size(); i++)
283 if (FieldInfo(i).storeTermVector)
285 hasVectors = true;
286 break;
289 return hasVectors;
292 public void Write(Directory d, System.String name)
294 IndexOutput output = d.CreateOutput(name);
297 Write(output);
299 finally
301 output.Close();
305 public void Write(IndexOutput output)
307 output.WriteVInt(Size());
308 for (int i = 0; i < Size(); i++)
310 FieldInfo fi = FieldInfo(i);
311 byte bits = (byte) (0x0);
312 if (fi.isIndexed)
313 bits |= IS_INDEXED;
314 if (fi.storeTermVector)
315 bits |= STORE_TERMVECTOR;
316 if (fi.storePositionWithTermVector)
317 bits |= STORE_POSITIONS_WITH_TERMVECTOR;
318 if (fi.storeOffsetWithTermVector)
319 bits |= STORE_OFFSET_WITH_TERMVECTOR;
320 if (fi.omitNorms)
321 bits |= OMIT_NORMS;
322 output.WriteString(fi.name);
323 output.WriteByte(bits);
327 private void Read(IndexInput input)
329 int size = input.ReadVInt(); //read in the size
330 for (int i = 0; i < size; i++)
332 System.String name = String.Intern(input.ReadString());
333 byte bits = input.ReadByte();
334 bool isIndexed = (bits & IS_INDEXED) != 0;
335 bool storeTermVector = (bits & STORE_TERMVECTOR) != 0;
336 bool storePositionsWithTermVector = (bits & STORE_POSITIONS_WITH_TERMVECTOR) != 0;
337 bool storeOffsetWithTermVector = (bits & STORE_OFFSET_WITH_TERMVECTOR) != 0;
338 bool omitNorms = (bits & OMIT_NORMS) != 0;
340 AddInternal(name, isIndexed, storeTermVector, storePositionsWithTermVector, storeOffsetWithTermVector, omitNorms);