Skip to content

Commit ec10766

Browse files
author
Vladimir Kotal
committed
refactor cache creation to avoid instanceOf
1 parent 8fc8ddc commit ec10766

File tree

2 files changed

+34
-26
lines changed

2 files changed

+34
-26
lines changed

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

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,10 @@ protected String getRevisionForAnnotate(String history_revision) {
349349
return history_revision;
350350
}
351351

352+
protected void doCreateCache(HistoryCache cache, String sinceRevision, File directory) throws HistoryException {
353+
finishCreateCache(cache, getHistory(directory, sinceRevision), null);
354+
}
355+
352356
/**
353357
* Create a history log cache for all files in this repository.
354358
* {@code getHistory()} is used to fetch the history for the entire
@@ -380,34 +384,10 @@ final void createCache(HistoryCache cache, String sinceRevision) throws HistoryE
380384

381385
File directory = new File(getDirectoryName());
382386

383-
History history;
384-
if (!(this instanceof RepositoryWithPerPartesHistory)) {
385-
history = getHistory(directory, sinceRevision);
386-
finishCreateCache(cache, history, null);
387-
return;
388-
}
389-
390-
// For repositories that supports this, avoid storing complete History in memory
391-
// (which can be sizeable, at least for the initial indexing, esp. if merge changeset support is enabled),
392-
// by splitting the work into multiple chunks.
393-
RepositoryWithPerPartesHistory repo = (RepositoryWithPerPartesHistory) this;
394-
BoundaryChangesets boundaryChangesets = new BoundaryChangesets(repo);
395-
List<String> boundaryChangesetList = boundaryChangesets.getBoundaryChangesetIDs(sinceRevision);
396-
LOGGER.log(Level.FINE, "boundary changesets: {0}", boundaryChangesetList);
397-
int cnt = 0;
398-
for (String tillRevision: boundaryChangesetList) {
399-
Statistics stat = new Statistics();
400-
LOGGER.log(Level.FINEST, "getting history for ({0}, {1})", new Object[]{sinceRevision, tillRevision});
401-
history = repo.getHistory(directory, sinceRevision, tillRevision);
402-
finishCreateCache(cache, history, tillRevision);
403-
sinceRevision = tillRevision;
404-
405-
stat.report(LOGGER, Level.FINE, String.format("finished chunk %d/%d of history cache for repository ''%s''",
406-
++cnt, boundaryChangesetList.size(), this.getDirectoryName()));
407-
}
387+
doCreateCache(cache, sinceRevision, directory);
408388
}
409389

410-
private void finishCreateCache(HistoryCache cache, History history, String tillRevision) throws HistoryException {
390+
void finishCreateCache(HistoryCache cache, History history, String tillRevision) throws HistoryException {
411391
// We need to refresh list of tags for incremental reindex.
412392
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
413393
if (env.isTagsEnabled() && this.hasFileBasedTags()) {

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,14 @@
2222
*/
2323
package org.opengrok.indexer.history;
2424

25+
import org.opengrok.indexer.logger.LoggerFactory;
26+
import org.opengrok.indexer.util.Statistics;
27+
2528
import java.io.File;
29+
import java.util.List;
2630
import java.util.function.Consumer;
31+
import java.util.logging.Level;
32+
import java.util.logging.Logger;
2733

2834
/**
2935
* Repositories extending this class will benefit from per partes history
@@ -33,6 +39,8 @@ public abstract class RepositoryWithPerPartesHistory extends Repository {
3339
private static final long serialVersionUID = -3433255821312805064L;
3440
public static final int MAX_CHANGESETS = 128;
3541

42+
private static final Logger LOGGER = LoggerFactory.getLogger(RepositoryWithPerPartesHistory.class);
43+
3644
/**
3745
* Just like for {@link Repository#getHistory(File)} it is expected that the lists of (renamed) files
3846
* individual files (i.e. not directory) are empty.
@@ -58,4 +66,24 @@ public int getPerPartesCount() {
5866
* @throws HistoryException on error during history retrieval
5967
*/
6068
public abstract void accept(String sinceRevision, Consumer<String> visitor) throws HistoryException;
69+
70+
@Override
71+
protected void doCreateCache(HistoryCache cache, String sinceRevision, File directory) throws HistoryException {
72+
// For repositories that supports this, avoid storing complete History in memory
73+
// (which can be sizeable, at least for the initial indexing, esp. if merge changeset support is enabled),
74+
// by splitting the work into multiple chunks.
75+
BoundaryChangesets boundaryChangesets = new BoundaryChangesets(this);
76+
List<String> boundaryChangesetList = boundaryChangesets.getBoundaryChangesetIDs(sinceRevision);
77+
LOGGER.log(Level.FINE, "boundary changesets: {0}", boundaryChangesetList);
78+
int cnt = 0;
79+
for (String tillRevision: boundaryChangesetList) {
80+
Statistics stat = new Statistics();
81+
LOGGER.log(Level.FINEST, "getting history for ({0}, {1})", new Object[]{sinceRevision, tillRevision});
82+
finishCreateCache(cache, getHistory(directory, sinceRevision, tillRevision), tillRevision);
83+
sinceRevision = tillRevision;
84+
85+
stat.report(LOGGER, Level.FINE, String.format("finished chunk %d/%d of history cache for repository ''%s''",
86+
++cnt, boundaryChangesetList.size(), this.getDirectoryName()));
87+
}
88+
}
6189
}

0 commit comments

Comments
 (0)