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 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.
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
49 /// <param name="d">The directory to open the IndexInput from
51 /// <param name="name">The name of the file to open the IndexInput from in the Directory
53 /// <throws> IOException </throws>
54 public /*internal*/ FieldInfos(Directory d
, System
.String name
)
56 IndexInput input
= d
.OpenInput(name
);
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.
79 /// <param name="names">The names of the fields
81 /// <param name="storeTermVectors">Whether the fields store term vectors or not
83 /// <param name="storePositionWithTermVector">treu if positions should be stored.
85 /// <param name="storeOffsetWithTermVector">true if offsets should be stored
87 public void AddIndexed(System
.Collections
.ICollection names
, bool storeTermVectors
, bool storePositionWithTermVector
, bool storeOffsetWithTermVector
)
89 System
.Collections
.IEnumerator i
= names
.GetEnumerator();
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.
100 /// <param name="names">The names of the fields
102 /// <param name="isIndexed">Whether the fields are indexed or not
105 /// <seealso cref="boolean)">
107 public void Add(System
.Collections
.ICollection names
, bool isIndexed
)
109 System
.Collections
.IEnumerator i
= names
.GetEnumerator();
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.
121 /// <param name="name">The name of the Field
123 /// <param name="isIndexed">true if the field is indexed
125 /// <seealso cref="boolean, boolean, boolean, boolean)">
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.
135 /// <param name="name">The name of the field
137 /// <param name="isIndexed"> true if the field is indexed
139 /// <param name="storeTermVector">true if the term vector should be stored
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
152 /// <param name="name">The name of the field
154 /// <param name="isIndexed">true if the field is indexed
156 /// <param name="storeTermVector">true if the term vector should be stored
158 /// <param name="storePositionWithTermVector">true if the term vector with positions should be stored
160 /// <param name="storeOffsetWithTermVector">true if the term vector with offsets should be stored
162 public void Add(System
.String name
, bool isIndexed
, bool storeTermVector
, bool storePositionWithTermVector
, bool storeOffsetWithTermVector
)
164 FieldInfo fi
= FieldInfo(name
);
167 AddInternal(name
, isIndexed
, storeTermVector
, storePositionWithTermVector
, storeOffsetWithTermVector
);
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
);
197 public int FieldNumber(System
.String fieldName
)
199 FieldInfo fi
= FieldInfo(fieldName
);
205 public FieldInfo
FieldInfo(System
.String fieldName
)
207 return (FieldInfo
) byName
[fieldName
];
210 /// <summary> Return the fieldName identified by its number.
213 /// <param name="">fieldNumber
215 /// <returns> the fieldName or an empty string when the field
216 /// with the given number doesn't exist.
218 public System
.String
FieldName(int fieldNumber
)
220 FieldInfo info
= FieldInfo(fieldNumber
);
227 /// <summary> Return the fieldinfo object referenced by the fieldNumber.</summary>
228 /// <param name="">fieldNumber
230 /// <returns> the FieldInfo object or null when the given fieldNumber
233 public FieldInfo
FieldInfo(int fieldNumber
)
235 if (fieldNumber
< 0 || fieldNumber
>= byNumber
.Count
)
240 return (FieldInfo
) byNumber
[fieldNumber
];
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
)
262 public void Write(Directory d
, System
.String name
)
264 IndexOutput output
= d
.CreateOutput(name
);
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);
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
);