Skip to content

Commit 2049f2b

Browse files
committed
Use object for multi-return value. Fix value check.
1 parent e829566 commit 2049f2b

File tree

3 files changed

+54
-53
lines changed

3 files changed

+54
-53
lines changed

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

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,16 @@ Executor getRenamedFilesExecutor(final File file, String sinceRevision) throws I
175175
* Try to get file contents for given revision.
176176
*
177177
* @param sink a required target sink
178-
* @param outIterations a required out array for storing the number of calls
179-
* made to {@code sink}
180178
* @param fullpath full pathname of the file
181179
* @param rev revision
182-
* @return {@code true} if any contents were found
180+
* @return a defined instance with {@code success} == {@code true} if no
181+
* error occurred and with non-zero {@code iterations} if some data was
182+
* transferred
183183
*/
184-
private boolean getHistoryRev(BufferSink sink, int[] outIterations,
185-
String fullpath, String rev) {
184+
private HistoryRevResult getHistoryRev(
185+
BufferSink sink, String fullpath, String rev) {
186186

187-
outIterations[0] = 0;
187+
HistoryRevResult result = new HistoryRevResult();
188188
File directory = new File(getDirectoryName());
189189
try {
190190
/*
@@ -203,29 +203,19 @@ private boolean getHistoryRev(BufferSink sink, int[] outIterations,
203203
Executor executor = new Executor(Arrays.asList(argv), directory,
204204
RuntimeEnvironment.getInstance().getInteractiveCommandTimeout());
205205
int status = executor.exec();
206-
207-
byte[] buffer = new byte[32 * 1024];
208-
try (InputStream in = executor.getOutputStream()) {
209-
int len;
210-
while ((len = in.read(buffer)) != -1) {
211-
if (len > 0) {
212-
outIterations[0]++;
213-
sink.write(buffer, 0, len);
214-
}
215-
}
216-
}
206+
result.iterations = transferExecutorOutput(sink, executor);
217207

218208
/*
219209
* If exit value of the process was not 0 then the file did
220210
* not exist or internal git error occured.
221211
*/
222-
return status == 0;
212+
result.success = (status == 0);
223213
} catch (Exception exp) {
224214
LOGGER.log(Level.SEVERE,
225215
"Failed to get history for file {0} in revision {1}: ",
226216
new Object[]{fullpath, rev, exp.getClass().toString(), exp});
227-
return false;
228217
}
218+
return result;
229219
}
230220

231221
/**
@@ -274,10 +264,8 @@ public String get() {
274264
return false;
275265
}
276266

277-
int[] iterations = new int[1];
278-
boolean ret = getHistoryRev((buf, offset, n) -> sink.write(buf, offset,
279-
n), iterations, fullpath, rev);
280-
if (!ret && iterations[0] < 1) {
267+
HistoryRevResult result = getHistoryRev(sink::write, fullpath, rev);
268+
if (!result.success && result.iterations < 1) {
281269
/*
282270
* If we failed to get the contents it might be that the file was
283271
* renamed so we need to find its original name in that revision
@@ -296,11 +284,11 @@ public String get() {
296284
return false;
297285
}
298286
if (origpath != null) {
299-
ret = getHistoryRev(sink, iterations, origpath, rev);
287+
result = getHistoryRev(sink, origpath, rev);
300288
}
301289
}
302290

303-
return ret;
291+
return result.success;
304292
}
305293

306294
/**

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

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -210,20 +210,19 @@ Executor getHistoryLogExecutor(File file, String sinceRevision)
210210
* Try to get file contents for given revision.
211211
*
212212
* @param sink a required target sink
213-
* @param outIterations a required out array for storing the number of calls
214-
* made to {@code sink}
215213
* @param fullpath full pathname of the file
216214
* @param rev revision
217-
* @return {@code true} if any contents were found
215+
* @return a defined instance with {@code success} == {@code true} if no
216+
* error occurred and with non-zero {@code iterations} if some data was
217+
* transferred
218218
*/
219-
private boolean getHistoryRev(BufferSink sink, int[] outIterations,
220-
String fullpath, String rev) {
221-
InputStream ret = null;
219+
private HistoryRevResult getHistoryRev(
220+
BufferSink sink, String fullpath, String rev) {
222221

223-
outIterations[0] = 0;
222+
HistoryRevResult result = new HistoryRevResult();
224223
File directory = new File(getDirectoryName());
225-
String revision = rev;
226224

225+
String revision = rev;
227226
if (rev.indexOf(':') != -1) {
228227
revision = rev.substring(0, rev.indexOf(':'));
229228
}
@@ -235,27 +234,17 @@ private boolean getHistoryRev(BufferSink sink, int[] outIterations,
235234
Executor executor = new Executor(Arrays.asList(argv), directory,
236235
RuntimeEnvironment.getInstance().getInteractiveCommandTimeout());
237236
int status = executor.exec();
238-
239-
byte[] buffer = new byte[32 * 1024];
240-
try (InputStream in = executor.getOutputStream()) {
241-
int len;
242-
while ((len = in.read(buffer)) != -1) {
243-
if (len > 0) {
244-
outIterations[0]++;
245-
sink.write(buffer, 0, len);
246-
}
247-
}
248-
}
237+
result.iterations = transferExecutorOutput(sink, executor);
249238

250239
/*
251240
* If exit value of the process was not 0 then the file did
252241
* not exist or internal hg error occured.
253242
*/
254-
return status == 0;
243+
result.success = (status == 0);
255244
} catch (Exception exp) {
256245
LOGGER.log(Level.SEVERE, "Failed to get history", exp);
257-
return false;
258246
}
247+
return result;
259248
}
260249

261250
/**
@@ -399,10 +388,8 @@ protected boolean getHistoryGet(BufferSink sink, String parent,
399388
return false;
400389
}
401390

402-
int[] iterations = new int[1];
403-
boolean ret = getHistoryRev((buf, offset, n) -> sink.write(buf, offset,
404-
n), iterations, fullpath, rev);
405-
if (!ret && iterations[0] < 1) {
391+
HistoryRevResult result = getHistoryRev(sink::write, fullpath, rev);
392+
if (!result.success && result.iterations < 1) {
406393
/*
407394
* If we failed to get the contents it might be that the file was
408395
* renamed so we need to find its original name in that revision
@@ -418,11 +405,11 @@ protected boolean getHistoryGet(BufferSink sink, String parent,
418405
return false;
419406
}
420407
if (origpath != null) {
421-
ret = getHistoryRev(sink, iterations, origpath, rev);
408+
result = getHistoryRev(sink, origpath, rev);
422409
}
423410
}
424411

425-
return ret;
412+
return result.success;
426413
}
427414

428415
/**

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.util.logging.Logger;
4444
import org.opengrok.indexer.configuration.RuntimeEnvironment;
4545
import org.opengrok.indexer.logger.LoggerFactory;
46+
import org.opengrok.indexer.util.BufferSink;
4647
import org.opengrok.indexer.util.Executor;
4748

4849
/**
@@ -215,7 +216,7 @@ public boolean getHistoryGet(File target, String parent, String basename,
215216
try (OutputStream out = new FileOutputStream(target)) {
216217
byte[] buf = new byte[8 * 1024];
217218
int n;
218-
while ((n = in.read(buf)) != 0) {
219+
while ((n = in.read(buf)) != -1) {
219220
out.write(buf, 0, n);
220221
}
221222
}
@@ -582,6 +583,31 @@ protected String getRepoRelativePath(final File file)
582583
return filename;
583584
}
584585

586+
/**
587+
* Transfers the {@code executor} output to the {@code sink}.
588+
* @return the number of calls to {@code sink}
589+
*/
590+
static int transferExecutorOutput(BufferSink sink, Executor executor)
591+
throws IOException {
592+
try (InputStream in = executor.getOutputStream()) {
593+
byte[] buffer = new byte[8 * 1024];
594+
int iterations = 0;
595+
int len;
596+
while ((len = in.read(buffer)) != -1) {
597+
if (len > 0) {
598+
++iterations;
599+
sink.write(buffer, 0, len);
600+
}
601+
}
602+
return iterations;
603+
}
604+
}
605+
606+
class HistoryRevResult {
607+
public boolean success;
608+
public int iterations;
609+
}
610+
585611
private class RepositoryDateFormat extends DateFormat {
586612
private static final long serialVersionUID = -6951382723884436414L;
587613

0 commit comments

Comments
 (0)