我正在使用迭代方法删除文件夹及其所有子文件。因此,如果文件夹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)