Skip to content

Commit

Permalink
restore Groups operations
Browse files Browse the repository at this point in the history
  • Loading branch information
vladak committed Jul 11, 2023
1 parent 181a64f commit a579c20
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ public Configuration() {
setFetchHistoryWhenNotInCache(true);
setFoldingEnabled(true);
setGenerateHtml(true);
setGroups(new HashMap<>());
setGroups(new TreeMap<>());
setGroupsCollapseThreshold(4);
setHandleHistoryOfRenamedFiles(false);
setHistoryBasedReindex(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
*/
package org.opengrok.indexer.configuration;

Expand All @@ -32,6 +32,7 @@
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.opengrok.indexer.util.Getopt;

Expand Down Expand Up @@ -152,7 +153,7 @@ public static void main(String[] argv) {
usage(System.err);
System.exit(1);
}
matchGroups(System.out, new HashSet<>(cfg.getGroups().values()), match);
matchGroups(System.out, cfg.getGroups(), match);
} else if (empty) {
// just list the groups
if (parent != null || groupname != null || grouppattern != null) {
Expand All @@ -174,15 +175,15 @@ public static void main(String[] argv) {
usage(System.err);
System.exit(1);
}
deleteGroup(new HashSet<>(cfg.getGroups().values()), groupname);
deleteGroup(cfg.getGroups(), groupname);
out = prepareOutput(outFile);
printOut(list, cfg, out);
} else if (groupname != null) {
if (grouppattern == null) {
grouppattern = "";
}
// perform insert/update. parent may be null
if (!modifyGroup(new HashSet<>(cfg.getGroups().values()), groupname, grouppattern, parent)) {
if (!modifyGroup(cfg.getGroups(), groupname, grouppattern, parent)) {
System.err.println("Parent group does not exist \"" + parent + "\"");
} else {
out = prepareOutput(outFile);
Expand All @@ -207,9 +208,8 @@ public static void main(String[] argv) {
* Prints the configuration to the stream.
*
* @param list if true then it lists all available groups in configuration
* if @param out is different than stdout it also prints the current
* if @param out is different from stdout it also prints the current
* configuration to that stream otherwise it prints the configuration to the
* @param out stream.
* @param cfg configuration
* @param out output stream
*/
Expand Down Expand Up @@ -261,7 +261,7 @@ private static void listGroups(PrintStream out, Set<Group> groups) {
* @param groups set of groups
* @param match project name
*/
private static void matchGroups(PrintStream out, Set<Group> groups, String match) {
private static void matchGroups(PrintStream out, Map<String, Group> groups, String match) {
Project p = new Project(match);

List<Group> matched = new ArrayList<>();
Expand All @@ -280,32 +280,32 @@ private static void matchGroups(PrintStream out, Set<Group> groups, String match

/**
* Adds a group into the xml tree.
*
* <p>
* If group already exists, only the pattern is modified. Parent group can
* be null, in that case a new group is inserted as a top level group.
*
* </p>
* @param groups existing groups
* @param groupname new group name
* @param grouppattern new group pattern
* @param groupName new group name
* @param groupPattern new group pattern
* @param parent parent
* @return false if parent group was not found, true otherwise
*/
private static boolean modifyGroup(Set<Group> groups, String groupname, String grouppattern, String parent) {
Group g = new Group(groupname, grouppattern);
private static boolean modifyGroup(Map<String, Group> groups, String groupName, String groupPattern, String parent) {
Group g = new Group(groupName, groupPattern);

if (updateGroup(groups, groupname, grouppattern)) {
if (updateGroup(groups, groupName, groupPattern)) {
return true;
}

if (parent != null) {
if (insertToParent(groups, parent, g)) {
groups.add(g);
groups.put(groupName, g);
return true;
}
return false;
}

groups.add(g);
groups.put(groupName, g);
return true;
}

Expand All @@ -315,12 +315,12 @@ private static boolean modifyGroup(Set<Group> groups, String groupname, String g
* @param groups existing groups
* @param groupname group to remove
*/
private static void deleteGroup(Set<Group> groups, String groupname) {
for (Group g : groups) {
if (g.getName().equals(groupname)) {
groups.remove(g);
groups.removeAll(g.getDescendants());
return;
private static void deleteGroup(Map<String, Group> groups, String groupname) {
Group group = groups.get(groupname);
if (group != null) {
groups.remove(groupname);
for (Group g : group.getDescendants()) {
groups.remove(g.getName());
}
}
}
Expand Down Expand Up @@ -370,16 +370,16 @@ private static boolean treeTraverseGroups(Set<Group> groups, Walker walker) {
* traversed group
* @return true if {@code walker} emits true for any of the groups; false
*/
private static boolean linearTraverseGroups(Set<Group> groups, Walker walker) {
for (Group g : groups) {
private static boolean linearTraverseGroups(Map<String, Group> groups, Walker walker) {
for (Group g : groups.values()) {
if (walker.call(g)) {
return true;
}
}
return false;
}

private static boolean insertToParent(Set<Group> groups, String parent, Group g) {
private static boolean insertToParent(Map<String, Group> groups, String parent, Group g) {
return linearTraverseGroups(groups, x -> {
if (x.getName().equals(parent)) {
x.addGroup(g);
Expand All @@ -394,7 +394,7 @@ private static boolean insertToParent(Set<Group> groups, String parent, Group g)
});
}

private static boolean updateGroup(Set<Group> groups, String groupname, String grouppattern) {
private static boolean updateGroup(Map<String, Group> groups, String groupname, String grouppattern) {
return linearTraverseGroups(groups, g -> {
if (g.getName().equals(groupname)) {
g.setPattern(grouppattern);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
*/
package org.opengrok.indexer.configuration;

Expand All @@ -29,8 +29,7 @@
import java.io.IOException;
import java.io.PrintStream;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand All @@ -51,34 +50,34 @@ public void testBasicConfiguration() {

@Test
public void testDeleteGroup() {
Set<Group> groups = new HashSet<>(cfg.getGroups().values());
Map<String, Group> groups = cfg.getGroups();
invokeMethod("deleteGroup",
new Class<?>[]{Set.class, String.class},
new Class<?>[]{Map.class, String.class},
new Object[]{groups, "random not existing group"});

assertEquals(6, groups.size());

invokeMethod("deleteGroup",
new Class<?>[]{Set.class, String.class},
new Class<?>[]{Map.class, String.class},
new Object[]{groups, "apache"});

assertEquals(5, groups.size());

invokeMethod("deleteGroup",
new Class<?>[]{Set.class, String.class},
new Class<?>[]{Map.class, String.class},
new Object[]{groups, "ctags"});

assertEquals(1, groups.size());
}

@Test
public void testAddGroup() {
Set<Group> groups = new HashSet<>(cfg.getGroups().values());
Map<String, Group> groups = cfg.getGroups();
Group grp = findGroup(groups, "new fantastic group");
assertNull(grp);

invokeMethod("modifyGroup",
new Class<?>[]{Set.class, String.class, String.class, String.class},
new Class<?>[]{Map.class, String.class, String.class, String.class},
new Object[]{groups, "new fantastic group", "some pattern", null});

assertEquals(7, groups.size());
Expand All @@ -91,15 +90,15 @@ public void testAddGroup() {

@Test
public void testAddGroupToParent() {
Set<Group> groups = new HashSet<>(cfg.getGroups().values());
Map<String, Group> groups = cfg.getGroups();
Group grp = findGroup(groups, "apache");
assertNotNull(grp);

grp = findGroup(groups, "new fantastic group");
assertNull(grp);

invokeMethod("modifyGroup",
new Class<?>[]{Set.class, String.class, String.class, String.class},
new Class<?>[]{Map.class, String.class, String.class, String.class},
new Object[]{groups, "new fantastic group", "some pattern", "apache"});

assertEquals(7, groups.size());
Expand All @@ -118,14 +117,14 @@ public void testAddGroupToParent() {

@Test
public void testModifyGroup() {
Set<Group> groups = new HashSet<>(cfg.getGroups().values());
Map<String, Group> groups = cfg.getGroups();
Group grp = findGroup(groups, "apache");
assertNotNull(grp);
assertEquals(grp.getName(), "apache");
assertEquals(grp.getPattern(), "apache-.*");

invokeMethod("modifyGroup",
new Class<?>[]{Set.class, String.class, String.class, String.class},
new Class<?>[]{Map.class, String.class, String.class, String.class},
new Object[]{groups, "apache", "different pattern", null});

grp = findGroup(groups, "apache");
Expand All @@ -146,18 +145,18 @@ public void testMatchGroup() {
{"opengrok-12.0-rc3", 1},
{"opengrk", 0}
};
Set<Group> groups = new HashSet<>(cfg.getGroups().values());
Map<String, Group> groups = cfg.getGroups();

for (Object[] test : tests) {
testSingleMatch(groups, (int) test[1], (String) test[0]);
}
}

private void testSingleMatch(Set<Group> groups, int expectedlines, String match) {
private void testSingleMatch(Map<String, Group> groups, int expectedlines, String match) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
PrintStream out = new PrintStream(os);
invokeMethod("matchGroups",
new Class<?>[]{PrintStream.class, Set.class, String.class},
new Class<?>[]{PrintStream.class, Map.class, String.class},
new Object[]{out, groups, match});

String output = os.toString();
Expand All @@ -177,13 +176,8 @@ private void invokeMethod(String name, Class<?>[] params, Object[] values) {
}
}

private Group findGroup(Set<Group> groups, String needle) {
for (Group g : groups) {
if (g.getName().equals(needle)) {
return g;
}
}
return null;
private Group findGroup(Map<String, Group> groups, String needle) {
return groups.get(needle);
}

static final String BASIC_CONFIGURATION = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
*/
package org.opengrok.web.api.v1.controller;

Expand Down Expand Up @@ -70,7 +70,7 @@ private List<String> listGroups() {

@Test
void emptyGroups() {
env.setGroups(new HashMap<>());
env.setGroups(new TreeMap<>());
assertFalse(env.hasGroups());
List<String> groups = listGroups();
assertNotNull(groups);
Expand Down

0 comments on commit a579c20

Please sign in to comment.