Grails 深度优先搜索Vrs递归删除

Grails 深度优先搜索Vrs递归删除,grails,recursion,groovy,depth-first-search,Grails,Recursion,Groovy,Depth First Search,我正在使用迭代方法删除文件夹及其所有子文件夹。因此,如果文件夹A被删除,那么它的所有子文件夹,即b、c、d、e、f也会被删除 我已经做了很多测试,但我不确定是否应该使用递归更有效地完成它?如果我使用它,从长远来看,我的数据库可能相当大,这意味着性能将是未来的一个问题 int thisId = params.int('thisId') def datafile = Datafile.get(thisId) int parentId = datafil

我正在使用迭代方法删除文件夹及其所有子文件夹。因此,如果文件夹A被删除,那么它的所有子文件夹,即b、c、d、e、f也会被删除

我已经做了很多测试,但我不确定是否应该使用递归更有效地完成它?如果我使用它,从长远来看,我的数据库可能相当大,这意味着性能将是未来的一个问题

        int thisId = params.int('thisId')
        def datafile = Datafile.get(thisId)
        int parentId = datafile.parent_id 
        boolean continueIteration = true
        List<Datafile> itemsToBeDeleted = new ArrayList<Datafile>();
        Set<Datafile> folderfileList;
        Set<Datafile> tempfolderfileList = new HashSet<Datafile>();
        Set<Datafile> temp;
        List<Datafile> initialDatafileList = Datafile.findAllByParent_id(thisId)
        itemsToBeDeleted.add(Datafile.findById(thisId))
        for(int i=0; i<initialDatafileList.size(); i++){
            continueIteration = true;
            System.out.println("1st   "+initialDatafileList.get(i).id)
            folderfileList = Datafile.findAllByParent_id(initialDatafileList.get(i).id)
            itemsToBeDeleted.add(Datafile.findById(initialDatafileList.get(i).id))
            while(continueIteration) {
                if(folderfileList.size() >=1){
                    for(Datafile df: folderfileList){
                      // System.out.println("2nd   "+df.id)
                        temp = Datafile.findAllByParent_id(df.id)
                        for(Datafile z: temp ){
                          System.out.println("temp    "+z.id)
            }
                        if(temp.size()>=1){
                            tempfolderfileList.addAll(temp)
                        }
                        temp.clear()
                    }
                }
                else{
                    continueIteration = false
                }
        for(Datafile y: folderfileList ){
//                System.out.println("see if they are here   "+y.id)
            }
                itemsToBeDeleted.addAll(folderfileList)
                folderfileList.clear();
                folderfileList.addAll(tempfolderfileList); //changed from =
                tempfolderfileList.clear();
            }
        }
        for(Datafile y: itemsToBeDeleted ){
                System.out.println("deleted   "+y.id)
            }
        itemsToBeDeleted*.delete(flush:true)
int thisId=params.int('thisId')
def datafile=datafile.get(thisId)
int parentId=datafile.parent\u id
布尔连续化=真
List itemsToBeDeleted=new ArrayList();
设置folderfileList;
Set tempfolderfileList=newhashset();
设定温度;
List initialDatafileList=Datafile.findAllByParent\u id(thisId)
itemsToBeDeleted.add(Datafile.findById(thisId))
对于(int i=0;i=1){
for(数据文件df:folderfileList){
//系统输出打印项次(“第二”+df.id)
temp=Datafile.findAllByParent\u id(df.id)
用于(数据文件z:temp){
系统输出打印项次(“温度”+z.id)
}
如果(温度大小()>=1){
tempfolderfileList.addAll(临时)
}
温度清除()
}
}
否则{
连续性=假
}
用于(数据文件y:folderfileList){
//System.out.println(“查看它们是否在这里”+y.id)
}
itemsToBeDeleted.addAll(folderfileList)
folderfileList.clear();
addAll(tempfolderfileList)//从=
tempfolderfileList.clear();
}
}
对于(数据文件y:itemsToBeDeleted){
系统输出打印项次(“已删除”+y.id)
}
itemsToBeDeleted*.delete(刷新:true)

您当前的方法似乎是在DFS遍历期间收集文件,并在最后一次将其全部删除。从内存消耗的角度来看,在删除每个文件的所有子体之后,立即删除每个文件和每个目录当然会更有效。但是,您发布的代码存在一些格式问题,并且缺少描述每个部分意图的注释。如果不解决这些问题,则很难读取代码,这使得很难说出代码中需要更改哪些内容才能达到预期的结果。您当前的方法似乎是在DFS遍历期间收集文件,并在最后一次将其全部删除。从内存消耗的角度来看,在删除每个文件的所有子体之后,立即删除每个文件和每个目录当然会更有效。但是,您发布的代码存在一些格式问题,并且缺少描述每个部分意图的注释。如果不解决这些问题,就很难阅读您的代码,这使得很难说出您的代码中到底需要更改哪些内容才能达到预期的结果。