db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kahat...@apache.org
Subject svn commit: r481181 - in /db/derby/code/trunk/java/engine/org/apache/derby/iapi/store/raw: ContainerLock.java RowLock.java
Date Fri, 01 Dec 2006 08:16:11 GMT
Author: kahatlen
Date: Fri Dec  1 00:16:09 2006
New Revision: 481181

URL: http://svn.apache.org/viewvc?view=rev&rev=481181
Log:
DERBY-2122: Optimize ContainerLock.isCompatible() and RowLock.isCompatible()

Build a bit mask from the compatibility tables in ContainerLock and
RowLock, and replace look-up in two-dimensional array with bitwise
and.

Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/store/raw/ContainerLock.java
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/store/raw/RowLock.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/store/raw/ContainerLock.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/store/raw/ContainerLock.java?view=diff&rev=481181&r1=481180&r2=481181
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/store/raw/ContainerLock.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/store/raw/ContainerLock.java Fri
Dec  1 00:16:09 2006
@@ -34,10 +34,43 @@
 
 public final class ContainerLock {
 
+	/** Integer representation of the type of the lock. */
 	private final int type;
+	/** Bit mask with one bit set. The position of the bit tells the type of
+	 * the lock. */
+	private final int typeBit;
+	/** Bit mask which represents the lock types that are compatible with this
+	 * lock type. */
+	private final int compat;
+
+	/** Number of types of container locks. */
+	public static final int C_NUMBER = 5;
+
+	/** Container lock compatibility table. */
+	private static final boolean[][] C_COMPAT = {
+
+	//                          Granted
+	// Request \	CIS		CIX		CS		CU		CX        
+	//	
+	/* CIS	*/  {	true,	true,	true,	false,	false    },
+	/* CIX	*/  {	true,	true,	false,	false,	false    },
+	/* CS	*/  {	true,	false,	true,	false,	false    },
+	/* CU	*/	{	false,	false,	true,	false,	false    },
+	/* CX	*/  {	false,	false,	false,	false,	false    }
+
+	};
 
 	private ContainerLock(int type) {
 		this.type = type;
+		typeBit = (1 << type);
+		int bitmask = 0;
+		for (int i = 0; i < C_NUMBER; i++) {
+			// set a bit in bitmask for each compatible lock type
+			if (C_COMPAT[type][i]) {
+				bitmask |= (1 << i);
+			}
+		}
+		compat = bitmask;
 	}
 
     // Names of locks for virtual lock table print out
@@ -54,23 +87,6 @@
 	/** Container Exclusive lock */
 	public static final ContainerLock CX  = new ContainerLock(4);
 
-	/** number of types of container locks */
-	public static final int C_NUMBER = 5;
-
-	/** Container lock compatability table */
-	private static final boolean[][] C_COMPAT = {
-
-	//                          Granted
-	// Request \	CIS		CIX		CS		CU		CX        
-	//	
-	/* CIS	*/  {	true,	true,	true,	false,	false    },
-	/* CIX	*/  {	true,	true,	false,	false,	false    },
-	/* CS	*/  {	true,	false,	true,	false,	false    },
-	/* CU	*/	{	false,	false,	true,	false,	false    },
-	/* CX	*/  {	false,	false,	false,	false,	false    }
-
-	};
-
 	/**
 		Get an integer representation of the type of the lock. This method is guaranteed
 		to return an integer >= 0 and < C_NUMBER. No correlation between the value
@@ -82,13 +98,7 @@
 	}
 
 	public boolean isCompatible(ContainerLock granted) {
-
-		return isCompatible(granted.getType());
-	}
-
-	public boolean isCompatible(int granted) {
-
-		return C_COMPAT[getType()][granted];
+		return (granted.typeBit & compat) != 0;
 	}
 
 	public String toString() {

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/store/raw/RowLock.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/store/raw/RowLock.java?view=diff&rev=481181&r1=481180&r2=481181
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/store/raw/RowLock.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/store/raw/RowLock.java Fri Dec 
1 00:16:09 2006
@@ -34,11 +34,36 @@
 
 public final class RowLock {
 
+	/** Integer representation of the type of the lock. */
 	private final int   type;
+	/** Bit mask with one bit set. The position of the bit tells the type of
+	 * the lock. */
+	private final int typeBit;
+	/** Bit mask which represents the lock types that are compatible with this
+	 * lock type. */
+	private final int compat;
 
     // Names of locks for virtual lock table print out
 	private static String[] shortnames =  { "S", "S", "U", "U", "X", "X", "X", "X" };
 
+	/** Number of row locks. */
+	public static final int R_NUMBER = 8;
+
+	/** Row lock compatibility table. */
+	public static final boolean[][] R_COMPAT = {
+        //          Granted
+        // Request   RS2     RS3    RU2    RU3    RIP    RI     RX2    RX3
+        //
+        /* RS2 */    {true,  true,  true,  true,  true,  false, false, false },
+        /* RS3 */    {true,  true,  true,  true,  false, false, false, false },
+        /* RU2 */    {true,  true,  false, false, true,  false, false, false },
+        /* RU3 */    {true,  true,  false, false, false, false, false, false },
+        /* RIP */    {true,  false, true,  false, true,  true , true,  false },
+        /* RI  */    {false, false, false, false, true,  false, false, false },
+        /* RX2 */    {false, false, false, false, true,  false, false, false },
+        /* RX3 */    {false, false, false, false, false, false, false, false }
+	};
+
 	/* Row Shared lock for repeatable read and below isolation level */
 	public static final RowLock RS2  = new RowLock(0);
 	/* Row Shared lock for serialized read isolation level */
@@ -56,24 +81,6 @@
 	/* Row exclusive write lock for serializable isolation level */
 	public static final RowLock RX3  = new RowLock(7);
 
-	/** Number of row locks */
-	public static final int R_NUMBER = 8;
-
-	/** Row lock compatability table */
-	public static final boolean[][] R_COMPAT = {
-        //          Granted
-        // Request   RS2     RS3    RU2    RU3    RIP    RI     RX2    RX3
-        //
-        /* RS2 */    {true,  true,  true,  true,  true,  false, false, false },
-        /* RS3 */    {true,  true,  true,  true,  false, false, false, false },
-        /* RU2 */    {true,  true,  false, false, true,  false, false, false },
-        /* RU3 */    {true,  true,  false, false, false, false, false, false },
-        /* RIP */    {true,  false, true,  false, true,  true , true,  false },
-        /* RI  */    {false, false, false, false, true,  false, false, false },
-        /* RX2 */    {false, false, false, false, true,  false, false, false },
-        /* RX3 */    {false, false, false, false, false, false, false, false }
-	};
-
     /* lock debugging stuff */
     public static final String DIAG_INDEX       = "index";
     public static final String DIAG_XACTID      = "xactid";
@@ -90,6 +97,15 @@
 
 	private RowLock(int type) {
 		this.type = type;
+		typeBit = (1 << type);
+		int bitmask = 0;
+		for (int i = 0; i < R_NUMBER; i++) {
+			// set a bit in bitmask for each compatible lock type
+			if (R_COMPAT[type][i]) {
+				bitmask |= (1 << i);
+			}
+		}
+		compat = bitmask;
 	}
 
 	/**
@@ -103,13 +119,7 @@
 	}
 
 	public boolean isCompatible(RowLock granted) {
-
-		return isCompatible(granted.getType());
-	}
-
-	public boolean isCompatible(int granted) {
-
-		return R_COMPAT[getType()][granted];
+		return (granted.typeBit & compat) != 0;
 	}
 
 	public String toString()



Mime
View raw message