Skip to content

Commit 6e09019

Browse files
committed
Use BufferSink generally in Repositories. Remove redundancies.
1 parent 2049f2b commit 6e09019

16 files changed

+222
-266
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/history/AccuRevRepository.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@
1919

2020
/*
2121
* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
22+
* Portions Copyright (c) 2018, Chris Fraire <[email protected]>.
2223
*/
2324
package org.opengrok.indexer.history;
2425

2526
import java.io.BufferedReader;
26-
import java.io.ByteArrayInputStream;
2727
import java.io.File;
2828
import java.io.IOException;
29-
import java.io.InputStream;
3029
import java.nio.file.Files;
3130
import java.nio.file.Paths;
3231
import java.nio.file.Path;
@@ -38,6 +37,7 @@
3837
import java.util.regex.Pattern;
3938
import org.opengrok.indexer.configuration.RuntimeEnvironment;
4039
import org.opengrok.indexer.logger.LoggerFactory;
40+
import org.opengrok.indexer.util.BufferSink;
4141
import org.opengrok.indexer.util.Executor;
4242

4343
/**
@@ -165,10 +165,10 @@ Executor getHistoryLogExecutor(File file) throws IOException {
165165
}
166166

167167
@Override
168-
InputStream getHistoryGet(String parent, String basename, String rev) {
168+
boolean getHistoryGet(
169+
BufferSink sink, String parent, String basename, String rev) {
169170

170171
ArrayList<String> cmd = new ArrayList<>();
171-
InputStream inputStream = null;
172172
File directory = new File(parent);
173173

174174
/*
@@ -216,12 +216,16 @@ InputStream getHistoryGet(String parent, String basename, String rev) {
216216

217217
executor = new Executor(cmd, directory);
218218
executor.exec();
219-
220-
inputStream
221-
= new ByteArrayInputStream(executor.getOutputString().getBytes());
219+
try {
220+
copyBytes(sink, executor.getOutputStream());
221+
return true;
222+
} catch (IOException e) {
223+
LOGGER.log(Level.SEVERE, "Failed to obtain content for {0}",
224+
basename);
225+
}
222226
}
223227

224-
return inputStream;
228+
return false;
225229
}
226230

227231
@Override

opengrok-indexer/src/main/java/org/opengrok/indexer/history/BazaarRepository.java

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@
1919

2020
/*
2121
* Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
22-
* Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
22+
* Portions Copyright (c) 2017-2018, Chris Fraire <[email protected]>.
2323
*/
2424
package org.opengrok.indexer.history;
2525

26-
import java.io.ByteArrayInputStream;
27-
import java.io.ByteArrayOutputStream;
2826
import java.io.File;
2927
import java.io.IOException;
3028
import java.io.InputStream;
@@ -35,6 +33,7 @@
3533
import java.util.logging.Logger;
3634
import org.opengrok.indexer.configuration.RuntimeEnvironment;
3735
import org.opengrok.indexer.logger.LoggerFactory;
36+
import org.opengrok.indexer.util.BufferSink;
3837
import org.opengrok.indexer.util.Executor;
3938

4039
/**
@@ -96,31 +95,21 @@ Executor getHistoryLogExecutor(final File file, final String sinceRevision)
9695
}
9796

9897
@Override
99-
public InputStream getHistoryGet(String parent, String basename, String rev) {
100-
InputStream ret = null;
98+
boolean getHistoryGet(
99+
BufferSink sink, String parent, String basename, String rev) {
101100

102101
File directory = new File(getDirectoryName());
103-
104102
Process process = null;
105103
try {
106104
String filename = (new File(parent, basename)).getCanonicalPath()
107105
.substring(getDirectoryName().length() + 1);
108106
ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK);
109107
String[] argv = {RepoCommand, "cat", "-r", rev, filename};
110108
process = Runtime.getRuntime().exec(argv, null, directory);
111-
112-
ByteArrayOutputStream out = new ByteArrayOutputStream();
113-
byte[] buffer = new byte[32 * 1024];
114-
InputStream in = process.getInputStream();
115-
int len;
116-
117-
while ((len = in.read(buffer)) != -1) {
118-
if (len > 0) {
119-
out.write(buffer, 0, len);
120-
}
109+
try (InputStream in = process.getInputStream()) {
110+
copyBytes(sink, in);
121111
}
122-
123-
ret = new ByteArrayInputStream(out.toByteArray());
112+
return true;
124113
} catch (Exception exp) {
125114
LOGGER.log(Level.SEVERE,
126115
"Failed to get history: " + exp.getClass().toString(), exp);
@@ -136,7 +125,7 @@ public InputStream getHistoryGet(String parent, String basename, String rev) {
136125
}
137126
}
138127

139-
return ret;
128+
return false;
140129
}
141130

142131
/**

opengrok-indexer/src/main/java/org/opengrok/indexer/history/BitKeeperRepository.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,23 @@
1717
* CDDL HEADER END
1818
*/
1919

