<commit_before>//
// $Id$
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001-2008 <NAME>
// 
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package com.samskivert.jdbc.depot;

import java.util.Collection;

import com.samskivert.jdbc.depot.expression.ExpressionVisitor;
import com.samskivert.jdbc.depot.expression.LiteralExp;
import com.samskivert.jdbc.depot.expression.SQLExpression;
import com.samskivert.jdbc.depot.operator.Conditionals;
import com.samskivert.jdbc.depot.operator.Logic;

/**
 * Contains a set of primary keys that match a set of persistent records. This is used internally
 * in Depot when decomposing queries into two parts: first a query for the primary keys that
 * identify the records that match a free-form query and then another query that operates on the
 * previously identified keys. The keys obtained in the first query are used to create a KeySet and
 * modifications and deletons using this set will automatically flush the appropriate records from
 * the cache.
 */
public class KeySet<T extends PersistentRecord> extends WhereClause
    implements SQLExpression, ValidatingCacheInvalidator
{
    /**
     * Creates a set from the supplied primary keys.
     */
    public KeySet (Class<T> pClass, Collection<Key<T>> keys)
    {
        _pClass = pClass;
        _keys = keys;

        String[] keyFields = KeyUtil.getKeyFields(pClass);
        if (keys.size() == 0) {
            _condition = new LiteralExp("false");

        } else if (keyFields.length == 1) {
            if (keys.size() > Conditionals.In.MAX_KEYS) {
                throw new IllegalArgumentException("Cannot create where clause for more than " +
                                                   Conditionals.In.MAX_KEYS + " at a time.");
            }

            // Single-column keys result in the compact IN(keyVal1, keyVal2, ...)
            
            Comparable<?>[] keyFieldValues = new Comparable<?>[keys.size()];
            int ii = 0;
            for (Key<T> key : keys) {
                keyFieldValues[ii++] = key.getValues().get(0);
            }
            _condition = new Conditionals.In(pClass, keyFields[0], keyFieldValues);

        } else {
            // TODO: is there a maximum size of an or query? 32768?

            // Multi-column keys result in OR'd AND's, of unknown efficiency (TODO check).
            SQLExpression[] keyArray = new SQLExpression[keys.size()];
            int ii = 0;
            for (Key<T> key : keys) {
                keyArray[ii ++] = key.getWhereExpression();<commit_msg>We need to use the key in our or clause, not its non-existent where expression.<commit_after>//
// $Id$
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001-2008 <NAME>
// 
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package com.samskivert.jdbc.depot;

import java.util.Collection;

import com.samskivert.jdbc.depot.expression.ExpressionVisitor;
import com.samskivert.jdbc.depot.expression.LiteralExp;
import com.samskivert.jdbc.depot.expression.SQLExpression;
import com.samskivert.jdbc.depot.operator.Conditionals;
import com.samskivert.jdbc.depot.operator.Logic;

/**
 * Contains a set of primary keys that match a set of persistent records. This is used internally
 * in Depot when decomposing queries into two parts: first a query for the primary keys that
 * identify the records that match a free-form query and then another query that operates on the
 * previously identified keys. The keys obtained in the first query are used to create a KeySet and
 * modifications and deletons using this set will automatically flush the appropriate records from
 * the cache.
 */
public class KeySet<T extends PersistentRecord> extends WhereClause
    implements SQLExpression, ValidatingCacheInvalidator
{
    /**
     * Creates a set from the supplied primary keys.
     */
    public KeySet (Class<T> pClass, Collection<Key<T>> keys)
    {
        _pClass = pClass;
        _keys = keys;

        String[] keyFields = KeyUtil.getKeyFields(pClass);
        if (keys.size() == 0) {
            _condition = new LiteralExp("false");

        } else if (keyFields.length == 1) {
            if (keys.size() > Conditionals.In.MAX_KEYS) {
                throw new IllegalArgumentException("Cannot create where clause for more than " +
                                                   Conditionals.In.MAX_KEYS + " at a time.");
            }

            // Single-column keys result in the compact IN(keyVal1, keyVal2, ...)
            Comparable<?>[] keyFieldValues = new Comparable<?>[keys.size()];
            int ii = 0;
            for (Key<T> key : keys) {
                keyFieldValues[ii++] = key.getValues().get(0);
            }
            _condition = new Conditionals.In(pClass, keyFields[0], keyFieldValues);

        } else {
            // TODO: is there a maximum size of an or query? 32768?

            // Multi-column keys result in OR'd AND's, of unknown efficiency (TODO check).
            SQLExpression[] keyArray = new SQLExpression[keys.size()];
            int ii = 0;
            for (Key<T> key : keys) {
                keyArray[ii++] = key;
{'fraction_non_alphanumeric': 0.09011761111959676, 'fraction_numerical': 0.011302886818390102, 'mean_word_length': 2.6581005586592177, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3862409', 'n_tokens_mistral': 1786, 'n_tokens_neox': 1700, 'n_words': 699}