Update the thread-local storage patch, to fix #335178
[beagle.git] / beagled / Lucene.Net / Index / TermInfosReader.cs
blob3a27f809667bcde3c408e7c25d3c6233f4bd54b6
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 using System;
17 using Directory = Lucene.Net.Store.Directory;
18 namespace Lucene.Net.Index
21 /// <summary>This stores a monotonically increasing set of <Term, TermInfo> pairs in a
22 /// Directory. Pairs are accessed either by Term or by ordinal position the
23 /// set.
24 /// </summary>
26 sealed public class TermInfosReader
28 private Directory directory;
29 private System.String segment;
30 private FieldInfos fieldInfos;
32 [ThreadStatic]
33 private SegmentTermEnum termEnum;
35 private SegmentTermEnum origEnum;
36 private long size;
38 private Term[] indexTerms = null;
39 private TermInfo[] indexInfos;
40 private long[] indexPointers;
42 private SegmentTermEnum indexEnum;
44 public /*internal*/ TermInfosReader(Directory dir, System.String seg, FieldInfos fis)
46 directory = dir;
47 segment = seg;
48 fieldInfos = fis;
50 origEnum = new SegmentTermEnum(directory.OpenInput(segment + ".tis"), fieldInfos, false);
51 size = origEnum.size;
53 indexEnum = new SegmentTermEnum(directory.OpenInput(segment + ".tii"), fieldInfos, true);
56 public int GetSkipInterval()
58 return origEnum.skipInterval;
61 public /*internal*/ void Close()
63 if (origEnum != null)
64 origEnum.Close();
65 if (indexEnum != null)
66 indexEnum.Close();
69 /// <summary>Returns the number of term/value pairs in the set. </summary>
70 internal long Size()
72 return size;
75 private SegmentTermEnum GetEnum()
77 if (termEnum == null)
79 termEnum = Terms();
81 return termEnum;
84 private void EnsureIndexIsRead()
86 lock (this)
88 if (indexTerms != null)
89 // index already read
90 return ; // do nothing
91 try
93 int indexSize = (int) indexEnum.size; // otherwise read index
95 indexTerms = new Term[indexSize];
96 indexInfos = new TermInfo[indexSize];
97 indexPointers = new long[indexSize];
99 for (int i = 0; indexEnum.Next(); i++)
101 indexTerms[i] = indexEnum.Term();
102 indexInfos[i] = indexEnum.TermInfo();
103 indexPointers[i] = indexEnum.indexPointer;
106 finally
108 indexEnum.Close();
109 indexEnum = null;
114 /// <summary>Returns the offset of the greatest index entry which is less than or equal to term.</summary>
115 private int GetIndexOffset(Term term)
117 int lo = 0; // binary search indexTerms[]
118 int hi = indexTerms.Length - 1;
120 while (hi >= lo)
122 int mid = (lo + hi) >> 1;
123 int delta = term.CompareTo(indexTerms[mid]);
124 if (delta < 0)
125 hi = mid - 1;
126 else if (delta > 0)
127 lo = mid + 1;
128 else
129 return mid;
131 return hi;
134 private void SeekEnum(int indexOffset)
136 GetEnum().Seek(indexPointers[indexOffset], (indexOffset * GetEnum().indexInterval) - 1, indexTerms[indexOffset], indexInfos[indexOffset]);
139 /// <summary>Returns the TermInfo for a Term in the set, or null. </summary>
140 public /*internal*/ TermInfo Get(Term term)
142 if (size == 0)
143 return null;
145 EnsureIndexIsRead();
147 // optimize sequential access: first try scanning cached enum w/o seeking
148 SegmentTermEnum enumerator = GetEnum();
149 if (enumerator.Term() != null && ((enumerator.Prev() != null && term.CompareTo(enumerator.Prev()) > 0) || term.CompareTo(enumerator.Term()) >= 0))
151 int enumOffset = (int) (enumerator.position / enumerator.indexInterval) + 1;
152 if (indexTerms.Length == enumOffset || term.CompareTo(indexTerms[enumOffset]) < 0)
153 return ScanEnum(term); // no need to seek
156 // random-access: must seek
157 SeekEnum(GetIndexOffset(term));
158 return ScanEnum(term);
161 /// <summary>Scans within block for matching term. </summary>
162 private TermInfo ScanEnum(Term term)
164 SegmentTermEnum enumerator = GetEnum();
165 enumerator.ScanTo(term);
166 if (enumerator.Term() != null && term.CompareTo(enumerator.Term()) == 0)
167 return enumerator.TermInfo();
168 else
169 return null;
172 /// <summary>Returns the nth term in the set. </summary>
173 internal Term Get(int position)
175 if (size == 0)
176 return null;
178 SegmentTermEnum enumerator = GetEnum();
179 if (enumerator != null && enumerator.Term() != null && position >= enumerator.position && position < (enumerator.position + enumerator.indexInterval))
180 return ScanEnum(position); // can avoid seek
182 SeekEnum(position / enumerator.indexInterval); // must seek
183 return ScanEnum(position);
186 private Term ScanEnum(int position)
188 SegmentTermEnum enumerator = GetEnum();
189 while (enumerator.position < position)
190 if (!enumerator.Next())
191 return null;
193 return enumerator.Term();
196 /// <summary>Returns the position of a Term in the set or -1. </summary>
197 internal long GetPosition(Term term)
199 if (size == 0)
200 return - 1;
202 EnsureIndexIsRead();
203 int indexOffset = GetIndexOffset(term);
204 SeekEnum(indexOffset);
206 SegmentTermEnum enumerator = GetEnum();
207 while (term.CompareTo(enumerator.Term()) > 0 && enumerator.Next())
211 if (term.CompareTo(enumerator.Term()) == 0)
212 return enumerator.position;
213 else
214 return - 1;
217 /// <summary>Returns an enumeration of all the Terms and TermInfos in the set. </summary>
218 public SegmentTermEnum Terms()
220 return (SegmentTermEnum) origEnum.Clone();
223 /// <summary>Returns an enumeration of terms starting at or after the named term. </summary>
224 public SegmentTermEnum Terms(Term term)
226 Get(term);
227 return (SegmentTermEnum) GetEnum().Clone();