看板 java 關於我們 聯絡資訊
各位前輩好,小弟有一支搜尋Google Drive的程式 因為使用者通常不知道folder id,所以預設的搜尋位置是從根目錄(root)開始搜尋檔案 我採用的是深度優先搜尋法(DFS),也就是搜尋到的檔案如果是資料夾 那麼接著就開始搜尋該資料夾下的檔案,以此類推 如果要搜尋的檔案在很前面 (不清楚一開始搜尋的資料夾是依據什麼), 那麼該檔案就很有可能被找到 反之,就有可能回傳 HTTP 500 Internal Server Error(應該是Time out) 程式碼如下,是使用遞迴搜尋: ... 省略 ... service = new Drive.Builder(httpTransport, JSON_FACTORY, credential) .setApplicationName(APPLICATION_NAME).build(); System.out.println("=== Start to search ==="); long startTime = System.currentTimeMillis(); File searchResult = recursiveSearch(folderID, searchFileName); if (searchResult != null) { result = searchResult.getName(); // 結束時間 long endTime = System.currentTimeMillis(); long totTime = (endTime - startTime) / 1000; // 印出花費時間 System.out.println("花費時間:" + totTime + "秒"); } 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(); System.out.println("files size is " + items.size()); 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()); } if (searchResult != null) { System.out.println("Finish"); break; } } return searchResult; } public static void main(String[] args) throws IOException { DriveSearch driveSearch = new DriveSearch(); String result = driveSearch.fetchData("hfjBV5Z3V2c", "test.txt"); System.out.println(result); } 在Google Drive上面的根目錄搜尋同樣檔案一下子就找到了, 所以是演算法的問題嗎? 程式該怎麼改寫才能增進搜尋效率? -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 59.124.165.65 ※ 文章網址: https://www.ptt.cc/bbs/java/M.1505895115.A.8A3.html ※ 編輯: FacetheFaith (59.124.165.65), 09/20/2017 16:58:17
nanashi07: drive api 可以直接用條件搜尋 09/20 19:37
jack21: Drive API Search for Files https://goo.gl/Ya5pF1 09/20 21:00
jack21: 裡面有各參數說明及範例可參考 09/20 21:01
jack21: 另外可去 https://goo.gl/YPe5Gm 09/20 21:02
jack21: 在不用寫程式下,先測試 API 的各個參數 09/20 21:03
Jichang: 遞迴 ... 看起來很爽跑起來很慢 ... 09/21 12:59
popcorny: 撇開driver api不談,recursive做io sensitive的task 09/21 13:12
popcorny: 可以用ForkJoinPool 09/21 13:13
popcorny: 補充: io sensitive的recusive task 09/21 13:13