Skip to content

Commit 4468758

Browse files
authored
groupChangedToMapFromSet (#4306)
fixes #1587
1 parent 0acde30 commit 4468758

File tree

17 files changed

+178
-125
lines changed

17 files changed

+178
-125
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/Configuration.java

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@
4646
import java.util.LinkedList;
4747
import java.util.List;
4848
import java.util.Map;
49+
import java.util.Objects;
4950
import java.util.Set;
50-
import java.util.TreeSet;
51+
import java.util.TreeMap;
5152
import java.util.concurrent.ConcurrentHashMap;
5253
import java.util.logging.Level;
5354
import java.util.logging.Logger;
@@ -134,7 +135,7 @@ public final class Configuration {
134135
private boolean authorizationWatchdogEnabled;
135136
private AuthorizationStack pluginStack;
136137
private Map<String, Project> projects; // project name -> Project
137-
private Set<Group> groups;
138+
private Map<String, Group> groups; // project name -> Group
138139
private String sourceRoot;
139140
private String dataRoot;
140141
/**
@@ -427,9 +428,9 @@ public int getIndexCheckTimeout() {
427428
/**
428429
* Set the index check timeout (performed by the webapp on startup) to a new value.
429430
*
430-
* @see org.opengrok.indexer.index.IndexCheck
431431
* @param timeout the new value
432432
* @throws IllegalArgumentException when the timeout is negative
433+
* @see org.opengrok.indexer.index.IndexCheck
433434
*/
434435
public void setIndexCheckTimeout(int timeout) throws IllegalArgumentException {
435436
if (timeout < 0) {
@@ -569,7 +570,7 @@ public Configuration() {
569570
setFetchHistoryWhenNotInCache(true);
570571
setFoldingEnabled(true);
571572
setGenerateHtml(true);
572-
setGroups(new TreeSet<>());
573+
setGroups(new HashMap<>());
573574
setGroupsCollapseThreshold(4);
574575
setHandleHistoryOfRenamedFiles(false);
575576
setHistoryBasedReindex(true);
@@ -636,6 +637,7 @@ public Map<String, String> getCmds() {
636637
}
637638

638639
/**
640+
* @return int the current message limit
639641
* @see org.opengrok.indexer.web.messages.MessagesContainer
640642
*
641643
* @return int the current message limit
@@ -689,6 +691,7 @@ public void setCmds(Map<String, String> cmds) {
689691

690692
/**
691693
* Gets the configuration's ctags command. Default is null.
694+
*
692695
* @return the configured value
693696
*/
694697
public String getCtags() {
@@ -898,24 +901,25 @@ public void setProjects(Map<String, Project> projects) {
898901
}
899902

900903
/**
901-
* Adds a group to the set. This is performed upon configuration parsing
904+
* Adds a group to the map. This is performed upon configuration parsing
902905
*
903906
* @param group group
904-
* @throws IOException when group is not unique across the set
907+
* @throws IOException when group is not unique across the map
905908
*/
906909
public void addGroup(Group group) throws IOException {
907-
if (!groups.add(group)) {
910+
if (groups.containsKey(group.getName())) {
908911
throw new IOException(
909912
String.format("Duplicate group name '%s' in configuration.",
910913
group.getName()));
911914
}
915+
groups.put(group.getName(), group);
912916
}
913917

914-
public Set<Group> getGroups() {
918+
public Map<String, Group> getGroups() {
915919
return groups;
916920
}
917921

918-
public void setGroups(Set<Group> groups) {
922+
public void setGroups(Map<String, Group> groups) {
919923
this.groups = groups;
920924
}
921925

@@ -1541,24 +1545,30 @@ private static Configuration decodeObject(InputStream in) throws IOException {
15411545
// This ensures that when the configuration is reloaded then the set
15421546
// contains only root groups. Subgroups are discovered again
15431547
// as follows below
1544-
conf.groups.removeIf(g -> g.getParent() != null);
15451548

1549+
List<Group> nonRootGroups = conf.groups.values().stream()
1550+
.filter(g -> Objects.nonNull(g.getParent()))
1551+
.collect(Collectors.toList());
1552+
nonRootGroups.forEach(g -> {
1553+
conf.groups.remove(g.getName());
1554+
}
1555+
);
15461556
// Traversing subgroups and checking for duplicates,
15471557
// effectively transforms the group tree to a structure (Set)
15481558
// supporting an iterator.
1549-
TreeSet<Group> copy = new TreeSet<>();
1550-
LinkedList<Group> stack = new LinkedList<>(conf.groups);
1559+
Map<String, Group> copy = new TreeMap<>();
1560+
LinkedList<Group> stack = new LinkedList<>(conf.groups.values());
15511561
while (!stack.isEmpty()) {
15521562
Group group = stack.pollFirst();
15531563
stack.addAll(group.getSubgroups());
15541564

1555-
if (!copy.add(group)) {
1565+
if (copy.containsKey(group.getName())) {
15561566
throw new IOException(
15571567
String.format("Duplicate group name '%s' in configuration.",
15581568
group.getName()));
15591569
}
1560-
1561-
// populate groups where the current group in in their subtree
1570+
copy.put(group.getName(), group);
1571+
// populate groups where the current group is in their subtree
15621572
Group tmp = group.getParent();
15631573
while (tmp != null) {
15641574
tmp.addDescendant(group);

opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/ConfigurationHelp.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ private static Object getSampleMapValue(Type genType) {
223223
String nm = "user-defined-key";
224224
strmap.put(nm, getSampleProject(nm));
225225
res = strmap;
226+
} else if (actType1 == Group.class) {
227+
Map<String, Group> strmap = new TreeMap<>();
228+
String nm = "user-defined-key";
229+
strmap.put(nm, getSampleGroup(nm));
230+
res = strmap;
226231
} else {
227232
throw new UnsupportedOperationException(
228233
"Not supported yet for " + actType0 + " " + actType1);
@@ -270,6 +275,12 @@ private static Project getSampleProject(String name) {
270275
return p;
271276
}
272277

278+
private static Group getSampleGroup(String name) {
279+
Group grp = new Group(name, name);
280+
grp.setFlag(1);
281+
return grp;
282+
}
283+
273284
private static Object getDefaultValue(Class<?> klass, Method setter,
274285
Configuration cinst) {
275286

opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/Group.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ public static Group getByName(String name) {
295295
Group ret = null;
296296
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
297297
if (env.hasGroups()) {
298-
for (Group grp : env.getGroups()) {
298+
for (Group grp : env.getGroups().values()) {
299299
if (name.equals(grp.getName())) {
300300
ret = grp;
301301
}

opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/Groups.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.nio.charset.StandardCharsets;
3030
import java.text.ParseException;
3131
import java.util.ArrayList;
32+
import java.util.HashSet;
3233
import java.util.LinkedList;
3334
import java.util.List;
3435
import java.util.Set;
@@ -151,7 +152,7 @@ public static void main(String[] argv) {
151152
usage(System.err);
152153
System.exit(1);
153154
}
154-
matchGroups(System.out, cfg.getGroups(), match);
155+
matchGroups(System.out, new HashSet<>(cfg.getGroups().values()), match);
155156
} else if (empty) {
156157
// just list the groups
157158
if (parent != null || groupname != null || grouppattern != null) {
@@ -173,15 +174,15 @@ public static void main(String[] argv) {
173174
usage(System.err);
174175
System.exit(1);
175176
}
176-
deleteGroup(cfg.getGroups(), groupname);
177+
deleteGroup(new HashSet<>(cfg.getGroups().values()), groupname);
177178
out = prepareOutput(outFile);
178179
printOut(list, cfg, out);
179180
} else if (groupname != null) {
180181
if (grouppattern == null) {
181182
grouppattern = "";
182183
}
183184
// perform insert/update. parent may be null
184-
if (!modifyGroup(cfg.getGroups(), groupname, grouppattern, parent)) {
185+
if (!modifyGroup(new HashSet<>(cfg.getGroups().values()), groupname, grouppattern, parent)) {
185186
System.err.println("Parent group does not exist \"" + parent + "\"");
186187
} else {
187188
out = prepareOutput(outFile);
@@ -214,7 +215,7 @@ public static void main(String[] argv) {
214215
*/
215216
private static void printOut(boolean list, Configuration cfg, PrintStream out) {
216217
if (list) {
217-
listGroups(System.out, cfg.getGroups());
218+
listGroups(System.out, new HashSet<>(cfg.getGroups().values()));
218219
if (out != System.out) {
219220
out.print(cfg.getXMLRepresentationAsString());
220221
}

0 commit comments

Comments
 (0)