Skip to content

Commit

Permalink
Cache for SHA-512 generation
Browse files Browse the repository at this point in the history
  • Loading branch information
tsantalis committed Nov 5, 2024
1 parent ffbb8fd commit f5d649e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 68 deletions.
87 changes: 19 additions & 68 deletions src/main/java/org/codetracker/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,89 +5,39 @@
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Util {
private static boolean enableCache;
private static Map<String, String> shaCache = new HashMap<>();
private Util() {
}

public static String getPath(String filePath, String className) {
String srcFile = filePath.substring(0, filePath.lastIndexOf("/") + 1);
CharSequence charSequence = longestSubstring(srcFile, className.replace(".", "/"));
if (className.startsWith(charSequence.toString().replace("/", "."))) {
srcFile = filePath.toLowerCase().replace(charSequence, "$");
srcFile = srcFile.substring(0, srcFile.lastIndexOf("$"));
return srcFile;
}
return srcFile;
public static String annotationsToString(List<UMLAnnotation> umlAnnotations) {
return umlAnnotations != null && !umlAnnotations.isEmpty()
? String.format("[%s]", umlAnnotations.stream().map(UMLAnnotation::toString).sorted().collect(Collectors.joining(";")))
: "";
}

public static String longestSubstring(String str1, String str2) {

StringBuilder sb = new StringBuilder();
if (str1 == null || str1.isEmpty() || str2 == null || str2.isEmpty())
return "";

// ignore case
str1 = str1.toLowerCase();
str2 = str2.toLowerCase();

// java initializes them already with 0
int[][] num = new int[str1.length()][str2.length()];
int maxLength = 0;
int lastSubsBegin = 0;

for (int i = 0; i < str1.length(); i++) {
for (int j = 0; j < str2.length(); j++) {
if (str1.charAt(i) == str2.charAt(j)) {
if ((i == 0) || (j == 0))
num[i][j] = 1;
else
num[i][j] = 1 + num[i - 1][j - 1];

if (num[i][j] > maxLength) {
maxLength = num[i][j];
// generate substring from str1 => i
int thisSubsBegin = i - num[i][j] + 1;
if (lastSubsBegin == thisSubsBegin) {
//if the current LCS is the same as the last time this block ran
sb.append(str1.charAt(i));
} else {
//this block resets the string builder if a different LCS is found
lastSubsBegin = thisSubsBegin;
sb = new StringBuilder();
sb.append(str1, lastSubsBegin, i + 1);
}
}
}
}
}

return sb.toString();
public static void enableSHACache() {
enableCache = true;
}

protected static String getPackage(String filePath, String className) {
try {
String replace = className.replace(filePath.substring(filePath.lastIndexOf("/") + 1).replace(".java", ""), "$");
String packageName = replace.substring(0, replace.lastIndexOf("$") - 1);
packageName = getPath(filePath, className) + packageName;
return packageName;
} catch (Exception ex) {
ex.printStackTrace();
}
return "";
public static void disableSHACache() {
enableCache = false;
}

public static String annotationsToString(List<UMLAnnotation> umlAnnotations) {
return umlAnnotations != null && !umlAnnotations.isEmpty()
? String.format("[%s]", umlAnnotations.stream().map(UMLAnnotation::toString).sorted().collect(Collectors.joining(";")))
: "";
public static void clearSHACache() {
shaCache.clear();
}


public static String getSHA512(String input) {
String toReturn = null;
if(shaCache.containsKey(input))
return shaCache.get(input);
String toReturn = null;
try {
MessageDigest digest = MessageDigest.getInstance("SHA-512");
digest.reset();
Expand All @@ -96,7 +46,8 @@ public static String getSHA512(String input) {
} catch (Exception e) {
e.printStackTrace();
}

if(enableCache)
shaCache.put(input, toReturn);
return toReturn;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public void testBlameWithLocalRepoUsingFileTracker(String url, String filePath,
String expectedFilePath = System.getProperty("user.dir") + testResultFileName;
String commitId = URLHelper.getCommit(url);
Repository repository = gitService.cloneIfNotExists(REPOS_PATH + "/" + getOwner(url) + "/" + getProject(url), URLHelper.getRepo(url));
org.codetracker.util.Util.enableSHACache();
FileTrackerImpl fileTracker = new FileTrackerImpl(repository, commitId, filePath);
fileTracker.blame();
BlameFormatter blameFormatter = new BlameFormatter(fileTracker.getLines());
Expand Down

0 comments on commit f5d649e

Please sign in to comment.