org.apache.derby.impl.sql.execute
Class CardinalityCounter

java.lang.Object
  extended byorg.apache.derby.impl.sql.execute.CardinalityCounter
All Implemented Interfaces:
RowLocationRetRowSource, RowSource

public class CardinalityCounter
extends java.lang.Object
implements RowLocationRetRowSource

This is a decorator (in Design Patterns Terminology) class to enhance the functionality of a RowLocationRetRowSource. It assumes that the rows are coming in sorted order from the row source and it simply keeps track of the cardinality of all the leading columns.


Field Summary
private  long[] cardinality
           
private  long numRows
           
private  DataValueDescriptor[] prevKey
           
private  RowLocationRetRowSource rowSource
           
 
Constructor Summary
CardinalityCounter(RowLocationRetRowSource rowSource)
           
 
Method Summary
private  DataValueDescriptor[] clone(DataValueDescriptor[] clonee)
           
 void closeRowSource()
          closeRowSource tells the RowSource that it will no longer need to return any rows and it can release any resource it may have.
 long[] getCardinality()
          return the array of cardinalities that are kept internally.
 DataValueDescriptor[] getNextRowFromRowSource()
          Gets next row from the row source and update the count of unique values that are returned.
 long getRowCount()
          get the number of rows seen in the row source thus far.
 FormatableBitSet getValidColumns()
          getValidColumns describes the DataValueDescriptor[] returned by all calls to the getNextRowFromRowSource() call.
 void keepCount(DataValueDescriptor[] currentKey)
           
 boolean needsRowLocation()
          needsRowLocation returns true iff this the row source expects the drainer of the row source to call rowLocation after getting a row from getNextRowFromRowSource.
 boolean needsToClone()
          Does the caller of getNextRowFromRowSource() need to clone the row in order to keep a reference to the row past the getNextRowFromRowSource() call which returned the row.
 void rowLocation(RowLocation rl)
          rowLocation is a callback for the drainer of the row source to return the rowLocation of the current row, i.e, the row that is being returned by getNextRowFromRowSource.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

rowSource

private RowLocationRetRowSource rowSource

prevKey

private DataValueDescriptor[] prevKey

cardinality

private long[] cardinality

numRows

private long numRows
Constructor Detail

CardinalityCounter

public CardinalityCounter(RowLocationRetRowSource rowSource)
Method Detail

needsRowLocation

public boolean needsRowLocation()
Description copied from interface: RowLocationRetRowSource
needsRowLocation returns true iff this the row source expects the drainer of the row source to call rowLocation after getting a row from getNextRowFromRowSource.

Specified by:
needsRowLocation in interface RowLocationRetRowSource
Returns:
true iff this row source expects some row location to be returned
See Also:
RowLocationRetRowSource.needsRowLocation()

rowLocation

public void rowLocation(RowLocation rl)
                 throws StandardException
Description copied from interface: RowLocationRetRowSource
rowLocation is a callback for the drainer of the row source to return the rowLocation of the current row, i.e, the row that is being returned by getNextRowFromRowSource. This interface is for the purpose of loading a base table with index. In that case, the indices can be built at the same time the base table is laid down once the row location of the base row is known. This is an example pseudo code on how this call is expected to be used:
                boolean needsRL = rowSource.needsRowLocation();
                DataValueDescriptor[] row;
                while((row = rowSource.getNextRowFromRowSource()) != null)
                {
                        RowLocation rl = heapConglomerate.insertRow(row);
                        if (needsRL)
                                rowSource.rowLocation(rl);
                }
                

NeedsRowLocation and rowLocation will ONLY be called by a drainer of the row source which CAN return a row location. Drainer of row source which cannot return rowLocation will guarentee to not call either callbacks. Conversely, if NeedsRowLocation is called and it returns true, then for every row return by getNextRowFromRowSource, a rowLocation callback must also be issued with the row location of the row. Implementor of both the source and the drain of the row source must be aware of this protocol.
The RowLocation object is own by the caller of rowLocation, in other words, the drainer of the RowSource. This is so that we don't need to new a row location for every row. If the Row Source wants to keep the row location, it needs to clone it (RowLocation is a ClonableObject).

Specified by:
rowLocation in interface RowLocationRetRowSource
Throws:
StandardException - on error
See Also:
RowLocationRetRowSource.rowLocation(org.apache.derby.iapi.types.RowLocation)

getNextRowFromRowSource

public DataValueDescriptor[] getNextRowFromRowSource()
                                              throws StandardException
Gets next row from the row source and update the count of unique values that are returned.

Specified by:
getNextRowFromRowSource in interface RowSource
Throws:
StandardException - Cloudscape Standard Error Policy
See Also:
RowSource.getNextRowFromRowSource()

needsToClone

public boolean needsToClone()
Description copied from interface: RowSource
Does the caller of getNextRowFromRowSource() need to clone the row in order to keep a reference to the row past the getNextRowFromRowSource() call which returned the row. This call must always return the same for all rows in a RowSource (ie. the caller will call this once per scan from a RowSource and assume the behavior is true for all rows in the RowSource).

Specified by:
needsToClone in interface RowSource
See Also:
RowSource.needsToClone()

getValidColumns

public FormatableBitSet getValidColumns()
Description copied from interface: RowSource
getValidColumns describes the DataValueDescriptor[] returned by all calls to the getNextRowFromRowSource() call. If getValidColumns returns null, the number of columns is given by the DataValueDescriptor.length where DataValueDescriptor[] is returned by the preceeding getNextRowFromRowSource() call. Column N maps to DataValueDescriptor[N], where column numbers start at zero. If getValidColumns return a non null validColumns FormatableBitSet the number of columns is given by the number of bits set in validColumns. Column N is not in the partial row if validColumns.get(N) returns false. Column N is in the partial row if validColumns.get(N) returns true. If column N is in the partial row then it maps to DataValueDescriptor[M] where M is the count of calls to validColumns.get(i) that return true where i < N. If DataValueDescriptor.length is greater than the number of columns indicated by validColumns the extra entries are ignored.

Specified by:
getValidColumns in interface RowSource
See Also:
RowSource.getValidColumns()

closeRowSource

public void closeRowSource()
Description copied from interface: RowSource
closeRowSource tells the RowSource that it will no longer need to return any rows and it can release any resource it may have. Subsequent call to any method on the RowSource will result in undefined behavior. A closed rowSource can be closed again.

Specified by:
closeRowSource in interface RowSource
See Also:
RowSource.closeRowSource()

clone

private DataValueDescriptor[] clone(DataValueDescriptor[] clonee)

keepCount

public void keepCount(DataValueDescriptor[] currentKey)
               throws StandardException
Throws:
StandardException

getCardinality

public long[] getCardinality()
return the array of cardinalities that are kept internally. One value for each leading key; i.e c1, (c1,c2), (c1,c2,c3) etc.

Returns:
an array of unique values.

getRowCount

public long getRowCount()
get the number of rows seen in the row source thus far.

Returns:
total rows seen from the row source.

Built on Mon 2007-06-04 09:58:47+0400, from revision ???

Apache Derby V10.1 Engine Documentation - Copyright © 1997,2005 The Apache Software Foundation or its licensors, as applicable.