<commit_before>package org.intermine.webservice.server.query;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import org.intermine.pathquery.PathQuery;
import org.intermine.pathquery.PathQueryBinding;
import org.intermine.webservice.server.exceptions.BadRequestException;

public class QueryStore {
    private static List<String> queries = new ArrayList<String>(1024);

    public static int putQuery(String xml) {
        try {
            PathQueryBinding.unmarshalPathQuery(new StringReader(xml), PathQuery.USERPROFILE_VERSION);
        } catch (Exception e) {
            String message = "XML is not well formatted.";
            throw new BadRequestException(message, e);
        }
        queries.add(xml);
        return queries.size() - 1;
    }

    public static String getQuery(String key) {
        int idx;
        try {
            idx = Integer.parseInt(key, 10);
        } catch (NumberFormatException e) {
            throw new BadRequestException("qid must be an integer", e);
        }
        String ret = queries.get(idx);
        if (ret == null) {
            throw new BadRequestException("qid " + key + " not in QueryStore");
        }
        return ret;
    }<commit_msg>Added the facilities for efficient bi-directional lookup.<commit_after>package org.intermine.webservice.server.query;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import org.intermine.pathquery.PathQuery;
import org.intermine.pathquery.PathQueryBinding;
import org.intermine.webservice.server.exceptions.BadRequestException;

public class QueryStore {
    private static final List<String> queries = new ArrayList<String>();
    private static final Map<String, Integer> indexes = new HashMap<String, Integer>();

    public static int putQuery(String xml) {
        Integer id = indexes.get(xml);
        if (id != null) {
            return id;
        }
        try {
            PathQueryBinding.unmarshalPathQuery(new StringReader(xml), PathQuery.USERPROFILE_VERSION);
        } catch (Exception e) {
            String message = "XML is not well formatted.";
            throw new BadRequestException(message, e);
        }
        id = Integer.valueOf(queries.size());
        queries.add(xml);
        indexes.put(xml, id);
        return id.intValue();
    }

    public static String getQuery(String key) {
        Integer id;
        try {
            id = Integer.valueOf(key, 10);
        } catch (NumberFormatException e) {
            throw new BadRequestException("qid must be an integer", e);
        }
        try {
            return queries.get(id);
        } catch (IndexOutOfBoundsException e) {
            throw new BadRequestException("qid " + key + " not in QueryStore");
        }
    }
{'fraction_non_alphanumeric': 0.09887005649717515, 'fraction_numerical': 0.0031779661016949155, 'mean_word_length': 2.604325699745547, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '1795608', 'n_tokens_mistral': 778, 'n_tokens_neox': 762, 'n_words': 203}