Skip to content

Commit 7fc04a4

Browse files
committed
make sure to close the term docs when loading version
1 parent 914407e commit 7fc04a4

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

modules/benchmark/micro/src/main/java/org/elasticsearch/benchmark/common/lucene/uidscan/LuceneUidScanBenchmark.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,17 @@ public static void main(String[] args) throws Exception {
7474
uid.next();
7575
uid.nextPosition();
7676
if (!uid.isPayloadAvailable()) {
77+
uid.close();
7778
System.err.println("no payload...");
7879
break;
7980
}
8081
byte[] payload = uid.getPayload(new byte[8], 0);
8182
if (Numbers.bytesToLong(payload) != id) {
83+
uid.close();
8284
System.err.println("wrong id...");
8385
break;
8486
}
87+
uid.close();
8588
}
8689
} catch (Exception e) {
8790
e.printStackTrace();

modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/uid/UidField.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ public DocIdAndVersion(int docId, long version) {
5252

5353
public static DocIdAndVersion loadDocIdAndVersion(IndexReader reader, Term term) {
5454
int docId = Lucene.NO_DOC;
55+
TermPositions uid = null;
5556
try {
56-
TermPositions uid = reader.termPositions(term);
57+
uid = reader.termPositions(term);
5758
if (!uid.next()) {
5859
return new DocIdAndVersion(Lucene.NO_DOC, -1);
5960
}
@@ -69,6 +70,14 @@ public static DocIdAndVersion loadDocIdAndVersion(IndexReader reader, Term term)
6970
return new DocIdAndVersion(docId, Numbers.bytesToLong(payload));
7071
} catch (Exception e) {
7172
return new DocIdAndVersion(docId, -2);
73+
} finally {
74+
if (uid != null) {
75+
try {
76+
uid.close();
77+
} catch (IOException e) {
78+
// nothing to do here...
79+
}
80+
}
7281
}
7382
}
7483

@@ -77,8 +86,9 @@ public static DocIdAndVersion loadDocIdAndVersion(IndexReader reader, Term term)
7786
* no version is available (for backward comp.)
7887
*/
7988
public static long loadVersion(IndexReader reader, Term term) {
89+
TermPositions uid = null;
8090
try {
81-
TermPositions uid = reader.termPositions(term);
91+
uid = reader.termPositions(term);
8292
if (!uid.next()) {
8393
return -1;
8494
}
@@ -93,6 +103,14 @@ public static long loadVersion(IndexReader reader, Term term) {
93103
return Numbers.bytesToLong(payload);
94104
} catch (Exception e) {
95105
return -2;
106+
} finally {
107+
if (uid != null) {
108+
try {
109+
uid.close();
110+
} catch (IOException e) {
111+
// nothing to do here...
112+
}
113+
}
96114
}
97115
}
98116

modules/elasticsearch/src/main/java/org/elasticsearch/search/fetch/version/VersionSearchHitPhase.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ public class VersionSearchHitPhase implements SearchHitPhase {
4444
}
4545

4646
@Override public void execute(SearchContext context, HitContext hitContext) throws ElasticSearchException {
47+
// it might make sense to cache the TermDocs on a shared fetch context and just skip here)
48+
// it is going to mean we work on the high level multi reader and not the lower level reader as is
49+
// the case below...
4750
long version = UidField.loadVersion(hitContext.reader(), new Term(UidFieldMapper.NAME, hitContext.doc().get(UidFieldMapper.NAME)));
4851
if (version < 0) {
4952
version = -1;

0 commit comments

Comments
 (0)