Permalink
Browse files

Fix small bug in TermsEnumCompatibility and in how it's used.

  • Loading branch information...
1 parent b230a98 commit f494df27c1f54da21c044c4219efd955f748b938 @dsmiley committed Apr 19, 2012
@@ -14,20 +14,35 @@
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 {
- return termEnum.next() ? term() : null;
+ //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}
@@ -88,27 +88,32 @@ public static ValueSource getValueSource(String fieldName, FunctionQParser parse
IndexReader reader = searcher.getIndexReader();
TermsEnumCompatibility termsEnum = new TermsEnumCompatibility(reader,fieldName);
TermDocs termDocs = reader.termDocs(); //cached for termsEnum.docs() calls
- while(true) {
- final Term term = termsEnum.next();
- if (term == null)
- break;
- if (term.text().length() != gridReferenceSystem.getPrecision())
- continue;
- Point2D point = gridReferenceSystem.decodeXY(term.text());
- termDocs.seek(term);
- while(termDocs.next()) {
- final int docId = termDocs.doc();
- if (docId == DocIdSetIterator.NO_MORE_DOCS)
+ try {
+ while(true) {
+ final Term term = termsEnum.next();
+ if (term == null)
break;
- if (doc2PointsCache == null)
- doc2PointsCache = (List<Point2D>[]) new List[reader.maxDoc()];//java generics hack
- List<Point2D> points = doc2PointsCache[docId];
- if (points == null) {
- points = new ArrayList<Point2D>(DEFAULT_ARRAY_CAPACITY);
- doc2PointsCache[docId] = points;
+ if (term.text().length() != gridReferenceSystem.getPrecision())
+ continue;
+ Point2D point = gridReferenceSystem.decodeXY(term.text());
+ termDocs.seek(termsEnum.getTermEnum());
+ while(termDocs.next()) {
+ final int docId = termDocs.doc();
+ if (docId == DocIdSetIterator.NO_MORE_DOCS)
+ break;
+ if (doc2PointsCache == null)
+ doc2PointsCache = (List<Point2D>[]) new List[reader.maxDoc()];//java generics hack
+ List<Point2D> points = doc2PointsCache[docId];
+ if (points == null) {
+ points = new ArrayList<Point2D>(DEFAULT_ARRAY_CAPACITY);
+ doc2PointsCache[docId] = points;
+ }
+ points.add(point);
}
- points.add(point);
}
+ } finally { // in Lucene 3 these should be closed (not in Lucene 4)
+ termDocs.close();
+ termsEnum.close();
}
//Log statistics

0 comments on commit f494df2

Please sign in to comment.