--- /dev/null
+/*\r
+\r
+ Derby - Class org.apache.derby.iapi.store.raw.RowLock\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.iapi.store.raw;\r
+\r
+/**\r
+ A RowLock represents a qualifier that is to be used when\r
+ locking a Row through a RecordHandle.\r
+\r
+ <BR>\r
+ MT - Immutable\r
+\r
+ @see RecordHandle\r
+ @see LockingPolicy\r
+*/\r
+\r
+public final class RowLock {\r
+\r
+ /** Integer representation of the type of the lock. */\r
+ private final int type;\r
+ /** Bit mask with one bit set. The position of the bit tells the type of\r
+ * the lock. */\r
+ private final int typeBit;\r
+ /** Bit mask which represents the lock types that are compatible with this\r
+ * lock type. */\r
+ private final int compat;\r
+\r
+ // Names of locks for virtual lock table print out\r
+ private static String[] shortnames = { "S", "S", "U", "U", "X", "X", "X", "X" };\r
+\r
+ /** Number of row locks. */\r
+ public static final int R_NUMBER = 8;\r
+\r
+ /** Row lock compatibility table. */\r
+ public static final boolean[][] R_COMPAT = {\r
+ // Granted\r
+ // Request RS2 RS3 RU2 RU3 RIP RI RX2 RX3\r
+ //\r
+ /* RS2 */ {true, true, true, true, true, false, false, false },\r
+ /* RS3 */ {true, true, true, true, false, false, false, false },\r
+ /* RU2 */ {true, true, false, false, true, false, false, false },\r
+ /* RU3 */ {true, true, false, false, false, false, false, false },\r
+ /* RIP */ {true, false, true, false, true, true , true, false },\r
+ /* RI */ {false, false, false, false, true, false, false, false },\r
+ /* RX2 */ {false, false, false, false, true, false, false, false },\r
+ /* RX3 */ {false, false, false, false, false, false, false, false }\r
+ };\r
+\r
+ /* Row Shared lock for repeatable read and below isolation level */\r
+ public static final RowLock RS2 = new RowLock(0);\r
+ /* Row Shared lock for serialized read isolation level */\r
+ public static final RowLock RS3 = new RowLock(1);\r
+ /* Row Update lock for reapeatable read and below isolation level*/\r
+ public static final RowLock RU2 = new RowLock(2);\r
+ /* Row Update lock for serializable isolation level*/\r
+ public static final RowLock RU3 = new RowLock(3);\r
+ /* Row Insert previous key lock */\r
+ public static final RowLock RIP = new RowLock(4);\r
+ /* Row Insert lock */\r
+ public static final RowLock RI = new RowLock(5);\r
+ /* Row exclusive write lock for repeatable read and below isolation level */\r
+ public static final RowLock RX2 = new RowLock(6);\r
+ /* Row exclusive write lock for serializable isolation level */\r
+ public static final RowLock RX3 = new RowLock(7);\r
+\r
+ /* lock debugging stuff */\r
+ public static final String DIAG_INDEX = "index";\r
+ public static final String DIAG_XACTID = "xactid";\r
+ public static final String DIAG_LOCKTYPE = "locktype";\r
+ public static final String DIAG_LOCKMODE = "lockmode";\r
+ public static final String DIAG_CONGLOMID = "conglomId";\r
+ public static final String DIAG_CONTAINERID = "containerId";\r
+ public static final String DIAG_SEGMENTID = "segmentId";\r
+ public static final String DIAG_PAGENUM = "pageNum";\r
+ public static final String DIAG_RECID = "RecId";\r
+ public static final String DIAG_COUNT = "count";\r
+ public static final String DIAG_GROUP = "group";\r
+ public static final String DIAG_STATE = "state";\r
+\r
+ private RowLock(int type) {\r
+ this.type = type;\r
+ typeBit = (1 << type);\r
+ int bitmask = 0;\r
+ for (int i = 0; i < R_NUMBER; i++) {\r
+ // set a bit in bitmask for each compatible lock type\r
+ if (R_COMPAT[type][i]) {\r
+ bitmask |= (1 << i);\r
+ }\r
+ }\r
+ compat = bitmask;\r
+ }\r
+\r
+ /**\r
+ Get an integer representation of the type of the lock. This method is \r
+ guaranteed to return an integer >= 0 and < R_NUMBER. No correlation \r
+ between the value and one of the static variables (CIS etc.) is \r
+ guaranteed, except that the values returned do not change.\r
+ */\r
+ public int getType() {\r
+ return type;\r
+ }\r
+\r
+ public boolean isCompatible(RowLock granted) {\r
+ return (granted.typeBit & compat) != 0;\r
+ }\r
+\r
+ public String toString()\r
+ {\r
+ return shortnames[getType()];\r
+ }\r
+}\r