<commit_before>package org.intermine.api.config;

/*
 * Copyright (C) 2002-2012 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.intermine.metadata.ClassDescriptor;
import org.intermine.metadata.FieldDescriptor;
import org.intermine.metadata.Model;
import org.intermine.model.FastPathObject;
import org.intermine.util.DynamicUtil;
import org.intermine.util.TypeUtil;

/**
 * Methods to read and manage keys for classes. Keys define how certain classes
 * are identified and are used in defining bag creation.
 *
 * @author rns
 *
 */
public final class ClassKeyHelper
{
    private ClassKeyHelper() {
        // don't instantiate
    }

    private static final Logger LOG = Logger.getLogger(ClassKeyHelper.class);

    /**
     * Read class keys from a properties into a map from classname to set of
     * available keys.
     *
     * @param model
     *            the data model
     * @param props
     *            a properties object describing class keys
     * @return map from class name to set of available keys
     */
    public static Map<String, List<FieldDescriptor>> readKeys(Model model, Properties props) {
        Map<String, List<FieldDescriptor>> classKeys = new HashMap<String, List<FieldDescriptor>>();
        for (ClassDescriptor cld : model.getTopDownLevelTraversal()) {
            String clsName = cld.getUnqualifiedName();
            if (props.containsKey(cld.getUnqualifiedName())) {
                String keys = (String) props.get(clsName);
                String[] tokens = keys.split(",");
                for (String token : tokens) {
                    String keyString = token.trim();
                    FieldDescriptor fld = cld.getFieldDescriptorByName(keyString);
                    if (fld != null) {
                        ClassKeyHelper.addKey(classKeys, clsName, fld);
                        for (ClassDescriptor subCld : model.getAllSubs(cld)) {
                            ClassKeyHelper.addKey(classKeys, subCld.getUnqualifiedName(), fld);
                        }
                    } else {
                        LOG.warn("problem loading class key: " + keyString
                                + " for class " + clsName);
                    }
                }
            } else {
                LOG.warn("No key defined for " + clsName);
            }
        }
        return classKeys;<commit_msg>Don't do I/O every time the ClassKeys are retrieved by caching the calculated values<commit_after>package org.intermine.api.config;

/*
 * Copyright (C) 2002-2012 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.intermine.metadata.ClassDescriptor;
import org.intermine.metadata.FieldDescriptor;
import org.intermine.metadata.Model;
import org.intermine.model.FastPathObject;
import org.intermine.util.DynamicUtil;
import org.intermine.util.TypeUtil;

/**
 * Methods to read and manage keys for classes. Keys define how certain classes
 * are identified and are used in defining bag creation.
 *
 * @author rns
 *
 */
public final class ClassKeyHelper
{
    private ClassKeyHelper() {
        // don't instantiate
    }

    private static final Logger LOG = Logger.getLogger(ClassKeyHelper.class);

    private static final Map<Model, Map<String, List<FieldDescriptor>>> classKeys =
        new HashMap<Model, Map<String, List<FieldDescriptor>>>();

    /**
     * Read class keys from a properties into a map from classname to set of
     * available keys.
     *
     * @param model
     *            the data model
     * @param props
     *            a properties object describing class keys
     * @return map from class name to set of available keys
     */
    public static Map<String, List<FieldDescriptor>> readKeys(Model model, Properties props) {
        if (!classKeys.containsKey(model)) {
            Map<String, List<FieldDescriptor>> theseKeys = new HashMap<String, List<FieldDescriptor>>();
            for (ClassDescriptor cld : model.getTopDownLevelTraversal()) {
                String clsName = cld.getUnqualifiedName();
                if (props.containsKey(cld.getUnqualifiedName())) {
                    String keys = (String) props.get(clsName);
                    String[] tokens = keys.split(",");
                    for (String token : tokens) {
                        String keyString = token.trim();
                        FieldDescriptor fld = cld.getFieldDescriptorByName(keyString);
                        if (fld != null) {
                            ClassKeyHelper.addKey(theseKeys, clsName, fld);
                            for (ClassDescriptor subCld : model.getAllSubs(cld)) {
                                ClassKeyHelper.addKey(theseKeys, subCld.getUnqualifiedName(), fld);
                            }
                        } else {
                            LOG.warn("problem loading class key: " + keyString
                                    + " for class " + clsName);
                        }
                    }
                } else {
                    LOG.warn("No key defined for " + clsName);
                }
                classKeys.put(model, theseKeys);
            }
        }
        return classKeys.get(model);
{'fraction_non_alphanumeric': 0.09052667003197039, 'fraction_numerical': 0.003028773346794548, 'mean_word_length': 2.07819782496116, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5527801', 'n_tokens_mistral': 1572, 'n_tokens_neox': 1508, 'n_words': 449}