Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / MyDerby-10.3 / java / engine / org / apache / derby / impl / sql / execute / CountAggregator.java
diff --git a/JMCR-Stable/real-world application/MyDerby-10.3/java/engine/org/apache/derby/impl/sql/execute/CountAggregator.java b/JMCR-Stable/real-world application/MyDerby-10.3/java/engine/org/apache/derby/impl/sql/execute/CountAggregator.java
new file mode 100644 (file)
index 0000000..36ca3cd
--- /dev/null
@@ -0,0 +1,161 @@
+/*\r
+\r
+   Derby - Class org.apache.derby.impl.sql.execute.CountAggregator\r
+\r
+   Licensed to the Apache Software Foundation (ASF) under one or more\r
+   contributor license agreements.  See the NOTICE file distributed with\r
+   this work for additional information regarding copyright ownership.\r
+   The ASF licenses this file to you under the Apache License, Version 2.0\r
+   (the "License"); you may not use this file except in compliance with\r
+   the License.  You may obtain a copy of the License at\r
+\r
+      http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+   Unless required by applicable law or agreed to in writing, software\r
+   distributed under the License is distributed on an "AS IS" BASIS,\r
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+   See the License for the specific language governing permissions and\r
+   limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.impl.sql.execute;\r
+\r
+import org.apache.derby.iapi.types.DataValueDescriptor;\r
+import org.apache.derby.iapi.services.sanity.SanityManager;\r
+import org.apache.derby.iapi.error.StandardException;\r
+\r
+import org.apache.derby.iapi.sql.execute.ExecAggregator;\r
+import org.apache.derby.iapi.services.io.StoredFormatIds;\r
+import org.apache.derby.iapi.services.io.Formatable;\r
+import java.io.ObjectOutput;\r
+import java.io.ObjectInput;\r
+import java.io.IOException;\r
+\r
+/**\r
+ * Aggregator for COUNT()/COUNT(*).  \r
+ */\r
+public final class CountAggregator \r
+       extends SystemAggregator\r
+{\r
+       private long value;\r
+       private boolean isCountStar;\r
+\r
+       /**\r
+        */\r
+       public void setup(String aggregateName)\r
+       {\r
+               isCountStar = aggregateName.equals("COUNT(*)");\r
+       }\r
+\r
+       /**\r
+        * @see ExecAggregator#merge\r
+        *\r
+        * @exception   StandardException       on error\r
+        */\r
+       public void merge(ExecAggregator addend)\r
+               throws StandardException\r
+       {\r
+               if (SanityManager.DEBUG)\r
+               {\r
+                       SanityManager.ASSERT(addend instanceof CountAggregator,\r
+                               "addend is supposed to be the same type of aggregator for the merge operator");\r
+               }\r
+\r
+               value += ((CountAggregator)addend).value;\r
+       }\r
+\r
+       /**\r
+        * Return the result of the aggregation.  Just\r
+        * spit out the running count.\r
+        *\r
+        * @return the value as a Long \r
+        */\r
+       public DataValueDescriptor getResult()\r
+       {\r
+               return new org.apache.derby.iapi.types.SQLLongint(value);\r
+       }\r
+\r
+\r
+       /**\r
+        * Accumulate for count().  Toss out all nulls in this kind of count.\r
+        * Increment the count for count(*). Count even the null values.\r
+        *\r
+        * @param addend        value to be added in\r
+        * @param ga            the generic aggregator that is calling me\r
+        *\r
+        * @see ExecAggregator#accumulate\r
+        */\r
+       public void accumulate(DataValueDescriptor addend, Object ga)\r
+               throws StandardException\r
+       {\r
+               if (isCountStar)\r
+                       value++;\r
+               else\r
+                       super.accumulate(addend, ga);\r
+       }\r
+\r
+       protected final void accumulate(DataValueDescriptor addend) {\r
+                       value++;\r
+       }\r
+\r
+       /**\r
+        * @return ExecAggregator the new aggregator\r
+        */\r
+       public ExecAggregator newAggregator()\r
+       {\r
+               CountAggregator ca = new CountAggregator();\r
+               ca.isCountStar = isCountStar;\r
+               return ca;\r
+       }\r
+\r
+       public boolean isCountStar()\r
+       {\r
+               return isCountStar;\r
+       }\r
+\r
+       /////////////////////////////////////////////////////////////\r
+       // \r
+       // EXTERNALIZABLE INTERFACE\r
+       // \r
+       /////////////////////////////////////////////////////////////\r
+       /** \r
+        * Although we are not expected to be persistent per se,\r
+        * we may be written out by the sorter temporarily.  So\r
+        * we need to be able to write ourselves out and read\r
+        * ourselves back in.  \r
+        * \r
+        * @exception IOException thrown on error\r
+        */\r
+       public final void writeExternal(ObjectOutput out) throws IOException\r
+       {\r
+               super.writeExternal(out);\r
+               out.writeBoolean(isCountStar);\r
+               out.writeLong(value);\r
+       }\r
+\r
+       /** \r
+       * @see java.io.Externalizable#readExternal \r
+       *\r
+       * @exception IOException io exception\r
+       * @exception ClassNotFoundException on error\r
+       */\r
+       public final void readExternal(ObjectInput in) \r
+               throws IOException, ClassNotFoundException\r
+       {\r
+               super.readExternal(in);\r
+               isCountStar = in.readBoolean();\r
+               value = in.readLong();\r
+       }       \r
+       /////////////////////////////////////////////////////////////\r
+       // \r
+       // FORMATABLE INTERFACE\r
+       // \r
+       /////////////////////////////////////////////////////////////\r
+       /**\r
+        * Get the formatID which corresponds to this class.\r
+        *\r
+        *      @return the formatID of this class\r
+        */\r
+       public  int     getTypeFormatId() { return StoredFormatIds.AGG_COUNT_V01_ID; }\r
+}\r