/* // $Id: //guest/julian_hyde/saffron/src/main/saffron/rel/convert/Converter.java#1 $ // Saffron preprocessor and data engine // Copyright (C) 2002 Julian Hyde <julian.hyde@mail.com> // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Library General Public // License as published by the Free Software Foundation; either // version 2 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 // Library General Public License for more details. // // You should have received a copy of the GNU Library 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. // // See the COPYING file located in the top-level-directory of // the archive of this library for complete text of license. */ package saffron.rel.convert; import openjava.mop.OJClass; import saffron.Planner; import saffron.opt.CallingConvention; import saffron.opt.Cluster; import saffron.opt.Cost; import saffron.opt.Implementor; import saffron.rel.Rel; import saffron.rel.java.ExistsConverter; import saffron.rel.plan.PlanConverter; import saffron.util.Util; /** * <code>Converter</code> does nothing except convert its input from one * calling convention to another. Sometimes this conversion is expensive; * for example, to convert a non-distinct to a distinct object stream, we * have to clone every object in the input. **/ public abstract class Converter extends Rel { protected Rel child; protected int inConvention; protected Converter(Cluster cluster, Rel child, int inConvention) { super(cluster); this.child = child; this.inConvention = inConvention; Util.assert(child.getConvention() == inConvention); } // override Object public boolean equals(Object o) { if (getClass() != o.getClass()) { return false; } Converter other = (Converter) o; return this.inConvention == other.inConvention && this.child.equals((Object) other.child); } // override Object public int hashCode() { return Util.hash(child.hashCode(), inConvention); } // implement Rel protected OJClass deriveRowType() { return child.getRowType(); } // implement Rel public Rel[] getInputs() { return new Rel[] {child}; } // implement Rel public void replaceInput(int ordinalInParent, Rel rel) { Util.assert(ordinalInParent == 0); child = rel; } // implement Rel public Cost computeSelfCost(Planner planner) { double dRows = child.getRows(), dCpu = child.getRows(), dIo = 0; return planner.makeCost(dRows, dCpu, dIo); } public abstract int getConvention(); /** * Return whether it is possible to convert from * <code>inConvention</code> to <code>outConvention</code>. */ public static boolean canConvert(int inConvention, int outConvention) { switch (outConvention) { case CallingConvention.NONE: return NoneConverter.canConvert(inConvention); // case CallingConvention.PLAN: // return PlanConverter.canConvert(inConvention); case CallingConvention.ITERATOR: return IterConverter.canConvert(inConvention); case CallingConvention.JAVA: return JavaConverter.canConvert(inConvention); case CallingConvention.ARRAY: return ArrayConverter.canConvert(inConvention); case CallingConvention.COLLECTION: return CollectionConverter.canConvert(inConvention); case CallingConvention.VECTOR: return VectorConverter.canConvert(inConvention); case CallingConvention.ENUMERATION: return EnumerationConverter.canConvert(inConvention); case CallingConvention.EXISTS: return ExistsConverter.canConvert(inConvention); default: return false; } } public static Converter create( Cluster cluster, Rel child, int inConvention, int outConvention) { Util.assert(inConvention == child.getConvention()); Util.assert(canConvert(inConvention, outConvention)); switch (outConvention) { case CallingConvention.NONE: return new NoneConverter(cluster, child, inConvention); // case CallingConvention.PLAN: // return new PlanConverter(cluster, child, inConvention); case CallingConvention.ITERATOR: return new IterConverter(cluster, child, inConvention); case CallingConvention.JAVA: return new JavaConverter(cluster, child, inConvention); case CallingConvention.ARRAY: return new ArrayConverter(cluster, child, inConvention); case CallingConvention.COLLECTION: return new CollectionConverter(cluster, child, inConvention); case CallingConvention.VECTOR: return new VectorConverter(cluster, child, inConvention); case CallingConvention.ENUMERATION: return new EnumerationConverter(cluster, child, inConvention); case CallingConvention.EXISTS: return new ExistsConverter(cluster, child, inConvention); default: throw new UnsupportedOperationException(); } } // implement Rel public Object implement(Implementor implementor, int ordinal) { switch (inConvention) { case CallingConvention.ITERATOR: return implementIterator(implementor, ordinal); case CallingConvention.JAVA: return implementJava(implementor, ordinal); case CallingConvention.COLLECTION: return implementCollection(implementor, ordinal); case CallingConvention.VECTOR: return implementVector(implementor, ordinal); case CallingConvention.ARRAY: return implementArray(implementor, ordinal); case CallingConvention.ENUMERATION: return implementEnumeration(implementor, ordinal); case CallingConvention.MAP: return implementMap(implementor, ordinal); case CallingConvention.HASHTABLE: return implementHashtable(implementor, ordinal); default: throw newCannotHandle(); } } protected Object implementIterator(Implementor implementor, int ordinal) { throw newCannotHandle(); } protected Object implementJava(Implementor implementor, int ordinal) { throw newCannotHandle(); } protected Object implementCollection(Implementor implementor, int ordinal) { throw newCannotHandle(); } protected Object implementVector(Implementor implementor, int ordinal) { throw newCannotHandle(); } protected Object implementArray(Implementor implementor, int ordinal) { throw newCannotHandle(); } protected Object implementEnumeration(Implementor implementor, int ordinal) { throw newCannotHandle(); } protected Object implementMap(Implementor implementor, int ordinal) { throw newCannotHandle(); } protected Object implementHashtable(Implementor implementor, int ordinal) { throw newCannotHandle(); } protected Error newCannotHandle() { return Util.newInternal( getClass() + " cannot handle inConvention=" + inConvention); } static Class mapConventionToClass(int convention) { switch (convention) { case CallingConvention.PLAN: return PlanConverter.class; case CallingConvention.ARRAY: return ArrayConverter.class; case CallingConvention.JAVA: return JavaConverter.class; default: return null; } } } // End Converter.java
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 1748 | Julian Hyde |
saffron: convert unit tests to JUnit; add CallingConvention.ITERABLE; lots of other stuff; release 0.1. |
||
#1 | 1467 | Julian Hyde |
saffron: First saffron check-in; incorporate my changes to openjava. |