2 * Copyright 2004 The Apache Software Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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 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.
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()
41 /// <summary> Construct a FieldInfos object using the directory and the name of the file
44 /// <param name="d">The directory to open the InputStream from
46 /// <param name="name">The name of the file to open the InputStream from in the Directory
48 /// <throws> IOException </throws>
51 /// <seealso cref="#read">
53 public /*internal*/ FieldInfos(Directory d
, System
.String name
)
55 InputStream input
= d
.OpenFile(name
);
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
77 /// <param name="storeTermVectors">Whether the fields store term vectors or not
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
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
95 /// <param name="isIndexed">Whether the fields are indexed or not
98 /// <seealso cref="boolean)">
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
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
116 /// <param name="isIndexed">true if the Field is indexed
118 /// <seealso cref="boolean, boolean)">
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
131 /// <param name="name">The name of the Field
133 /// <param name="isIndexed">true if the Field is indexed
135 /// <param name="storeTermVector">true if the term vector should be stored
137 public void Add(System
.String name
, bool isIndexed
, bool storeTermVector
)
139 FieldInfo fi
= FieldInfo(name
);
142 AddInternal(name
, isIndexed
, storeTermVector
);
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
);
164 public int FieldNumber(System
.String fieldName
)
166 FieldInfo fi
= FieldInfo(fieldName
);
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
];
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
)
204 public void Write(Directory d
, System
.String name
)
206 OutputStream output
= d
.CreateFile(name
);
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);
225 bits
|= (byte) (0x1);
226 if (fi
.storeTermVector
)
227 bits
|= (byte) (0x2);
228 output
.WriteString(fi
.name
);
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
);