Search in sources :

Example 1 with SearchcodeLib

use of com.searchcode.app.util.SearchcodeLib in project searchcode-server by boyter.

the class SearchRouteService method codeSearch.

public SearchResult codeSearch(Request request, Response response) {
    CodeSearcher cs = new CodeSearcher();
    CodeMatcher cm = new CodeMatcher(Singleton.getData());
    SearchcodeLib scl = Singleton.getSearchcodeLib(Singleton.getData());
    if (request.queryParams().contains("q") && !request.queryParams("q").trim().equals(Values.EMPTYSTRING)) {
        String query = request.queryParams("q").trim();
        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;
        String[] langs;
        String[] owners;
        String reposFilter = Values.EMPTYSTRING;
        String langsFilter = Values.EMPTYSTRING;
        String ownersFilter = 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.replace(" ", "_"))).collect(Collectors.toList());
                reposFilter = " && (" + StringUtils.join(reposList, " || ") + ")";
            }
        }
        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.replace(" ", "_"))).collect(Collectors.toList());
                langsFilter = " && (" + StringUtils.join(langsList, " || ") + ")";
            }
        }
        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.replace(" ", "_"))).collect(Collectors.toList());
                ownersFilter = " && (" + StringUtils.join(ownersList, " || ") + ")";
            }
        }
        // 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));
        searchResult.setQuery(query);
        for (String altQuery : scl.generateAltQueries(query)) {
            searchResult.addAltQuery(altQuery);
        }
        // Null out code as it isnt required and there is no point in bloating our ajax requests
        for (CodeResult codeSearchResult : searchResult.getCodeResultList()) {
            codeSearchResult.setCode(null);
        }
        return searchResult;
    }
    return null;
}
Also used : Arrays(java.util.Arrays) SearchResult(com.searchcode.app.dto.SearchResult) Singleton(com.searchcode.app.service.Singleton) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) Values(com.searchcode.app.config.Values) CodeMatcher(com.searchcode.app.service.CodeMatcher) SearchcodeLib(com.searchcode.app.util.SearchcodeLib) List(java.util.List) CodeResult(com.searchcode.app.dto.CodeResult) CodeSearcher(com.searchcode.app.service.CodeSearcher) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) Request(spark.Request) Response(spark.Response) SearchcodeLib(com.searchcode.app.util.SearchcodeLib) CodeResult(com.searchcode.app.dto.CodeResult) CodeMatcher(com.searchcode.app.service.CodeMatcher) SearchResult(com.searchcode.app.dto.SearchResult) CodeSearcher(com.searchcode.app.service.CodeSearcher)

Example 2 with SearchcodeLib

use of com.searchcode.app.util.SearchcodeLib in project searchcode-server by boyter.

the class TimeSearchRouteService method getTimeSearch.