20+
/*
21+
* Author: James Service <[email protected]>
22+
* Portions by: Oracle and/or its affiliates.
23+
* Portions Copyright (c) 2018, Chris Fraire <[email protected]>.
24+
*/
25+
2026
package org.opengrok.indexer.history;
2127

2228
import java.io.File;
2329
import java.io.IOException;
24-
import java.io.InputStream;
2530
import java.util.ArrayList;
2631
import java.util.logging.Level;
2732
import java.util.logging.Logger;
2833
import java.util.regex.Matcher;
2934
import java.util.regex.Pattern;
3035

36+
import org.opengrok.indexer.util.BufferSink;
3137
import org.suigeneris.jrcs.rcs.InvalidVersionNumberException;
3238
import org.suigeneris.jrcs.rcs.Version;
3339
import org.opengrok.indexer.configuration.RuntimeEnvironment;
@@ -294,18 +300,11 @@ History getHistory(File file, String sinceRevision) throws HistoryException {
294300
return history;
295301
}
296302

297-
/**
298-
* Return an InputStream of the content of a given file at a given revision.
299-
*
300-
* @param parent the directory the file is in
301-
* @param basename the basename of the file
302-
* @param revision revision, or null for latest
303-
* @return output an input stream
304-
*/
305303
@Override
306-
public InputStream getHistoryGet(String parent, String basename, String revision) {
307-
final File directory = new File(parent).getAbsoluteFile();
304+
boolean getHistoryGet(
305+
BufferSink sink, String parent, String basename, String revision) {
308306

307+
final File directory = new File(parent).getAbsoluteFile();
309308
final ArrayList<String> argv = new ArrayList<String>();
310309
ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK);
311310
argv.add(RepoCommand);
@@ -320,10 +319,18 @@ public InputStream getHistoryGet(String parent, String basename, String revision
320319
RuntimeEnvironment.getInstance().getInteractiveCommandTimeout());
321320
if (executor.exec(true) != 0) {
322321
LOGGER.log(Level.SEVERE, "Failed to get history: {0}", executor.getErrorString());
323-
return null;
322+
return false;
324323
}
325324

326-
return executor.getOutputStream();
325+
try {
326+
copyBytes(sink, executor.getOutputStream());
327+
return true;
328+
} catch (IOException e) {
329+
LOGGER.log(Level.SEVERE, "Failed to get content for {0}",
330+
basename);
331+
}
332+
333+
return false;
327334
}
328335

329336
/* Annotation Stuff */

opengrok-indexer/src/main/java/org/opengrok/indexer/history/ClearCaseRepository.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,14 @@
1919

2020
/*
2121
* Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
22-
* Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
22+
* Portions Copyright (c) 2017-2018, Chris Fraire <[email protected]>.
2323
*/
2424
package org.opengrok.indexer.history;
2525

26-
import java.io.BufferedInputStream;
2726
import java.io.BufferedReader;
2827
import java.io.File;
2928
import java.io.FileInputStream;
3029
import java.io.IOException;
31-
import java.io.InputStream;
3230
import java.io.InputStreamReader;
3331
import java.util.ArrayList;
3432
import java.util.Arrays;
@@ -38,6 +36,7 @@
3836
import java.util.regex.Pattern;
3937
import org.opengrok.indexer.configuration.RuntimeEnvironment;
4038
import org.opengrok.indexer.logger.LoggerFactory;
39+
import org.opengrok.indexer.util.BufferSink;
4140
import org.opengrok.indexer.util.Executor;
4241

