Search in sources :

Example 1 with CodeMatchResult

use of com.searchcode.app.dto.CodeMatchResult in project searchcode-server by boyter.

the class CodeMatcher method formatResults.

/**
     * Entry point for matching lines
     */
public List<CodeResult> formatResults(List<CodeResult> codeResult, String matchTerms, boolean highlightLine) {
    List<String> lstMatchTerms = splitTerms(matchTerms);
    List<CodeResult> results = new ArrayList<>();
    for (CodeResult code : codeResult) {
        List<CodeMatchResult> result = matchResults(code.getCode(), lstMatchTerms, highlightLine);
        if (result != null) {
            code.setMatchingResults(result);
            results.add(code);
        }
    }
    return results;
}
Also used : CodeResult(com.searchcode.app.dto.CodeResult) CodeMatchResult(com.searchcode.app.dto.CodeMatchResult)

Example 2 with CodeMatchResult

use of com.searchcode.app.dto.CodeMatchResult in project searchcode-server by boyter.

the class CodeMatcherTest method testFindBestMatchingLinesOver1000Limit.

public void testFindBestMatchingLinesOver1000Limit() {
    CodeMatcher cm = new CodeMatcher();
    List<String> matchTerms = new ArrayList<>();
    matchTerms.add("re.compile");
    matchTerms.add("compile");
    matchTerms.add("re");
    List<String> code = new ArrayList<>();
    for (int i = 0; i < 1000; i++) {
        code.add("re");
    }
    code.add("re.compile");
    List<CodeMatchResult> result = cm.findMatchingLines(code, matchTerms, true);
    // What we want to find here is the re.compile line since it is the best match even though the others
    // are also matches
    boolean found = false;
    for (CodeMatchResult line : result) {
        if (line.getLine().contains("re.compile")) {
            found = true;
        }
    }
    assertThat(found).isTrue();
}
Also used : ArrayList(java.util.ArrayList) CodeMatchResult(com.searchcode.app.dto.CodeMatchResult)

Example 3 with CodeMatchResult

use of com.searchcode.app.dto.CodeMatchResult in project searchcode-server by boyter.

the class CodeMatcherTest method testFindMatchingLinesPerformance.

/**
     * This tests the worst possible case of matching were the only match is literally at the end of the string
     * but there is a lot to check in each string.
     * Included because findMatchingLines is the slowest method and drags down the user experience if too slow
     * The longest the worst possible case should take to run is 1 wall clock second
     */
public void testFindMatchingLinesPerformance() {
    CodeMatcher cm = new CodeMatcher();
    List<String> matchTerms = new ArrayList<String>();
    matchTerms.add("code");
    matchTerms.add("this");
    // Simulates the worst possible case where the matching lines are right
    // at the end
    String addString = "some additional stuff that random stuff that should not match but force it to work a bit harder then it normally would";
    List<String> code = new ArrayList<>();
    for (int i = 0; i < 9999; i++) {
        StringBuffer stringBuffer = new StringBuffer();
        for (int j = 0; j < 5; j++) {
            stringBuffer.append(addString);
        }
        code.add(stringBuffer.toString());
    }
    code.add("this is some code");
    Instant start = Instant.now();
    List<CodeMatchResult> result = cm.findMatchingLines(code, matchTerms, true);
    // Force no optimisations by the JVM
    assertThat(result).isNotNull();
    long seconds = Duration.between(start, Instant.now()).getSeconds();
    assertThat(seconds).isLessThanOrEqualTo(5);
}
Also used : Instant(java.time.Instant) ArrayList(java.util.ArrayList) CodeMatchResult(com.searchcode.app.dto.CodeMatchResult)

Example 4 with CodeMatchResult

use of com.searchcode.app.dto.CodeMatchResult in project searchcode-server by boyter.

the class CodeRouteServiceTest method testGetCodeWithParamsWithMatch.

