From f738c7ee36c0798a96b39aaf57aabf1f1843d60f Mon Sep 17 00:00:00 2001 From: Zeapo Date: Sun, 27 Jul 2014 22:40:15 +0100 Subject: performance optimization --- .../com/zeapo/pwdstore/PasswordRepository.java | 43 +++++++++++++++++++++- 1 file changed, 41 insertions(+), 2 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordRepository.java b/app/src/main/java/com/zeapo/pwdstore/PasswordRepository.java index 5861d229..75e65144 100644 --- a/app/src/main/java/com/zeapo/pwdstore/PasswordRepository.java +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordRepository.java @@ -11,11 +11,15 @@ import org.eclipse.jgit.storage.file.FileRepositoryBuilder; import java.io.File; import java.io.IOException; import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; public class PasswordRepository { private static Repository repository; + private static LinkedHashMap> mainPasswordMap; + private static ArrayList mainListOfFiles; protected PasswordRepository(){ } @@ -41,15 +45,50 @@ public class PasswordRepository { } public static ArrayList getFilesList(){ - return getFilesList(repository.getWorkTree()); + // Avoid multiple queries + if (null == mainListOfFiles) { + mainListOfFiles = getFilesList(repository.getWorkTree()); + } + return mainListOfFiles; + } + + public static LinkedHashMap> getPasswords() { + // avoid multiple queries + if (null == mainPasswordMap) { + mainPasswordMap = getPasswords(repository.getWorkTree()); + } + return mainPasswordMap; } public static ArrayList getFilesList(File path){ List files = (List) FileUtils.listFiles(path, new String[] {"gpg"}, true); ArrayList filePaths = new ArrayList(); for (File file : files) { - filePaths.add(file.getAbsolutePath().replace(repository.getWorkTree().getAbsolutePath(), "")); + filePaths.add(file.getAbsolutePath().replace(repository.getWorkTree().getAbsolutePath() + "/", "")); } return filePaths; } + + public static LinkedHashMap> getPasswords(File path) { + //We need to recover the passwords then parse the files + ArrayList passList = getFilesList(path); + LinkedHashMap> passMap = new LinkedHashMap>(); + passMap.put("Without Category", new ArrayList()); + + 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 tempList = new ArrayList(); + tempList.add(parts[1]); + passMap.put(parts[0], tempList); + } + } + } + return passMap; + } } -- cgit v1.2.3