Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / derby-10.3.2.1 / java / engine / org / apache / derby / impl / store / raw / data / LogicalUndoOperation.java
diff --git a/JMCR-Stable/real-world application/derby-10.3.2.1/java/engine/org/apache/derby/impl/store/raw/data/LogicalUndoOperation.java b/JMCR-Stable/real-world application/derby-10.3.2.1/java/engine/org/apache/derby/impl/store/raw/data/LogicalUndoOperation.java
new file mode 100644 (file)
index 0000000..bd50c73
--- /dev/null
@@ -0,0 +1,243 @@
+/*\r
+\r
+   Derby - Class org.apache.derby.impl.store.raw.data.LogicalUndoOperation\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.store.raw.data;\r
+\r
+import org.apache.derby.impl.store.raw.data.BasePage;\r
+\r
+import org.apache.derby.iapi.services.io.FormatIdUtil;\r
+import org.apache.derby.iapi.services.io.StoredFormatIds;\r
+import org.apache.derby.iapi.services.sanity.SanityManager;\r
+\r
+import org.apache.derby.iapi.store.raw.Compensation;\r
+import org.apache.derby.iapi.store.raw.Loggable;\r
+import org.apache.derby.iapi.store.raw.Transaction;\r
+import org.apache.derby.iapi.store.raw.Undoable;\r
+\r
+import org.apache.derby.iapi.store.raw.log.LogInstant;\r
+\r
+import org.apache.derby.iapi.error.StandardException;\r
+\r
+import org.apache.derby.iapi.services.io.CompressedNumber;\r
+import org.apache.derby.iapi.util.ByteArray;\r
+\r
+import java.io.OutputStream;\r
+import java.io.ObjectOutput;\r
+import java.io.ObjectInput;\r
+import java.io.IOException;\r
+\r
+import org.apache.derby.iapi.services.io.LimitObjectInput;\r
+\r
+/**\r
+       LogicalUndoOperation is a compensation operation that rolls back the change of\r
+       an LogicalUndoable operation.  A LogicalUndoOperation itself is not undo-able, i.e,\r
+       it is loggable but not undoable.\r
+\r
+       <PRE>\r
+       @format_id      LOGOP_PAGE_LOGICAL_UNDO\r
+               the formatId is written by FormatIdOutputStream when this object is\r
+               written out by writeObject\r
+       @purpose        undo a logical log operation \r
+       @upgrade\r
+       @disk_layout\r
+               PageBasicOperation      the super class\r
+               recordId(CompressedInt) the recordId of the changed row (this may not\r
+                               be the recordId during rollback if the record moved from one\r
+                               page to another) \r
+               OptionalData    none (compensation operation never have optional data)\r
+       @end_format\r
+       </PRE>\r
+\r
+*/\r
+public class LogicalUndoOperation extends PageBasicOperation implements Compensation {\r
+\r
+       protected int recordId;                         // the record id to call undoOp.undoMe with\r
+\r
+       /** The operation to be rolled back */\r
+       transient private       LogicalPageOperation undoOp = null; \r
+\r
+       protected LogicalUndoOperation(BasePage page)\r
+       {\r
+               super(page);\r
+       }\r
+\r
+       /** Set up a compensation operation during run time rollback */\r
+       public LogicalUndoOperation(BasePage page, int recordId, LogicalPageOperation op)\r
+       {\r
+               super(page);\r
+               undoOp = op;\r
+               this.recordId = recordId;\r
+       }\r
+\r
+       /**\r
+               Return my format identifier.\r
+       */\r
+\r
+       // no-arg constructor, required by Formatable \r
+       public LogicalUndoOperation() { super(); }\r
+\r
+       public int getTypeFormatId() {\r
+               return StoredFormatIds.LOGOP_PAGE_LOGICAL_UNDO;\r
+       }\r
+\r
+       /**\r
+               Write this out.\r
+               @exception IOException error writing to log stream\r
+       */\r
+       public void writeExternal(ObjectOutput out) throws IOException \r
+       {\r
+               super.writeExternal(out);\r
+               CompressedNumber.writeInt(out, recordId);\r
+       }\r
+\r
+\r
+       /**\r
+               Read this in\r
+               @exception IOException error reading from log stream\r
+               @exception ClassNotFoundException log stream corrupted\r
+       */\r
+       public void readExternal(ObjectInput in) \r
+                throws IOException, ClassNotFoundException\r
+       {\r
+               super.readExternal(in);\r
+               recordId = CompressedNumber.readInt(in);\r
+       }\r
+\r
+       public void restoreMe(Transaction xact, BasePage undoPage,\r
+                                                 LogInstant CLRinstant, LimitObjectInput in)\r
+       {\r
+               // Not undoable\r
+               if (SanityManager.DEBUG)\r
+                       SanityManager.THROWASSERT("cannot call restore me on PhysicalUndoOperation");\r
+       }\r
+\r
+       /** \r
+               Compensation methods\r
+       */\r
+\r
+       /** Set up a LogicalOperation during recovery redo. */\r
+       public void setUndoOp(Undoable op)\r
+       {\r
+               if (SanityManager.DEBUG) {\r
+                       SanityManager.ASSERT(op instanceof LogicalPageOperation);\r
+               }\r
+\r
+               undoOp = (LogicalPageOperation)op;\r
+       }\r
+\r
+\r
+       /**\r
+               Loggable methods\r
+       */\r
+\r
+       /** Apply the undo operation, in this implementation of the\r
+               RawStore, it can only call the undoMe method of undoOp\r
+\r
+               @param xact                     the Transaction that is doing the rollback\r
+               @param instant          the log instant of this undo operation\r
+               @param in                       optional data\r
+\r
+               @exception IOException Can be thrown by any of the methods of ObjectInput.\r
+               @exception StandardException Standard Derby policy.\r
+\r
+        */\r
+       public final void doMe(Transaction xact, LogInstant instant, LimitObjectInput in) \r
+                throws StandardException, IOException\r
+       {\r
+\r
+               long oldversion = 0;            // sanity check\r
+               LogInstant oldLogInstant = null; // sanity check\r
+               if (SanityManager.DEBUG)\r
+               {\r
+                       oldLogInstant = this.page.getLastLogInstant();\r
+                       oldversion = this.page.getPageVersion();\r
+\r
+                       SanityManager.ASSERT(oldversion == this.getPageVersion());\r
+                       SanityManager.ASSERT(oldLogInstant == null || instant == null \r
+                                                        || oldLogInstant.lessThan(instant));\r
+               }\r
+\r
+               // if this is called during runtime rollback, PageOp.generateUndo found\r
+               // the page and have it latched there.\r
+               // if this is called during recovery redo, this.needsRedo found the page and\r
+               // have it latched here\r
+               //\r
+               // in either case, this.page is the correct page and is latched.\r
+               //\r
+               // recordId is generated by generateUndo and is stored here.  If this\r
+               // is a physical undo, recordId is identical to that which is stored in\r
+               // undoOp.  If this is logical undo, it will be different if this.page\r
+               // is different from the undoOp's page (which is where the rollfoward\r
+               // change went to, and the record might have moved by now).\r
+               //\r
+               undoOp.undoMe(xact, this.page, recordId, instant, in);\r
+\r
+               if (SanityManager.DEBUG) {\r
+                       SanityManager.ASSERT(oldversion < this.page.getPageVersion());\r
+                       SanityManager.ASSERT(instant == null || instant.equals(this.page.getLastLogInstant()));\r
+               }\r
+\r
+               releaseResource(xact);\r
+       }\r
+\r
+       /* make sure resource found in undoOp is released */\r
+       public void releaseResource(Transaction xact)\r
+       {\r
+               if (undoOp != null)\r
+                       undoOp.releaseResource(xact);\r
+               super.releaseResource(xact);\r
+       }\r
+\r
+       /* Undo operation is a COMPENSATION log operation */\r
+       public int group()\r
+       {\r
+               return super.group() | Loggable.COMPENSATION | Loggable.RAWSTORE;\r
+       }\r
+\r
+       public final ByteArray getPreparedLog() {\r
+               // should never ever write optional data because this implementation of\r
+               // the recovery system  will never read this and pass this on to dome.\r
+               // Instead, the optional data of the undoOp will be used - since\r
+               // this.doMe can only call undoOP.undoMe, this has no use for any\r
+               // optional data.\r
+               return (ByteArray) null;\r
+       }\r
+\r
+       /**\r
+         DEBUG: Print self.\r
+       */\r
+       public String toString()\r
+       {\r
+               if (SanityManager.DEBUG)\r
+               {\r
+                       String str = "CLR : (logical undo) " + super.toString() + \r
+                               " undoRecordId = " + recordId;\r
+                       if (undoOp != null)\r
+                               str += "\n" + undoOp.toString();\r
+                       else\r
+                               str += " undo Operation not set";\r
+                       return str;     \r
+               }\r
+               else\r
+                       return null;\r
+       }\r
+\r
+}\r