2 * Copyright 2004 The Apache Software Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 sealed public class TermInfosReader
28 private Directory directory
;
29 private System
.String segment
;
30 private FieldInfos fieldInfos
;
33 private SegmentTermEnum termEnum
;
35 private SegmentTermEnum origEnum
;
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
)
50 origEnum
= new SegmentTermEnum(directory
.OpenInput(segment
+ ".tis"), fieldInfos
, false);
53 indexEnum
= new SegmentTermEnum(directory
.OpenInput(segment
+ ".tii"), fieldInfos
, true);
56 public int GetSkipInterval()
58 return origEnum
.skipInterval
;
61 public /*internal*/ void Close()
65 if (indexEnum
!= null)
69 /// <summary>Returns the number of term/value pairs in the set. </summary>
75 private SegmentTermEnum
GetEnum()
84 private void EnsureIndexIsRead()
88 if (indexTerms
!= null)
90 return ; // do nothing
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
;
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;
122 int mid
= (lo
+ hi
) >> 1;
123 int delta
= term
.CompareTo(indexTerms
[mid
]);
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
)
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();
172 /// <summary>Returns the nth term in the set. </summary>
173 internal Term
Get(int position
)
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())
193 return enumerator
.Term();
196 /// <summary>Returns the position of a Term in the set or -1. </summary>
197 internal long GetPosition(Term term
)
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
;
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
)
227 return (SegmentTermEnum
) GetEnum().Clone();