1 21 22 package org.apache.derby.impl.sql.execute; 23 24 import org.apache.derby.iapi.services.io.FormatableBitSet; 25 26 import org.apache.derby.iapi.services.io.Storable; 27 28 import org.apache.derby.iapi.error.StandardException; 29 30 import org.apache.derby.iapi.store.access.RowLocationRetRowSource; 31 32 import org.apache.derby.iapi.types.DataValueDescriptor; 33 34 import org.apache.derby.iapi.types.RowLocation; 35 36 43 44 public class CardinalityCounter implements RowLocationRetRowSource 45 { 46 private RowLocationRetRowSource rowSource; 47 private DataValueDescriptor[] prevKey; 48 private long[] cardinality; 49 private long numRows; 50 51 public CardinalityCounter(RowLocationRetRowSource rowSource) 52 { 53 this.rowSource = rowSource; 54 } 55 56 57 public boolean needsRowLocation() 58 { 59 return rowSource.needsRowLocation(); 60 } 61 62 63 public void rowLocation(RowLocation rl) throws StandardException 64 { 65 rowSource.rowLocation(rl); 66 } 67 68 73 public DataValueDescriptor[] getNextRowFromRowSource() throws StandardException 74 { 75 DataValueDescriptor[] nextRow; 76 nextRow = rowSource.getNextRowFromRowSource(); 77 if (nextRow != null) 78 keepCount(nextRow); 79 return nextRow; 80 } 81 82 83 84 public boolean needsToClone() 85 { 86 return rowSource.needsToClone(); 87 } 88 89 90 public FormatableBitSet getValidColumns() 91 { 92 return rowSource.getValidColumns(); 93 } 94 95 96 public void closeRowSource() 97 { 98 rowSource.closeRowSource(); 99 } 100 101 private DataValueDescriptor[] clone(DataValueDescriptor[] clonee) 102 { 103 DataValueDescriptor[] cloned; 104 105 cloned = new DataValueDescriptor[clonee.length]; 106 for (int i = 0; i < clonee.length - 1; i++) 107 { 108 cloned[i] = ((DataValueDescriptor)clonee[i]).getClone(); 109 } 110 return cloned; 111 } 112 113 public void keepCount(DataValueDescriptor[] currentKey) throws StandardException 114 { 115 int numKeys = currentKey.length - 1; numRows++; 117 if (prevKey == null) 118 { 119 prevKey = clone(currentKey); 120 cardinality = new long[currentKey.length - 1]; 121 for (int i = 0; i < numKeys; i++) 122 cardinality[i] = 1; 123 return; 124 } 125 126 int i; 127 for (i = 0; i < numKeys; i++) 128 { 129 if (((DataValueDescriptor)prevKey[i]).isNull()) 130 break; 131 132 if ((prevKey[i]).compare(currentKey[i]) != 0) 133 { 134 prevKey = null; 140 prevKey = clone(currentKey); 141 break; 142 } 143 } 145 for (int j = i; j < numKeys; j++) 146 cardinality[j]++; 147 } 148 149 153 public long[] getCardinality() { return cardinality; } 154 155 159 public long getRowCount() { return numRows; } 160 } 161 | Popular Tags |