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
FieldEnumerator.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 System.Collections;
20 using System.Collections.Generic;
21 using System.Linq;
22 using System.Text;
23 
24 using Lucene.Net.Util;
25 
26 namespace Lucene.Net.Index
27 {
31  public enum FieldParser
32  {
33  String,
34  Numeric
35  }
36 
49  public abstract class FieldEnumerator<T> : IDisposable
50  {
54  protected bool includeDocs;
55 
59  private TermEnum termEnum;
60 
64  private TermDocs termDocs;
65 
69  protected TermEnumerator tEnum;
70 
74  private TermDocEnumerator.TermDocUsingTermsEnumerator tdEnum;
75 
79  private bool disposed;
80 
87  protected void Init(IndexReader reader, string field)
88  {
89  this.Init(reader, field, true);
90  }
91 
99  protected void Init(IndexReader reader, string fieldName, bool includeDocs)
100  {
101  this.termEnum = reader.Terms(new Term(fieldName));
102  if (includeDocs)
103  {
104  this.termDocs = reader.TermDocs();
105  this.tdEnum = new TermDocEnumerator.TermDocUsingTermsEnumerator(this.termDocs, this.termEnum);
106  }
107  this.tEnum = new TermEnumerator(termEnum, termDocs, fieldName, this);
108  }
109 
117  protected abstract bool TryParse(string s);
118 
122  public TermEnumerator Terms
123  {
124  get { return this.tEnum; }
125  }
126 
130  public TermDocEnumerator.TermDocUsingTermsEnumerator Docs
131  {
132  get
133  {
134  if (this.termDocs == null)
135  {
136  throw new NotSupportedException("This instance does not support enumeration over the document ids.");
137  }
138  else
139  {
140  return this.tdEnum;
141  }
142  }
143  }
144 
145  #region IDisposable Members
146 
150  public void Dispose()
151  {
152  if (!this.disposed)
153  {
154  this.disposed = true;
155  if (this.termEnum != null)
156  {
157  this.termEnum.Close();
158  }
159  if (this.termDocs != null)
160  {
161  this.termDocs.Close();
162  }
163  GC.SuppressFinalize(this);
164  }
165  }
166 
167  #endregion
168 
172  public class TermEnumerator : IEnumerator<T>, IEnumerable<T>
173  {
177  private TermEnum termEnum;
178 
182  private TermDocs termDocs;
183 
187  private Term currentTerm;
188 
192  protected string fieldName;
193 
197  private bool isFirst = true;
198 
202  private FieldEnumerator<T> enclosing;
203 
211  public TermEnumerator(TermEnum termEnum, TermDocs termDocs, string field, FieldEnumerator<T> enclosing)
212  {
213  this.termEnum = termEnum;
214  this.termDocs = termDocs;
215  this.fieldName = field;
216  this.enclosing = enclosing;
217  }
218 
219  #region IEnumerator<T> Members
220 
224  public T Current
225  {
226  get;
227  internal set;
228  }
229 
230  #endregion
231 
232  #region IEnumerator Members
233 
237  object IEnumerator.Current
238  {
239  get { return (object)this.Current; }
240  }
241 
246  public bool MoveNext()
247  {
248  if (this.isFirst)
249  {
250  this.isFirst = false;
251  }
252  else
253  {
254  if (!this.termEnum.Next())
255  {
256  return false;
257  }
258  }
259 
260  this.currentTerm = termEnum.Term;
261  if (this.currentTerm == null || (!this.currentTerm.Field.Equals(this.fieldName)))
262  {
263  return false;
264  }
265 
266  if (this.enclosing.TryParse(this.currentTerm.Text))
267  {
268  if (this.termDocs != null)
269  {
270  this.termDocs.Seek(this.termEnum);
271  }
272  return true;
273  }
274 
275  return false;
276  }
277 
281  public void Reset()
282  {
283  throw new NotSupportedException("The enumerator cannot be reset");
284  }
285 
286  #endregion
287 
288  #region IDisposable Members
289 
290  public void Dispose()
291  {
292  // noop
293  }
294 
295  #endregion
296 
297  #region IEnumerable<T> Members
298 
303  public IEnumerator<T> GetEnumerator()
304  {
305  return this;
306  }
307 
308  #endregion
309 
310  #region IEnumerable Members
311 
316  IEnumerator IEnumerable.GetEnumerator()
317  {
318  return this.GetEnumerator();
319  }
320 
321  #endregion
322  }
323  }
324 
330  public class TermDocEnumerator : IEnumerable<int>, IDisposable
331  {
335  private TermDocs termDocs;
336 
341  public TermDocEnumerator(TermDocs termDocs)
342  {
343  this.termDocs = termDocs;
344  }
345 
350  public void Seek(Term t)
351  {
352  this.termDocs.Seek(t);
353  }
354 
355  #region IEnumerable<int> Members
356 
357  public IEnumerator<int> GetEnumerator()
358  {
359  return new TermDocUsingTermsEnumerator(this.termDocs);
360  }
361 
362  #endregion
363 
364  #region IEnumerable Members
365 
366  IEnumerator IEnumerable.GetEnumerator()
367  {
368  return this.GetEnumerator();
369  }
370 
371  #endregion
372 
373  #region IDisposable Members
374 
378  public void Dispose()
379  {
380  if (this.termDocs != null)
381  {
382  termDocs.Close();
383  }
384  }
385 
386  #endregion
387 
392  public class TermDocUsingTermsEnumerator : IEnumerable<int>, IEnumerator<int>
393  {
398  private TermEnum termEnum;
399 
403  private TermDocs termDocs;
404 
409  internal TermDocUsingTermsEnumerator(TermDocs termDocs)
410  : this(termDocs, null)
411  { }
412 
418  internal TermDocUsingTermsEnumerator(TermDocs td, TermEnum termEnum)
419  {
420  this.termDocs = td;
421  this.termEnum = termEnum;
422  }
423 
428  internal void Seek(TermEnum te)
429  {
430  this.termDocs.Seek(te);
431  }
432 
433  #region IEnumerable<int> Members
434 
439  public IEnumerator<int> GetEnumerator()
440  {
441  return this;
442  }
443 
444  #endregion
445 
446  #region IEnumerable Members
447 
452  IEnumerator IEnumerable.GetEnumerator()
453  {
454  return this.GetEnumerator();
455  }
456 
457  #endregion
458 
459  #region IEnumerator<int> Members
460 
464  public int Current
465  {
466  get { return this.termDocs.Doc; }
467  }
468 
469  #endregion
470 
471  #region IDisposable Members
472 
476  public void Dispose()
477  {
478  // noop as the closing of the underlying
479  // TermDocs is handled by the containing class
480  }
481 
482  #endregion
483 
484  #region IEnumerator Members
485 
489  object IEnumerator.Current
490  {
491  get { throw new NotImplementedException(); }
492  }
493 
498  public bool MoveNext()
499  {
500  return this.termDocs.Next();
501  }
502 
506  public void Reset()
507  {
508  throw new NotImplementedException();
509  }
510 
511  #endregion
512  }
513  }
514 
515 
519  public class StringFieldEnumerator : FieldEnumerator<string>
520  {
526  public StringFieldEnumerator(IndexReader reader, string fieldName)
527  {
528  this.Init(reader, fieldName);
529  }
530 
537  public StringFieldEnumerator(IndexReader reader, string fieldName, bool includeDocs)
538  {
539  this.Init(reader, fieldName, includeDocs);
540  }
541 
547  protected override bool TryParse(string s)
548  {
549  this.tEnum.Current = s;
550  return true;
551  }
552  }
553 
558  public abstract class NumericFieldEnum<T> : FieldEnumerator<T>
559  {
563  private FieldParser parser;
564 
572  protected void Init(IndexReader reader, string field, bool includeDocs, FieldParser parser)
573  {
574  base.Init(reader, field, includeDocs);
575  this.parser = parser;
576  }
577 
583  protected override bool TryParse(string s)
584  {
585  if (this.parser == FieldParser.Numeric)
586  {
587  return this.TryParseNumeric(s);
588  }
589  else
590  {
591  return this.TryParseString(s);
592  }
593  }
594 
600  protected abstract bool TryParseString(string s);
601 
607  protected abstract bool TryParseNumeric(string s);
608  }
609 
613  public class IntFieldEnumerator : NumericFieldEnum<int>
614  {
620  public IntFieldEnumerator(IndexReader reader, string fieldName, FieldParser parser)
621  {
622  this.Init(reader, fieldName, true, parser);
623  }
624 
631  public IntFieldEnumerator(IndexReader reader, string fieldName, FieldParser parser, bool includeDocs)
632  {
633  this.Init(reader, fieldName, includeDocs, parser);
634  }
635 
641  protected override bool TryParseString(string s)
642  {
643  this.tEnum.Current = Int32.Parse(s);
644  return true;
645  }
646 
653  protected override bool TryParseNumeric(string s)
654  {
655  int shift = s[0] - NumericUtils.SHIFT_START_INT;
656  if (shift > 0 && shift <= 31)
657  {
658  return false;
659  }
660  else
661  {
662  this.tEnum.Current = NumericUtils.PrefixCodedToInt(s);
663  return true;
664  }
665  }
666  }
667 
671  public class FloatFieldEnumerator : NumericFieldEnum<float>
672  {
673 
679  public FloatFieldEnumerator(IndexReader reader, string fieldName, FieldParser parser)
680  {
681  this.Init(reader, fieldName, true, parser);
682  }
683 
690  public FloatFieldEnumerator(IndexReader reader, string fieldName, FieldParser parser, bool includeDocs)
691  {
692  this.Init(reader, fieldName, includeDocs, parser);
693  }
694 
700  protected override bool TryParseString(string s)
701  {
702  this.tEnum.Current = float.Parse(s);
703  return true;
704  }
705 
712  protected override bool TryParseNumeric(string s)
713  {
714  int shift = s[0] - NumericUtils.SHIFT_START_INT;
715  if (shift > 0 && shift <= 31)
716  {
717  return false;
718  }
719  else
720  {
722  return true;
723  }
724  }
725  }
726 
730  public class DoubleFieldEnumerator : NumericFieldEnum<double>
731  {
737  public DoubleFieldEnumerator(IndexReader reader, string fieldName, FieldParser parser)
738  {
739  this.Init(reader, fieldName, true, parser);
740  }
741 
748  public DoubleFieldEnumerator(IndexReader reader, string fieldName, FieldParser parser, bool includeDocs)
749  {
750  this.Init(reader, fieldName, includeDocs, parser);
751  }
752 
758  protected override bool TryParseString(string s)
759  {
760  this.tEnum.Current = Double.Parse(s);
761  return true;
762  }
763 
770  protected override bool TryParseNumeric(string s)
771  {
772  int shift = s[0] - NumericUtils.SHIFT_START_LONG;
773  if (shift > 0 && shift <= 63)
774  {
775  return false;
776  }
777  else
778  {
780  return true;
781  }
782  }
783  }
784 
788  public class LongFieldEnumerator : NumericFieldEnum<long>
789  {
795  public LongFieldEnumerator(IndexReader reader, string fieldName, FieldParser parser)
796  {
797  this.Init(reader, fieldName, true, parser);
798  }
799 
806  public LongFieldEnumerator(IndexReader reader, string fieldName, FieldParser parser, bool includeDocs)
807  {
808  this.Init(reader, fieldName, includeDocs, parser);
809  }
810 
816  protected override bool TryParseString(string s)
817  {
818  this.tEnum.Current = long.Parse(s);
819  return true;
820  }
821 
828  protected override bool TryParseNumeric(string s)
829  {
830  int shift = s[0] - NumericUtils.SHIFT_START_LONG;
831  if (shift > 0 && shift <= 63)
832  {
833  return false;
834  }
835  else
836  {
837  this.tEnum.Current = NumericUtils.PrefixCodedToLong(s);
838  return true;
839  }
840  }
841  }
842 }