Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AMQ-9532] Convert DestinationMapNode from recursion to loops. #1254

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void unsynchronizedPut(ActiveMQDestination key, Object value) {
return;
}
String[] paths = key.getDestinationPaths();
getRootNode(key).add(paths, 0, value);
getRootNode(key).add(paths, value);
}


Expand All @@ -123,7 +123,7 @@ public void unsynchronizedRemove(ActiveMQDestination key, Object value) {
return;
}
String[] paths = key.getDestinationPaths();
getRootNode(key).remove(paths, 0, value);
getRootNode(key).remove(paths, value);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.activemq.filter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -73,14 +74,19 @@ public int getChildCount() {
* Returns the child node for the given named path, lazily creating one if
* it does not yet exist
*/
public DestinationMapNode getChildOrCreate(String path) {
DestinationMapNode answer = (DestinationMapNode)childNodes.get(path);
if (answer == null) {
answer = createChildNode();
answer.path = path;
childNodes.put(path, answer);
public DestinationMapNode getChildOrCreate(String[] paths) {
DestinationMapNode node = this;
for (String path : paths) {
DestinationMapNode child = (DestinationMapNode) node.childNodes.get(path);
if (child == null) {
child = node.createChildNode();
child.path = path;
node.childNodes.put(path, child);
}
node = child;
}
return answer;

return node;
}

/**
Expand Down Expand Up @@ -134,32 +140,44 @@ public Set getDesendentValues() {
return answer;
}

public void add(String[] paths, int idx, Object value) {
if (idx >= paths.length) {
values.add(value);
public void add(String[] paths, Object value) {
if (paths.length == 0) {
add(value);
} else {
getChildOrCreate(paths[idx]).add(paths, idx + 1, value);
getChildOrCreate(paths).add(value);
}
}

public void set(String[] paths, int idx, Object value) {
if (idx >= paths.length) {
values.clear();
values.add(value);
private void add(Object value) {
values.add(value);
}

public void set(String[] paths, Object value) {
if (paths.length == 0) {
set(value);
} else {
getChildOrCreate(paths[idx]).set(paths, idx + 1, value);
getChildOrCreate(paths).set(value);
}
}

public void remove(String[] paths, int idx, Object value) {
if (idx >= paths.length) {
values.remove(value);
pruneIfEmpty();
private void set(Object value) {
values.clear();
values.add(value);
}

public void remove(String[] paths, Object value) {
if (paths.length == 0) {
remove(value);
} else {
getChildOrCreate(paths[idx]).remove(paths, ++idx, value);
getChildOrCreate(paths).remove(value);
}
}

private void remove(Object value) {
values.remove(value);
pruneIfEmpty();
}

public void removeAll(Set<DestinationNode> answer, String[] paths, int startIndex) {
DestinationNode node = this;
int size = paths.length;
Expand Down