<commit_before>package com.atlassian.jira.plugins.dvcs.dao.impl;

import com.atlassian.jira.plugins.dvcs.model.GlobalFilter;
import org.apache.commons.lang.StringUtils;

/**
 *
 */
public class GlobalFilterQueryWhereClauseBuilder
{
    private GlobalFilter gf;

    public GlobalFilterQueryWhereClauseBuilder(GlobalFilter gf)
    {
        this.gf = gf;
    }

    public String build()
    {
        StringBuilder whereClauseProjectsSb = new StringBuilder();
        StringBuilder whereClauseIssueKyesSb = new StringBuilder();
        StringBuilder whereClauseUsersSb = new StringBuilder();
        if (gf != null)
        {
            if (gf.getInProjects() != null && gf.getInProjects().iterator().hasNext())
            {
                for (String projectKey : gf.getInProjects())
                {
                    if (StringUtils.isBlank(projectKey))
                    {
                        continue;
                    }
                    if (whereClauseProjectsSb.length() != 0)
                    {
                        whereClauseProjectsSb.append(" OR ");
                    }
                    whereClauseProjectsSb.append("ISSUE_KEY like '").append(projectKey).append("-%'");
                }
            }
            if (gf.getNotInProjects() != null && gf.getNotInProjects().iterator().hasNext())
            {
                for (String projectKey : gf.getNotInProjects())
                {
                    if (StringUtils.isBlank(projectKey))
                    {
                        continue;
                    }
                    if (whereClauseProjectsSb.length() != 0)
                    {
                        whereClauseProjectsSb.append(" AND ");
                    }
                    whereClauseProjectsSb.append("ISSUE_KEY not like '").append(projectKey).append("-%'");
                }
            }

            if (gf.getInIssues() != null && gf.getInIssues().iterator().hasNext())
            {
                for (String issueKey : gf.getInIssues())
                {
                    if (StringUtils.isBlank(issueKey))
                    {
                        continue;
                    }
                    if (whereClauseIssueKyesSb.length() != 0)
                    {
                        whereClauseIssueKyesSb.append(" OR ");
                    }
                    whereClauseIssueKyesSb.append("ISSUE_KEY like '").append(issueKey.toUpperCase()).append("'");
                }
            }
            if (gf.getNotInIssues() != null && gf.getNotInIssues().iterator().hasNext())
            {
                for (String issueKey : gf.getNotInIssues())
                {
                    if (StringUtils.isBlank(issueKey))
                    {
                        continue;
                    }
                    if (whereClauseIssueKyesSb.length() != 0)
                    {
                        whereClauseIssueKyesSb.append(" AND ");
                    }
                    whereClauseIssueKyesSb.append("ISSUE_KEY not like '").append(issueKey.toUpperCase()).append("'");
                }
            }

            if (gf.getInUsers() != null && gf.getInUsers().iterator().hasNext())
            {
                for (String username : gf.getInUsers())
                {
                    if (StringUtils.isBlank(username))
                    {
                        continue;
                    }
                    if (whereClauseUsersSb.length() != 0)
                    {
                        whereClauseUsersSb.append(" OR ");
                    }
                    whereClauseUsersSb.append("AUTHOR like '").append(username).append("'");
                }
            }
            if (gf.getNotInUsers() != null && gf.getNotInUsers().iterator().hasNext())
            {
                for (String username : gf.getNotInUsers())
                {
                    if (StringUtils.isBlank(username))
                    {
                        continue;
                    }
                    if (whereClauseUsersSb.length() != 0)
                    {
                        whereClauseUsersSb.append(" AND ");
                    }
                    whereClauseUsersSb.append("AUTHOR not like '").append(username).append("'");
                }
            }
        }
        StringBuilder whereClauseSb = new StringBuilder();
        if (whereClauseProjectsSb.length() != 0)
        {
            whereClauseSb.append("(").append(whereClauseProjectsSb.toString()).append(")");
        }
        if (whereClauseIssueKyesSb.length() != 0)
        {
            if (whereClauseSb.length() != 0)
            {
                whereClauseSb.append(" AND ");
            }
            whereClauseSb.append("(").append(whereClauseIssueKyesSb.toString()).append(")");
        }
        if (whereClauseUsersSb.length() != 0)
        {
            if (whereClauseSb.length() != 0)
            {
                whereClauseSb.append(" AND ");
            }
            whereClauseSb.append("(").append(whereClauseUsersSb.toString()).append(")");
        }

        // if no filter applyied than "no" where clause should be used
        if (whereClauseSb.length() == 0)
        {
            whereClauseSb.append(" true ");
        }
        return whereClauseSb.toString();
    }
}<commit_msg>BBC-166: performance tunnig in building where clause for activity stream filter<commit_after>package com.atlassian.jira.plugins.dvcs.dao.impl;

