performance optimization

This commit is contained in:
Zeapo 2014-07-27 22:40:15 +01:00
parent 3f5e93e5c3
commit f738c7ee36

View file

@ -11,11 +11,15 @@ import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
public class PasswordRepository { public class PasswordRepository {
private static Repository repository; private static Repository repository;
private static LinkedHashMap<String, ArrayList<String>> mainPasswordMap;
private static ArrayList<String> mainListOfFiles;
protected PasswordRepository(){ } protected PasswordRepository(){ }
@ -41,15 +45,50 @@ public class PasswordRepository {
} }
public static ArrayList<String> getFilesList(){ public static ArrayList<String> getFilesList(){
return getFilesList(repository.getWorkTree()); // Avoid multiple queries
if (null == mainListOfFiles) {
mainListOfFiles = getFilesList(repository.getWorkTree());
}
return mainListOfFiles;
}
public static LinkedHashMap<String, ArrayList<String>> getPasswords() {
// avoid multiple queries
if (null == mainPasswordMap) {
mainPasswordMap = getPasswords(repository.getWorkTree());
}
return mainPasswordMap;
} }
public static ArrayList<String> getFilesList(File path){ public static ArrayList<String> getFilesList(File path){
List<File> files = (List<File>) FileUtils.listFiles(path, new String[] {"gpg"}, true); List<File> files = (List<File>) FileUtils.listFiles(path, new String[] {"gpg"}, true);
ArrayList<String> filePaths = new ArrayList<String>(); ArrayList<String> filePaths = new ArrayList<String>();
for (File file : files) { for (File file : files) {
filePaths.add(file.getAbsolutePath().replace(repository.getWorkTree().getAbsolutePath(), "")); filePaths.add(file.getAbsolutePath().replace(repository.getWorkTree().getAbsolutePath() + "/", ""));
} }
return filePaths; return filePaths;
} }
public static LinkedHashMap<String, ArrayList<String>> getPasswords(File path) {
//We need to recover the passwords then parse the files
ArrayList<String> passList = getFilesList(path);
LinkedHashMap<String, ArrayList<String>> passMap = new LinkedHashMap<String, ArrayList<String>>();
passMap.put("Without Category", new ArrayList<String>());
for (String file : passList) {
String[] parts = file.split("/");
if (parts.length == 1) {
passMap.get("Without Category").add(parts[0]);
} else {
if (passMap.containsKey(parts[0])) {
passMap.get(parts[0]).add(parts[1]);
} else {
ArrayList<String> tempList = new ArrayList<String>();
tempList.add(parts[1]);
passMap.put(parts[0], tempList);
}
}
}
return passMap;
}
} }