简体   繁体   中英

Stopping and later continuing a recursive tree traversal

I'm using a breadth-first depth-first (?) search to map out files and folders. I'm also recording the mapped data, and building a JSON structure that shows the folders and their files/details.

I want to halt the execution at an arbitrary point, saving the JSON data, and then restarting the execution, and continuing from where I left off. Here is my code (This is Google Apps Script, essentially glorified cloud-executed JavaScript).

function MapDrive() {      
  var structure = {};
  try {    
    var parentFolder = DriveApp.getRootFolder();

    getChildFolders(parentFolder, structure, {path: parentFolder.getName()});
    var blob = CreateJSONBlob(structure);
    MailApp.sendEmail('myEmail', 'Folder Tree', 'Drive Map Attached' ,{ attachments: [blob]})
  } catch (e) {
    Logger.log(e.toString());   
  }
}

//Recursive breadth-first folder and file mapping
function TraverseDriveTree(parent, structure, path){
  if(CheckExecutionTime()){
    var childFolders = parent.getFolders();
    var parentName = parent.getName(); 

    //If there is no parent path add it
    if(typeof path[parentName] === 'undefined'){
      path[parentName] = {path: path.path};
    }

    //If parent doe snot exist in structure, then add it and it's files
    if(typeof structure[parentName] === 'undefined'){
      structure[parentName] = {};
      structure[parentName]['files'] = GetFilesInfo(parent.getFiles(), path[parentName].path);
    }  

    while (childFolders.hasNext()) {    
      var childFolder = childFolders.next();
      var childfolderName = childFolder.getName();  
      path[parentName][childfolderName] = {path: path[parentName].path + " > " +childfolderName};

      if(typeof structure[parentName][childfolderName] === 'undefined'){
        structure[parentName][childfolderName] = {};
      }

      structure[parentName][childfolderName]["files"] = GetFilesInfo(childFolder.getFiles(), path[parentName][childfolderName].path);

      // Recursive call for any sub-folders
      TraverseDriveTree(childFolder, structure[parentName][childfolderName], path[parentName]);    
    }     

  } else {
    SaveCurrentState(structure, parent, path)
  }
}

I do not need help with saving the JSON, or anything like that. I need help figuring out how to continue a recursive tree traversal after stopping it, and continuing to add items to the data structure from where I left off.

AFAIK no JavaScript runtime gives you the kind of low-level access to manipulate the call stack that you would need to do what you're asking.

Your best bet is to use your own stack and skip the recursion altogether. It looks like you are doing a pre-order tree traversal , which can be done recursively as you are doing it but can also be done iteratively using a loop and a stack—and it is much easier to save and resume state for a loop than for the execution call stack.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM