Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / Lucene.Net / Index / FieldInfos.cs
blobe0c097eb55852a1099131d3e01ed7cd8b99c77ef
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 InputStream = Lucene.Net.Store.InputStream;
21 using OutputStream = Lucene.Net.Store.OutputStream;
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
33 private System.Collections.ArrayList byNumber = new System.Collections.ArrayList();
34 private System.Collections.Hashtable byName = new System.Collections.Hashtable();
36 public /*internal*/ FieldInfos()
38 Add("", false);
41 /// <summary> Construct a FieldInfos object using the directory and the name of the file
42 /// InputStream
43 /// </summary>
44 /// <param name="d">The directory to open the InputStream from
45 /// </param>
46 /// <param name="name">The name of the file to open the InputStream from in the Directory
47 /// </param>
48 /// <throws> IOException </throws>
49 /// <summary>
50 /// </summary>
51 /// <seealso cref="#read">
52 /// </seealso>
53 public /*internal*/ FieldInfos(Directory d, System.String name)
55 InputStream input = d.OpenFile(name);
56 try
58 Read(input);
60 finally
62 input.Close();
66 /// <summary>Adds Field info for a Document. </summary>
67 public void Add(Document doc)
69 foreach (Field field in doc.Fields())
71 Add(field.Name(), field.IsIndexed(), field.IsTermVectorStored());
75 /// <param name="names">The names of the fields
76 /// </param>
77 /// <param name="storeTermVectors">Whether the fields store term vectors or not
78 /// </param>
79 public void AddIndexed(System.Collections.ICollection names, bool storeTermVectors)
81 System.Collections.IEnumerator i = names.GetEnumerator();
82 // FIXED trow@novell.com 18 Feb 2005
83 // Commented out unused variable j to avoid compiler warning
84 //int j = 0;
85 while (i.MoveNext())
87 System.Collections.DictionaryEntry t = (System.Collections.DictionaryEntry) i.Current;
88 Add((System.String) t.Key, true, storeTermVectors);
92 /// <summary> Assumes the Field is not storing term vectors </summary>
93 /// <param name="names">The names of the fields
94 /// </param>
95 /// <param name="isIndexed">Whether the fields are indexed or not
96 ///
97 /// </param>
98 /// <seealso cref="boolean)">
99 /// </seealso>
100 public void Add(System.Collections.ICollection names, bool isIndexed)
102 System.Collections.IEnumerator i = names.GetEnumerator();
103 // FIXED trow@novell.com 18 Feb 2005
104 // Commented out unused variable j to avoid compiler warning
105 //int j = 0;
106 while (i.MoveNext())
108 System.Collections.DictionaryEntry t = (System.Collections.DictionaryEntry) i.Current;
109 Add((System.String) t.Key, isIndexed);
113 /// <summary> Calls three parameter add with false for the storeTermVector parameter </summary>
114 /// <param name="name">The name of the Field
115 /// </param>
116 /// <param name="isIndexed">true if the Field is indexed
117 /// </param>
118 /// <seealso cref="boolean, boolean)">
119 /// </seealso>
120 public void Add(System.String name, bool isIndexed)
122 Add(name, isIndexed, false);
126 /// <summary>If the Field is not yet known, adds it. If it is known, checks to make
127 /// sure that the isIndexed flag is the same as was given previously for this
128 /// Field. If not - marks it as being indexed. Same goes for storeTermVector
129 ///
130 /// </summary>
131 /// <param name="name">The name of the Field
132 /// </param>
133 /// <param name="isIndexed">true if the Field is indexed
134 /// </param>
135 /// <param name="storeTermVector">true if the term vector should be stored
136 /// </param>
137 public void Add(System.String name, bool isIndexed, bool storeTermVector)
139 FieldInfo fi = FieldInfo(name);
140 if (fi == null)
142 AddInternal(name, isIndexed, storeTermVector);
144 else
146 if (fi.isIndexed != isIndexed)
148 fi.isIndexed = true; // once indexed, always index
150 if (fi.storeTermVector != storeTermVector)
152 fi.storeTermVector = true; // once vector, always vector
157 private void AddInternal(System.String name, bool isIndexed, bool storeTermVector)
159 FieldInfo fi = new FieldInfo(name, isIndexed, byNumber.Count, storeTermVector);
160 byNumber.Add(fi);
161 byName[name] = fi;
164 public int FieldNumber(System.String fieldName)
166 FieldInfo fi = FieldInfo(fieldName);
167 if (fi != null)
168 return fi.number;
169 else
170 return - 1;
173 public FieldInfo FieldInfo(System.String fieldName)
175 return (FieldInfo) byName[fieldName];
178 public System.String FieldName(int fieldNumber)
180 return FieldInfo(fieldNumber).name;
183 public FieldInfo FieldInfo(int fieldNumber)
185 return (FieldInfo) byNumber[fieldNumber];
188 public int Size()
190 return byNumber.Count;
193 public bool HasVectors()
195 bool hasVectors = false;
196 for (int i = 0; i < Size(); i++)
198 if (FieldInfo(i).storeTermVector)
199 hasVectors = true;
201 return hasVectors;
204 public void Write(Directory d, System.String name)
206 OutputStream output = d.CreateFile(name);
209 Write(output);
211 finally
213 output.Close();
217 public void Write(OutputStream output)
219 output.WriteVInt(Size());
220 for (int i = 0; i < Size(); i++)
222 FieldInfo fi = FieldInfo(i);
223 byte bits = (byte) (0x0);
224 if (fi.isIndexed)
225 bits |= (byte) (0x1);
226 if (fi.storeTermVector)
227 bits |= (byte) (0x2);
228 output.WriteString(fi.name);
229 //Was REMOVE
230 //output.writeByte((byte)(fi.isIndexed ? 1 : 0));
231 output.WriteByte(bits);
235 private void Read(InputStream input)
237 int size = input.ReadVInt(); //read in the size
238 for (int i = 0; i < size; i++)
240 System.String name = String.Intern(input.ReadString());
241 byte bits = input.ReadByte();
242 bool isIndexed = (bits & 0x1) != 0;
243 bool storeTermVector = (bits & 0x2) != 0;
244 AddInternal(name, isIndexed, storeTermVector);