public void testGetCodeWithParamsWithMatch() {
    Request request = Mockito.mock(Request.class);
    Response response = Mockito.mock(Response.class);
    CodeSearcher codeSearcher = Mockito.mock(CodeSearcher.class);
    CodeRouteService codeRouteService = new CodeRouteService(codeSearcher);
    CodeResult codeResult = new CodeResult(new ArrayList<String>(), new ArrayList<CodeMatchResult>());
    codeResult.setCodeLines("100");
    codeResult.setLanguageName("LanguageName");
    codeResult.setMd5hash("md5hash");
    codeResult.setRepoName("myRepo");
    codeResult.setRepoLocation("repoLocation");
    codeResult.setCodeOwner("codeOwner");
    when(request.params(":codeid")).thenReturn("MATCH-MOCK");
    when(codeSearcher.getByCodeId("MATCH-MOCK")).thenReturn(codeResult);
    Map<String, Object> map = codeRouteService.getCode(request, response);
    assertThat(map.get("codePath")).isEqualTo("/");
    assertThat(map.get("codeLength")).isEqualTo("100");
    assertThat(map.get("languageName")).isEqualTo("LanguageName");
    assertThat(map.get("md5Hash")).isEqualTo("md5hash");
    assertThat(map.get("repoName")).isEqualTo("myRepo");
    assertThat(map.get("highlight")).isEqualTo(true);
    assertThat(map.get("repoLocation")).isEqualTo("repoLocation");
    assertThat(map.get("codeValue")).isEqualTo("");
    assertThat(map.get("highligher")).isNotNull();
    assertThat(map.get("codeOwner")).isEqualTo("codeOwner");
    assertThat(map.get("owaspResults")).isNotNull();
    assertThat(map.get("logoImage")).isNotNull();
    assertThat(map.get("isCommunity")).isEqualTo(App.ISCOMMUNITY);
    assertThat(map.get("estimatedCost")).isNull();
}
Also used : Response(spark.Response) CodeRouteService(com.searchcode.app.service.route.CodeRouteService) Request(spark.Request) CodeResult(com.searchcode.app.dto.CodeResult) CodeMatchResult(com.searchcode.app.dto.CodeMatchResult)

Example 5 with CodeMatchResult

use of com.searchcode.app.dto.CodeMatchResult in project searchcode-server by boyter.

the class CodeMatcher method findMatchingLines.

/**
     * If changing anything in here be wary of performance issues as it is the slowest method by a long shot.
     * Be especially careful of branch prediction issues which is why this method has been re-written several times
     * just to avoid those issues even though the result was a LONGER method
     * TODO wring more performance out of this method where possible
     */
public List<CodeMatchResult> findMatchingLines(List<String> code, List<String> matchTerms, boolean highlightLine) {
    List<CodeMatchResult> resultLines = new LinkedList<>();
    int codesize = code.size();
    int searchThrough = codesize > this.MAXLINEDEPTH ? this.MAXLINEDEPTH : codesize;
    int matching = 0;
    // Go through each line finding matching lines
    for (int i = 0; i < searchThrough; i++) {
        String matchRes = code.get(i).toLowerCase().replaceAll("\\s+", " ");
        matching = 0;
        for (String matchTerm : matchTerms) {
            if (matchRes.contains(matchTerm.replace("*", ""))) {
                matching++;
            }
        }
        if (matching != 0) {
            resultLines.add(new CodeMatchResult(code.get(i), true, false, matching, i));
        }
    }
    // Get the adjacent lines
    List<CodeMatchResult> adajacentLines = new LinkedList<>();
    for (CodeMatchResult cmr : resultLines) {
        int linenumber = cmr.getLineNumber();
        int previouslinenumber = linenumber - 1;
        int nextlinenumber = linenumber + 1;
        if (previouslinenumber >= 0 && !this.resultExists(resultLines, previouslinenumber)) {
            adajacentLines.add(new CodeMatchResult(code.get(previouslinenumber), false, false, 0, previouslinenumber));
        }
        if (nextlinenumber < codesize && !this.resultExists(resultLines, nextlinenumber)) {
            adajacentLines.add(new CodeMatchResult(code.get(nextlinenumber), false, false, 0, nextlinenumber));
        }
    }
    resultLines.addAll(adajacentLines);
    // If not matching we probably matched on the filename or past 10000
    if (resultLines.size() == 0) {
        searchThrough = codesize > MATCHLINES ? MATCHLINES : codesize;
        for (int i = 0; i < searchThrough; i++) {
            resultLines.add(new CodeMatchResult(code.get(i), false, false, 0, i));
        }
    }
    // Highlight the lines if required but always escape everything
    if (highlightLine) {
        for (CodeMatchResult cmr : resultLines) {
            if (cmr.isMatching()) {
                String line = Values.EMPTYSTRING;
                try {
                    line = this.highlightLine(cmr.getLine(), matchTerms);
                } catch (StringIndexOutOfBoundsException ex) {
                    Singleton.getLogger().severe("Unable to highlightLine " + cmr.getLine() + " using terms " + String.join(",", matchTerms) + " " + ex.toString());
                }
                cmr.setLine(line);
            } else {
                cmr.setLine(StringEscapeUtils.escapeHtml4(cmr.getLine()));
            }
        }
    } else {
        for (CodeMatchResult cmr : resultLines) {
            cmr.setLine(StringEscapeUtils.escapeHtml4(cmr.getLine()));
        }
    }
    return resultLines;
}
Also used : CodeMatchResult(com.searchcode.app.dto.CodeMatchResult)

Aggregations

CodeMatchResult (com.searchcode.app.dto.CodeMatchResult)6 CodeResult (com.searchcode.app.dto.CodeResult)2 ArrayList (java.util.ArrayList)2 CodeRouteService (com.searchcode.app.service.route.CodeRouteService)1 Instant (java.time.Instant)1 Request (spark.Request)1 Response (spark.Response)1