Reputation: 473
I developed a Java program to search particular file in particular folder on Google Drive. It uses DFS(Depth First Search) algorithm to search.
However, the searching efficiency is very bad. If the file is located in the "later" or deeper folder, the program will take several minutes to find it if found, or sometimes it will be time out and respond an HTTP 500 Internal Server Error.
The program is as follows:
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;
}
Since specific file can be found immediately on Google Drive search bar, can it also be immediately found in specific folder? If yes, what should I do to improve the searching efficiency? Thank you for any suggestion.
Upvotes: 2
Views: 192
Reputation: 4705
The expensive part are the calls to Google Drive in each recursive call. They are not necessary. You can fetch the list of all files in one call:
/**
* 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;
}
Afterwards search the list of files for your files. Of course you may add a filter o your request as you did in your code, e.g. to filter out trashed files.
Upvotes: 4