Java:如何通过从mongodb(远程服务器)读取数据来加速JTree重建?

发布于 2024-11-28 09:56:26 字数 1502 浏览 1 评论 0原文

基本上 mongodb 存储每个节点用户对象(NodePro)和parentId。

此类通过查询给定parentId 的所有子级递归地构建Jtree。

问题是,在处理一棵树的数百或数千个节点时,我必须等待长达 5 分钟才能加载整个树。

有没有办法显着加快速度?目前,即使处理不到一百个节点,树也需要很长时间才能完成。

public class BuildTree {

    public BuildTree(DefaultMutableTreeNode treeNode){
        DefaultMutableTreeNode aParentNode = treeNode;
        try {
            processChildren(aParentNode);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void processChildren(DefaultMutableTreeNode parentNode) throws Exception{
        NodePro np = (NodePro) parentNode.getUserObject();      

        List<NodePro> nodelist = NodeDAO.getInstance().getChildrenOfParent(np.getId().toString());

        if (nodelist.isEmpty()){    
            System.out.println("empty");
            return;
        }else{

             for (int i=0; i< nodelist.size(); i++)
              {            

                 NodePro childnode = nodelist.get(i);
                 DefaultMutableTreeNode child = new DefaultMutableTreeNode(childnode);

                 TreeModel.getInstance().insertNodeInto(child,parentNode,TreeModel.getInstance().getChildCount(parentNode));

                 DefaultMutableTreeNode parent = (DefaultMutableTreeNode) 
                    TreeModel.getInstance().getChild(parentNode, i);

                 processChildren(parent);
              }

            return;
        }   
    }

basically mongodb stores each node userobject(NodePro) with parentId.

This class recursively builds a Jtree from querying all the children of a given parentId.

The problem is I have to wait up to 5 minutes for the entire tree to load when dealing with hundreds or thousands of nodes of a single tree.

Is there a way to speed this up significantly? currently even dealing with under hundred nodes, it will take a long time for the tree to become complete.

public class BuildTree {

    public BuildTree(DefaultMutableTreeNode treeNode){
        DefaultMutableTreeNode aParentNode = treeNode;
        try {
            processChildren(aParentNode);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void processChildren(DefaultMutableTreeNode parentNode) throws Exception{
        NodePro np = (NodePro) parentNode.getUserObject();      

        List<NodePro> nodelist = NodeDAO.getInstance().getChildrenOfParent(np.getId().toString());

        if (nodelist.isEmpty()){    
            System.out.println("empty");
            return;
        }else{

             for (int i=0; i< nodelist.size(); i++)
              {            

                 NodePro childnode = nodelist.get(i);
                 DefaultMutableTreeNode child = new DefaultMutableTreeNode(childnode);

                 TreeModel.getInstance().insertNodeInto(child,parentNode,TreeModel.getInstance().getChildCount(parentNode));

                 DefaultMutableTreeNode parent = (DefaultMutableTreeNode) 
                    TreeModel.getInstance().getChild(parentNode, i);

                 processChildren(parent);
              }

            return;
        }   
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅暮の光 2024-12-05 09:56:26

@trashgod 的评论是正确的:代码把时间花在哪里了?是在对象创建中吗?当您需要创建数千个节点时,DefaultMutableTreeNode 的性能不佳。是在从MongoDB检索节点信息的过程中吗?

如果您无法加快树的创建速度,您将需要研究延迟加载节点的子节点。也就是说,在用户请求查看子节点之前不要加载节点。

其他可能性是在后台线程中加载子节点。

但是,与任何性能改进工作一样,您需要测量代码的哪一部分速度较慢,然后找出改进方法。

@trashgod is right in his comment: where is the code spending its time? Is it in the object creation? DefaultMutableTreeNode doesn't perform well when you've got thousands of node to create. Is it during retrieval of the node information from MongoDB?

If you can't speed up the creation of the tree, you'll want to investigate lazily-loading the children of a node. That is, don't load the node until the user requests to see the children.

Other possibilities are to load the children nodes in a background thread.

However, like any performance improvement work, you need to measure which part of your code is slow and then figure out how to improve it.

若有似无的小暗淡 2024-12-05 09:56:26

这将是评论,但

删除主题中的 MongoDb 引擎,这与您的问题(以及您的帖子数量)无关,

存在三个区域

1/ 您不熟悉(摘要/默认) ) TreeModel 正确且正确的方法,创建 JTree 及其 Model 一次,而不是像您显示的那样,然后严格分离 Swing JComponets和业务逻辑(加载/更改/管理/保存/使用 Swing JComponents 的数据进行任何操作),您可以为任何操作

2/ 问题创建新的 void() < a href="http://download.oracle.com/javase/tutorial/uiswing/concurrency/index.html" rel="nofollow noreferrer">Swing 中的并发,因为从/到的任何内容数据库基本上是 BackGroung 任务,任何内容都必须包装到 invokeLater() 中,并且有 自定义 Java 外观和感觉,然后进入 invokeAndWait()

3 / 扩展为 @trashgod 和 @StanislavL 正确建议仔细阅读此 关于未封闭的 JDBC 对象的帖子

that would be comment but

remove enless MongoDb engine in your topics, that nothing to do with your issue (and for number of your posts)

there are three areas

1/ you are not familair with (Abstract/Default) TreeModel correct and proper way, create JTree and its Model once, not as you displayed, then strictly separeted Swing JComponets and Bussines Logics (load/change/manage/save/whatever with data for Swing JComponents), your create new void() for any of your action

2/ issues with Concurency in Swing, bacause anything from/to database is basically BackGroung Task, anything must be wrappef into invokeLater() and is is there Custom Java Look and Feel then into invokeAndWait()

3/ expands as @trashgod and @StanislavL correctly suggested carefully reads this post about unclosed JDBC Objects

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文