Search in sources :

Example 1 with CodeSearcher

use of com.searchcode.app.service.CodeSearcher in project searchcode-server by boyter.

the class IndexBaseAndGitRepoJobTest method testMissingPathFilesShouldPage.

public void testMissingPathFilesShouldPage() {
    IndexGitRepoJob gitRepoJob = new IndexGitRepoJob();
    CodeSearcher codeSearcherMock = Mockito.mock(CodeSearcher.class);
    List<String> repoReturn = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        repoReturn.add("string" + i);
    }
    when(codeSearcherMock.getRepoDocuments("testRepoName", 0)).thenReturn(repoReturn);
    when(codeSearcherMock.getRepoDocuments("testRepoName", 1)).thenReturn(repoReturn);
    when(codeSearcherMock.getRepoDocuments("testRepoName", 2)).thenReturn(new ArrayList<>());
    gitRepoJob.cleanMissingPathFiles(codeSearcherMock, "testRepoName", new HashMap<String, String>());
    verify(codeSearcherMock, times(1)).getRepoDocuments("testRepoName", 0);
    verify(codeSearcherMock, times(1)).getRepoDocuments("testRepoName", 1);
    verify(codeSearcherMock, times(1)).getRepoDocuments("testRepoName", 2);
}
Also used : ArrayList(java.util.ArrayList) CodeSearcher(com.searchcode.app.service.CodeSearcher) IndexGitRepoJob(com.searchcode.app.jobs.repository.IndexGitRepoJob)

Example 2 with CodeSearcher

use of com.searchcode.app.service.CodeSearcher in project searchcode-server by boyter.

the class EndToEndITCase method testEndToEndSvnPath.