public SearchResult getTimeSearch(Request request, Response response) {
    Data data = Singleton.getData();
    SearchcodeLib scl = Singleton.getSearchcodeLib(data);
    TimeCodeSearcher cs = new TimeCodeSearcher();
    CodeMatcher cm = new CodeMatcher(data);
    response.header("Content-Encoding", "gzip");
    if (request.queryParams().contains("q") == false || Values.EMPTYSTRING.equals(request.queryParams("q").trim())) {
        return null;
    }
    String query = request.queryParams("q").trim();
    int page = this.getPage(request);
    String[] repos;
    String[] langs;
    String[] owners;
    String[] year;
    String[] yearmonth;
    String[] yearmonthday;
    String[] revisions;
    String[] deleted;
    String reposFilter = Values.EMPTYSTRING;
    String langsFilter = Values.EMPTYSTRING;
    String ownersFilter = Values.EMPTYSTRING;
    String yearFilter = Values.EMPTYSTRING;
    String yearMonthFilter = Values.EMPTYSTRING;
    String yearMonthDayFilter = Values.EMPTYSTRING;
    String revisionsFilter = Values.EMPTYSTRING;
    String deletedFilter = Values.EMPTYSTRING;
    if (request.queryParams().contains("repo")) {
        repos = request.queryParamsValues("repo");
        reposFilter = getRepos(repos, reposFilter);
    }
    if (request.queryParams().contains("lan")) {
        langs = request.queryParamsValues("lan");
        langsFilter = getLanguages(langs, langsFilter);
    }
    if (request.queryParams().contains("own")) {
        owners = request.queryParamsValues("own");
        ownersFilter = getOwners(owners, ownersFilter);
    }
    if (request.queryParams().contains("year")) {
        year = request.queryParamsValues("year");
        yearFilter = this.getYears(year, yearFilter);
    }
    if (request.queryParams().contains("ym")) {
        yearmonth = request.queryParamsValues("ym");
        yearMonthFilter = this.getYearMonths(yearmonth, yearMonthFilter);
    }
    if (request.queryParams().contains("ymd")) {
        yearmonthday = request.queryParamsValues("ymd");
        yearMonthDayFilter = this.getYearMonthDays(yearmonthday, yearMonthDayFilter);
    }
    if (request.queryParams().contains("rev")) {
        revisions = request.queryParamsValues("rev");
        revisionsFilter = this.getRevisions(revisions, revisionsFilter);
    }
    if (request.queryParams().contains("del")) {
        deleted = request.queryParamsValues("del");
        deletedFilter = this.getDeleted(deleted, deletedFilter);
    }
    // split the query escape it and and it together
    String cleanQueryString = scl.formatQueryString(query);
    SearchResult searchResult = cs.search(cleanQueryString + reposFilter + langsFilter + ownersFilter + yearFilter + yearMonthFilter + yearMonthDayFilter + revisionsFilter + deletedFilter, page);
    searchResult.setCodeResultList(cm.formatResults(searchResult.getCodeResultList(), query, true));
    searchResult.setQuery(query);
    this.getAltQueries(scl, query, searchResult);
    // Null out code as it isn't required and there is no point in bloating our ajax requests
    for (CodeResult codeSearchResult : searchResult.getCodeResultList()) {
        codeSearchResult.setCode(null);
    }
    return searchResult;
}
Also used : SearchcodeLib(com.searchcode.app.util.SearchcodeLib) CodeResult(com.searchcode.app.dto.CodeResult) CodeMatcher(com.searchcode.app.service.CodeMatcher) Data(com.searchcode.app.dao.Data) TimeCodeSearcher(com.searchcode.app.service.TimeCodeSearcher) SearchResult(com.searchcode.app.dto.SearchResult)

Example 3 with SearchcodeLib

use of com.searchcode.app.util.SearchcodeLib in project searchcode-server by boyter.

the class IndexBaseRepoJob method indexDocsByDelta.

/**
     * Indexes all the documents in the repository changed file effectively performing a delta update
     * Should only be called when there is a genuine update IE something was indexed previously and
     * has has a new commit.
     */
