db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From krist...@apache.org
Subject svn commit: r1054588 - /db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/AlterTableConstantAction.java
Date Mon, 03 Jan 2011 12:09:27 GMT
Author: kristwaa
Date: Mon Jan  3 12:09:27 2011
New Revision: 1054588

URL: http://svn.apache.org/viewvc?rev=1054588&view=rev
Log:
DERBY-4937: Enable explicit mode istat daemon code for UPDATE_STATISTICS

Use new/relocated code to serve UPDATE_STATISTICS (ALTER TABLE) requests.
The new code has been written to work similar to the old code, but it is
likely that there is at least one difference (timing of invalidation).
The calculation of the index cardinality statistics is unchanged.

Patch file: derby-4937-1b-explicit_update_stats.diff


Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/AlterTableConstantAction.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/AlterTableConstantAction.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/AlterTableConstantAction.java?rev=1054588&r1=1054587&r2=1054588&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/AlterTableConstantAction.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/AlterTableConstantAction.java
Mon Jan  3 12:09:27 2011
@@ -129,17 +129,6 @@ class AlterTableConstantAction extends D
 	 * whose statistics need to be updated.
 	 */
     private	    String						indexNameForUpdateStatistics;
-    /**
-     * RUNTIME state of the system is maintained in these objects.
-     * rowBufferOne simply reuses the index row prepared by
-     * makeConstantAction. rowBufferTwo is a clone (an extra copy) of
-     * objects. rowBufferCurrent just switches between rowBufferOne and
-     * rowBufferTwo. 
-     */
-    private DataValueDescriptor[][] rowBufferArray;
-    private DataValueDescriptor[] rowBuffer;
-    private DataValueDescriptor[] lastUniqueKey;
-    private static final int GROUP_FETCH_SIZE = 16;
     
     // Alter table compress and Drop column
     private     boolean					    doneScan;
