Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
TermsEnumCompatibility.cs
Go to the documentation of this file.
1 /*
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements. See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership. The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License. You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied. See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20 */
21 
22 using System;
23 using System.Collections.Generic;
24 using Lucene.Net.Index;
25 using Lucene.Net.Util;
26 
27 namespace Lucene.Net.Spatial.Util
28 {
29  /// <summary>
30  /// Wraps Lucene 3 TermEnum to make it look like a Lucene 4 TermsEnum
31  /// SOLR-2155
32  /// @author dsmiley
33  /// </summary>
35  {
36  private readonly IndexReader reader;
37  private readonly String fieldName;
38  private TermEnum termEnum;
39  private bool initialState = true;
40 
41  public TermsEnumCompatibility(IndexReader reader, String fieldName)
42  {
43  this.reader = reader;
44  this.fieldName = string.Intern(fieldName);
45  this.termEnum = reader.Terms(new Term(this.fieldName));
46  }
47 
48  public TermEnum GetTermEnum()
49  {
50  return termEnum;
51  }
52 
53  public Term Term()
54  {
55  Term t = termEnum.Term;
56  return t != null && t.Field == fieldName ? t : null;
57  }
58 
59  public Term Next()
60  {
61  //in Lucene 3, a call to reader.terms(term) is already pre-positioned, you don't call next first
62  if (initialState)
63  {
64  initialState = false;
65  return Term();
66  }
67  else
68  {
69  return termEnum.Next() ? Term() : null;
70  }
71  }
72 
73  public void Close()
74  {
75  termEnum.Close();
76  }
77 
78  public enum SeekStatus
79  {
80  END,
81  FOUND,
82  NOT_FOUND
83  }
84 
85  public SeekStatus Seek(String value)
86  {
87  termEnum = reader.Terms(new Term(this.fieldName, value));
88  Term t = Term();
89  if (t == null)
90  return SeekStatus.END;
91  return (t.Text.Equals(value)) ? SeekStatus.FOUND : SeekStatus.NOT_FOUND;
92  }
93 
94  /// <summary>
95  /// Seeks to the specified term, if it exists, or to the
96  /// next (ceiling) term. Returns SeekStatus to
97  /// indicate whether exact term was found, a different
98  /// term was found, or EOF was hit. The target term may
99  /// be before or after the current term. If this returns
100  /// SeekStatus.END, the enum is unpositioned.
101  /// </summary>
102  /// <param name="value"></param>
103  /// <returns></returns>
104  public SeekStatus SeekCeil(String value)
105  {
106  return Seek(value);
107  }
108 
109  /// <summary>
110  /// Returns the number of documents that have at least one
111  /// term for this field, or -1 if this measure isn't
112  /// stored by the codec. Note that, just like other term
113  /// measures, this measure does not take deleted documents
114  /// into account.
115  /// </summary>
116  /// <returns></returns>
117  public int GetDocCount()
118  {
119  return -1; // TODO find a way to efficiently determine this
120  }
121 
122  public void Docs(OpenBitSet bits)
123  {
124  var termDocs = reader.TermDocs(new Term(fieldName, Term().Text));
125  while (termDocs.Next())
126  {
127  bits.FastSet(termDocs.Doc);
128  }
129  }
130 
131  public void Docs(FixedBitSet bits)
132  {
133  var termDocs = reader.TermDocs(new Term(fieldName, Term().Text));
134  while (termDocs.Next())
135  {
136  bits.Set(termDocs.Doc);
137  }
138  }
139  }
140 }