4342
/**
@@ -91,8 +90,8 @@ Executor getHistoryLogExecutor(final File file) throws IOException {
9190
}
9291

9392
@Override
94-
public InputStream getHistoryGet(String parent, String basename, String rev) {
95-
InputStream ret = null;
93+
boolean getHistoryGet(
94+
BufferSink sink, String parent, String basename, String rev) {
9695

9796
File directory = new File(getDirectoryName());
9897

@@ -117,28 +116,26 @@ public InputStream getHistoryGet(String parent, String basename, String rev) {
117116
if (status != 0) {
118117
LOGGER.log(Level.SEVERE, "Failed to get history: {0}",
119118
executor.getErrorString());
120-
return null;
119+
return false;
121120
}
122121

123-
ret = new BufferedInputStream(new FileInputStream(tmp)) {
124-
125-
@Override
126-
public void close() throws IOException {
127-
super.close();
128-
// delete the temporary file on close
129-
if (!tmp.delete()) {
130-
// failed, lets do the next best thing then ..
131-
// delete it on JVM exit
132-
tmp.deleteOnExit();
133-
}
122+
try (FileInputStream in = new FileInputStream(tmp)) {
123+
copyBytes(sink, in);
124+
} finally {
125+
// delete the temporary file on close
126+
if (!tmp.delete()) {
127+
// failed, lets do the next best thing then ..
128+
// delete it on JVM exit
129+
tmp.deleteOnExit();
134130
}
135-
};
131+
}
132+
return true;
136133
} catch (Exception exp) {
137134
LOGGER.log(Level.WARNING,
138135
"Failed to get history: " + exp.getClass().toString(), exp);
139136
}
140137

141-
return ret;
138+
return false;
142139
}
143140

144141
/**

opengrok-indexer/src/main/java/org/opengrok/indexer/history/GitRepository.java

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,10 @@
2424
package org.opengrok.indexer.history;
2525

2626
import java.io.BufferedReader;
27-
import java.io.ByteArrayInputStream;
28-
import java.io.ByteArrayOutputStream;
2927
import java.io.File;
30-
import java.io.FileOutputStream;
3128
import java.io.IOException;
3229
import java.io.InputStream;
3330
import java.io.InputStreamReader;
34-
import java.io.OutputStream;
3531
import java.io.Reader;
3632
import java.nio.file.Paths;
3733
import java.text.ParseException;
@@ -203,7 +199,7 @@ private HistoryRevResult getHistoryRev(
203199
Executor executor = new Executor(Arrays.asList(argv), directory,
204200
RuntimeEnvironment.getInstance().getInteractiveCommandTimeout());
205201
int status = executor.exec();
206-
result.iterations = transferExecutorOutput(sink, executor);
202+
result.iterations = copyBytes(sink, executor.getOutputStream());
207203

208204
/*
209205
* If exit value of the process was not 0 then the file did
@@ -218,39 +214,10 @@ private HistoryRevResult getHistoryRev(
218214
return result;
219215
}
220216

221-
/**
222-
* Gets the contents of a specific version of a named file into the
223-
* specified target without a full, in-memory buffer.
224-
*
225-
* @param target a required target file which will be overwritten
226-
* @param parent the name of the directory containing the file
227-
* @param basename the name of the file to get
228-
* @param rev the revision to get
229-
* @return {@code true} if contents were found
230-
* @throws java.io.IOException if an I/O error occurs
231-
*/
232-
@Override
233-
public boolean getHistoryGet(File target, String parent, String basename,
234-
String rev) throws IOException {
235-
try (OutputStream out = new FileOutputStream(target)) {
236-
return getHistoryGet((buf, offset, n) -> out.write(buf, offset, n),
237-
parent, basename, rev);
238-
}
239-
}
240-
241217
@Override
242-
public InputStream getHistoryGet(String parent, String basename,
243-
String rev) {
244-
ByteArrayOutputStream out = new ByteArrayOutputStream();
245-
if (getHistoryGet((buf, offset, n) -> out.write(buf, offset, n),
246-
parent, basename, rev)) {
247-
return new ByteArrayInputStream(out.toByteArray());
248-
}
249-
return null;
250-
}
218+
boolean getHistoryGet(
219+
BufferSink sink, String parent, String basename, String rev) {
251220

252-
protected boolean getHistoryGet(BufferSink sink, String parent,
253-
String basename, String rev) {
254221
String fullpath;
255222
try {
256223
fullpath = new File(parent, basename).getCanonicalPath();
@@ -283,7 +250,7 @@ public String get() {
283250
});
284251
return false;
285252
}
286-
if (origpath != null) {
253+
if (origpath != null && !origpath.equals(fullpath)) {
287254
result = getHistoryRev(sink, origpath, rev);
288255
}
289256
}

0 commit comments

Comments
 (0)