@@ -628,209 +617,19 @@ class AlterTableConstantAction extends D
 	 */
     private void updateStatistics()
             throws StandardException {
-		ConglomerateDescriptor[] cds;
-		long[] conglomerateNumber;
-		ExecIndexRow[] indexRow;
-		UUID[] objectUUID;
-		GroupFetchScanController gsc;
-		//initialize numRows to -1 so we can tell if we scanned an index.	
-		long numRows = -1;		
-		
-		td = dd.getTableDescriptor(tableId);
-		if (updateStatisticsAll)
-		{
-			cds = td.getConglomerateDescriptors();
-		}
-		else
-		{
-			cds = new ConglomerateDescriptor[1];
-			cds[0] = dd.getConglomerateDescriptor(indexNameForUpdateStatistics, sd, false);
-		}
-
-		conglomerateNumber = new long[cds.length];
-		indexRow = new ExecIndexRow[cds.length];
-		objectUUID = new UUID[cds.length];
-		ConglomerateController heapCC =
-			tc.openConglomerate(td.getHeapConglomerateId(), false, 0,
-					TransactionController.MODE_RECORD,
-					TransactionController.ISOLATION_REPEATABLE_READ);
-
-		try
-		{
-			for (int i = 0; i < cds.length; i++)
-			{
-				if (!cds[i].isIndex())
-				{
-					conglomerateNumber[i] = -1;
-					continue;
-				}
-
-				conglomerateNumber[i] = cds[i].getConglomerateNumber();
-
-				objectUUID[i] = cds[i].getUUID();
-
-				indexRow[i] =
-					cds[i].getIndexDescriptor().getNullIndexRow(
-						td.getColumnDescriptorList(),
-						heapCC.newRowLocationTemplate());
-			}
-		}
-		finally
-		{
-			heapCC.close();
-		}
-
-		dd.startWriting(lcc);
-
-		dm.invalidateFor(td, DependencyManager.UPDATE_STATISTICS, lcc);
-
-		for (int indexNumber = 0; indexNumber < conglomerateNumber.length;
-			 indexNumber++)
-		{
-			if (conglomerateNumber[indexNumber] == -1)
-				continue;
-
-			int numCols = indexRow[indexNumber].nColumns() - 1;
-			long[] cardinality = new long[numCols];
-			numRows = 0;
-			initializeRowBuffers(indexRow[indexNumber]);
-
-			/* Read uncommited, with record locking. Actually CS store may
-			   not hold record locks */
-			gsc = 
-				tc.openGroupFetchScan(
-						conglomerateNumber[indexNumber],
-						false,  // hold
-						0,      // openMode: for read
-						TransactionController.MODE_RECORD, // locking
-						TransactionController.ISOLATION_READ_UNCOMMITTED, //isolation level
-						null,   // scancolumnlist-- want everything.
-						null,   // startkeyvalue-- start from the beginning.
-						0,
-						null,   // qualifiers, none!
-						null,   // stopkeyvalue,
-						0);
-
-			try
-			{
-				boolean firstRow = true;
-				int rowsFetched = 0;
-				while ((rowsFetched = gsc.fetchNextGroup(rowBufferArray, null)) > 0)
-				{
-					for (int i = 0; i < rowsFetched; i++)
-					{
-						int whichPositionChanged = compareWithPrevKey(i, firstRow);
-						firstRow = false;
-						if (whichPositionChanged >= 0)
-						{
-							for (int j = whichPositionChanged; j < cardinality.length; j++)
-								cardinality[j]++;
-						}
-						numRows++;
-					}
+        ConglomerateDescriptor[] cds;
+        td = dd.getTableDescriptor(tableId);
 
-					DataValueDescriptor[] tmp;
-					tmp = rowBufferArray[GROUP_FETCH_SIZE - 1];
-					rowBufferArray[GROUP_FETCH_SIZE - 1] = lastUniqueKey;
-					lastUniqueKey = tmp;
-				} // while
-				gsc.setEstimatedRowCount(numRows);
-			} // try
-			finally
-			{
-				gsc.close();
-				gsc = null;
-			}
-
-			if (numRows == 0)
-			{
-				/* if there is no data in the table: no need to write anything
-				 * to sys.sysstatstics
-				 */
-				break;			
-			}
-
-			StatisticsDescriptor statDesc;
-
-			dd.dropStatisticsDescriptors(tableId, objectUUID[indexNumber],
-										 tc);
-
-			for (int i = 0; i < indexRow[indexNumber].nColumns() - 1; i++)
-			{
-				statDesc = new StatisticsDescriptor(dd, dd.getUUIDFactory().createUUID(),
-						objectUUID[indexNumber],
-						tableId,
-						"I",
-						new StatisticsImpl(numRows,
-								cardinality[i]),
-								i + 1);
-				dd.addDescriptor(statDesc, null,
-						DataDictionary.SYSSTATISTICS_CATALOG_NUM,
-								 true, tc);
-			} // for each leading column (c1) (c1,c2)....
-
-		} // for each index.
-
-		// DERBY-4116 if there were indexes we scanned, we now know the row count.
-		// Update statistics should update the store estimated row count for the table.
-		// If we didn't scan an index and don't know, numRows will still be -1 and
-		// we skip the estimatedRowCount update.
-		
-		if (numRows == -1)
-			return;
-		
-		ScanController heapSC = tc.openScan(td.getHeapConglomerateId(),
-				false,  // hold
-				0,      // openMode: for read
-				TransactionController.MODE_RECORD, // locking
-				TransactionController.ISOLATION_READ_UNCOMMITTED, //isolation level
-				null,   // scancolumnlist-- want everything.
-				null,   // startkeyvalue-- start from the beginning.
-				0,
-				null,   // qualifiers, none!
-				null,   // stopkeyvalue,
-				0);
-		
-		try {	
-			heapSC.setEstimatedRowCount(numRows);
-		} finally {			
-			heapSC.close();
-		}
-
-	}
-
-	private void initializeRowBuffers(ExecIndexRow ir)
-	{
-
-		rowBufferArray = new DataValueDescriptor[GROUP_FETCH_SIZE][];
-		lastUniqueKey = ir.getRowArrayClone();
-		rowBufferArray[0] = ir.getRowArray(); // 1 gets old objects.
-	}
-
-  	private int compareWithPrevKey(int index, boolean firstRow)
-  		throws StandardException
-  	{
-  		if (firstRow)
-  			return 0;
-
-  		DataValueDescriptor[] prev = (index == 0) ? lastUniqueKey : rowBufferArray[index - 1];
-  		DataValueDescriptor[] curr = rowBufferArray[index];
-  		// no point trying to do rowlocation; hence - 1
-  		for (int i = 0; i < (prev.length - 1); i++)
-  		{
-			DataValueDescriptor dvd = (DataValueDescriptor)prev[i];
-
-			if (dvd.isNull())
-				return i;// nulls are counted as unique values.
-
-  			if (prev[i].compare(curr[i]) != 0)
-  			{
-  				return i;
-  			}
-  		}
-
-  		return -1;
-  	}
+        if (updateStatisticsAll) {
+            cds = td.getConglomerateDescriptors();
+        } else {
+            cds = new ConglomerateDescriptor[1];
+            cds[0] = dd.getConglomerateDescriptor(
+                    indexNameForUpdateStatistics, sd, false);
+        }
+        dd.getIndexStatsRefresher(false).runExplicitly(
+                                                lcc, td, cds, "ALTER TABLE");
+    }
 
     /**
      * Truncate end of conglomerate.



Mime
View raw message