Skip to content

Commit 7759fde

Browse files
AROP-REPLICATIONxcx
andauthored
Fix resource leak in loadMainDataFromFile (#14727)
Co-authored-by: xcx <[email protected]>
1 parent 5c88660 commit 7759fde

File tree

5 files changed

+141
-128
lines changed

5 files changed

+141
-128
lines changed

lucene/CHANGES.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ Bug Fixes
148148
* GITHUB#14161: PointInSetQuery's constructor now throws IllegalArgumentException
149149
instead of UnsupportedOperationException when values are out of order. (Shubham Sharma)
150150

151+
* GITHUB#14727: Fix several resource leaks. (Xu Chengxin)
152+
151153
Build
152154
---------------------
153155
* Upgrade forbiddenapis to version 3.9. (Uwe Schindler)

lucene/analysis/smartcn/src/java/org/apache/lucene/analysis/cn/smart/hhmm/BigramDictionary.java

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -150,53 +150,56 @@ public void loadFromFile(String dctFilePath) throws IOException {
150150
int[] buffer = new int[3];
151151
byte[] intBuffer = new byte[4];
152152
String tmpword;
153-
DataInputStream dctFile = new DataInputStream(Files.newInputStream(Paths.get(dctFilePath)));
154-
155-
// GB2312 characters 0 - 6768
156-
for (i = GB2312_FIRST_CHAR; i < GB2312_FIRST_CHAR + CHAR_NUM_IN_FILE; i++) {
157-
String currentStr = getCCByGB2312Id(i);
158-
// if (i == 5231)
159-
// System.out.println(i);
160-
161-
dctFile.read(intBuffer);
162-
// the dictionary was developed for C, and byte order must be converted to work with Java
163-
cnt = ByteBuffer.wrap(intBuffer).order(ByteOrder.LITTLE_ENDIAN).getInt();
164-
if (cnt <= 0) {
165-
continue;
166-
}
167-
int j = 0;
168-
while (j < cnt) {
169-
dctFile.read(intBuffer);
170-
buffer[0] = ByteBuffer.wrap(intBuffer).order(ByteOrder.LITTLE_ENDIAN).getInt(); // frequency
171-
dctFile.read(intBuffer);
172-
buffer[1] = ByteBuffer.wrap(intBuffer).order(ByteOrder.LITTLE_ENDIAN).getInt(); // length
153+
// Using try-with-resources
154+
try (DataInputStream dctFile =
155+
new DataInputStream(Files.newInputStream(Paths.get(dctFilePath)))) {
156+
157+
// GB2312 characters 0 - 6768
158+
for (i = GB2312_FIRST_CHAR; i < GB2312_FIRST_CHAR + CHAR_NUM_IN_FILE; i++) {
159+
String currentStr = getCCByGB2312Id(i);
160+
// if (i == 5231)
161+
// System.out.println(i);
162+
173163
dctFile.read(intBuffer);
174-
// buffer[2] = ByteBuffer.wrap(intBuffer).order(
175-
// ByteOrder.LITTLE_ENDIAN).getInt();// handle
176-
177-
length = buffer[1];
178-
if (length > 0) {
179-
byte[] lchBuffer = new byte[length];
180-
dctFile.read(lchBuffer);
181-
tmpword = new String(lchBuffer, "GB2312");
182-
if (i != 3755 + GB2312_FIRST_CHAR) {
183-
tmpword = currentStr + tmpword;
184-
}
185-
char[] carray = tmpword.toCharArray();
186-
long hashId = hash1(carray);
187-
int index = getAvaliableIndex(hashId, carray);
188-
if (index != -1) {
189-
if (bigramHashTable[index] == 0) {
190-
bigramHashTable[index] = hashId;
191-
// bigramStringTable[index] = tmpword;
164+
// the dictionary was developed for C, and byte order must be converted to work with Java
165+
cnt = ByteBuffer.wrap(intBuffer).order(ByteOrder.LITTLE_ENDIAN).getInt();
166+
if (cnt <= 0) {
167+
continue;
168+
}
169+
int j = 0;
170+
while (j < cnt) {
171+
dctFile.read(intBuffer);
172+
buffer[0] =
173+
ByteBuffer.wrap(intBuffer).order(ByteOrder.LITTLE_ENDIAN).getInt(); // frequency
174+
dctFile.read(intBuffer);
175+
buffer[1] = ByteBuffer.wrap(intBuffer).order(ByteOrder.LITTLE_ENDIAN).getInt(); // length
176+
dctFile.read(intBuffer);
177+
// buffer[2] = ByteBuffer.wrap(intBuffer).order(
178+
// ByteOrder.LITTLE_ENDIAN).getInt();// handle
179+
180+
length = buffer[1];
181+
if (length > 0) {
182+
byte[] lchBuffer = new byte[length];
183+
dctFile.read(lchBuffer);
184+
tmpword = new String(lchBuffer, "GB2312");
185+
if (i != 3755 + GB2312_FIRST_CHAR) {
186+
tmpword = currentStr + tmpword;
187+
}
188+
char[] carray = tmpword.toCharArray();
189+
long hashId = hash1(carray);
190+
int index = getAvaliableIndex(hashId, carray);
191+
if (index != -1) {
192+
if (bigramHashTable[index] == 0) {
193+
bigramHashTable[index] = hashId;
194+
// bigramStringTable[index] = tmpword;
195+
}
196+
frequencyTable[index] += buffer[0];
192197
}
193-
frequencyTable[index] += buffer[0];
194198
}
199+
j++;
195200
}
196-
j++;
197201
}
198202
}
199-
dctFile.close();
200203
// log.info("load dictionary done! " + dctFilePath + " total:" + total);
201204
}
202205

lucene/analysis/smartcn/src/java/org/apache/lucene/analysis/cn/smart/hhmm/WordDictionary.java

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -189,57 +189,60 @@ private int loadMainDataFromFile(String dctFilePath) throws IOException {
189189
int[] buffer = new int[3];
190190
byte[] intBuffer = new byte[4];
191191
String tmpword;
192-
DataInputStream dctFile = new DataInputStream(Files.newInputStream(Paths.get(dctFilePath)));
193-
194-
// GB2312 characters 0 - 6768
195-
for (i = GB2312_FIRST_CHAR; i < GB2312_FIRST_CHAR + CHAR_NUM_IN_FILE; i++) {
196-
// if (i == 5231)
197-
// System.out.println(i);
198-
199-
dctFile.read(intBuffer);
200-
// the dictionary was developed for C, and byte order must be converted to work with Java
201-
cnt = ByteBuffer.wrap(intBuffer).order(ByteOrder.LITTLE_ENDIAN).getInt();
202-
if (cnt <= 0) {
203-
wordItem_charArrayTable[i] = null;
204-
wordItem_frequencyTable[i] = null;
205-
continue;
206-
}
207-
wordItem_charArrayTable[i] = new char[cnt][];
208-
wordItem_frequencyTable[i] = new int[cnt];
209-
total += cnt;
210-
int j = 0;
211-
while (j < cnt) {
212-
// wordItemTable[i][j] = new WordItem();
213-
dctFile.read(intBuffer);
214-
buffer[0] = ByteBuffer.wrap(intBuffer).order(ByteOrder.LITTLE_ENDIAN).getInt(); // frequency
215-
dctFile.read(intBuffer);
216-
buffer[1] = ByteBuffer.wrap(intBuffer).order(ByteOrder.LITTLE_ENDIAN).getInt(); // length
192+
// Use try-with-resources to ensure the stream is always closed
193+
try (DataInputStream dctFile =
194+
new DataInputStream(Files.newInputStream(Paths.get(dctFilePath)))) {
195+
196+
// GB2312 characters 0 - 6768
197+
for (i = GB2312_FIRST_CHAR; i < GB2312_FIRST_CHAR + CHAR_NUM_IN_FILE; i++) {
198+
// if (i == 5231)
199+
// System.out.println(i);
200+
217201
dctFile.read(intBuffer);
218-
buffer[2] = ByteBuffer.wrap(intBuffer).order(ByteOrder.LITTLE_ENDIAN).getInt(); // handle
219-
220-
// wordItemTable[i][j].frequency = buffer[0];
221-
wordItem_frequencyTable[i][j] = buffer[0];
222-
223-
length = buffer[1];
224-
if (length > 0) {
225-
byte[] lchBuffer = new byte[length];
226-
dctFile.read(lchBuffer);
227-
tmpword = new String(lchBuffer, "GB2312");
228-
// indexTable[i].wordItems[j].word = tmpword;
229-
// wordItemTable[i][j].charArray = tmpword.toCharArray();
230-
wordItem_charArrayTable[i][j] = tmpword.toCharArray();
231-
} else {
232-
// wordItemTable[i][j].charArray = null;
233-
wordItem_charArrayTable[i][j] = null;
202+
// the dictionary was developed for C, and byte order must be converted to work with Java
203+
cnt = ByteBuffer.wrap(intBuffer).order(ByteOrder.LITTLE_ENDIAN).getInt();
204+
if (cnt <= 0) {
205+
wordItem_charArrayTable[i] = null;
206+
wordItem_frequencyTable[i] = null;
207+
continue;
208+
}
209+
wordItem_charArrayTable[i] = new char[cnt][];
210+
wordItem_frequencyTable[i] = new int[cnt];
211+
total += cnt;
212+
int j = 0;
213+
while (j < cnt) {
214+
// wordItemTable[i][j] = new WordItem();
215+
dctFile.read(intBuffer);
216+
buffer[0] =
217+
ByteBuffer.wrap(intBuffer).order(ByteOrder.LITTLE_ENDIAN).getInt(); // frequency
218+
dctFile.read(intBuffer);
219+
buffer[1] = ByteBuffer.wrap(intBuffer).order(ByteOrder.LITTLE_ENDIAN).getInt(); // length
220+
dctFile.read(intBuffer);
221+
buffer[2] = ByteBuffer.wrap(intBuffer).order(ByteOrder.LITTLE_ENDIAN).getInt(); // handle
222+
223+
// wordItemTable[i][j].frequency = buffer[0];
224+
wordItem_frequencyTable[i][j] = buffer[0];
225+
226+
length = buffer[1];
227+
if (length > 0) {
228+
byte[] lchBuffer = new byte[length];
229+
dctFile.read(lchBuffer);
230+
tmpword = new String(lchBuffer, "GB2312");
231+
// indexTable[i].wordItems[j].word = tmpword;
232+
// wordItemTable[i][j].charArray = tmpword.toCharArray();
233+
wordItem_charArrayTable[i][j] = tmpword.toCharArray();
234+
} else {
235+
// wordItemTable[i][j].charArray = null;
236+
wordItem_charArrayTable[i][j] = null;
237+
}
238+
// System.out.println(indexTable[i].wordItems[j]);
239+
j++;
234240
}
235-
// System.out.println(indexTable[i].wordItems[j]);
236-
j++;
237-
}
238241

239-
String str = getCCByGB2312Id(i);
240-
setTableIndex(str.charAt(0), i);
242+
String str = getCCByGB2312Id(i);
243+
setTableIndex(str.charAt(0), i);
244+
}
241245
}
242-
dctFile.close();
243246
return total;
244247
}
245248

lucene/benchmark/src/java/org/apache/lucene/benchmark/byTask/feeds/DirContentSource.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -210,18 +210,23 @@ public DocData getNextDocData(DocData docData) throws NoMoreDataException, IOExc
210210
name = f.toRealPath() + "_" + iteration;
211211
}
212212

213-
BufferedReader reader = Files.newBufferedReader(f, StandardCharsets.UTF_8);
214213
String line = null;
215-
// First line is the date, 3rd is the title, rest is body
216-
String dateStr = reader.readLine();
217-
reader.readLine(); // skip an empty line
218-
String title = reader.readLine();
219-
reader.readLine(); // skip an empty line
220-
StringBuilder bodyBuf = new StringBuilder(1024);
221-
while ((line = reader.readLine()) != null) {
222-
bodyBuf.append(line).append(' ');
214+
String title = null;
215+
String dateStr = null;
216+
StringBuilder bodyBuf = null;
217+
218+
// Use try-with-resources to ensure the reader is closed
219+
try (BufferedReader reader = Files.newBufferedReader(f, StandardCharsets.UTF_8)) {
220+
// First line is the date, 3rd is the title, rest is body
221+
dateStr = reader.readLine();
222+
reader.readLine(); // skip an empty line
223+
title = reader.readLine();
224+
reader.readLine(); // skip an empty line
225+
bodyBuf = new StringBuilder(1024);
226+
while ((line = reader.readLine()) != null) {
227+
bodyBuf.append(line).append(' ');
228+
}
223229
}
224-
reader.close();
225230
addBytes(Files.size(f));
226231

227232
Date date = parseDate(dateStr);

lucene/benchmark/src/java/org/apache/lucene/benchmark/quality/trec/QueryDriver.java

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -57,45 +57,45 @@ public static void main(String[] args) throws Exception {
5757
new SubmissionReport(
5858
new PrintWriter(Files.newBufferedWriter(submissionFile, StandardCharsets.UTF_8)),
5959
"lucene");
60-
FSDirectory dir = FSDirectory.open(Paths.get(args[3]));
6160
String fieldSpec = args.length == 5 ? args[4] : "T"; // default to Title-only if not specified.
62-
IndexReader reader = DirectoryReader.open(dir);
63-
IndexSearcher searcher = new IndexSearcher(reader);
6461

65-
int maxResults = 1000;
66-
String docNameField = "docname";
62+
// --- Use try-with-resources for FSDirectory and IndexReader ---
63+
try (FSDirectory dir = FSDirectory.open(Paths.get(args[3]));
64+
IndexReader reader = DirectoryReader.open(dir)) {
65+
IndexSearcher searcher = new IndexSearcher(reader);
66+
int maxResults = 1000;
67+
String docNameField = "docname";
6768

68-
PrintWriter logger =
69-
new PrintWriter(new OutputStreamWriter(System.out, Charset.defaultCharset()), true);
69+
PrintWriter logger =
70+
new PrintWriter(new OutputStreamWriter(System.out, Charset.defaultCharset()), true);
7071

71-
// use trec utilities to read trec topics into quality queries
72-
TrecTopicsReader qReader = new TrecTopicsReader();
73-
QualityQuery[] qqs =
74-
qReader.readQueries(Files.newBufferedReader(topicsFile, StandardCharsets.UTF_8));
72+
// use trec utilities to read trec topics into quality queries
73+
TrecTopicsReader qReader = new TrecTopicsReader();
74+
QualityQuery[] qqs =
75+
qReader.readQueries(Files.newBufferedReader(topicsFile, StandardCharsets.UTF_8));
7576

76-
// prepare judge, with trec utilities that read from a QRels file
77-
Judge judge = new TrecJudge(Files.newBufferedReader(qrelsFile, StandardCharsets.UTF_8));
77+
// prepare judge, with trec utilities that read from a QRels file
78+
Judge judge = new TrecJudge(Files.newBufferedReader(qrelsFile, StandardCharsets.UTF_8));
7879

79-
// validate topics & judgments match each other
80-
judge.validateData(qqs, logger);
80+
// validate topics & judgments match each other
81+
judge.validateData(qqs, logger);
8182

82-
Set<String> fieldSet = new HashSet<>();
83-
if (fieldSpec.indexOf('T') >= 0) fieldSet.add("title");
84-
if (fieldSpec.indexOf('D') >= 0) fieldSet.add("description");
85-
if (fieldSpec.indexOf('N') >= 0) fieldSet.add("narrative");
83+
Set<String> fieldSet = new HashSet<>();
84+
if (fieldSpec.indexOf('T') >= 0) fieldSet.add("title");
85+
if (fieldSpec.indexOf('D') >= 0) fieldSet.add("description");
86+
if (fieldSpec.indexOf('N') >= 0) fieldSet.add("narrative");
8687

87-
// set the parsing of quality queries into Lucene queries.
88-
QualityQueryParser qqParser = new SimpleQQParser(fieldSet.toArray(new String[0]), "body");
88+
// set the parsing of quality queries into Lucene queries.
89+
QualityQueryParser qqParser = new SimpleQQParser(fieldSet.toArray(new String[0]), "body");
8990

90-
// run the benchmark
91-
QualityBenchmark qrun = new QualityBenchmark(qqs, qqParser, searcher, docNameField);
92-
qrun.setMaxResults(maxResults);
93-
QualityStats[] stats = qrun.execute(judge, submitLog, logger);
91+
// run the benchmark
92+
QualityBenchmark qrun = new QualityBenchmark(qqs, qqParser, searcher, docNameField);
93+
qrun.setMaxResults(maxResults);
94+
QualityStats[] stats = qrun.execute(judge, submitLog, logger);
9495

95-
// print an avarage sum of the results
96-
QualityStats avg = QualityStats.average(stats);
97-
avg.log("SUMMARY", 2, logger, " ");
98-
reader.close();
99-
dir.close();
96+
// print an avarage sum of the results
97+
QualityStats avg = QualityStats.average(stats);
98+
avg.log("SUMMARY", 2, logger, " ");
99+
}
100100
}
101101
}

0 commit comments

Comments
 (0)