ARTEMIS-3180 - fix multiple path match case in wildcard address map

This commit is contained in:
gtully 2021-03-15 14:30:39 +00:00 committed by Gary Tully
parent 321802f525
commit 052bd60da4
2 changed files with 81 additions and 9 deletions

View File

@ -17,6 +17,7 @@
package org.apache.activemq.artemis.core.postoffice.impl;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@ -137,23 +138,33 @@ public final class AddressPartNode<T> {
}
// look for a path match after 0-N skips among immediate children
AddressPartNode<T> match = null;
ArrayList<AddressPartNode> visitedSet = new ArrayList<>(paths.length);
for (int i = startIndex; i < paths.length; i++) {
match = getChild(paths[i]);
final AddressPartNode<T> match = getChild(paths[i]);
if (match != null) {
match.visitMatchingWildcards(paths, i + 1, collector);
break;
visitedSet.add(match);
}
}
// walk the rest of the sub tree to find a tail path match
for (AddressPartNode<T> child : childNodes.values()) {
// instance equality arranged in node creation
if (child != match && ANY_DESCENDENT != child.getPath()) {
if (alreadyVisited(child, visitedSet)) {
continue;
}
child.visitPathTailMatch(paths, startIndex, collector);
}
}
child.visitPathTailMatch(paths, startIndex, collector);
private boolean alreadyVisited(final AddressPartNode<T> child, final ArrayList<AddressPartNode> matches) {
if (!matches.isEmpty()) {
for (AddressPartNode alreadyMatched : matches) {
if (child == alreadyMatched) {
return true;
}
}
}
return false;
}
// wildcards in the paths, ignore wildcard expansions in the map

View File

@ -638,7 +638,7 @@ public class AddressMapUnitTest {
assertFalse(aABCA.matches(aHashA));
assertFalse(aHashA.matches(aABCA));
assertEquals(0, countMatchingWildcards(abcaS));
assertEquals(1, countMatchingWildcards(abcaS));
assertEquals(0, countMatchingWildcards(new SimpleString("a.b")));
@ -650,7 +650,7 @@ public class AddressMapUnitTest {
SimpleString AHashA = new SimpleString("a.#.a");
underTest.put(AHashA, AHashA);
assertEquals(1, countMatchingWildcards(new SimpleString("a.b.c.a")));
assertEquals(2, countMatchingWildcards(new SimpleString("a.b.c.a")));
assertEquals(0, countNonWildcardMatching(new SimpleString("a.b.c.a")));
@ -771,6 +771,67 @@ public class AddressMapUnitTest {
assertEquals(2, countMatchingWildcards(reqular));
}
@Test
public void testHashAandHashB() throws Exception {
SimpleString hashAhash = new SimpleString("test.#.aaa.#");
underTest.put(hashAhash, hashAhash);
SimpleString hashBhash = new SimpleString("test.#.bbb.#");
underTest.put(hashBhash, hashBhash);
assertEquals(2, countMatchingWildcards(SimpleString.toSimpleString("test.aaa.bbb")));
assertEquals(2, countMatchingWildcards(SimpleString.toSimpleString("test.bbb.aaa")));
assertEquals(2, countMatchingWildcards(SimpleString.toSimpleString("test.bbb.aaa.ccc")));
assertEquals(2, countMatchingWildcards(SimpleString.toSimpleString("test.aaa.bbb.ccc")));
}
@Test
public void testHashNoHashNo() throws Exception {
SimpleString hashAhash = new SimpleString("test.#.0.#.168");
underTest.put(hashAhash, hashAhash);
assertEquals(1, countMatchingWildcards(SimpleString.toSimpleString("test.0.168")));
}
@Test
public void testHashNoHashHashNo() throws Exception {
SimpleString v = new SimpleString("test.#.0.#.168");
underTest.put(v, v);
v = new SimpleString("test.#.0.#");
underTest.put(v, v);
v = new SimpleString("test.0.#");
underTest.put(v, v);
assertEquals(3, countMatchingWildcards(SimpleString.toSimpleString("test.0.168")));
}
@Test
public void testHashNoHashNoWithNMatch() throws Exception {
for (String s : new String[] {"t.#.0.#", "t.#.1.#", "t.#.2.#", "t.#.3.#", "t.#.1.2.3", "t.0.1.2.3"}) {
SimpleString v = new SimpleString(s);
underTest.put(v, v);
}
assertEquals(6, countMatchingWildcards(SimpleString.toSimpleString("t.0.1.2.3")));
}
@Test
public void testSomeMoreHashPlacement() throws Exception {
for (String s : new String[] {"t.#.0.#", "t.0.1.#", "t.0.1.2.#", "t.0.1.#.2.3", "t.*.#.1.2.3"}) {
SimpleString v = new SimpleString(s);
underTest.put(v, v);
}
assertEquals(5, countMatchingWildcards(SimpleString.toSimpleString("t.0.1.2.3")));
assertEquals(3, countMatchingWildcards(SimpleString.toSimpleString("t.0.1.2.3.4")));
}
@Test
public void testManyEntries() throws Exception {
@ -875,7 +936,7 @@ public class AddressMapUnitTest {
underTest.put(new SimpleString("#.a"), new SimpleString("#.a"));
assertEquals(3, countMatchingWildcards(new SimpleString("test.a")));
assertEquals(1, countMatchingWildcards(new SimpleString("test.a.a")));
assertEquals(3, countMatchingWildcards(new SimpleString("test.a.a")));
}
}