import com.atlassian.jira.plugins.dvcs.model.GlobalFilter;
import org.apache.commons.lang.StringUtils;

/**
 *
 */
public class GlobalFilterQueryWhereClauseBuilder
{
    private GlobalFilter gf;

    public GlobalFilterQueryWhereClauseBuilder(GlobalFilter gf)
    {
        this.gf = gf;
    }

    public String build()
    {
        StringBuilder whereClauseProjectsSb = new StringBuilder();
        StringBuilder whereClauseIssueKyesSb = new StringBuilder();
        StringBuilder whereClauseUsersSb = new StringBuilder();
        if (gf != null)
        {

            if (gf.getInProjects() != null && gf.getInProjects().iterator().hasNext())
            {
                whereClauseProjectsSb.append("PROJECT_KEY in ").append(joinStrigsToSet(gf.getInProjects())).append(" ");
            }
            if (gf.getNotInProjects() != null && gf.getNotInProjects().iterator().hasNext())
            {
                if (whereClauseProjectsSb.length() != 0)
                {
                    whereClauseProjectsSb.append(" AND ");
                }


                whereClauseProjectsSb.append("PROJECT_KEY not in ").append(joinStrigsToSet(gf.getNotInProjects())).append(" ");
            }

            if (gf.getInIssues() != null && gf.getInIssues().iterator().hasNext())
            {
                whereClauseIssueKyesSb.append("ISSUE_KEY in ").append(joinStrigsToSet(gf.getInIssues())).append(" ");
            }
            if (gf.getNotInIssues() != null && gf.getNotInIssues().iterator().hasNext())
            {
                if (whereClauseIssueKyesSb.length() != 0)
                {
                    whereClauseIssueKyesSb.append(" AND ");
                }


                whereClauseIssueKyesSb.append("ISSUE_KEY not in ").append(joinStrigsToSet(gf.getNotInIssues())).append(" ");
            }

            if (gf.getInUsers() != null && gf.getInUsers().iterator().hasNext())
            {
                whereClauseUsersSb.append("AUTHOR in ").append(joinStrigsToSet(gf.getInUsers())).append(" ");
            }
            if (gf.getNotInUsers() != null && gf.getNotInUsers().iterator().hasNext())
            {
                whereClauseUsersSb.append("AUTHOR not in ").append(joinStrigsToSet(gf.getInUsers())).append(" ");
            }
        }
        StringBuilder whereClauseSb = new StringBuilder();
        if (whereClauseProjectsSb.length() != 0)
        {
            whereClauseSb.append("(").append(whereClauseProjectsSb.toString()).append(")");
        }
        if (whereClauseIssueKyesSb.length() != 0)
        {
            if (whereClauseSb.length() != 0)
            {
                whereClauseSb.append(" AND ");
            }
            whereClauseSb.append("(").append(whereClauseIssueKyesSb.toString()).append(")");
        }
        if (whereClauseUsersSb.length() != 0)
        {
            if (whereClauseSb.length() != 0)
            {
                whereClauseSb.append(" AND ");
            }
            whereClauseSb.append("(").append(whereClauseUsersSb.toString()).append(")");
        }

        // if no filter applyied than "no" where clause should be used
        if (whereClauseSb.length() == 0)
        {
            whereClauseSb.append(" true ");
        }
        return whereClauseSb.toString();
    }

    private StringBuilder joinStrigsToSet(Iterable<String> strings)
    {
        StringBuilder builder = new StringBuilder("(");
        for (String string : strings)
        {
            if(StringUtils.isEmpty(string)) {
                continue;
            }
            if (builder.length() > 1) {
                builder.append(", ");
            }
            builder.append("'").append(string).append("'");
        }
        builder.append(")");

        return builder;
    }
}
{'fraction_non_alphanumeric': 0.12357004101014461, 'fraction_numerical': 0.0025901143967191884, 'mean_word_length': 1.4698827292110874, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6352219', 'n_tokens_mistral': 2568, 'n_tokens_neox': 2412, 'n_words': 362}