Skip to content

Commit

Permalink
fixing bug in treenode
Browse files Browse the repository at this point in the history
  • Loading branch information
ck-c8y committed Oct 9, 2024
1 parent bfad6cd commit b93721e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public List<MappingTreeNode> resolveTopicPath(List<String> remainingLevels) thro
Set<String> set = childNodes.keySet();
String joinedSet = String.join(",", set);
String joinedPath = String.join("", remainingLevels);
log.debug("Tenant {} - Trying to resolve: '{}' in [{}]", tenant, joinedPath, joinedSet);
log.info("Tenant {} - Trying to resolve: '{}' in [{}]", tenant, joinedPath, joinedSet);
List<MappingTreeNode> results = new ArrayList<MappingTreeNode>();
if (remainingLevels.size() >= 1) {
String currentLevel = remainingLevels.get(0);
Expand Down Expand Up @@ -128,17 +128,32 @@ public List<MappingTreeNode> resolveTopicPath(List<String> remainingLevels) thro
results.add(this);
} else {
String remaining = String.join("/", remainingLevels);
throw new ResolveException(
String.format("No mapping registered for this path: %s %s!", this.getAbsolutePath(),
remaining));
String msg = String.format("Sibling path mapping registered for this path: %s %s!",
this.getAbsolutePath(), remaining);
log.info(msg);

// String remaining = String.join("/", remainingLevels);
// throw new ResolveException(
// String.format("No mapping registered for this path: %s %s!",
// this.getAbsolutePath(),
// remaining));
}
// if (childNodes != null && childNodes.size() == 1) {
// results.add(childNodes.get(0).));
// } else {
// String remaining = String.join("/", remainingLevels);
// throw new ResolveException(
// String.format("No mapping registered for this path: %s %s!",
// this.getAbsolutePath(),
// remaining));
// }
}
return results;
}

private void addMapping(Mapping mapping, List<String> levels, int currentLevel)
throws ResolveException {
List<MappingTreeNode> specificChildren = getChildNodes().getOrDefault(levels.get(currentLevel),
List<MappingTreeNode> children = getChildNodes().getOrDefault(levels.get(currentLevel),
new ArrayList<MappingTreeNode>());
String currentPathMonitoring = createPathMonitoring(levels, currentLevel);
if (currentLevel == levels.size() - 1) {
Expand All @@ -149,53 +164,57 @@ private void addMapping(Mapping mapping, List<String> levels, int currentLevel)
log.debug("Tenant {} - Adding mappingNode : currentPathMonitoring {}, child: {}", tenant,
currentPathMonitoring,
child.toString());
specificChildren.add(child);
getChildNodes().put(levels.get(currentLevel), specificChildren);
children.add(child);
getChildNodes().put(levels.get(currentLevel), children);
} else if (currentLevel < levels.size() - 1) {
log.debug(
"Tenant {} - Adding innerNode : currentPathMonitoring: {}, currentNode.absolutePath: {}",
tenant, currentPathMonitoring, getLevel(), getAbsolutePath());
MappingTreeNode child;

// is currentLevel a known children
if (getChildNodes().containsKey(levels.get(currentLevel))) {
// if (specificChildren.size() == 1) {
// if (!specificChildren.get(0).isMappingNode()) {
// child = specificChildren.get(0);
// if (children.size() == 1) {
// if (!children.get(0).isMappingNode()) {
// child = children.get(0);
// } else {
// throw new ResolveException(String.format(
// "Could not add mapping to tree, since at this node is already blocked by
// mappingId : %s",
// specificChildren.get(0).toString()));
// children.get(0).toString()));
// }
// } else {
// throw new ResolveException(String.format(
// "Could not add mapping to tree, multiple mappings are only allowed at the end
// of the tree. This node already contains: %s nodes",
// specificChildren.size()));
// children.size()));
// }

// find the one node that is an inner node, so that we can descend further in
// the tree
List<MappingTreeNode> innerNodes = specificChildren.stream()
List<MappingTreeNode> innerNodes = children.stream()
.filter(node -> !node.isMappingNode())
.collect(Collectors.toList());
if (innerNodes != null && innerNodes.size() > 1) {
throw new ResolveException(String.format(
"multiple inner nodes are registered : %s",
specificChildren.toString()));
children.toString()));
} else if (innerNodes.size() == 1) {
child = innerNodes.get(0);
} else {
child = MappingTreeNode.createInnerNode(this, levels.get(currentLevel));
specificChildren.add(child);
getChildNodes().put(levels.get(currentLevel), specificChildren);
children.add(child);
getChildNodes().put(levels.get(currentLevel), children);
}
// currentLevel is not a known children, is empty and has to be linked to its
// parent
} else {
child = MappingTreeNode.createInnerNode(this, levels.get(currentLevel));
log.debug("Tenant {} - Adding innerNode: currentPathMonitoring: {}, child: {}, {}", tenant,
currentPathMonitoring,
child.toString());
specificChildren.add(child);
getChildNodes().put(levels.get(currentLevel), specificChildren);
children.add(child);
getChildNodes().put(levels.get(currentLevel), children);
}
child.addMapping(mapping, levels, currentLevel + 1);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public void serialize(
jgen.writeStartObject();
jgen.writeNumberField("depthIndex", value.getDepthIndex());
jgen.writeStringField("level", value.getLevel());
jgen.writeBooleanField("isMappingNode", value.isMappingNode());
jgen.writeStringField("parentNode",
(value.getParentNode() != null ? value.getParentNode().getAbsolutePath() : "null"));
jgen.writeStringField("absolutePath", value.getAbsolutePath());
Expand Down

0 comments on commit b93721e

Please sign in to comment.