Update the thread-local storage patch, to fix #335178
[beagle.git] / beagled / Lucene.Net / Index / FieldInfos.cs
blob5a2d9b414969346a88b372395f7a848b298b0d9d
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.
16 using System;
17 using Document = Lucene.Net.Documents.Document;
18 using Field = Lucene.Net.Documents.Field;
19 using Directory = Lucene.Net.Store.Directory;
20 using IndexInput = Lucene.Net.Store.IndexInput;
21 using IndexOutput = Lucene.Net.Store.IndexOutput;
22 namespace Lucene.Net.Index
25 /// <summary>Access to the Field Info file that describes document fields and whether or
26 /// not they are indexed. Each segment has a separate Field Info file. Objects
27 /// of this class are thread-safe for multiple readers, but only one thread can
28 /// be adding documents at a time, with no other reader or writer threads
29 /// accessing this object.
30 /// </summary>
31 sealed public class FieldInfos
34 internal const byte IS_INDEXED = (byte) (0x1);
35 internal const byte STORE_TERMVECTOR = (byte) (0x2);
36 internal const byte STORE_POSITIONS_WITH_TERMVECTOR = (byte) (0x4);
37 internal const byte STORE_OFFSET_WITH_TERMVECTOR = (byte) (0x8);
39 private System.Collections.ArrayList byNumber = new System.Collections.ArrayList();
40 private System.Collections.Hashtable byName = new System.Collections.Hashtable();
42 public /*internal*/ FieldInfos()
46 /// <summary> Construct a FieldInfos object using the directory and the name of the file
47 /// IndexInput
48 /// </summary>
49 /// <param name="d">The directory to open the IndexInput from
50 /// </param>
51 /// <param name="name">The name of the file to open the IndexInput from in the Directory
52 /// </param>
53 /// <throws> IOException </throws>
54 public /*internal*/ FieldInfos(Directory d, System.String name)
56 IndexInput input = d.OpenInput(name);
57 try
59 Read(input);
61 finally
63 input.Close();
67 /// <summary>Adds Field info for a Document. </summary>
68 public void Add(Document doc)
70 foreach (Field field in doc.Fields())
72 Add(field.Name(), field.IsIndexed(), field.IsTermVectorStored(), field.IsStorePositionWithTermVector(), field.IsStoreOffsetWithTermVector());
76 /// <summary> Add fields that are indexed. Whether they have termvectors has to be specified.
77 ///
78 /// </summary>
79 /// <param name="names">The names of the fields
80 /// </param>
81 /// <param name="storeTermVectors">Whether the fields store term vectors or not
82 /// </param>
83 /// <param name="storePositionWithTermVector">treu if positions should be stored.
84 /// </param>
85 /// <param name="storeOffsetWithTermVector">true if offsets should be stored
86 /// </param>
87 public void AddIndexed(System.Collections.ICollection names, bool storeTermVectors, bool storePositionWithTermVector, bool storeOffsetWithTermVector)
89 System.Collections.IEnumerator i = names.GetEnumerator();
90 while (i.MoveNext())
92 System.Collections.DictionaryEntry t = (System.Collections.DictionaryEntry) i.Current;
93 Add((System.String) t.Key, true, storeTermVectors, storePositionWithTermVector, storeOffsetWithTermVector);
97 /// <summary> Assumes the fields are not storing term vectors.
98 ///
99 /// </summary>
100 /// <param name="names">The names of the fields
101 /// </param>
102 /// <param name="isIndexed">Whether the fields are indexed or not
103 ///
104 /// </param>
105 /// <seealso cref="boolean)">
106 /// </seealso>
107 public void Add(System.Collections.ICollection names, bool isIndexed)
109 System.Collections.IEnumerator i = names.GetEnumerator();
110 int j = 0;
111 while (i.MoveNext())
113 System.Collections.DictionaryEntry t = (System.Collections.DictionaryEntry) i.Current;
114 Add((System.String) t.Key, isIndexed);
118 /// <summary> Calls 5 parameter add with false for all TermVector parameters.
119 ///
120 /// </summary>
121 /// <param name="name">The name of the Field
122 /// </param>
123 /// <param name="isIndexed">true if the field is indexed
124 /// </param>
125 /// <seealso cref="boolean, boolean, boolean, boolean)">
126 /// </seealso>
127 public void Add(System.String name, bool isIndexed)
129 Add(name, isIndexed, false, false, false);
132 /// <summary> Calls 5 parameter add with false for term vector positions and offsets.
133 ///
134 /// </summary>
135 /// <param name="name">The name of the field
136 /// </param>
137 /// <param name="isIndexed"> true if the field is indexed
138 /// </param>
139 /// <param name="storeTermVector">true if the term vector should be stored
140 /// </param>
141 public void Add(System.String name, bool isIndexed, bool storeTermVector)
143 Add(name, isIndexed, storeTermVector, false, false);
146 /// <summary>If the field is not yet known, adds it. If it is known, checks to make
147 /// sure that the isIndexed flag is the same as was given previously for this
148 /// field. If not - marks it as being indexed. Same goes for the TermVector
149 /// parameters.
150 ///
151 /// </summary>
152 /// <param name="name">The name of the field
153 /// </param>
154 /// <param name="isIndexed">true if the field is indexed
155 /// </param>
156 /// <param name="storeTermVector">true if the term vector should be stored
157 /// </param>
158 /// <param name="storePositionWithTermVector">true if the term vector with positions should be stored
159 /// </param>
160 /// <param name="storeOffsetWithTermVector">true if the term vector with offsets should be stored
161 /// </param>
162 public void Add(System.String name, bool isIndexed, bool storeTermVector, bool storePositionWithTermVector, bool storeOffsetWithTermVector)
164 FieldInfo fi = FieldInfo(name);
165 if (fi == null)
167 AddInternal(name, isIndexed, storeTermVector, storePositionWithTermVector, storeOffsetWithTermVector);
169 else
171 if (fi.isIndexed != isIndexed)
173 fi.isIndexed = true; // once indexed, always index
175 if (fi.storeTermVector != storeTermVector)
177 fi.storeTermVector = true; // once vector, always vector
179 if (fi.storePositionWithTermVector != storePositionWithTermVector)
181 fi.storePositionWithTermVector = true; // once vector, always vector
183 if (fi.storeOffsetWithTermVector != storeOffsetWithTermVector)
185 fi.storeOffsetWithTermVector = true; // once vector, always vector
190 private void AddInternal(System.String name, bool isIndexed, bool storeTermVector, bool storePositionWithTermVector, bool storeOffsetWithTermVector)
192 FieldInfo fi = new FieldInfo(name, isIndexed, byNumber.Count, storeTermVector, storePositionWithTermVector, storeOffsetWithTermVector);
193 byNumber.Add(fi);
194 byName[name] = fi;
197 public int FieldNumber(System.String fieldName)
199 FieldInfo fi = FieldInfo(fieldName);
200 if (fi != null)
201 return fi.number;
202 return - 1;
205 public FieldInfo FieldInfo(System.String fieldName)
207 return (FieldInfo) byName[fieldName];
210 /// <summary> Return the fieldName identified by its number.
211 ///
212 /// </summary>
213 /// <param name="">fieldNumber
214 /// </param>
215 /// <returns> the fieldName or an empty string when the field
216 /// with the given number doesn't exist.
217 /// </returns>
218 public System.String FieldName(int fieldNumber)
220 FieldInfo info = FieldInfo(fieldNumber);
221 if (info == null)
222 return "";
223 else
224 return info.name;
227 /// <summary> Return the fieldinfo object referenced by the fieldNumber.</summary>
228 /// <param name="">fieldNumber
229 /// </param>
230 /// <returns> the FieldInfo object or null when the given fieldNumber
231 /// doesn't exist.
232 /// </returns>
233 public FieldInfo FieldInfo(int fieldNumber)
235 if (fieldNumber < 0 || fieldNumber >= byNumber.Count)
237 return null;
240 return (FieldInfo) byNumber[fieldNumber];
243 public int Size()
245 return byNumber.Count;
248 public bool HasVectors()
250 bool hasVectors = false;
251 for (int i = 0; i < Size(); i++)
253 if (FieldInfo(i).storeTermVector)
255 hasVectors = true;
256 break;
259 return hasVectors;
262 public void Write(Directory d, System.String name)
264 IndexOutput output = d.CreateOutput(name);
267 Write(output);
269 finally
271 output.Close();
275 public void Write(IndexOutput output)
277 output.WriteVInt(Size());
278 for (int i = 0; i < Size(); i++)
280 FieldInfo fi = FieldInfo(i);
281 byte bits = (byte) (0x0);
282 if (fi.isIndexed)
283 bits |= IS_INDEXED;
284 if (fi.storeTermVector)
285 bits |= STORE_TERMVECTOR;
286 if (fi.storePositionWithTermVector)
287 bits |= STORE_POSITIONS_WITH_TERMVECTOR;
288 if (fi.storeOffsetWithTermVector)
289 bits |= STORE_OFFSET_WITH_TERMVECTOR;
290 output.WriteString(fi.name);
291 output.WriteByte(bits);
295 private void Read(IndexInput input)
297 int size = input.ReadVInt(); //read in the size
298 for (int i = 0; i < size; i++)
300 System.String name = String.Intern(input.ReadString());
301 byte bits = input.ReadByte();
302 bool isIndexed = (bits & IS_INDEXED) != 0;
303 bool storeTermVector = (bits & STORE_TERMVECTOR) != 0;
304 bool storePositionsWithTermVector = (bits & STORE_POSITIONS_WITH_TERMVECTOR) != 0;
305 bool storeOffsetWithTermVector = (bits & STORE_OFFSET_WITH_TERMVECTOR) != 0;
306 AddInternal(name, isIndexed, storeTermVector, storePositionsWithTermVector, storeOffsetWithTermVector);