public void indexDocsByDelta(Path path, String repoName, String repoLocations, String repoRemoteLocation, RepositoryChanged repositoryChanged) {
    // Should have data object by this point
    SearchcodeLib scl = Singleton.getSearchCodeLib();
    Queue<CodeIndexDocument> codeIndexDocumentQueue = Singleton.getCodeIndexQueue();
    String fileRepoLocations = FilenameUtils.separatorsToUnix(repoLocations);
    // Used to hold the reports of what was indexed
    List<String[]> reportList = new ArrayList<>();
    for (String changedFile : repositoryChanged.getChangedFiles()) {
        if (this.shouldJobPauseOrTerminate()) {
            return;
        }
        if (Singleton.getDataService().getPersistentDelete().contains(repoName)) {
            return;
        }
        String[] split = changedFile.split("/");
        String fileName = split[split.length - 1];
        changedFile = fileRepoLocations + "/" + repoName + "/" + changedFile;
        changedFile = changedFile.replace("//", "/");
        CodeLinesReturn codeLinesReturn = this.getCodeLines(changedFile, reportList);
        if (codeLinesReturn.isError()) {
            break;
        }
        IsMinifiedReturn isMinified = this.getIsMinified(codeLinesReturn.getCodeLines(), fileName, reportList);
        if (isMinified.isMinified()) {
            break;
        }
        if (this.checkIfEmpty(codeLinesReturn.getCodeLines(), changedFile, reportList)) {
            break;
        }
        if (this.determineBinary(changedFile, fileName, codeLinesReturn.getCodeLines(), reportList)) {
            break;
        }
        String md5Hash = this.getFileMd5(changedFile);
        String languageName = Singleton.getFileClassifier().languageGuesser(changedFile, codeLinesReturn.getCodeLines());
        String fileLocation = this.getRelativeToProjectPath(path.toString(), changedFile);
        String fileLocationFilename = changedFile.replace(fileRepoLocations, Values.EMPTYSTRING);
        String repoLocationRepoNameLocationFilename = changedFile;
        String newString = this.getBlameFilePath(fileLocationFilename);
        String codeOwner = this.getCodeOwner(codeLinesReturn.getCodeLines(), newString, repoName, fileRepoLocations, scl);
        if (this.LOWMEMORY) {
            try {
                Singleton.getCodeIndexer().indexDocument(new CodeIndexDocument(repoLocationRepoNameLocationFilename, repoName, fileName, fileLocation, fileLocationFilename, md5Hash, languageName, codeLinesReturn.getCodeLines().size(), StringUtils.join(codeLinesReturn.getCodeLines(), " "), repoRemoteLocation, codeOwner));
            } catch (IOException ex) {
                Singleton.getLogger().warning("ERROR - caught a " + ex.getClass() + " in " + this.getClass() + "\n with message: " + ex.getMessage());
            }
        } else {
            this.sharedService.incrementCodeIndexLinesCount(codeLinesReturn.getCodeLines().size());
            codeIndexDocumentQueue.add(new CodeIndexDocument(repoLocationRepoNameLocationFilename, repoName, fileName, fileLocation, fileLocationFilename, md5Hash, languageName, codeLinesReturn.getCodeLines().size(), StringUtils.join(codeLinesReturn.getCodeLines(), " "), repoRemoteLocation, codeOwner));
        }
        if (this.LOGINDEXED) {
            reportList.add(new String[] { changedFile, "included", "" });
        }
    }
    if (this.LOGINDEXED && reportList.isEmpty() == false) {
        this.logIndexed(repoName + "_delta", reportList);
    }
    for (String deletedFile : repositoryChanged.getDeletedFiles()) {
        deletedFile = fileRepoLocations + "/" + repoName + "/" + deletedFile;
        deletedFile = deletedFile.replace("//", "/");
        Singleton.getLogger().info("Missing from disk, removing from index " + deletedFile);
        try {
            Singleton.getCodeIndexer().deleteByCodeId(DigestUtils.sha1Hex(deletedFile));
        } catch (IOException ex) {
            Singleton.getLogger().warning("ERROR - caught a " + ex.getClass() + " in " + this.getClass() + " indexDocsByDelta deleteByFileLocationFilename for " + repoName + " " + deletedFile + "\n with message: " + ex.getMessage());
        }
    }
}
Also used : SearchcodeLib(com.searchcode.app.util.SearchcodeLib) CodeIndexDocument(com.searchcode.app.dto.CodeIndexDocument) IOException(java.io.IOException)

Aggregations

SearchcodeLib (com.searchcode.app.util.SearchcodeLib)3 CodeResult (com.searchcode.app.dto.CodeResult)2 SearchResult (com.searchcode.app.dto.SearchResult)2 CodeMatcher (com.searchcode.app.service.CodeMatcher)2 Values (com.searchcode.app.config.Values)1 Data (com.searchcode.app.dao.Data)1 CodeIndexDocument (com.searchcode.app.dto.CodeIndexDocument)1 CodeSearcher (com.searchcode.app.service.CodeSearcher)1 Singleton (com.searchcode.app.service.Singleton)1 TimeCodeSearcher (com.searchcode.app.service.TimeCodeSearcher)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 StringUtils (org.apache.commons.lang3.StringUtils)1 QueryParser (org.apache.lucene.queryparser.classic.QueryParser)1 Request (spark.Request)1 Response (spark.Response)1