[问题] 如何加快搜寻效率

楼主: FacetheFaith (Yes Man)   2017-09-20 16:11:52
各位前辈好,小弟有一支搜寻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上面的根目录搜寻同样档案一下子就找到了,
所以是算法的问题吗?
程式该怎么改写才能增进搜寻效率?
作者: nanashi07 (NaNashi)   2017-09-20 19:37:00
drive api 可以直接用条件搜寻
作者: jack21 (Jack)   2017-09-20 21:00:00
Drive API Search for Files https://goo.gl/Ya5pF1里面有各参数说明及范例可参考另外可去 https://goo.gl/YPe5Gm在不用写程式下,先测试 API 的各个参数
作者: Jichang (C.C.Lemon)   2017-09-21 12:59:00
递回 ... 看起来很爽跑起来很慢 ...
作者: popcorny (毕业了..@@")   2017-09-21 13:12:00
撇开driver api不谈,recursive做io sensitive的task可以用ForkJoinPool补充: io sensitive的recusive task

Links booklink

Contact Us: admin [ a t ] ucptt.com