Skip to content

Commit 087b889

Browse files
committed
Fixed topic tree compaction
1 parent d4cf8eb commit 087b889

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

src/main/java/com/hivemq/client/internal/mqtt/handler/publish/incoming/MqttSubscriptionFlowTree.java

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,13 @@ private static class TopicTreeNode {
197197
multiLevelEntries = null;
198198
}
199199
multiLevelSubscriptions--;
200-
compact();
201200
} else {
202201
if (remove(entries, flow)) {
203202
entries = null;
204203
}
205204
subscriptions--;
206-
compact();
207205
}
206+
compact();
208207
return null;
209208
}
210209

@@ -236,13 +235,12 @@ private static boolean remove(
236235
unsubscribe(multiLevelEntries, unsubscribedCallback);
237236
multiLevelEntries = null;
238237
multiLevelSubscriptions = 0;
239-
compact();
240238
} else {
241239
unsubscribe(entries, unsubscribedCallback);
242240
entries = null;
243241
subscriptions = 0;
244-
compact();
245242
}
243+
compact();
246244
return null;
247245
}
248246

@@ -450,30 +448,35 @@ private static void add(
450448
}
451449

452450
private void compact() {
453-
final TopicTreeNode parent = this.parent;
454-
if (isEmpty() && (parent != null)) {
455-
parent.removeNext(this);
456-
if ((parent.multiLevelSubscriptions + parent.subscriptions) == 0) {
457-
if ((parent.singleLevel != null) && (parent.next == null)) {
458-
parent.fuse(parent.singleLevel);
459-
} else if ((parent.singleLevel == null) && (parent.next != null) && (parent.next.size() == 1)) {
460-
parent.fuse(parent.next.values().iterator().next());
461-
}
451+
if ((parent != null) && ((subscriptions + multiLevelSubscriptions) == 0)) {
452+
final boolean hasSingleLevel = singleLevel != null;
453+
final boolean hasNext = next != null;
454+
if (!hasSingleLevel && !hasNext) {
455+
parent.removeNext(this);
456+
parent.compact();
457+
} else if (hasSingleLevel && !hasNext) {
458+
fuse(singleLevel);
459+
} else if (!hasSingleLevel && next.size() == 1) {
460+
fuse(next.values().iterator().next());
462461
}
463462
}
464463
}
465464

466465
private void fuse(final @NotNull TopicTreeNode child) {
466+
assert parent != null;
467+
assert topicLevel != null;
467468
assert child.parent == this;
468469
assert child.topicLevel != null;
469-
if (topicLevel != null) {
470-
topicLevel = MqttTopicLevels.concat(topicLevel, child.topicLevel);
471-
next = child.next;
472-
singleLevel = child.singleLevel;
473-
entries = child.entries;
474-
multiLevelEntries = child.multiLevelEntries;
475-
subscriptions = child.subscriptions;
476-
multiLevelSubscriptions = child.multiLevelSubscriptions;
470+
final TopicTreeNode parent = this.parent;
471+
final MqttTopicLevels fusedTopicLevel = MqttTopicLevels.concat(topicLevel, child.topicLevel);
472+
child.parent = parent;
473+
child.topicLevel = fusedTopicLevel;
474+
if (fusedTopicLevel.isSingleLevelWildcard()) {
475+
parent.singleLevel = child;
476+
} else {
477+
assert parent.next != null;
478+
parent.next.remove(topicLevel);
479+
parent.next.put(fusedTopicLevel, child);
477480
}
478481
}
479482

@@ -491,7 +494,7 @@ private void removeNext(final @NotNull TopicTreeNode node) {
491494
}
492495

493496
boolean isEmpty() {
494-
return (subscriptions == 0) && (multiLevelSubscriptions == 0) && (singleLevel == null) && (next == null);
497+
return ((subscriptions + multiLevelSubscriptions) == 0) && (singleLevel == null) && (next == null);
495498
}
496499
}
497500
}

0 commit comments

Comments
 (0)