001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.execute.OrderableAggregator
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.sql.execute;
023:
024: import org.apache.derby.iapi.services.sanity.SanityManager;
025: import org.apache.derby.iapi.sql.execute.ExecAggregator;
026: import org.apache.derby.iapi.types.DataValueDescriptor;
027: import org.apache.derby.iapi.error.StandardException;
028: import org.apache.derby.iapi.services.io.Formatable;
029:
030: import java.io.ObjectOutput;
031: import java.io.ObjectInput;
032: import java.io.IOException;
033:
034: /**
035: * Abstract aggregator for Orderable aggregates (max/min).
036: *
037: * @author jamie
038: */
039: abstract class OrderableAggregator extends SystemAggregator {
040: protected DataValueDescriptor value;
041:
042: /**
043: */
044: public void setup(String aggregateName) {
045: }
046:
047: /**
048: * @see ExecAggregator#merge
049: *
050: * @exception StandardException on error
051: */
052: public void merge(ExecAggregator addend) throws StandardException {
053: if (SanityManager.DEBUG) {
054: SanityManager
055: .ASSERT(addend instanceof OrderableAggregator,
056: "addend is supposed to be the same type of aggregator for the merge operator");
057: }
058:
059: // Don't bother merging if the other has never been used.
060: DataValueDescriptor bv = ((OrderableAggregator) addend).value;
061: if (bv != null)
062: this .accumulate(bv);
063: }
064:
065: /**
066: * Return the result of the operations that we
067: * have been performing. Returns a DataValueDescriptor.
068: *
069: * @return the result as a DataValueDescriptor
070: */
071: public DataValueDescriptor getResult() throws StandardException {
072: return value;
073: }
074:
075: /////////////////////////////////////////////////////////////
076: //
077: // EXTERNALIZABLE INTERFACE
078: //
079: /////////////////////////////////////////////////////////////
080: /**
081: * Although we are not expected to be persistent per se,
082: * we may be written out by the sorter temporarily. So
083: * we need to be able to write ourselves out and read
084: * ourselves back in. We rely on formatable to handle
085: * situations where <I>value</I> is null.
086: * <p>
087: * Why would we be called to write ourselves out if we
088: * are null? For scalar aggregates, we don't bother
089: * setting up the aggregator since we only need a single
090: * row. So for a scalar aggregate that needs to go to
091: * disk, the aggregator might be null.
092: *
093: * @exception IOException on error
094: *
095: * @see java.io.Externalizable#writeExternal
096: */
097: public void writeExternal(ObjectOutput out) throws IOException {
098: super .writeExternal(out);
099: out.writeObject(value);
100: }
101:
102: /**
103: * @see java.io.Externalizable#readExternal
104: *
105: * @exception IOException on error
106: * @exception ClassNotFoundException on error
107: */
108: public void readExternal(ObjectInput in) throws IOException,
109: ClassNotFoundException {
110: super .readExternal(in);
111: value = (DataValueDescriptor) in.readObject();
112: }
113: }
|