Skip to content

Commit cfb0b5c

Browse files
vladakVladimir Kotal
authored andcommitted
convert Project List to HashMap
1 parent 505b161 commit cfb0b5c

18 files changed

+140
-156
lines changed

src/org/opensolaris/opengrok/configuration/Configuration.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public final class Configuration {
116116
*/
117117
private boolean authorizationWatchdogEnabled;
118118
private AuthorizationStack pluginStack;
119-
private List<Project> projects;
119+
private Map<String,Project> projects; // project name -> Project
120120
private Set<Group> groups;
121121
private String sourceRoot;
122122
private String dataRoot;
@@ -125,8 +125,8 @@ public final class Configuration {
125125
private boolean generateHtml;
126126
/**
127127
* Default projects will be used, when no project is selected and no project
128-
* is in cookie, so basically only the first time you open the first page,
129-
* or when you clear your web cookies
128+
* is in cookie, so basically only the first time a page is opened,
129+
* or when web cookies are cleared.
130130
*/
131131
private Set<Project> defaultProjects;
132132
/**
@@ -374,7 +374,7 @@ public Configuration() {
374374
setPluginDirectory(null);
375375
setPluginStack(new AuthorizationStack(AuthControlFlag.REQUIRED, "default stack"));
376376
setPrintProgress(false);
377-
setProjects(new ArrayList<>());
377+
setProjects(new HashMap<>());
378378
setQuickContextScan(true);
379379
//below can cause an outofmemory error, since it is defaulting to NO LIMIT
380380
setRamBufferSize(defaultRamBufferSize); //MB
@@ -572,11 +572,11 @@ public void setHandleHistoryOfRenamedFiles(boolean enable) {
572572
this.handleHistoryOfRenamedFiles = enable;
573573
}
574574

575-
public List<Project> getProjects() {
575+
public Map<String,Project> getProjects() {
576576
return projects;
577577
}
578578

579-
public void setProjects(List<Project> projects) {
579+
public void setProjects(Map<String,Project> projects) {
580580
this.projects = projects;
581581
}
582582

src/org/opensolaris/opengrok/configuration/Groups.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,10 @@ public boolean call(Group g) {
254254
*
255255
* @param out stream to write the results
256256
* @param groups set of groups
257-
* @param match project description
257+
* @param match project name
258258
*/
259259
private static void matchGroups(PrintStream out, Set<Group> groups, String match) {
260-
Project p = new Project();
261-
p.setName(match);
260+
Project p = new Project(match);
262261

263262
List<Group> matched = new ArrayList<>();
264263
linearTraverseGroups(groups, new Walker() {

src/org/opensolaris/opengrok/configuration/Project.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@ public class Project implements Comparable<Project>, Nameable {
5353
*/
5454
private Set<Group> groups = new TreeSet<>();
5555

56+
// This empty constructor is needed for serialization.
57+
public Project () {
58+
}
59+
60+
public Project (String name) {
61+
this.name = name;
62+
}
63+
64+
public Project (String name, String path) {
65+
this.name = name;
66+
this.path = path;
67+
}
68+
5669
/**
5770
* Get a textual name of this project
5871
*
@@ -95,6 +108,9 @@ public int getTabSize() {
95108
* Set a textual name of this project, prefferably don't use " , " in the
96109
* name, since it's used as delimiter for more projects
97110
*
111+
* XXX we should not allow setting project name after it has been constructed
112+
* because it is probably part of HashMap.
113+
*
98114
* @param name a textual name of the project
99115
*/
100116
@Override
@@ -171,7 +187,7 @@ public static Project getProject(String path) {
171187
final RuntimeEnvironment env = RuntimeEnvironment.getInstance();
172188
if (env.hasProjects()) {
173189
final String lpath = path.replace(File.separatorChar, '/');
174-
for (Project p : env.getProjects()) {
190+
for (Project p : env.getProjectList()) {
175191
String pp = p.getPath();
176192
// Check if the project's path is a prefix of the given
177193
// path. It has to be an exact match, or the project's path
@@ -217,10 +233,9 @@ public static Project getProject(File file) {
217233
public static Project getByName(String name) {
218234
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
219235
if (env.hasProjects()) {
220-
for (Project proj : env.getProjects()) {
221-
if (name.equals(proj.getName())) {
236+
Project proj;
237+
if ((proj = env.getProjects().get(name)) != null) {
222238
return (proj);
223-
}
224239
}
225240
}
226241
return null;

src/org/opensolaris/opengrok/configuration/RuntimeEnvironment.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -395,21 +395,29 @@ public String getPathRelativeToSourceRoot(File file, int stripCount) throws IOEx
395395
}
396396

397397
/**
398-
* Do we have projects?
398+
* Do we have any projects ?
399399
*
400400
* @return true if we have projects
401401
*/
402402
public boolean hasProjects() {
403-
List<Project> proj = getProjects();
404-
return (proj != null && !proj.isEmpty());
403+
return (getProjects().size() > 0);
405404
}
406405

407406
/**
408-
* Get all of the projects
407+
* Get list of projects.
409408
*
410-
* @return a list containing all of the projects (may be null)
409+
* @return a list containing all of the projects
411410
*/
412-
public List<Project> getProjects() {
411+
public List<Project> getProjectList() {
412+
return new ArrayList<Project>(threadConfig.get().getProjects().values());
413+
}
414+
415+
/**
416+
* Get project map.
417+
*
418+
* @return a Map with all of the projects
419+
*/
420+
public Map<String,Project> getProjects() {
413421
return threadConfig.get().getProjects();
414422
}
415423

@@ -419,17 +427,19 @@ public List<Project> getProjects() {
419427
* @return a list containing descriptions of all projects.
420428
*/
421429
public List<String> getProjectDescriptions() {
422-
return threadConfig.get().getProjects().stream().
430+
return getProjectList().stream().
423431
map(Project::getName).collect(Collectors.toList());
424432
}
425433

426434
/**
427435
* Set the list of the projects
428436
*
429-
* @param projects the list of projects to use
437+
* @param projects the map of projects to use
430438
*/
431-
public void setProjects(List<Project> projects) {
432-
populateGroups(getGroups(), projects);
439+
public void setProjects(Map<String,Project> projects) {
440+
if (projects != null) {
441+
populateGroups(getGroups(), new TreeSet<Project>(projects.values()));
442+
}
433443
threadConfig.get().setProjects(projects);
434444
}
435445

@@ -457,7 +467,7 @@ public Set<Group> getGroups() {
457467
* @param groups the set of groups to use
458468
*/
459469
public void setGroups(Set<Group> groups) {
460-
populateGroups(groups, getProjects());
470+
populateGroups(groups, new TreeSet<Project>(getProjects().values()));
461471
threadConfig.get().setGroups(groups);
462472
}
463473

@@ -1213,7 +1223,7 @@ private void generateProjectRepositoriesMap() throws IOException {
12131223
/**
12141224
* Classifies projects and puts them in their groups.
12151225
*/
1216-
private void populateGroups(Set<Group> groups, List<Project> projects) {
1226+
private void populateGroups(Set<Group> groups, Set<Project> projects) {
12171227
if (projects == null || groups == null) {
12181228
return;
12191229
}
@@ -1259,7 +1269,7 @@ public void setConfiguration(Configuration configuration, List<String> subFileLi
12591269
} catch (IOException ex) {
12601270
LOGGER.log(Level.SEVERE, "Cannot generate project - repository map", ex);
12611271
}
1262-
populateGroups(getGroups(), getProjects());
1272+
populateGroups(getGroups(), new TreeSet<Project>(getProjects().values()));
12631273
if (subFileList != null) {
12641274
HistoryGuru.getInstance().invalidateRepositories(
12651275
configuration.getRepositories(), subFileList);

src/org/opensolaris/opengrok/index/IndexDatabase.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opensolaris.opengrok.index;
2424

@@ -164,7 +164,7 @@ static void updateAll(ExecutorService executor, IndexChangedListener listener) t
164164
List<IndexDatabase> dbs = new ArrayList<>();
165165

166166
if (env.hasProjects()) {
167-
for (Project project : env.getProjects()) {
167+
for (Project project : env.getProjectList()) {
168168
dbs.add(new IndexDatabase(project));
169169
}
170170
} else {
@@ -465,7 +465,7 @@ static void optimizeAll(ExecutorService executor) throws IOException {
465465
List<IndexDatabase> dbs = new ArrayList<>();
466466
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
467467
if (env.hasProjects()) {
468-
for (Project project : env.getProjects()) {
468+
for (Project project : env.getProjectList()) {
469469
dbs.add(new IndexDatabase(project));
470470
}
471471
} else {
@@ -949,7 +949,7 @@ public static void listAllFiles(List<String> subFiles) throws IOException {
949949
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
950950
if (env.hasProjects()) {
951951
if (subFiles == null || subFiles.isEmpty()) {
952-
for (Project project : env.getProjects()) {
952+
for (Project project : env.getProjectList()) {
953953
IndexDatabase db = new IndexDatabase(project);
954954
db.listFiles();
955955
}
@@ -1015,9 +1015,9 @@ static void listFrequentTokens(List<String> subFiles) throws IOException {
10151015
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
10161016
if (env.hasProjects()) {
10171017
if (subFiles == null || subFiles.isEmpty()) {
1018-
for (Project project : env.getProjects()) {
1018+
for (Project project : env.getProjectList()) {
10191019
IndexDatabase db = new IndexDatabase(project);
1020-
db.listTokens(4);
1020+
db.listTokens(limit);
10211021
}
10221022
} else {
10231023
for (String path : subFiles) {
@@ -1026,7 +1026,7 @@ static void listFrequentTokens(List<String> subFiles) throws IOException {
10261026
LOGGER.log(Level.WARNING, "Could not find a project for \"{0}\"", path);
10271027
} else {
10281028
IndexDatabase db = new IndexDatabase(project);
1029-
db.listTokens(4);
1029+
db.listTokens(limit);
10301030
}
10311031
}
10321032
}

src/org/opensolaris/opengrok/index/Indexer.java

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -696,13 +696,13 @@ public void prepareIndexer(RuntimeEnvironment env,
696696

697697
if (addProjects) {
698698
File files[] = env.getSourceRootFile().listFiles();
699-
List<Project> projects = env.getProjects();
699+
Map<String,Project> projects = env.getProjects();
700700

701701
// Keep a copy of the old project list so that we can preserve
702702
// the customization of existing projects.
703703
Map<String, Project> oldProjects = new HashMap<>();
704-
for (Project p : projects) {
705-
oldProjects.put(p.getPath(), p);
704+
for (Project p : projects.values()) {
705+
oldProjects.put(p.getName(), p);
706706
}
707707

708708
projects.clear();
@@ -711,48 +711,29 @@ public void prepareIndexer(RuntimeEnvironment env,
711711
for (File file : files) {
712712
String name = file.getName();
713713
String path = "/" + name;
714-
if (oldProjects.containsKey(path)) {
714+
if (oldProjects.containsKey(name)) {
715715
// This is an existing object. Reuse the old project,
716716
// possibly with customizations, instead of creating a
717717
// new with default values.
718-
projects.add(oldProjects.get(path));
718+
projects.put(name, oldProjects.get(name));
719719
} else if (!name.startsWith(".") && file.isDirectory()) {
720720
// Found a new directory with no matching project, so
721721
// create a new project with default properties.
722-
Project p = new Project();
723-
p.setName(name);
724-
p.setPath(path);
722+
Project p = new Project(name, path);
725723
p.setTabSize(env.getConfiguration().getTabSize());
726-
projects.add(p);
724+
projects.put(p.getName(), p);
727725
}
728726
}
729-
730-
// The projects should be sorted...
731-
Collections.sort(projects, new Comparator<Project>() {
732-
@Override
733-
public int compare(Project p1, Project p2) {
734-
String s1 = p1.getName();
735-
String s2 = p2.getName();
736-
737-
int ret;
738-
if (s1 == null) {
739-
ret = (s2 == null) ? 0 : 1;
740-
} else {
741-
ret = s1.compareTo(s2);
742-
}
743-
return ret;
744-
}
745-
});
746727
}
747728

748729
if (defaultProjects != null && !defaultProjects.isEmpty()) {
749730
Set<Project> projects = new TreeSet<>();
750731
for (String projectPath : defaultProjects) {
751732
if (projectPath.equals("__all__")) {
752-
projects.addAll(env.getProjects());
733+
projects.addAll(env.getProjects().values());
753734
break;
754735
}
755-
for (Project p : env.getProjects()) {
736+
for (Project p : env.getProjectList()) {
756737
if (p.getPath().equals(projectPath)) {
757738
projects.add(p);
758739
break;

src/org/opensolaris/opengrok/search/SearchEngine.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ private void searchMultiDatabase(List<Project> root, boolean paging) throws IOEx
214214
for (Project p : root) {
215215
projects.add(p.getName());
216216
}
217+
217218
// We use MultiReader even for single project. This should
218219
// not matter given that MultiReader is just a cheap wrapper
219220
// around set of IndexReader objects.
@@ -261,7 +262,7 @@ public int search(HttpServletRequest req, String... projectNames) {
261262
List<Project> filteredProjects = new ArrayList<Project>();
262263
for(Project project: projects) {
263264
for (String name : projectNames) {
264-
if(project.getName().equalsIgnoreCase(name)) {
265+
if (project.getName().equalsIgnoreCase(name)) {
265266
filteredProjects.add(project);
266267
}
267268
}
@@ -310,7 +311,7 @@ public int search(HttpServletRequest req) {
310311
public int search() {
311312
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
312313
return search(
313-
env.hasProjects() ? env.getProjects() : new ArrayList<Project>(),
314+
env.hasProjects() ? env.getProjectList() : new ArrayList<Project>(),
314315
new File(env.getDataRootFile(), IndexDatabase.INDEX_DIR));
315316
}
316317

0 commit comments

Comments
 (0)