/
TermsEnumCompatibility.java
57 lines (47 loc) · 1.5 KB
/
TermsEnumCompatibility.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package solr2155.lucene;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermEnum;
import java.io.IOException;
/**
* Wraps Lucene 3 TermEnum to make it look like a Lucene 4 TermsEnum
* @author dsmiley
*/
public class TermsEnumCompatibility {
private final IndexReader reader;
private final String fieldName;
private TermEnum termEnum;
private boolean initialState = true;
public TermsEnumCompatibility(IndexReader reader, String fieldName) throws IOException {
this.reader = reader;
this.fieldName = fieldName.intern();
this.termEnum = reader.terms(new Term(this.fieldName));
}
public TermEnum getTermEnum() {
return termEnum;
}
public Term term() {
Term t = termEnum.term();
return t != null && t.field() == fieldName ? t : null;
}
public Term next() throws IOException {
//in Lucene 3, a call to reader.terms(term) is already pre-positioned, you don't call next first
if (initialState) {
initialState = false;
return term();
} else {
return termEnum.next() ? term() : null;
}
}
public void close() throws IOException {
termEnum.close();
}
public static enum SeekStatus {END, FOUND, NOT_FOUND}
public SeekStatus seek(String value) throws IOException {
termEnum = reader.terms(new Term(this.fieldName,value));
Term t = term();
if (t == null)
return SeekStatus.END;
return (t.text().equals(value)) ? SeekStatus.FOUND : SeekStatus.NOT_FOUND;
}
}