Skip to content

Commit 7e5adef

Browse files
author
Vladimir Kotal
authored
fix Indexer nits (#3844)
* fix some nits * check data root instead of null configuration * unwrap comments * add comma * fix wording * refactor project addition * remove error suppression * fix wording
1 parent 750b311 commit 7e5adef

File tree

1 file changed

+54
-55
lines changed
  • opengrok-indexer/src/main/java/org/opengrok/indexer/index

1 file changed

+54
-55
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/index/Indexer.java

Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,13 @@ public final class Indexer {
141141
private static OptionParser optParser = null;
142142
private static boolean verbose = false;
143143

144+
private static final String[] ON_OFF = {ON, OFF};
145+
private static final String[] REMOTE_REPO_CHOICES = {ON, OFF, DIRBASED, UIONLY};
146+
private static final String[] LUCENE_LOCKS = {ON, OFF, "simple", "native"};
147+
private static final String OPENGROK_JAR = "opengrok.jar";
148+
149+
private static final int WEBAPP_CONNECT_TIMEOUT = 1000; // in milliseconds
150+
144151
public static Indexer getInstance() {
145152
return index;
146153
}
@@ -161,12 +168,10 @@ public static void main(String[] argv) {
161168

162169
boolean createDict = false;
163170

164-
int CONNECT_TIMEOUT = 1000; // in milliseconds
165-
166171
try {
167172
argv = parseOptions(argv);
168173

169-
if (webappURI != null && !HostUtil.isReachable(webappURI, CONNECT_TIMEOUT)) {
174+
if (webappURI != null && !HostUtil.isReachable(webappURI, WEBAPP_CONNECT_TIMEOUT)) {
170175
System.err.println(webappURI + " is not reachable.");
171176
System.exit(1);
172177
}
@@ -236,17 +241,16 @@ public static void main(String[] argv) {
236241
canonicalRoots.addAll(cfg.getCanonicalRoots());
237242
cfg.setCanonicalRoots(canonicalRoots);
238243

239-
// Assemble the unprocessed command line arguments (possibly
240-
// a list of paths). This will be used to perform more fine
241-
// grained checking in invalidateRepositories().
244+
// Assemble the unprocessed command line arguments (possibly a list of paths).
245+
// This will be used to perform more fine-grained checking in invalidateRepositories().
242246
for (String arg : argv) {
243247
String path = Paths.get(cfg.getSourceRoot(), arg).toString();
244248
subFilesList.add(path);
245249
}
246250

247-
// If an user used customizations for projects he perhaps just
251+
// If a user used customizations for projects he perhaps just
248252
// used the key value for project without a name but the code
249-
// expects a name for the project. Therefore we fill the name
253+
// expects a name for the project. Therefore, we fill the name
250254
// according to the project key which is the same.
251255
for (Entry<String, Project> entry : cfg.getProjects().entrySet()) {
252256
if (entry.getValue().getName() == null) {
@@ -257,8 +261,8 @@ public static void main(String[] argv) {
257261
// Check version of index(es) versus current Lucene version and exit
258262
// with return code upon failure.
259263
if (checkIndex) {
260-
if (cfg == null) {
261-
System.err.println("Need configuration to check index (use -R)");
264+
if (cfg.getDataRoot() == null || cfg.getDataRoot().isEmpty()) {
265+
System.err.println("Need data root in configuration for index check (use -R)");
262266
System.exit(1);
263267
}
264268

@@ -374,7 +378,7 @@ public static void main(String[] argv) {
374378

375379
writeConfigToFile(env, configFilename);
376380

377-
// Finally ping webapp to refresh indexes in the case of partial reindex
381+
// Finally, ping webapp to refresh indexes in the case of partial reindex
378382
// or send new configuration to the web application in the case of full reindex.
379383
if (webappURI != null) {
380384
if (!subFiles.isEmpty()) {
@@ -413,19 +417,15 @@ public static void main(String[] argv) {
413417
*/
414418
public static String[] parseOptions(String[] argv) throws ParseException {
415419
final String[] usage = {HELP_OPT_1};
416-
final String program = "opengrok.jar";
417-
final String[] ON_OFF = {ON, OFF};
418-
final String[] REMOTE_REPO_CHOICES = {ON, OFF, DIRBASED, UIONLY};
419-
final String[] LUCENE_LOCKS = {ON, OFF, "simple", "native"};
420420

421421
if (argv.length == 0) {
422422
argv = usage; // will force usage output
423423
status = 1; // with non-zero EXIT STATUS
424424
}
425425

426426
/*
427-
* Pre-match any of the --help options so that some possible exception-
428-
* generating args handlers (e.g. -R) can be short-circuited.
427+
* Pre-match any of the --help options so that some possible exception-generating args handlers (e.g. -R)
428+
* can be short-circuited.
429429
*/
430430
boolean preHelp = Arrays.stream(argv).anyMatch(s -> HELP_OPT_1.equals(s) ||
431431
HELP_OPT_2.equals(s) || HELP_OPT_3.equals(s));
@@ -448,8 +448,7 @@ public static String[] parseOptions(String[] argv) throws ParseException {
448448
// Limit usage lines to 72 characters for concise formatting.
449449

450450
optParser = OptionParser.execute(parser -> {
451-
parser.setPrologue(
452-
String.format("\nUsage: java -jar %s [options] [subDir1 [...]]%n", program));
451+
parser.setPrologue(String.format("%nUsage: java -jar %s [options] [subDir1 [...]]%n", OPENGROK_JAR));
453452

454453
parser.on(HELP_OPT_3, HELP_OPT_2, HELP_OPT_1, "=[mode]",
455454
"With no mode specified, display this usage summary. Or specify a mode:",
@@ -813,7 +812,7 @@ public static String[] parseOptions(String[] argv) throws ParseException {
813812

814813
parser.on("--userPage", "=URL",
815814
"Base URL of the user Information provider.",
816-
"Example: \"http://www.myserver.org/viewProfile.jspa?username=\".",
815+
"Example: \"https://www.example.org/viewProfile.jspa?username=\".",
817816
"Use \"none\" to disable link.").execute(v -> cfg.setUserPage((String) v));
818817

819818
parser.on("--userPageSuffix", "=URL-suffix",
@@ -864,7 +863,7 @@ private static void checkConfiguration() {
864863
die("Missing webappURI setting");
865864
}
866865

867-
if (repositories.size() > 0 && !cfg.isHistoryEnabled()) {
866+
if (!repositories.isEmpty() && !cfg.isHistoryEnabled()) {
868867
die("Repositories were specified; history is off however");
869868
}
870869

@@ -965,9 +964,6 @@ public void prepareIndexer(RuntimeEnvironment env,
965964
* generated for repositories (at least for those which support getting
966965
* history per directory).
967966
*
968-
* PMD wants us to use length() &gt; 0 &amp;&amp; charAt(0) instead of startsWith()
969-
* for performance. We prefer clarity over performance here, so silence it.
970-
*
971967
* @param env runtime environment
972968
* @param searchPaths list of paths in which to search for repositories
973969
* @param addProjects if true, add projects
@@ -978,7 +974,6 @@ public void prepareIndexer(RuntimeEnvironment env,
978974
* @throws IndexerException indexer exception
979975
* @throws IOException I/O exception
980976
*/
981-
@SuppressWarnings("PMD.SimplifyStartsWith")
982977
public void prepareIndexer(RuntimeEnvironment env,
983978
Set<String> searchPaths,
984979
boolean addProjects,
@@ -992,39 +987,12 @@ public void prepareIndexer(RuntimeEnvironment env,
992987
}
993988

994989
// Projects need to be created first since when adding repositories below,
995-
// some of the project properties might be needed for that.
990+
// some project properties might be needed for that.
996991
if (addProjects) {
997992
File[] files = env.getSourceRootFile().listFiles();
998993
Map<String, Project> projects = env.getProjects();
999994

1000-
// Keep a copy of the old project list so that we can preserve
1001-
// the customization of existing projects.
1002-
Map<String, Project> oldProjects = new HashMap<>();
1003-
for (Project p : projects.values()) {
1004-
oldProjects.put(p.getName(), p);
1005-
}
1006-
1007-
projects.clear();
1008-
1009-
// Add a project for each top-level directory in source root.
1010-
for (File file : files) {
1011-
String name = file.getName();
1012-
String path = '/' + name;
1013-
if (oldProjects.containsKey(name)) {
1014-
// This is an existing object. Reuse the old project,
1015-
// possibly with customizations, instead of creating a
1016-
// new with default values.
1017-
Project p = oldProjects.get(name);
1018-
p.setPath(path);
1019-
p.setName(name);
1020-
p.completeWithDefaults();
1021-
projects.put(name, p);
1022-
} else if (!name.startsWith(".") && file.isDirectory()) {
1023-
// Found a new directory with no matching project, so
1024-
// create a new project with default properties.
1025-
projects.put(name, new Project(name, path));
1026-
}
1027-
}
995+
addProjects(files, projects);
1028996
}
1029997

1030998
if (!searchPaths.isEmpty()) {
@@ -1053,6 +1021,37 @@ public void prepareIndexer(RuntimeEnvironment env,
10531021
}
10541022
}
10551023

1024+
private void addProjects(File[] files, Map<String, Project> projects) {
1025+
// Keep a copy of the old project list so that we can preserve
1026+
// the customization of existing projects.
1027+
Map<String, Project> oldProjects = new HashMap<>();
1028+
for (Project p : projects.values()) {
1029+
oldProjects.put(p.getName(), p);
1030+
}
1031+
1032+
projects.clear();
1033+
1034+
// Add a project for each top-level directory in source root.
1035+
for (File file : files) {
1036+
String name = file.getName();
1037+
String path = '/' + name;
1038+
if (oldProjects.containsKey(name)) {
1039+
// This is an existing object. Reuse the old project,
1040+
// possibly with customizations, instead of creating a
1041+
// new with default values.
1042+
Project p = oldProjects.get(name);
1043+
p.setPath(path);
1044+
p.setName(name);
1045+
p.completeWithDefaults();
1046+
projects.put(name, p);
1047+
} else if (!name.startsWith(".") && file.isDirectory()) {
1048+
// Found a new directory with no matching project, so
1049+
// create a new project with default properties.
1050+
projects.put(name, new Project(name, path));
1051+
}
1052+
}
1053+
}
1054+
10561055
/**
10571056
* This is the second phase of the indexer which generates Lucene index
10581057
* by passing source code files through ctags, generating xrefs
@@ -1068,9 +1067,9 @@ public void doIndexerExecution(final boolean update, List<String> subFiles,
10681067
IndexChangedListener progress)
10691068
throws IOException {
10701069
Statistics elapsed = new Statistics();
1071-
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
10721070
LOGGER.info("Starting indexing");
10731071

1072+
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
10741073
IndexerParallelizer parallelizer = env.getIndexerParallelizer();
10751074
final CountDownLatch latch;
10761075
if (subFiles == null || subFiles.isEmpty()) {

0 commit comments

Comments
 (0)