public void testEndToEndSvnPath() throws IOException {
    CodeSearcher cs = new CodeSearcher();
    File directoryWithFiles = TestHelpers.createDirectoryWithFiles("EndToEndSvnTest");
    IndexSvnRepoJob indexSvnRepoJob = new IndexSvnRepoJob();
    indexSvnRepoJob.indexDocsByPath(Paths.get(directoryWithFiles.toString()), "ENDTOENDTEST", "", directoryWithFiles.toString(), false);
    SearchResult searchResult = cs.search("endtoendtestfile", 0);
    assertThat(searchResult.getCodeResultList().size()).isEqualTo(3);
    CodeResult codeResult1 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile1.php")).findFirst().get();
    CodeResult codeResult2 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile2.py")).findFirst().get();
    CodeResult codeResult3 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile3.java")).findFirst().get();
    assertThat(codeResult1.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile1");
    assertThat(codeResult2.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile2");
    assertThat(codeResult3.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile3");
    // Delete a single file
    String codeId = searchResult.getCodeResultList().get(0).getCodeId();
    Singleton.getCodeIndexer().deleteByCodeId(codeId);
    searchResult = cs.search("endtoendtestfile".toLowerCase(), 0);
    assertThat(searchResult.getCodeResultList().size()).isEqualTo(2);
    // Delete file from disk then index to ensure it is removed from the index
    File toDelete = new File(directoryWithFiles.toString() + "/EndToEndTestFile2.py");
    toDelete.delete();
    indexSvnRepoJob.indexDocsByPath(Paths.get(directoryWithFiles.toString()), "ENDTOENDTEST", "", directoryWithFiles.toString(), true);
    searchResult = cs.search("endtoendtestfile", 0);
    assertThat(searchResult.getCodeResultList().size()).isEqualTo(2);
    codeResult1 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile1.php")).findFirst().get();
    codeResult2 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile3.java")).findFirst().get();
    assertThat(codeResult1.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile1");
    assertThat(codeResult2.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile3");
    Singleton.getCodeIndexer().deleteByReponame("ENDTOENDTEST");
    searchResult = cs.search("endtoendtestfile".toLowerCase(), 0);
    assertThat(searchResult.getCodeResultList().size()).isEqualTo(0);
}
Also used : IndexSvnRepoJob(com.searchcode.app.jobs.repository.IndexSvnRepoJob) SearchResult(com.searchcode.app.dto.SearchResult) Singleton(com.searchcode.app.service.Singleton) RepositoryChanged(com.searchcode.app.dto.RepositoryChanged) FileUtils(org.apache.commons.io.FileUtils) IndexGitRepoJob(com.searchcode.app.jobs.repository.IndexGitRepoJob) TestHelpers(com.searchcode.app.TestHelpers) Values(com.searchcode.app.config.Values) CodeIndexer(com.searchcode.app.service.CodeIndexer) CodeResult(com.searchcode.app.dto.CodeResult) CodeSearcher(com.searchcode.app.service.CodeSearcher) java.io(java.io) Paths(java.nio.file.Paths) AssertionsForInterfaceTypes.assertThat(org.assertj.core.api.AssertionsForInterfaceTypes.assertThat) Properties(com.searchcode.app.util.Properties) TestCase(junit.framework.TestCase) IndexFileRepoJob(com.searchcode.app.jobs.repository.IndexFileRepoJob) Path(java.nio.file.Path) CodeResult(com.searchcode.app.dto.CodeResult) IndexSvnRepoJob(com.searchcode.app.jobs.repository.IndexSvnRepoJob) SearchResult(com.searchcode.app.dto.SearchResult) CodeSearcher(com.searchcode.app.service.CodeSearcher)

Example 3 with CodeSearcher

use of com.searchcode.app.service.CodeSearcher in project searchcode-server by boyter.

the class EndToEndITCase method testEndToEndFilePath.

public void testEndToEndFilePath() throws IOException {
    CodeSearcher cs = new CodeSearcher();
    File directoryWithFiles = TestHelpers.createDirectoryWithFiles("EndToEndFileTest");
    IndexFileRepoJob indexFileRepoJob = new IndexFileRepoJob();
    // Index created files
    indexFileRepoJob.indexDocsByPath(Paths.get(directoryWithFiles.toString()), "ENDTOENDTEST", "", directoryWithFiles.toString(), false);
    SearchResult searchResult = cs.search("endtoendtestfile", 0);
    assertThat(searchResult.getCodeResultList().size()).isEqualTo(3);
    CodeResult codeResult1 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile1.php")).findFirst().get();
    CodeResult codeResult2 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile2.py")).findFirst().get();
    CodeResult codeResult3 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile3.java")).findFirst().get();
    assertThat(codeResult1.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile1");
    assertThat(codeResult2.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile2");
    assertThat(codeResult3.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile3");
    // Delete a single file
    String codeId = searchResult.getCodeResultList().get(0).getCodeId();
    Singleton.getCodeIndexer().deleteByCodeId(codeId);
    searchResult = cs.search("endtoendtestfile".toLowerCase(), 0);
    assertThat(searchResult.getCodeResultList().size()).isEqualTo(2);
    codeResult1 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile2.py")).findFirst().get();
    codeResult2 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile3.java")).findFirst().get();
    assertThat(codeResult1.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile2");
    assertThat(codeResult2.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile3");
    // Delete file from disk then index to ensure it is removed from the index
    File toDelete = new File(directoryWithFiles.toString() + "/EndToEndTestFile2.py");
    toDelete.delete();
    indexFileRepoJob.indexDocsByPath(Paths.get(directoryWithFiles.toString()), "ENDTOENDTEST", "", directoryWithFiles.toString(), true);
    searchResult = cs.search("endtoendtestfile", 0);
    assertThat(searchResult.getCodeResultList().size()).isEqualTo(2);
    codeResult1 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile1.php")).findFirst().get();
    codeResult2 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile3.java")).findFirst().get();
    assertThat(codeResult1.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile1");
    assertThat(codeResult2.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile3");
    // Delete everything
    Singleton.getCodeIndexer().deleteByReponame("ENDTOENDTEST");
    searchResult = cs.search("endtoendtestfile".toLowerCase(), 0);
    assertThat(searchResult.getCodeResultList().size()).isEqualTo(0);
}
Also used : IndexFileRepoJob(com.searchcode.app.jobs.repository.IndexFileRepoJob) IndexSvnRepoJob(com.searchcode.app.jobs.repository.IndexSvnRepoJob) SearchResult(com.searchcode.app.dto.SearchResult) Singleton(com.searchcode.app.service.Singleton) RepositoryChanged(com.searchcode.app.dto.RepositoryChanged) FileUtils(org.apache.commons.io.FileUtils) IndexGitRepoJob(com.searchcode.app.jobs.repository.IndexGitRepoJob) TestHelpers(com.searchcode.app.TestHelpers) Values(com.searchcode.app.config.Values) CodeIndexer(com.searchcode.app.service.CodeIndexer) CodeResult(com.searchcode.app.dto.CodeResult) CodeSearcher(com.searchcode.app.service.CodeSearcher) java.io(java.io) Paths(java.nio.file.Paths) AssertionsForInterfaceTypes.assertThat(org.assertj.core.api.AssertionsForInterfaceTypes.assertThat) Properties(com.searchcode.app.util.Properties) TestCase(junit.framework.TestCase) IndexFileRepoJob(com.searchcode.app.jobs.repository.IndexFileRepoJob) Path(java.nio.file.Path) CodeResult(com.searchcode.app.dto.CodeResult) SearchResult(com.searchcode.app.dto.SearchResult) CodeSearcher(com.searchcode.app.service.CodeSearcher)

Example 4 with CodeSearcher

use of com.searchcode.app.service.CodeSearcher in project searchcode-server by boyter.

the class IndexBaseRepoJob method indexDocsByPath.

/**
     * Indexes all the documents in the path provided. Will also remove anything from the index if not on disk
     * Generally this is a slow update used only for the initial clone of a repository
     * NB this can be used for updates but it will be much slower as it needs to to walk the contents of the disk
     */
public void indexDocsByPath(Path path, String repoName, String repoLocations, String repoRemoteLocation, boolean existingRepo) {
    String fileRepoLocations = FilenameUtils.separatorsToUnix(repoLocations);
    SearchcodeFileVisitor<Path> searchcodeFileVisitor = new SearchcodeFileVisitor<>(this, repoName, fileRepoLocations, repoRemoteLocation);
    try {
        if (this.FOLLOWLINKS) {
            Files.walkFileTree(path, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, searchcodeFileVisitor);
        } else {
            Files.walkFileTree(path, searchcodeFileVisitor);
        }
    } catch (IOException ex) {
        Singleton.getLogger().warning("ERROR - caught a " + ex.getClass() + " in " + this.getClass() + " indexDocsByPath walkFileTree\n with message: " + ex.getMessage());
    }
    if (this.LOGINDEXED) {
        logIndexed(repoName, searchcodeFileVisitor.reportList);
    }
    if (existingRepo) {
        CodeSearcher codeSearcher = new CodeSearcher();
        this.cleanMissingPathFiles(codeSearcher, repoName, searchcodeFileVisitor.fileLocationsMap);
    }
}
Also used : IOException(java.io.IOException) CodeSearcher(com.searchcode.app.service.CodeSearcher)

Example 5 with CodeSearcher

use of com.searchcode.app.service.CodeSearcher in project searchcode-server by boyter.

the class CodeRouteService method html.

public ModelAndView html(Request request, Response response) {
    Repo repo = Singleton.getRepo();
    Data data = Singleton.getData();
    SearchcodeLib scl = Singleton.getSearchcodeLib(data);
    CodeSearcher cs = new CodeSearcher();
    CodeMatcher cm = new CodeMatcher(data);
    Map<String, Object> map = new HashMap<>();
    map.put("repoCount", repo.getRepoCount());
    if (request.queryParams().contains("q")) {
        String query = request.queryParams("q").trim();
        String altquery = query.replaceAll("[^A-Za-z0-9 ]", " ").trim().replaceAll(" +", " ");
        int page = 0;
        if (request.queryParams().contains("p")) {
            try {
                page = Integer.parseInt(request.queryParams("p"));
                page = page > 19 ? 19 : page;
            } catch (NumberFormatException ex) {
                page = 0;
            }
        }
        String[] repos = new String[0];
        String[] langs = new String[0];
        String[] owners = new String[0];
        String reposFilter = Values.EMPTYSTRING;
        String langsFilter = Values.EMPTYSTRING;
        String ownersFilter = Values.EMPTYSTRING;
        String reposQueryString = Values.EMPTYSTRING;
        String langsQueryString = Values.EMPTYSTRING;
        String ownsQueryString = Values.EMPTYSTRING;
        if (request.queryParams().contains("repo")) {
            repos = request.queryParamsValues("repo");
            if (repos.length != 0) {
                List<String> reposList = Arrays.asList(repos).stream().map((s) -> "reponame:" + QueryParser.escape(s)).collect(Collectors.toList());
                reposFilter = " && (" + StringUtils.join(reposList, " || ") + ")";
                List<String> reposQueryList = Arrays.asList(repos).stream().map((s) -> "&repo=" + URLEncoder.encode(s)).collect(Collectors.toList());
                reposQueryString = StringUtils.join(reposQueryList, "");
            }
        }
        if (request.queryParams().contains("lan")) {
            langs = request.queryParamsValues("lan");
            if (langs.length != 0) {
                List<String> langsList = Arrays.asList(langs).stream().map((s) -> "languagename:" + QueryParser.escape(s)).collect(Collectors.toList());
                langsFilter = " && (" + StringUtils.join(langsList, " || ") + ")";
                List<String> langsQueryList = Arrays.asList(langs).stream().map((s) -> "&lan=" + URLEncoder.encode(s)).collect(Collectors.toList());
                langsQueryString = StringUtils.join(langsQueryList, "");
            }
        }
        if (request.queryParams().contains("own")) {
            owners = request.queryParamsValues("own");
            if (owners.length != 0) {
                List<String> ownersList = Arrays.asList(owners).stream().map((s) -> "codeowner:" + QueryParser.escape(s)).collect(Collectors.toList());
                ownersFilter = " && (" + StringUtils.join(ownersList, " || ") + ")";
                List<String> ownsQueryList = Arrays.asList(owners).stream().map((s) -> "&own=" + URLEncoder.encode(s)).collect(Collectors.toList());
                ownsQueryString = StringUtils.join(ownsQueryList, "");
            }
        }
        // split the query escape it and and it together
        String cleanQueryString = scl.formatQueryString(query);
        SearchResult searchResult = cs.search(cleanQueryString + reposFilter + langsFilter + ownersFilter, page);
        searchResult.setCodeResultList(cm.formatResults(searchResult.getCodeResultList(), query, true));
        for (CodeFacetRepo f : searchResult.getRepoFacetResults()) {
            if (Arrays.asList(repos).contains(f.getRepoName())) {
                f.setSelected(true);
            }
        }
        for (CodeFacetLanguage f : searchResult.getLanguageFacetResults()) {
            if (Arrays.asList(langs).contains(f.getLanguageName())) {
                f.setSelected(true);
            }
        }
        for (CodeFacetOwner f : searchResult.getOwnerFacetResults()) {
            if (Arrays.asList(owners).contains(f.getOwner())) {
                f.setSelected(true);
            }
        }
        map.put("searchValue", query);
        map.put("searchResult", searchResult);
        map.put("reposQueryString", reposQueryString);
        map.put("langsQueryString", langsQueryString);
        map.put("ownsQueryString", ownsQueryString);
        map.put("altQuery", altquery);
        map.put("totalPages", searchResult.getPages().size());
        map.put("isHtml", true);
        map.put("logoImage", CommonRouteService.getLogo());
        map.put("isCommunity", App.ISCOMMUNITY);
        map.put(Values.EMBED, Singleton.getData().getDataByName(Values.EMBED, Values.EMPTYSTRING));
        return new ModelAndView(map, "searchresults.ftl");
    }
    map.put("photoId", CommonRouteService.getPhotoId());
    map.put("numDocs", cs.getTotalNumberDocumentsIndexed());
    map.put("logoImage", CommonRouteService.getLogo());
    map.put("isCommunity", App.ISCOMMUNITY);
    map.put(Values.EMBED, Singleton.getData().getDataByName(Values.EMBED, Values.EMPTYSTRING));
    return new ModelAndView(map, "index.ftl");
}
Also used : java.util(java.util) Singleton(com.searchcode.app.service.Singleton) Spark.halt(spark.Spark.halt) RepoResult(com.searchcode.app.model.RepoResult) ModelAndView(spark.ModelAndView) Repo(com.searchcode.app.dao.Repo) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) Values(com.searchcode.app.config.Values) CodeMatcher(com.searchcode.app.service.CodeMatcher) com.searchcode.app.util(com.searchcode.app.util) CommonRouteService(com.searchcode.app.service.route.CommonRouteService) URLEncoder(java.net.URLEncoder) com.searchcode.app.dto(com.searchcode.app.dto) CodeSearcher(com.searchcode.app.service.CodeSearcher) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) Gson(com.google.gson.Gson) Request(spark.Request) Data(com.searchcode.app.dao.Data) Response(spark.Response) Properties(com.searchcode.app.util.Properties) StringEscapeUtils(org.apache.commons.lang3.StringEscapeUtils) App(com.searchcode.app.App) ModelAndView(spark.ModelAndView) Data(com.searchcode.app.dao.Data) CodeSearcher(com.searchcode.app.service.CodeSearcher) Repo(com.searchcode.app.dao.Repo) CodeMatcher(com.searchcode.app.service.CodeMatcher)

Aggregations

CodeSearcher (com.searchcode.app.service.CodeSearcher)11 IndexGitRepoJob (com.searchcode.app.jobs.repository.IndexGitRepoJob)6 Values (com.searchcode.app.config.Values)5 SearchResult (com.searchcode.app.dto.SearchResult)5 Singleton (com.searchcode.app.service.Singleton)5 CodeResult (com.searchcode.app.dto.CodeResult)4 RepositoryChanged (com.searchcode.app.dto.RepositoryChanged)4 Properties (com.searchcode.app.util.Properties)4 Path (java.nio.file.Path)4 TestHelpers (com.searchcode.app.TestHelpers)3 Repo (com.searchcode.app.dao.Repo)3 IndexFileRepoJob (com.searchcode.app.jobs.repository.IndexFileRepoJob)3 IndexSvnRepoJob (com.searchcode.app.jobs.repository.IndexSvnRepoJob)3 CodeIndexer (com.searchcode.app.service.CodeIndexer)3 CodeMatcher (com.searchcode.app.service.CodeMatcher)3 java.io (java.io)3 Paths (java.nio.file.Paths)3 TestCase (junit.framework.TestCase)3 FileUtils (org.apache.commons.io.FileUtils)3 AssertionsForInterfaceTypes.assertThat (org.assertj.core.api.AssertionsForInterfaceTypes.assertThat)3