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
MemoryTermEnum.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 System.Linq;
25 using System.Text;
26 
27 namespace Lucene.Net.Index.Memory
28 {
29  public partial class MemoryIndex
30  {
31  private sealed partial class MemoryIndexReader
32  {
33  private class MemoryTermEnum : TermEnum
34  {
35  private readonly MemoryIndex _index;
36  private readonly MemoryIndexReader _reader;
37  private int _i; // index into info.sortedTerms
38  private int _j; // index into sortedFields
39 
40  public MemoryTermEnum(MemoryIndex index, MemoryIndexReader reader, int ix, int jx)
41  {
42  _index = index;
43  _reader = reader;
44  _i = ix; // index into info.sortedTerms
45  _j = jx; // index into sortedFields
46  }
47 
48  public override bool Next()
49  {
50  if (DEBUG) System.Diagnostics.Debug.WriteLine("TermEnum.next");
51  if (_j >= _index.sortedFields.Length) return false;
52  Info info = _reader.GetInfo(_j);
53  if (++_i < info.SortedTerms.Length) return true;
54 
55  // move to successor
56  _j++;
57  _i = 0;
58  if (_j >= _index.sortedFields.Length) return false;
59  _reader.GetInfo(_j).SortTerms();
60  return true;
61  }
62 
63  public override Term Term
64  {
65  get
66  {
67  if (DEBUG) System.Diagnostics.Debug.WriteLine("TermEnum.term: " + _i);
68  if (_j >= _index.sortedFields.Length) return null;
69  Info info = _reader.GetInfo(_j);
70  if (_i >= info.SortedTerms.Length) return null;
71  // if (DEBUG) System.Diagnostics.Debug.WriteLine("TermEnum.term: " + i + ", " + info.sortedTerms[i].getKey());
72  return CreateTerm(info, _j, info.SortedTerms[_i].Key);
73  }
74  }
75 
76  public override int DocFreq()
77  {
78  if (DEBUG) System.Diagnostics.Debug.WriteLine("TermEnum.docFreq");
79  if (_j >= _index.sortedFields.Length) return 0;
80  Info info = _reader.GetInfo(_j);
81  if (_i >= info.SortedTerms.Length) return 0;
82  return _index.NumPositions(info.GetPositions(_i));
83  }
84 
85  protected override void Dispose(bool disposing)
86  {
87  if (DEBUG) System.Diagnostics.Debug.WriteLine("TermEnum.close");
88  }
89 
90  private Term CreateTerm(Info info, int pos, string text)
91  {
92  // Assertion: sortFields has already been called before
93  Term template = info.template;
94  if (template == null) { // not yet cached?
95  String fieldName = _index.sortedFields[pos].Key;
96  template = new Term(fieldName);
97  info.template = template;
98  }
99 
100  return template.CreateTerm(text);
101  }
102  }
103  }
104  }
105 }