java - 如何提高Java中搜索特定Google Drive文件的效率?

标签 java algorithm google-drive-api

我开发了一个 Java 程序来搜索 Google Drive 上特定文件夹中的特定文件。它使用DFS(深度优先搜索)算法进行搜索。

但是搜索效率很差。如果文件位于“稍后”或更深的文件夹中,则程序将花费几分钟的时间来查找它(如果找到),或者有时会超时并响应 HTTP 500 内部服务器错误。

程序如下:

public static void main(String[] args) throws IOException {
    DriveSearch driveSearch = new DriveSearch();
    String searchResult = driveSearch.fetchData("F1b1ZuRUpPLWh6",
            "test.txt");
    System.out.println(searchResult);
}

public String fetchData(String folderID, String searchFileName) {
    String result = "";

               ~~~~~  Skip Codes for Authorization  ~~~~
    // try {
    //     try {
    Drive service = new Drive.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build();
    long startTime = System.currentTimeMillis(); // Begin search time
    //         String pageToken = null;
    //         do {
    System.out.println("=== Begin Search ===");
    File searchResult = recursiveSearch(folderID, searchFileName);
    System.out.println("=== End Search ===");
    long endTime = System.currentTimeMillis(); // End search time
    long totTime = (endTime - startTime) / 1000;
    System.out.println("This search takes " + totTime + " seconds to find the file.");
    if (searchResult != null) {
        result = searchResult.getName();
    }
    //             pageToken = fileList.getNextPageToken();
    //         } while (pageToken != null);
    //     } catch (IOException e) {
    //         result = "invalid_grant";
    //         System.err.println(e.getMessage());
    //     }
    // } catch (Throwable t) {
    //     t.printStackTrace();
    // }
    return result;
}

public File recursiveSearch(String folderID, String searchFileName) throws IOException {
    File searchResult = null;
    FileList fileList = service.files().list().setQ("'" + folderID + "' in parents and trashed = false")
        // .setSpaces("drive")
        .setCorpora("user").setFields("nextPageToken, files(id, name, mimeType)").execute();
    List<File> items = fileList.getFiles();

    for (File file : items) {
        if (file.getName().equals(searchFileName)) {
            searchResult = file;
            System.out.println(file.getName() + " is found!");
            return searchResult;
        } else if (file.getMimeType().equals("application/vnd.google-apps.folder")) {
            System.out.println("Recursive Search");
            System.out.println("file.getId() is " + file.getId());
            searchResult = recursiveSearch(file.getId(), searchFileName);
        } else {
            System.out.println("file name is " + file.getName());
        }
    }
    return searchResult;
}

既然可以在 Google Drive 搜索栏上立即找到特定文件,那么在特定文件夹中也可以立即找到它吗?如果是,我该怎么做才能提高搜索效率?感谢您的任何建议。

最佳答案

成本高昂的部分是每次递归调用中对 Google Drive 的调用。它们是没有必要的。您可以在一次调用中获取所有文件的列表:

/**
 * Read file list from Google Drive.
 * @param service an authenticated <code>Drive</code> object needed to send the request
 * @return Answer the list of files.
 * @throws IOException
 */
protected List<File> readFiles( final Drive service ) throws IOException {
    final List<File> result = new ArrayList<File>();
    final Files.List request = service.files().list();
    do {
        final FileList files = request.execute();
        result.addAll(files.getItems());
        request.setPageToken(files.getNextPageToken());
    } while (request.getPageToken() != null && request.getPageToken().length() > 0);
    return result;
}

然后在文件列表中搜索您的文件。当然,您可以像在代码中一样添加您的请求过滤器,例如过滤掉垃圾文件。

关于java - 如何提高Java中搜索特定Google Drive文件的效率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46317746/

相关文章:

java - 如何在java中将矩阵添加到ArrayList

python - 在 Python 中,为什么压缩元素在添加到列表时会分开?

Android:Google Drive onActivityResult() 获取结果代码 0 (RESULT_CANCELED)

java - 我如何用java编写批处理程序来上传Google Drive中的文件?

python - Google Drive API 上传超时

Java 8 比较来自同一个列表的对象(数组索引越界)

java - 在 hibernate 中创建内部查询

java - 云配置收到请求执行错误。端点=默认端点{ serviceUrl='http ://localhost:8761/} from Eureka server

c++ - 动态(即可变大小)分域树?

algorithm - 我需要比较两个图像以查看不同的颜色。任何中级算法?