First post!
[beagle.git] / Lucene.Net / Index / SegmentTermEnum.cs
blob75873927ab5277c4c2fbc4d003127febc46cd585
1 using System;
2 using Lucene.Net.Store;
4 namespace Lucene.Net.Index
6 /* ====================================================================
7 * The Apache Software License, Version 1.1
9 * Copyright (c) 2001 The Apache Software Foundation. All rights
10 * reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in
21 * the documentation and/or other materials provided with the
22 * distribution.
24 * 3. The end-user documentation included with the redistribution,
25 * if any, must include the following acknowledgment:
26 * "This product includes software developed by the
27 * Apache Software Foundation (http://www.apache.org/)."
28 * Alternately, this acknowledgment may appear in the software itself,
29 * if and wherever such third-party acknowledgments normally appear.
31 * 4. The names "Apache" and "Apache Software Foundation" and
32 * "Apache Lucene" must not be used to endorse or promote products
33 * derived from this software without prior written permission. For
34 * written permission, please contact apache@apache.org.
36 * 5. Products derived from this software may not be called "Apache",
37 * "Apache Lucene", nor may "Apache" appear in their name, without
38 * prior written permission of the Apache Software Foundation.
40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 * ====================================================================
54 * This software consists of voluntary contributions made by many
55 * individuals on behalf of the Apache Software Foundation. For more
56 * information on the Apache Software Foundation, please see
57 * <http://www.apache.org/>.
60 public sealed class SegmentTermEnum : TermEnum, ICloneable
62 private InputStream input;
63 private FieldInfos fieldInfos;
64 internal int size;
65 internal int position = -1;
67 private Term term = new Term("", "");
68 private TermInfo termInfo = new TermInfo();
70 internal bool isIndex = false;
71 internal long indexPointer = 0;
72 internal Term prev;
74 private char[] buffer = {};
76 public SegmentTermEnum(InputStream i, FieldInfos fis, bool isi)
78 input = i;
79 fieldInfos = fis;
80 size = input.ReadInt();
81 isIndex = isi;
84 public Object Clone()
86 SegmentTermEnum clone = null;
87 try
89 clone = (SegmentTermEnum)base.MemberwiseClone();
91 catch (Exception) {}
93 clone.input = (InputStream)input.Clone();
94 clone.termInfo = new TermInfo(termInfo);
95 if (term != null) clone.GrowBuffer(term.text.Length);
97 return clone;
100 internal void Seek(long pointer, int p, Term t, TermInfo ti)
102 input.Seek(pointer);
103 position = p;
104 term = t;
105 prev = null;
106 termInfo.Set(ti);
107 GrowBuffer(term.text.Length); // copy term text into buffer
110 /// <summary>
111 /// Increments the enumeration to the next element.
112 /// </summary>
113 /// <returns>true if one exists.</returns>
114 override public bool Next()
116 if (position++ >= size-1)
118 term = null;
119 return false;
122 prev = term;
123 term = ReadTerm();
125 termInfo.docFreq = input.ReadVInt(); // read doc freq
126 termInfo.freqPointer += input.ReadVLong(); // read freq pointer
127 termInfo.proxPointer += input.ReadVLong(); // read prox pointer
129 if (isIndex)
130 indexPointer += input.ReadVLong(); // read index pointer
132 return true;
135 private Term ReadTerm()
137 int start = input.ReadVInt();
138 int length = input.ReadVInt();
139 int totalLength = start + length;
140 if (buffer.Length < totalLength)
141 GrowBuffer(totalLength);
143 input.ReadChars(buffer, start, length);
144 return new Term(fieldInfos.FieldName(input.ReadVInt()),
145 new String(buffer, 0, totalLength), false);
148 private void GrowBuffer(int length)
150 buffer = new char[length];
151 for (int i = 0; i < term.text.Length; i++) // copy contents
152 buffer[i] = term.text[i];
155 /// <summary>
156 /// Returns the current Term in the enumeration.
157 /// Initially invalid, valid after Next() called for the first time.
158 /// </summary>
159 /// <returns></returns>
160 override public Term Term()
162 return term;
165 /// <summary>
166 /// Returns the current TermInfo in the enumeration.
167 /// Initially invalid, valid after Next() called for the first time.
168 /// </summary>
169 /// <returns></returns>
170 public TermInfo TermInfo()
172 return new TermInfo(termInfo);
175 /// <summary>
176 /// Sets the argument to the current TermInfo in the enumeration.
177 /// Initially invalid, valid after Next() called for the first time.
178 /// </summary>
179 /// <param name="ti"></param>
180 internal void TermInfo(TermInfo ti)
182 ti.Set(termInfo);
185 /// <summary>
186 /// Returns the docFreq from the current termInfo in the enumeration.
187 /// Initially invalid, valid after Next() called for the first time.
188 /// </summary>
189 override public int DocFreq()
191 return termInfo.docFreq;
194 /// <summary>
195 /// Returns the freqPointer from the current termInfo in the enumeration.
196 /// Initially invalid, valid after Next() called for the first time.
197 /// </summary>
198 /// <returns></returns>
199 internal long FreqPointer()
201 return termInfo.freqPointer;
204 /// <summary>
205 /// Returns the proxPointer from the current termInfo in the enumeration.
206 /// Initially invalid, valid after Next() called for the first time.
207 /// </summary>
208 /// <returns></returns>
209 internal long ProxPointer()
211 return termInfo.proxPointer;
214 /// <summary>
215 /// Closes the enumeration to further activity, freeing resources.
216 /// </summary>
217 override public void Close()
219 input.Close();