REQ-2599: cache path for better performance
This commit is contained in:
parent
55588b89a3
commit
dcb5bc0a84
@ -3,7 +3,10 @@ package cn.axzo.msg.center.message.service.group;
|
||||
import cn.axzo.maokai.api.vo.response.tree.NodeValue;
|
||||
import cn.axzo.msg.center.domain.entity.MessageGroupNode;
|
||||
import cn.axzo.msg.center.utils.TreeHelperUtil;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Map;
|
||||
@ -16,6 +19,10 @@ public class NodeWrapper implements NodeValue {
|
||||
|
||||
private final MessageGroupNode node;
|
||||
private final Map<String, Long> code2Id;
|
||||
// cache path for better performance
|
||||
@Setter(AccessLevel.PACKAGE)
|
||||
@Getter(AccessLevel.PACKAGE)
|
||||
private String pathCache;
|
||||
|
||||
@Override
|
||||
public Long id() {
|
||||
|
||||
@ -1,11 +1,9 @@
|
||||
package cn.axzo.msg.center.message.service.group;
|
||||
|
||||
import cn.axzo.maokai.api.util.Ref;
|
||||
import cn.axzo.maokai.api.vo.response.tree.Node;
|
||||
import cn.axzo.maokai.api.vo.response.tree.RootNode;
|
||||
import cn.axzo.maokai.api.vo.response.tree.ValueNode;
|
||||
import cn.axzo.msg.center.common.utils.BizAssertions;
|
||||
import cn.axzo.msg.center.domain.entity.MessageGroupNode;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@ -15,8 +13,8 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static java.util.function.Function.identity;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
import static java.util.stream.Collectors.toMap;
|
||||
|
||||
@ -25,13 +23,14 @@ import static java.util.stream.Collectors.toMap;
|
||||
*/
|
||||
public class RootNodeWrapper {
|
||||
|
||||
private final RootNode<NodeWrapper> root;
|
||||
private final Map<String, ValueNode<NodeWrapper>> code2Node;
|
||||
private final Map<Long, ValueNode<NodeWrapper>> id2Node;
|
||||
|
||||
public RootNodeWrapper(RootNode<NodeWrapper> root) {
|
||||
this.root = root;
|
||||
RootNodeWrapper(RootNode<NodeWrapper> root) {
|
||||
this.code2Node = root.getValueNodes().stream()
|
||||
.collect(toMap(node -> node.getValue().getNodeCode(), Function.identity()));
|
||||
.collect(toMap(node -> node.getValue().getNodeCode(), identity()));
|
||||
this.id2Node = root.getValueNodes().stream()
|
||||
.collect(toMap(node -> node.getValue().unwrap().getId(), identity()));
|
||||
}
|
||||
|
||||
public Optional<ValueNode<NodeWrapper>> findValueNode(String groupNodeCode) {
|
||||
@ -39,18 +38,7 @@ public class RootNodeWrapper {
|
||||
}
|
||||
|
||||
public Optional<ValueNode<NodeWrapper>> findValueNode(Long id) {
|
||||
Ref<ValueNode<NodeWrapper>> ref = Ref.create();
|
||||
unwrap().walkDown(node -> {
|
||||
//noinspection unchecked
|
||||
ValueNode<NodeWrapper> valueNode = node.tryCast(ValueNode.class);
|
||||
if (valueNode == null)
|
||||
return true;
|
||||
MessageGroupNode groupNode = valueNode.getValue().unwrap();
|
||||
if (id.equals(groupNode.getId()))
|
||||
ref.set(valueNode);
|
||||
return ref.isNull();
|
||||
});
|
||||
return Optional.ofNullable(ref.get());
|
||||
return Optional.ofNullable(id2Node.get(id));
|
||||
}
|
||||
|
||||
public List<ValueNode<NodeWrapper>> getNodes(Collection<String> groupNodeCodes) {
|
||||
@ -62,6 +50,13 @@ public class RootNodeWrapper {
|
||||
}
|
||||
|
||||
public String getPath(ValueNode<NodeWrapper> node) {
|
||||
NodeWrapper wrapper = node.getValue();
|
||||
if (wrapper.getPathCache() == null)
|
||||
wrapper.setPathCache(getPathImpl(node));
|
||||
return wrapper.getPathCache();
|
||||
}
|
||||
|
||||
private static String getPathImpl(ValueNode<NodeWrapper> node) {
|
||||
ArrayList<String> paths = new ArrayList<>();
|
||||
Node current = node;
|
||||
int stack = 0;
|
||||
@ -79,7 +74,4 @@ public class RootNodeWrapper {
|
||||
return String.join(":", paths);
|
||||
}
|
||||
|
||||
public RootNode<NodeWrapper> unwrap() {
|
||||
return root;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user