db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kahat...@apache.org
Subject svn commit: r901597 - in /db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/xact: TransactionTable.java XactXAResourceManager.java
Date Thu, 21 Jan 2010 08:43:30 GMT
Author: kahatlen
Date: Thu Jan 21 08:43:14 2010
New Revision: 901597

URL: http://svn.apache.org/viewvc?rev=901597&view=rev
Log:
DERBY-3092 (partial) Don't expose TransactionTable's Hashtable

Added a visitEntries() method that could be used instead of
getTableForXA() to inspect the contents of the transaction table. Now,
the Hashtable is only visible to the TransactionTable class, which
will make it easier to replace it with another Map implementation
later.

Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/xact/TransactionTable.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/xact/XactXAResourceManager.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/xact/TransactionTable.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/xact/TransactionTable.java?rev=901597&r1=901596&r2=901597&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/xact/TransactionTable.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/xact/TransactionTable.java
Thu Jan 21 08:43:14 2010
@@ -117,7 +117,38 @@
 		return (TransactionTableEntry)trans.get(id);
 	}
 
+    /**
+     * Interface for visiting entries in the transaction table.
+     * @see #visitEntries(EntryVisitor)
+     */
+    static interface EntryVisitor {
+        /**
+         * Visit an entry. {@link #visitEntries(EntryVisitor)} will call this
+         * method once for each entry in the transaction table.
+         *
+         * @param entry the {@code TransactionTableEntry} being visited
+         */
+        void visit(TransactionTableEntry entry);
+    }
 
+    /**
+     * <p>
+     * Visit all the entries in the transaction table.
+     * </p>
+     *
+     * <p>
+     * MT - MT safe
+     * </p>
+     *
+     * @param visitor the visitor to apply on each transaction table entry
+     */
+    void visitEntries(EntryVisitor visitor) {
+        synchronized (trans) {
+            for (Iterator it = trans.values().iterator(); it.hasNext(); ) {
+                visitor.visit((TransactionTableEntry) it.next());
+            }
+        }
+    }
 
 
 	void add(Xact xact, boolean exclude)
@@ -276,27 +307,6 @@
      **************************************************************************
      */
 
-    /**
-     * Return the hash table to the XA layer.
-     * <p>
-     * The XA code will do linear read-only operations on the hash table,
-     * write operations are only done in this module.  It is a little ugly
-     * to export the hash table, but I wanted to move the XA specific code
-     * into the XA module, so that we could configure out the XA code if
-     * necessary.
-     * <p>
-     *
-	 * Must be MT -safe, depends on sync hash table, and must get 
-     *     synchronized(hash_table) for linear searches.
-     *
-	 * @return The ContextManager of the transaction being searched for.
-     *
-     **/
-	public Map getTableForXA()
-	{
-        return(trans);
-	}
-
 	/**
 	    Change transaction to prepared.
 

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/xact/XactXAResourceManager.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/xact/XactXAResourceManager.java?rev=901597&r1=901596&r2=901597&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/xact/XactXAResourceManager.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/store/raw/xact/XactXAResourceManager.java
Thu Jan 21 08:43:14 2010
@@ -37,8 +37,7 @@
 import org.apache.derby.iapi.store.raw.RawStoreFactory;
 import org.apache.derby.iapi.store.raw.Transaction;
 
-import java.util.Iterator;
-import java.util.Map;
+import java.util.ArrayList;
 
 import javax.transaction.xa.Xid;
 import javax.transaction.xa.XAResource;
@@ -244,48 +243,33 @@
 
         if ((flags & XAResource.TMSTARTRSCAN) != 0)
         {
-            Map         trans_hashtable = transaction_table.getTableForXA();
-            XAXactId[]  xid_list        = new XAXactId[trans_hashtable.size()];
-            int         num_prepared    = 0;
-
-            // Need to hold sync while linear searching the hash table.
-            synchronized (trans_hashtable)
-            {
-                int i = 0;
-
-                for (Iterator it = trans_hashtable.values().iterator();
-                     it.hasNext(); i++)
-                {
-                    Xact xact = 
-                        ((TransactionTableEntry) it.next()).getXact();
+            final ArrayList xid_list = new ArrayList();
 
+            // Create a visitor that adds each of the prepared transactions
+            // to xid_list.
+            final TransactionTable.EntryVisitor visitor =
+                    new TransactionTable.EntryVisitor() {
+                public void visit(TransactionTableEntry entry) {
+                    Xact xact = entry.getXact();
                     if (xact.isPrepared())
                     {
                         GlobalTransactionId xa_id = xact.getGlobalId();
 
-                        xid_list[i] = 
+                        xid_list.add(
                             new XAXactId(
                                 xa_id.getFormat_Id(), 
                                 xa_id.getGlobalTransactionId(), 
-                                xa_id.getBranchQualifier());
-                        num_prepared++;
+                                xa_id.getBranchQualifier()));
                     }
                 }
-            }
+            };
 
-            // now need to squish the nulls out of the array to return. 
-            ret_xid_list = new XAXactId[num_prepared];
-            int ret_index = 0;
-            for (int i = xid_list.length; i-- > 0; )
-            {
-                if (xid_list[i] != null)
-                    ret_xid_list[ret_index++] = xid_list[i];
-            }
-
-            if (SanityManager.DEBUG)
-            {
-                SanityManager.ASSERT(ret_index == num_prepared);
-            }
+            // Collect the prepared transactions.
+            transaction_table.visitEntries(visitor);
+
+            // Convert the list to an array suitable for being returned.
+            ret_xid_list = new XAXactId[xid_list.size()];
+            ret_xid_list = (XAXactId[]) xid_list.toArray(ret_xid_list);
         }
         else
         {



Mime
View raw message