<commit_before>/*
 * oxTrust is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
 *
 * Copyright (c) 2014, Gluu
 */

package org.gluu.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.gluu.model.DisplayNameEntry;
import org.gluu.persist.PersistenceEntryManager;
import org.gluu.persist.model.base.Entry;
import org.gluu.search.filter.Filter;
import org.gluu.util.OxConstants;
import org.slf4j.Logger;

/**
 * Provides operations with DisplayNameEntry
 *
 * @author <NAME> Date: 08/11/2010
 */
@ApplicationScoped
@Named
public class LookupService implements Serializable {

    private static final long serialVersionUID = -3707238475653913313L;

    @Inject
    private Logger log;

    @Inject
    private PersistenceEntryManager ldapEntryManager;

    @Inject
    private CacheService cacheService;

    /**
     * Returns DisplayNameEntry based on display name
     *
     * @param dn
     *            display name
     * @return DisplayNameEntry object
     */
    public DisplayNameEntry getDisplayNameEntry(String dn) throws Exception {
        String key = dn;
        DisplayNameEntry entry = (DisplayNameEntry) cacheService.get(OxConstants.CACHE_LOOKUP_NAME, key);
        if (entry == null) {
            entry = ldapEntryManager.find(key, DisplayNameEntry.class, null);

            cacheService.put(OxConstants.CACHE_LOOKUP_NAME, key, entry);
        }

        return entry;
    }

    /**
     * Returns list of DisplayNameEntry objects
     *
     * @param baseDn
     *            base DN
     * @param dns
     *            list of display names to find
     * @return list of DisplayNameEntry objects
     */
    @SuppressWarnings("unchecked")
    public List<DisplayNameEntry> getDisplayNameEntries(String baseDn, List<String> dns) {
        List<String> inums = getInumsFromDns(dns);
        if (inums.size() == 0) {
            return null;
        }

        String key = getCompoundKey(inums);
        List<DisplayNameEntry> entries = (List<DisplayNameEntry>) cacheService.get(OxConstants.CACHE_LOOKUP_NAME, key);
        if (entries == null) {
            Filter searchFilter = buildInumFilter(inums);
            entries = ldapEntryManager.findEntries(baseDn, DisplayNameEntry.class, searchFilter);
            cacheService.put(OxConstants.CACHE_LOOKUP_NAME, key, entries);
        }
        return entries;
    }

    public Filter buildInumFilter(List<String> inums) {
        List<Filter> inumFilters = new ArrayList<Filter>(inums.size());
        for (String inum : inums) {
            inumFilters.add(Filter.createEqualityFilter(OxConstants.INUM, inum).multiValued(false));
        }
       return Filter.createORFilter(inumFilters);
    }

    public List<String> getInumsFromDns(List<String> dns) {
        List<String> inums = new ArrayList<String>();

        if (dns == null) {
            return inums;
        }

        for (String dn : dns) {
            String inum = getInumFromDn(dn);
            if (inum != null) {
                inums.add(inum);
            }
        }

        Collections.sort(inums);

        return inums;
    }

    private String getCompoundKey(List<String> inums) {
        StringBuilder compoundKey = new StringBuilder();
        for (String inum : inums) {
            if (compoundKey.length() > 0) {
                compoundKey.append("_");
            }
            compoundKey.append(inum);
        }

        return compoundKey.toString();
    }<commit_msg>Fix lookup cache conflict with other caches<commit_after>/*
 * oxTrust is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
 *
 * Copyright (c) 2014, Gluu
 */

package org.gluu.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.gluu.model.DisplayNameEntry;
import org.gluu.persist.PersistenceEntryManager;
import org.gluu.persist.model.base.Entry;
import org.gluu.search.filter.Filter;
import org.gluu.util.OxConstants;
import org.slf4j.Logger;

/**
 * Provides operations with DisplayNameEntry
 *
 * @author <NAME> Date: 08/11/2010
 */
@ApplicationScoped
@Named
public class LookupService implements Serializable {

    private static final long serialVersionUID = -3707238475653913313L;

    @Inject
    private Logger log;

    @Inject
    private PersistenceEntryManager ldapEntryManager;

    @Inject
    private CacheService cacheService;

    /**
     * Returns DisplayNameEntry based on display name
     *
     * @param dn
     *            display name
     * @return DisplayNameEntry object
     */
    public DisplayNameEntry getDisplayNameEntry(String dn) throws Exception {
        String key = "l_" + dn;
        DisplayNameEntry entry = (DisplayNameEntry) cacheService.get(OxConstants.CACHE_LOOKUP_NAME, key);
        if (entry == null) {
            entry = ldapEntryManager.find(key, DisplayNameEntry.class, null);

            cacheService.put(OxConstants.CACHE_LOOKUP_NAME, key, entry);
        }

        return entry;
    }

    /**
     * Returns list of DisplayNameEntry objects
     *
     * @param baseDn
     *            base DN
     * @param dns
     *            list of display names to find
     * @return list of DisplayNameEntry objects
     */
    @SuppressWarnings("unchecked")
    public List<DisplayNameEntry> getDisplayNameEntries(String baseDn, List<String> dns) {
        List<String> inums = getInumsFromDns(dns);
        if (inums.size() == 0) {
            return null;
        }

        String key = getCompoundKey(inums);
        List<DisplayNameEntry> entries = (List<DisplayNameEntry>) cacheService.get(OxConstants.CACHE_LOOKUP_NAME, key);
        if (entries == null) {
            Filter searchFilter = buildInumFilter(inums);
            entries = ldapEntryManager.findEntries(baseDn, DisplayNameEntry.class, searchFilter);
            cacheService.put(OxConstants.CACHE_LOOKUP_NAME, key, entries);
        }
        return entries;
    }

    public Filter buildInumFilter(List<String> inums) {
        List<Filter> inumFilters = new ArrayList<Filter>(inums.size());
        for (String inum : inums) {
            inumFilters.add(Filter.createEqualityFilter(OxConstants.INUM, inum).multiValued(false));
        }
       return Filter.createORFilter(inumFilters);
    }

    public List<String> getInumsFromDns(List<String> dns) {
        List<String> inums = new ArrayList<String>();

        if (dns == null) {
            return inums;
        }

        for (String dn : dns) {
            String inum = getInumFromDn(dn);
            if (inum != null) {
                inums.add(inum);
            }
        }

        Collections.sort(inums);

        return inums;
    }

    private String getCompoundKey(List<String> inums) {
        StringBuilder compoundKey = new StringBuilder();
        for (String inum : inums) {
            if (compoundKey.length() > 0) {
                compoundKey.append("_");
            } else {
            	compoundKey.append("l_")
            }
            compoundKey.append(inum);
        }

        return compoundKey.toString();
    }
{'fraction_non_alphanumeric': 0.09792843691148775, 'fraction_numerical': 0.010223298358891578, 'mean_word_length': 2.538791051880057, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 33, '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': '1369968', 'n_tokens_mistral': 2240, 'n_tokens_neox': 2157, 'n_words': 489}