惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - GDOUJKZZ

.net 8程序如何在信创的arm64架构下运行【nuget包如何判断能不能在arm架构跑呢】 【AI应用】【声纹识别】一、mini io 来构建高可用的文件存储方案 【AI】第五篇 大话说神经网络第一篇 docker 目录下的overlay2层的太多,导致占用大量占用磁盘空间怎么办呢。【docker目录清理】 【AI】第四篇 什么是神经网络 - GDOUJKZZ 【AI】第三篇 RAG是什么 【AI】前置篇 Ai Agent的全貌概览 【AI】第二篇 为什么会有神经网络 【AI】第一篇 语言模型的前世 n-gram的简单介绍 【一】AI赋能 javascript里面的var let const关键字区别和使用 学习心态 从应用开发转到大数据开发 一分钟RequireJS介绍 .NET 6 在小并发下如何生成唯一单据号 垃圾回收机制简单理解 NPOI导出大量数据的避免OOM解决方案【SXSSFWorkbook】 C# 使用NPOI处理Excel模板-【前面部分固定,中间是动态的几行,尾部是固定的部分】 高并发的场景下,如何保证生产者投递到消息中间件的消息不丢失
递归有环问题解决方案
GDOUJKZZ · 2021-11-05 · via 博客园 - GDOUJKZZ

 场景引入

       今天碰到一个问题:当用户使用某个功能的时候,系统就直接挂了,一开始还以为是啥问题,最后发现是递归有环。

      业务场景:有个功能需要获取地区树,刚刚好地区树里面由于人为修改的原因,造成了环。

       A(广东,44)-->B(韶关,4402)-->C(南雄,44) 这种就是有环,会发生堆栈溢出,如果代码不处理,就会由于堆栈溢出,整个服务器直接挂了。

      直接上代码,模拟情况:

             1、建两个类,后面要使用

 public class DictRegion
    {
        public string RegionCode { get; set; }

        public string RegionName { get; set; }

        public string ParentCode { get; set; }

        public string ParentName { get; set; }
    }


    public class TreeNode
    {
        public string RegionCode { get; set;  }
        public string RegionName { get; set; }
        public List<TreeNode> Childs { get; set; }
    }

        2、递归函数的编写

    static void GetTreeNode(List<DictRegion> regions,string parentCode,List<TreeNode> nodes)
    {
            var regionArr=regions.Where(d=>d.ParentCode == parentCode).ToList();
            foreach(var region in regionArr)
            {

                var node = new TreeNode()
                {
                    RegionCode = region.RegionCode,
                    RegionName = region.RegionName,
                    Childs = new List<TreeNode>()
                };
                GetTreeNode(regions,region.RegionCode,node.Childs);
                nodes.Add(node);
            }
        }

       3、准备数据以及调用

 var regions = new List<DictRegion>() {
                    new DictRegion(){RegionCode="ROOT",RegionName="中国",ParentCode="",ParentName=""},
                    new DictRegion() { RegionCode = "44", RegionName = "广东", ParentCode = "ROOT", ParentName = "中国" },
                    new DictRegion() { RegionCode = "43", RegionName = "湖南", ParentCode = "ROOT", ParentName = "中国" },
                    new DictRegion() { RegionCode = "45", RegionName = "广西", ParentCode = "ROOT", ParentName = "中国" },

                    new DictRegion() { RegionCode = "4401", RegionName = "广州", ParentCode = "44", ParentName = "广东" },
                    new DictRegion() { RegionCode = "4402", RegionName = "韶关", ParentCode = "44", ParentName = "广东" },
                    new DictRegion() { RegionCode = "4403", RegionName = "深圳", ParentCode = "44", ParentName = "广东" },

                    new DictRegion() { RegionCode = "4501", RegionName = "南宁", ParentCode = "45", ParentName = "广西" },
                    new DictRegion() { RegionCode = "4502", RegionName = "柳州", ParentCode = "45", ParentName = "广西" },
                    new DictRegion() { RegionCode = "4503", RegionName = "桂林", ParentCode = "45", ParentName = "广西" },

                    new DictRegion() { RegionCode = "4301", RegionName = "长沙", ParentCode = "43", ParentName = "湖南" },
                    new DictRegion() { RegionCode = "4302", RegionName = "株洲", ParentCode = "43", ParentName = "湖南" },
                    new DictRegion() { RegionCode = "4303", RegionName = "湘潭", ParentCode = "43", ParentName = "湖南" },

                    new DictRegion() { RegionCode = "440111", RegionName = "从化", ParentCode = "4401", ParentName = "广州" },
                    new DictRegion() { RegionCode = "440112", RegionName = "天河", ParentCode = "4401", ParentName = "广州" },
                    new DictRegion() { RegionCode = "440113", RegionName = "增城", ParentCode = "4401", ParentName = "广州" },

                    new DictRegion() { RegionCode = "440211", RegionName = "仁化", ParentCode = "4402", ParentName = "韶关" },
                    new DictRegion() { RegionCode = "440212", RegionName = "乐昌", ParentCode = "4402", ParentName = "韶关" },
                    new DictRegion() { RegionCode = "44", RegionName = "南雄", ParentCode = "4402", ParentName = "韶关" },
            };


            //现在要求是,把中国下的一级节点都打展开。
            var treeNodes = new List<TreeNode>();

            GetTreeNode(regions, "ROOT", treeNodes);

        如红色部分,当我们调用获取地区树的方法时,就会发生堆栈溢出。

  解决方案

        1、限制递归调用的深度,简单,但是不好控制这个深度,如果写得太深,很浪费CPU,如果写得太浅,可能会导致需要的业务数据没有查出来。

             定义一个公共的变量

                //#region 限制递归深度

                //         _depth++;
                //        if (_depth >= 10000)
                //        {
                //            throw new Exception("递归超出深度");
                //         }
                //#endregion

       2、判断一下递归是否有环,在进行递归之前,把数据根据RegionCode进行查重处理,发现了有多个相同RegionCode即,报错。

       3、有同事提出,想在框架层面解决这个问题:

             简单的解决方案:建表的时候,对于regionCode这种字段,要不设置为主键,要不设置为唯一索引,可能更多时候,对于这种字典类的数据可以考虑这种方案。

             复杂的解决方案:暂无。。。,希望大家补充和分享