Lucene.Net  3.0.3
Lucene.Net is a .NET port of the Java Lucene Indexing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties
FieldInfos.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using System;
19 using Lucene.Net.Documents;
20 using Lucene.Net.Support;
21 using Document = Lucene.Net.Documents.Document;
22 using Directory = Lucene.Net.Store.Directory;
23 using IndexInput = Lucene.Net.Store.IndexInput;
24 using IndexOutput = Lucene.Net.Store.IndexOutput;
25 using StringHelper = Lucene.Net.Util.StringHelper;
26 
27 namespace Lucene.Net.Index
28 {
29 
36  public sealed class FieldInfos : ICloneable
37  {
38 
39  // Used internally (ie not written to *.fnm files) for pre-2.9 files
40  public const int FORMAT_PRE = - 1;
41 
42  // First used in 2.9; prior to 2.9 there was no format header
43  public const int FORMAT_START = - 2;
44 
45  internal static readonly int CURRENT_FORMAT = FORMAT_START;
46 
47  internal const byte IS_INDEXED = (0x1);
48  internal const byte STORE_TERMVECTOR = (0x2);
49  internal const byte STORE_POSITIONS_WITH_TERMVECTOR =(0x4);
50  internal const byte STORE_OFFSET_WITH_TERMVECTOR = (0x8);
51  internal const byte OMIT_NORMS = (0x10);
52  internal const byte STORE_PAYLOADS = (0x20);
53  internal const byte OMIT_TERM_FREQ_AND_POSITIONS = (0x40);
54 
55  private readonly System.Collections.Generic.List<FieldInfo> byNumber = new System.Collections.Generic.List<FieldInfo>();
56  private readonly HashMap<string, FieldInfo> byName = new HashMap<string, FieldInfo>();
57  private int format;
58 
59  public /*internal*/ FieldInfos()
60  {
61  }
62 
71  public /*internal*/ FieldInfos(Directory d, String name)
72  {
73  IndexInput input = d.OpenInput(name);
74  try
75  {
76  try
77  {
78  Read(input, name);
79  }
80  catch (System.IO.IOException)
81  {
82  if (format == FORMAT_PRE)
83  {
84  // LUCENE-1623: FORMAT_PRE (before there was a
85  // format) may be 2.3.2 (pre-utf8) or 2.4.x (utf8)
86  // encoding; retry with input set to pre-utf8
87  input.Seek(0);
88  input.SetModifiedUTF8StringsMode();
89  byNumber.Clear();
90  byName.Clear();
91 
92  bool rethrow = false;
93  try
94  {
95  Read(input, name);
96  }
97  catch (Exception)
98  {
99  // Ignore any new exception & set to throw original IOE
100  rethrow = true;
101  }
102  if(rethrow)
103  {
104  // Preserve stack trace
105  throw;
106  }
107  }
108  else
109  {
110  // The IOException cannot be caused by
111  // LUCENE-1623, so re-throw it
112  throw;
113  }
114  }
115  }
116  finally
117  {
118  input.Close();
119  }
120  }
121 
123  public Object Clone()
124  {
125  lock (this)
126  {
127  var fis = new FieldInfos();
128  int numField = byNumber.Count;
129  for (int i = 0; i < numField; i++)
130  {
131  var fi = (FieldInfo)byNumber[i].Clone();
132  fis.byNumber.Add(fi);
133  fis.byName[fi.name] = fi;
134  }
135  return fis;
136  }
137  }
138 
140  public void Add(Document doc)
141  {
142  lock (this)
143  {
144  System.Collections.Generic.IList<IFieldable> fields = doc.GetFields();
145  foreach(IFieldable field in fields)
146  {
147  Add(field.Name, field.IsIndexed, field.IsTermVectorStored,
149  false, field.OmitTermFreqAndPositions);
150  }
151  }
152  }
153 
155  internal bool HasProx()
156  {
157  int numFields = byNumber.Count;
158  for (int i = 0; i < numFields; i++)
159  {
160  FieldInfo fi = FieldInfo(i);
161  if (fi.isIndexed && !fi.omitTermFreqAndPositions)
162  {
163  return true;
164  }
165  }
166  return false;
167  }
168 
180  public void AddIndexed(System.Collections.Generic.ICollection<string> names, bool storeTermVectors, bool storePositionWithTermVector, bool storeOffsetWithTermVector)
181  {
182  lock (this)
183  {
184  foreach(string name in names)
185  {
186  Add(name, true, storeTermVectors, storePositionWithTermVector, storeOffsetWithTermVector);
187  }
188  }
189  }
190 
201  public void Add(System.Collections.Generic.ICollection<string> names, bool isIndexed)
202  {
203  lock (this)
204  {
205  foreach(string name in names)
206  {
207  Add(name, isIndexed);
208  }
209  }
210  }
211 
221  public void Add(String name, bool isIndexed)
222  {
223  lock (this)
224  {
225  Add(name, isIndexed, false, false, false, false);
226  }
227  }
228 
238  public void Add(System.String name, bool isIndexed, bool storeTermVector)
239  {
240  lock (this)
241  {
242  Add(name, isIndexed, storeTermVector, false, false, false);
243  }
244  }
245 
262  public void Add(System.String name, bool isIndexed, bool storeTermVector, bool storePositionWithTermVector, bool storeOffsetWithTermVector)
263  {
264  lock (this)
265  {
266 
267  Add(name, isIndexed, storeTermVector, storePositionWithTermVector, storeOffsetWithTermVector, false);
268  }
269  }
270 
289  public void Add(System.String name, bool isIndexed, bool storeTermVector, bool storePositionWithTermVector, bool storeOffsetWithTermVector, bool omitNorms)
290  {
291  lock (this)
292  {
293  Add(name, isIndexed, storeTermVector, storePositionWithTermVector, storeOffsetWithTermVector, omitNorms, false, false);
294  }
295  }
296 
319  public FieldInfo Add(System.String name, bool isIndexed, bool storeTermVector, bool storePositionWithTermVector, bool storeOffsetWithTermVector, bool omitNorms, bool storePayloads, bool omitTermFreqAndPositions)
320  {
321  lock (this)
322  {
323  FieldInfo fi = FieldInfo(name);
324  if (fi == null)
325  {
326  return AddInternal(name, isIndexed, storeTermVector, storePositionWithTermVector, storeOffsetWithTermVector, omitNorms, storePayloads, omitTermFreqAndPositions);
327  }
328  else
329  {
330  fi.Update(isIndexed, storeTermVector, storePositionWithTermVector, storeOffsetWithTermVector, omitNorms, storePayloads, omitTermFreqAndPositions);
331  }
332  return fi;
333  }
334  }
335 
336  private FieldInfo AddInternal(String name, bool isIndexed, bool storeTermVector, bool storePositionWithTermVector, bool storeOffsetWithTermVector, bool omitNorms, bool storePayloads, bool omitTermFreqAndPositions)
337  {
338  name = StringHelper.Intern(name);
339  var fi = new FieldInfo(name, isIndexed, byNumber.Count, storeTermVector, storePositionWithTermVector, storeOffsetWithTermVector, omitNorms, storePayloads, omitTermFreqAndPositions);
340  byNumber.Add(fi);
341  byName[name] = fi;
342  return fi;
343  }
344 
345  public int FieldNumber(System.String fieldName)
346  {
347  FieldInfo fi = FieldInfo(fieldName);
348  return (fi != null)?fi.number:- 1;
349  }
350 
351  public FieldInfo FieldInfo(System.String fieldName)
352  {
353  return byName[fieldName];
354  }
355 
364  public System.String FieldName(int fieldNumber)
365  {
366  FieldInfo fi = FieldInfo(fieldNumber);
367  return (fi != null) ? fi.name : "";
368  }
369 
376  public FieldInfo FieldInfo(int fieldNumber)
377  {
378  return (fieldNumber >= 0) ? byNumber[fieldNumber] : null;
379  }
380 
381  public int Size()
382  {
383  return byNumber.Count;
384  }
385 
386  public bool HasVectors()
387  {
388  bool hasVectors = false;
389  for (int i = 0; i < Size(); i++)
390  {
391  if (FieldInfo(i).storeTermVector)
392  {
393  hasVectors = true;
394  break;
395  }
396  }
397  return hasVectors;
398  }
399 
400  public void Write(Directory d, System.String name)
401  {
402  IndexOutput output = d.CreateOutput(name);
403  try
404  {
405  Write(output);
406  }
407  finally
408  {
409  output.Close();
410  }
411  }
412 
413  public void Write(IndexOutput output)
414  {
415  output.WriteVInt(CURRENT_FORMAT);
416  output.WriteVInt(Size());
417  for (int i = 0; i < Size(); i++)
418  {
419  FieldInfo fi = FieldInfo(i);
420  var bits = (byte) (0x0);
421  if (fi.isIndexed)
422  bits |= IS_INDEXED;
423  if (fi.storeTermVector)
424  bits |= STORE_TERMVECTOR;
425  if (fi.storePositionWithTermVector)
426  bits |= STORE_POSITIONS_WITH_TERMVECTOR;
427  if (fi.storeOffsetWithTermVector)
428  bits |= STORE_OFFSET_WITH_TERMVECTOR;
429  if (fi.omitNorms)
430  bits |= OMIT_NORMS;
431  if (fi.storePayloads)
432  bits |= STORE_PAYLOADS;
433  if (fi.omitTermFreqAndPositions)
434  bits |= OMIT_TERM_FREQ_AND_POSITIONS;
435 
436  output.WriteString(fi.name);
437  output.WriteByte(bits);
438  }
439  }
440 
441  private void Read(IndexInput input, String fileName)
442  {
443  int firstInt = input.ReadVInt();
444 
445  if (firstInt < 0)
446  {
447  // This is a real format
448  format = firstInt;
449  }
450  else
451  {
452  format = FORMAT_PRE;
453  }
454 
455  if (format != FORMAT_PRE & format != FORMAT_START)
456  {
457  throw new CorruptIndexException("unrecognized format " + format + " in file \"" + fileName + "\"");
458  }
459 
460  int size;
461  if (format == FORMAT_PRE)
462  {
463  size = firstInt;
464  }
465  else
466  {
467  size = input.ReadVInt(); //read in the size
468  }
469 
470  for (int i = 0; i < size; i++)
471  {
472  String name = StringHelper.Intern(input.ReadString());
473  byte bits = input.ReadByte();
474  bool isIndexed = (bits & IS_INDEXED) != 0;
475  bool storeTermVector = (bits & STORE_TERMVECTOR) != 0;
476  bool storePositionsWithTermVector = (bits & STORE_POSITIONS_WITH_TERMVECTOR) != 0;
477  bool storeOffsetWithTermVector = (bits & STORE_OFFSET_WITH_TERMVECTOR) != 0;
478  bool omitNorms = (bits & OMIT_NORMS) != 0;
479  bool storePayloads = (bits & STORE_PAYLOADS) != 0;
480  bool omitTermFreqAndPositions = (bits & OMIT_TERM_FREQ_AND_POSITIONS) != 0;
481 
482  AddInternal(name, isIndexed, storeTermVector, storePositionsWithTermVector, storeOffsetWithTermVector, omitNorms, storePayloads, omitTermFreqAndPositions);
483  }
484 
485  if (input.FilePointer != input.Length())
486  {
487  throw new CorruptIndexException("did not read all bytes from file \"" + fileName + "\": read " + input.FilePointer + " vs size " + input.Length());
488  }
489  }
490  }
491 }