Skip to content

Commit

Permalink
Sort CNode.children by token, made Token comparable, mode CNode.token…
Browse files Browse the repository at this point in the history
… final.
  • Loading branch information
hylkevds committed Sep 15, 2021
1 parent 1b1bac7 commit 2f77a27
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 35 deletions.
48 changes: 25 additions & 23 deletions broker/src/main/java/io/moquette/broker/subscriptions/CNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@

import java.util.*;

class CNode {
class CNode implements Comparable<CNode> {

private Token token;
private List<INode> children;
private final Token token;
private final List<INode> children;
Set<Subscription> subscriptions;

CNode() {
CNode(Token token) {
this.children = new ArrayList<>();
this.subscriptions = new HashSet<>();
this.token = token;
}

//Copy constructor
Expand All @@ -39,30 +40,20 @@ public Token getToken() {
return token;
}

public void setToken(Token token) {
this.token = token;
}

boolean anyChildrenMatch(Token token) {
for (INode iNode : children) {
final CNode child = iNode.mainNode();
if (child.equalsToken(token)) {
return true;
}
}
return false;
int idx = Collections.binarySearch(children, token, (Object node, Object token1) -> ((INode) node).mainNode().token.compareTo((Token) token1));
return idx >= 0;
}

List<INode> allChildren() {
return this.children;
return new ArrayList<>(this.children);
}

INode childOf(Token token) {
for (INode iNode : children) {
final CNode child = iNode.mainNode();
if (child.equalsToken(token)) {
return iNode;
}
int idx = Collections.binarySearch(children, token, (Object node, Object token1) -> ((INode) node).mainNode().token.compareTo((Token) token1));
INode child = children.get(idx);
if (child != null) {
return child;
}
throw new IllegalArgumentException("Asked for a token that doesn't exists in any child [" + token + "]");
}
Expand All @@ -81,11 +72,17 @@ CNode copy() {
}

public void add(INode newINode) {
this.children.add(newINode);
int idx = Collections.binarySearch(children, token, (Object node, Object token1) -> ((INode) node).mainNode().token.compareTo((Token) token1));
if (idx < 0) {
children.add(-1 - idx, newINode);
} else {
children.add(idx, newINode);
}
}

public void remove(INode node) {
this.children.remove(node);
int idx = Collections.binarySearch(children, node.mainNode().token, (Object node1, Object token1) -> ((INode) node1).mainNode().token.compareTo((Token) token1));
this.children.remove(idx);
}

CNode addSubscription(Subscription newSubscription) {
Expand Down Expand Up @@ -136,4 +133,9 @@ void removeSubscriptionsFor(String clientId) {
}
this.subscriptions.removeAll(toRemove);
}

@Override
public int compareTo(CNode o) {
return token.compareTo(o.token);
}
}
11 changes: 4 additions & 7 deletions broker/src/main/java/io/moquette/broker/subscriptions/CTrie.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ private enum Action {
INode root;

CTrie() {
final CNode mainNode = new CNode();
mainNode.setToken(ROOT);
final CNode mainNode = new CNode(ROOT);
this.root = new INode(mainNode);
}

Expand Down Expand Up @@ -133,8 +132,7 @@ private INode createPathRec(Topic topic, Subscription newSubscription) {
Topic remainingTopic = topic.exceptHeadToken();
if (!remainingTopic.isEmpty()) {
INode inode = createPathRec(remainingTopic, newSubscription);
CNode cnode = new CNode();
cnode.setToken(topic.headToken());
CNode cnode = new CNode(topic.headToken());
cnode.add(inode);
return new INode(cnode);
} else {
Expand All @@ -143,8 +141,7 @@ private INode createPathRec(Topic topic, Subscription newSubscription) {
}

private INode createLeafNodes(Token token, Subscription newSubscription) {
CNode newLeafCnode = new CNode();
newLeafCnode.setToken(token);
CNode newLeafCnode = new CNode(token);
newLeafCnode.addSubscription(newSubscription);

return new INode(newLeafCnode);
Expand Down Expand Up @@ -176,7 +173,7 @@ private Action remove(String clientId, Topic topic, INode inode, INode iParent)
if (inode == this.root) {
return inode.compareAndSet(cnode, inode.mainNode().copy()) ? Action.OK : Action.REPEAT;
}
TNode tnode = new TNode();
TNode tnode = new TNode(cnode.getToken());
return inode.compareAndSet(cnode, tnode) ? cleanTomb(inode, iParent) : Action.REPEAT;
} else if (cnode.contains(clientId) && topic.isEmpty()) {
CNode updatedCnode = cnode.copy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@

class TNode extends CNode {

@Override
public Token getToken() {
throw new IllegalStateException("Can't be invoked on TNode");
public TNode(Token token) {
super(token);
}

@Override
public void setToken(Token token) {
public Token getToken() {
throw new IllegalStateException("Can't be invoked on TNode");
}

Expand Down
16 changes: 15 additions & 1 deletion broker/src/main/java/io/moquette/broker/subscriptions/Token.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/**
* Internal use only class.
*/
public class Token {
public class Token implements Comparable<Token> {

static final Token EMPTY = new Token("");
static final Token MULTI = new Token("#");
Expand Down Expand Up @@ -72,4 +72,18 @@ public boolean equals(Object obj) {
public String toString() {
return name;
}

@Override
public int compareTo(Token other) {
if (name == null) {
if (other.name == null) {
return 0;
}
return 1;
}
if (other.name == null) {
return -1;
}
return name.compareTo(other.name);
}
}

0 comments on commit 2f77a27

Please sign in to comment.