From b8ac3750ebd1e5667b62a113a2f9b294b1b373b4 Mon Sep 17 00:00:00 2001 From: Par Winzell Date: Mon, 13 Apr 2009 15:56:00 +0000 Subject: [PATCH] The MultiKey is a lot of complicated code to optimize an obscure situation. I know it's obscure because there's not a single use of this class anywhere that I can find. --- src/java/com/samskivert/depot/MultiKey.java | 159 ------------------ .../samskivert/depot/impl/BuildVisitor.java | 40 ----- .../depot/impl/ExpressionVisitor.java | 2 - 3 files changed, 201 deletions(-) delete mode 100644 src/java/com/samskivert/depot/MultiKey.java diff --git a/src/java/com/samskivert/depot/MultiKey.java b/src/java/com/samskivert/depot/MultiKey.java deleted file mode 100644 index 7fa6be9..0000000 --- a/src/java/com/samskivert/depot/MultiKey.java +++ /dev/null @@ -1,159 +0,0 @@ -// -// $Id$ -// -// Depot library - a Java relational persistence library -// Copyright (C) 2006-2008 Michael Bayne and Pär Winzell -// -// 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.depot; - -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import com.google.common.collect.Maps; - -import com.samskivert.depot.clause.Where; -import com.samskivert.depot.clause.WhereClause; -import com.samskivert.depot.expression.SQLExpression; -import com.samskivert.depot.impl.ExpressionVisitor; - -/** - * A special form of {@link Where} clause that specifies an explicit range of database rows. It - * does not implement {@link CacheKey} but it does implement {@link CacheInvalidator} which means - * it can be sent into e.g. {@link DepotRepository#deleteAll} and have it clean up after itself. - */ -public class MultiKey extends WhereClause - implements ValidatingCacheInvalidator -{ - /** - * Constructs a new single-column {@code MultiKey} with the given value range. - */ - public MultiKey (Class pClass, String ix, Comparable... val) - { - this(pClass, new String[0], new Comparable[0], ix, val); - } - - /** - * Constructs a new two-column {@code MultiKey} with the given value range. - */ - public MultiKey (Class pClass, String ix1, Comparable val1, String ix2, - Comparable... val2) - { - this(pClass, new String[] { ix1 }, new Comparable[] { val1 }, ix2, val2); - } - - /** - * Constructs a new three-column {@code MultiKey} with the given value range. - */ - public MultiKey (Class pClass, String ix1, Comparable val1, - String ix2, Comparable val2, String ix3, Comparable... val3) - { - this(pClass, new String[] { ix1, ix2 }, new Comparable[] { val1, val2 }, ix3, val3); - } - - /** - * Constructs a new multi-column {@code MultiKey} with the given value range. - * See {@link Key#Key(Class,String[],Comparable[])} for somewhat relevant comments. - */ - public MultiKey (Class pClass, String[] sFields, Comparable[] sValues, - String mField, Comparable[] mValues) - { - if (sFields.length != sValues.length) { - throw new IllegalArgumentException( - "Key field and values arrays must be of equal length."); - } - _pClass = pClass; - _mField = mField; - _mValues = mValues; - _map = Maps.newHashMap(); - for (int i = 0; i < sFields.length; i ++) { - _map.put(sFields[i], sValues[i]); - } - } - - public Class getPersistentClass () - { - return _pClass; - } - - public Map> getSingleFieldsMap () - { - return _map; - } - - public String getMultiField () - { - return _mField; - } - - public Comparable[] getMultiValues () - { - return _mValues; - } - - // from WhereClause - public SQLExpression getWhereExpression () - { - throw new RuntimeException("Not used"); - } - - // from SQLExpression - public void accept (ExpressionVisitor builder) - { - builder.visit(this); - } - - // from SQLExpression - public void addClasses (Collection> classSet) - { - // nothing to add - } - - // from ValidatingCacheInvalidator - public void validateFlushType (Class pClass) - { - if (!pClass.equals(_pClass)) { - throw new IllegalArgumentException( - "Class mismatch between persistent record and cache invalidator " + - "[record=" + pClass.getSimpleName() + - ", invtype=" + _pClass.getSimpleName() + "]."); - } - } - - // from CacheInvalidator - public void invalidate (PersistenceContext ctx) - { - // must be a hashmap for serializability - HashMap> newMap = Maps.newHashMap(_map); - for (Comparable value : _mValues) { - newMap.put(_mField, value); - ctx.cacheInvalidate(new SimpleCacheKey(_pClass, newMap)); - } - } - - @Override // from WhereClause - public void validateQueryType (Class pClass) - { - super.validateQueryType(pClass); - validateTypesMatch(pClass, _pClass); - } - - protected String _mField; - protected Comparable[] _mValues; - protected Class _pClass; - protected Map> _map; -} diff --git a/src/java/com/samskivert/depot/impl/BuildVisitor.java b/src/java/com/samskivert/depot/impl/BuildVisitor.java index c36af9e..dab10e6 100644 --- a/src/java/com/samskivert/depot/impl/BuildVisitor.java +++ b/src/java/com/samskivert/depot/impl/BuildVisitor.java @@ -33,7 +33,6 @@ import com.samskivert.util.Tuple; import com.samskivert.depot.ByteEnum; import com.samskivert.depot.Key; -import com.samskivert.depot.MultiKey; import com.samskivert.depot.PersistentRecord; import com.samskivert.depot.annotation.Computed; import com.samskivert.depot.clause.FieldDefinition; @@ -137,45 +136,6 @@ public abstract class BuildVisitor implements ExpressionVisitor } } - public void visit (MultiKey key) - { - _builder.append(" where "); - boolean first = true; - for (Map.Entry> entry : key.getSingleFieldsMap().entrySet()) { - if (first) { - first = false; - } else { - _builder.append(" and "); - } - // A MultiKey's WHERE clause must mirror what's actually retrieved for the persistent - // object, so we turn on overrides here just as we do when expanding SELECT fields - boolean saved = _enableOverrides; - _enableOverrides = true; - appendRhsColumn(key.getPersistentClass(), entry.getKey()); - _enableOverrides = saved; - if (entry.getValue() == null) { - _builder.append(" is null "); - } else { - _builder.append(" = "); - bindValue(entry.getValue()); - } - } - if (!first) { - _builder.append(" and "); - } - appendRhsColumn(key.getPersistentClass(), key.getMultiField()); - _builder.append(" in ("); - - Comparable[] values = key.getMultiValues(); - for (int ii = 0; ii < values.length; ii ++) { - if (ii > 0) { - _builder.append(", "); - } - bindValue(values[ii]); - } - _builder.append(")"); - } - public void visit (FunctionExp functionExp) { _builder.append(functionExp.getFunction()); diff --git a/src/java/com/samskivert/depot/impl/ExpressionVisitor.java b/src/java/com/samskivert/depot/impl/ExpressionVisitor.java index 791b061..daa92ef 100644 --- a/src/java/com/samskivert/depot/impl/ExpressionVisitor.java +++ b/src/java/com/samskivert/depot/impl/ExpressionVisitor.java @@ -21,7 +21,6 @@ package com.samskivert.depot.impl; import com.samskivert.depot.Key; -import com.samskivert.depot.MultiKey; import com.samskivert.depot.PersistentRecord; import com.samskivert.depot.clause.FieldDefinition; @@ -81,7 +80,6 @@ public interface ExpressionVisitor public void visit (ValueExp valueExp); public void visit (WhereClause where); public void visit (Key.Expression key); - public void visit (MultiKey key); public void visit (Exists exists); public void visit (SelectClause selectClause); public void visit (UpdateClause updateClause);