org.apache.manifoldcf.core.database
Class BaseTable

java.lang.Object
  extended by org.apache.manifoldcf.core.database.BaseTable
Direct Known Subclasses:
AgentManager, AuthorityConnectionManager, AuthorityConnectorManager, Carrydown, ConnectorManager, EventManager, HopCount, HopDeleteDeps, HopFilterManager, IncrementalIngester, IntrinsicLink, JobQueue, Jobs, OutputConnectionManager, OutputConnectorManager, PrereqEventManager, RepositoryConnectionManager, RepositoryHistoryManager, ScheduleManager, ThrottleSpecManager

public class BaseTable
extends java.lang.Object

This class is a base class that provides a common foundation for table managers for various different tables in the system.


Field Summary
static java.lang.String _rcsid
           
protected  IDBInterface dbInterface
           
protected  java.lang.String tableName
           
 
Constructor Summary
BaseTable(IDBInterface dbInterface, java.lang.String tableName)
           
 
Method Summary
protected  void addTableIndex(boolean unique, java.util.ArrayList columnList)
          Add an index to a table.
protected  void analyzeTable()
          Analyze this table.
protected  void beginTransaction()
          Begin a database transaction.
 java.lang.String constructDistinctOnClause(java.util.ArrayList outputParameters, java.lang.String baseQuery, java.util.ArrayList baseParameters, java.lang.String[] distinctFields, java.util.Map otherFields)
          Construct a 'distinct on (x)' filter.
 java.lang.String constructOffsetLimitClause(int offset, int limit)
          Construct an offset/limit clause.
 java.lang.String constructRegexpClause(java.lang.String column, java.lang.String regularExpression, boolean caseInsensitive)
          Construct a regular-expression match clause.
 java.lang.String constructSubstringClause(java.lang.String column, java.lang.String regularExpression, boolean caseInsensitive)
          Construct a regular-expression substring clause.
protected  void endTransaction()
          End a database transaction, either performing a commit or a rollback (depending on whether signalRollback() was called within the transaction).
 java.lang.String getDatabaseCacheKey()
           
protected  IDBInterface getDBInterface()
           
protected  int getMaxInClause()
          Obtain the maximum number of individual items that should be present in an IN clause.
protected  int getMaxOrClause()
          Obtain the maximum number of individual clauses that should be present in a sequence of OR clauses.
protected  java.util.Map getTableIndexes(StringSet invalidateKeys, java.lang.String queryClass)
          Get a table's indexes.
 java.lang.String getTableName()
           
protected  java.util.Map getTableSchema(StringSet invalidateKeys, java.lang.String queryClass)
          Get the current table schema.
 java.lang.String getTransactionID()
           
 java.lang.String makeTableKey()
          Construct a key that is database specific, and applies to queries made against a specific table name.
 void noteModifications(int insertCount, int modifyCount, int deleteCount)
          Note a number of inserts, modifications, or deletions to a specific table.
protected  void performAddIndex(java.lang.String indexName, IndexDescription description)
          Add an index to a table.
 void performAlter(java.util.Map columnMap, java.util.Map columnModifyMap, java.util.ArrayList columnDeleteList, StringSet invalidateKeys)
          Perform a table alter operation.
protected  void performCreate(java.util.Map columnMap, StringSet invalidateKeys)
          Perform a table creation operation.
protected  void performDelete(java.lang.String whereClause, java.util.ArrayList whereParameters, StringSet invalidateKeys)
          Perform a delete operation.
protected  void performDrop(StringSet invalidateKeys)
          Perform a table drop operation.
protected  void performInsert(java.util.Map parameterMap, StringSet invalidateKeys)
          Perform an insert operation.
protected  void performLock()
          Perform a table lock operation.
protected  void performModification(java.lang.String query, java.util.ArrayList params, StringSet invalidateKeys)
          Perform a general database modification query.
protected  IResultSet performQuery(java.lang.String query, java.util.ArrayList params, StringSet cacheKeys, java.lang.String queryClass)
          Perform a general "data fetch" query.
protected  IResultSet performQuery(java.lang.String query, java.util.ArrayList params, StringSet cacheKeys, java.lang.String queryClass, int resultLimit)
          Perform a general "data fetch" query, with the ability to provide a limit.
 void performRemoveIndex(java.lang.String indexName)
          Remove an index.
protected  void performUpdate(java.util.Map parameterMap, java.lang.String whereClause, java.util.ArrayList whereParameters, StringSet invalidateKeys)
          Perform an update operation.
static java.util.Map prepareRowForSave(BaseObject object, StringSet fieldSet)
          Read the specified fields from the specified object, and build a Map, which can be used to write the data to the database.
static void readRow(BaseObject object, IResultRow resultRow)
          Set up a base object from a database row.
protected  void reindexTable()
          Reindex this table.
protected  void signalRollback()
          Signal that a rollback should occur on the next endTransaction().
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_rcsid

public static final java.lang.String _rcsid
See Also:
Constant Field Values

dbInterface

protected IDBInterface dbInterface

tableName

protected java.lang.String tableName
Constructor Detail

BaseTable

public BaseTable(IDBInterface dbInterface,
                 java.lang.String tableName)
Method Detail

getDBInterface

protected IDBInterface getDBInterface()

getTableName

public java.lang.String getTableName()

getDatabaseCacheKey

public java.lang.String getDatabaseCacheKey()

getTransactionID

public java.lang.String getTransactionID()

performLock

protected void performLock()
                    throws ManifoldCFException
Perform a table lock operation.

Throws:
ManifoldCFException

performInsert

protected void performInsert(java.util.Map parameterMap,
                             StringSet invalidateKeys)
                      throws ManifoldCFException
Perform an insert operation.

Parameters:
invalidateKeys - are the cache keys that should be invalidated.
parameterMap - is the map of column name/values to write.
Throws:
ManifoldCFException

performUpdate

protected void performUpdate(java.util.Map parameterMap,
                             java.lang.String whereClause,
                             java.util.ArrayList whereParameters,
                             StringSet invalidateKeys)
                      throws ManifoldCFException
Perform an update operation.

Parameters:
invalidateKeys - are the cache keys that should be invalidated.
parameterMap - is the map of column name/values to write.
whereClause - is the where clause describing the match (including the WHERE), or null if none.
whereParameters - are the parameters that come with the where clause, if any.
Throws:
ManifoldCFException

performDelete

protected void performDelete(java.lang.String whereClause,
                             java.util.ArrayList whereParameters,
                             StringSet invalidateKeys)
                      throws ManifoldCFException
Perform a delete operation.

Parameters:
invalidateKeys - are the cache keys that should be invalidated.
whereClause - is the where clause describing the match (including the WHERE), or null if none.
whereParameters - are the parameters that come with the where clause, if any.
Throws:
ManifoldCFException

performCreate

protected void performCreate(java.util.Map columnMap,
                             StringSet invalidateKeys)
                      throws ManifoldCFException
Perform a table creation operation.

Parameters:
columnMap - is the map describing the columns and types. NOTE that these are abstract types, which will be mapped to the proper types for the actual database inside this layer.
invalidateKeys - are the cache keys that should be invalidated, if any.
Throws:
ManifoldCFException

performAlter

public void performAlter(java.util.Map columnMap,
                         java.util.Map columnModifyMap,
                         java.util.ArrayList columnDeleteList,
                         StringSet invalidateKeys)
                  throws ManifoldCFException
Perform a table alter operation.

Parameters:
columnMap - is the map describing the columns and types to add. These are in the same form as for performCreate.
columnModifyMap - is the map describing the columns to modify. These are in the same form as for performCreate.
columnDeleteList - is the list of column names to delete.
invalidateKeys - are the cache keys that should be invalidated, if any.
Throws:
ManifoldCFException

addTableIndex

protected void addTableIndex(boolean unique,
                             java.util.ArrayList columnList)
                      throws ManifoldCFException
Add an index to a table.

Parameters:
unique - is a boolean that if true describes a unique index.
columnList - is the list of columns that need to be included in the index, in order.
Throws:
ManifoldCFException

performAddIndex

protected void performAddIndex(java.lang.String indexName,
                               IndexDescription description)
                        throws ManifoldCFException
Add an index to a table.

Parameters:
indexName - is the optional name of the table index. If null, a name will be chosen automatically.
description - is the index description.
Throws:
ManifoldCFException

performRemoveIndex

public void performRemoveIndex(java.lang.String indexName)
                        throws ManifoldCFException
Remove an index.

Parameters:
indexName - is the name of the index to remove.
Throws:
ManifoldCFException

analyzeTable

protected void analyzeTable()
                     throws ManifoldCFException
Analyze this table.

Throws:
ManifoldCFException

reindexTable

protected void reindexTable()
                     throws ManifoldCFException
Reindex this table.

Throws:
ManifoldCFException

performDrop

protected void performDrop(StringSet invalidateKeys)
                    throws ManifoldCFException
Perform a table drop operation.

Parameters:
invalidateKeys - are the cache keys that should be invalidated, if any.
Throws:
ManifoldCFException

getTableSchema

protected java.util.Map getTableSchema(StringSet invalidateKeys,
                                       java.lang.String queryClass)
                                throws ManifoldCFException
Get the current table schema.

Parameters:
invalidateKeys - are the cache keys, if needed (null if no cache desired).
queryClass - is the LRU class name against which this query would be cached, or null if no LRU behavior desired.
Returns:
a map of column names & ColumnDescription's, or null.
Throws:
ManifoldCFException

getTableIndexes

protected java.util.Map getTableIndexes(StringSet invalidateKeys,
                                        java.lang.String queryClass)
                                 throws ManifoldCFException
Get a table's indexes.

Parameters:
invalidateKeys - are the keys against which to cache the query, or null.
queryClass - is the name of the query class, or null.
Returns:
a map of index names and IndexDescription objects, describing the indexes.
Throws:
ManifoldCFException

performModification

protected void performModification(java.lang.String query,
                                   java.util.ArrayList params,
                                   StringSet invalidateKeys)
                            throws ManifoldCFException
Perform a general database modification query.

Parameters:
query - is the query string.
params - are the parameterized values, if needed.
invalidateKeys - are the cache keys to invalidate.
Throws:
ManifoldCFException

performQuery

protected IResultSet performQuery(java.lang.String query,
                                  java.util.ArrayList params,
                                  StringSet cacheKeys,
                                  java.lang.String queryClass)
                           throws ManifoldCFException
Perform a general "data fetch" query.

Parameters:
query - is the query string.
params - are the parameterized values, if needed.
cacheKeys - are the cache keys, if needed (null if no cache desired).
queryClass - is the LRU class name against which this query would be cached, or null if no LRU behavior desired.
Returns:
a resultset.
Throws:
ManifoldCFException

performQuery

protected IResultSet performQuery(java.lang.String query,
                                  java.util.ArrayList params,
                                  StringSet cacheKeys,
                                  java.lang.String queryClass,
                                  int resultLimit)
                           throws ManifoldCFException
Perform a general "data fetch" query, with the ability to provide a limit.

Parameters:
query - is the query string.
params - are the parameterized values, if needed.
cacheKeys - are the cache keys, if needed (null if no cache desired).
queryClass - is the LRU class name against which this query would be cached, or null if no LRU behavior desired.
resultLimit - is the maximum number of results desired.
Returns:
a resultset.
Throws:
ManifoldCFException

beginTransaction

protected void beginTransaction()
                         throws ManifoldCFException
Begin a database transaction. This method call MUST be paired with an endTransaction() call, or database handles will be lost. If the transaction should be rolled back, then signalRollback() should be called before the transaction is ended. It is strongly recommended that the code that uses transactions be structured so that a try block starts immediately after this method call. The body of the try block will contain all direct or indirect calls to executeQuery(). After this should be a catch for every exception type, including Error, which should call the signalRollback() method, and rethrow the exception. Then, after that a finally{} block which calls endTransaction().

Throws:
ManifoldCFException

signalRollback

protected void signalRollback()
Signal that a rollback should occur on the next endTransaction().


endTransaction

protected void endTransaction()
                       throws ManifoldCFException
End a database transaction, either performing a commit or a rollback (depending on whether signalRollback() was called within the transaction).

Throws:
ManifoldCFException

noteModifications

public void noteModifications(int insertCount,
                              int modifyCount,
                              int deleteCount)
                       throws ManifoldCFException
Note a number of inserts, modifications, or deletions to a specific table. This is so we can decide when to do appropriate maintenance.

Parameters:
tableName - is the name of the table being modified.
insertCount - is the number of inserts.
modifyCount - is the number of updates.
deleteCount - is the number of deletions.
Throws:
ManifoldCFException

makeTableKey

public java.lang.String makeTableKey()
Construct a key that is database specific, and applies to queries made against a specific table name.


constructRegexpClause

public java.lang.String constructRegexpClause(java.lang.String column,
                                              java.lang.String regularExpression,
                                              boolean caseInsensitive)
Construct a regular-expression match clause. This method builds both the text part of a regular-expression match.

Parameters:
column - is the column specifier string.
regularExpression - is the properly-quoted regular expression string, or "?" if a parameterized value is to be used.
caseInsensitive - is true of the regular expression match is to be case insensitive.
Returns:
the query chunk needed, not padded with spaces on either side.

constructSubstringClause

public java.lang.String constructSubstringClause(java.lang.String column,
                                                 java.lang.String regularExpression,
                                                 boolean caseInsensitive)
Construct a regular-expression substring clause. This method builds an expression that extracts a specified string section from a field, based on a regular expression.

Parameters:
column - is the column specifier string.
regularExpression - is the properly-quoted regular expression string, or "?" if a parameterized value is to be used.
caseInsensitive - is true if the regular expression match is to be case insensitive.
Returns:
the expression chunk needed, not padded with spaces on either side.

constructOffsetLimitClause

public java.lang.String constructOffsetLimitClause(int offset,
                                                   int limit)
Construct an offset/limit clause. This method constructs an offset/limit clause in the proper manner for the database in question.

Parameters:
offset - is the starting offset number.
limit - is the limit of result rows to return.
Returns:
the proper clause, with no padding spaces on either side.

constructDistinctOnClause

public java.lang.String constructDistinctOnClause(java.util.ArrayList outputParameters,
                                                  java.lang.String baseQuery,
                                                  java.util.ArrayList baseParameters,
                                                  java.lang.String[] distinctFields,
                                                  java.util.Map otherFields)
Construct a 'distinct on (x)' filter. This filter wraps a query and returns a new query whose results are similar to POSTGRESQL's DISTINCT-ON feature. Specifically, for each combination of the specified distinct fields in the result, only the first such row is included in the final result.

Parameters:
outputParameters - is a blank arraylist into which to put parameters. Null may be used if the baseParameters parameter is null.
baseQuery - is the base query, which can either be tables and where clause, or can be another SELECT in parens, e.g. "(SELECT ...) t3"
baseParameters - are the parameters corresponding to the baseQuery.
distinctFields - are the fields to consider to be distinct.
otherFields - are the rest of the fields to return, keyed by the AS name, value being the column value, e.g. "value AS key"
Returns:
a revised query that performs the necessary DISTINCT ON operation. The arraylist outputParameters will also be appropriately filled in.

getMaxInClause

protected int getMaxInClause()
Obtain the maximum number of individual items that should be present in an IN clause. Exceeding this amount will potentially cause the query performance to drop.

Returns:
the maximum number of IN clause members.

getMaxOrClause

protected int getMaxOrClause()
Obtain the maximum number of individual clauses that should be present in a sequence of OR clauses. Exceeding this amount will potentially cause the query performance to drop.

Returns:
the maximum number of OR clause members.

readRow

public static void readRow(BaseObject object,
                           IResultRow resultRow)
Set up a base object from a database row.

Parameters:
object - is the object to read into.
resultRow - is the row to use to initialize the object.

prepareRowForSave

public static java.util.Map prepareRowForSave(BaseObject object,
                                              StringSet fieldSet)
Read the specified fields from the specified object, and build a Map, which can be used to write the data to the database.

Parameters:
fieldSet - is the set of fields.
object - is the BaseObject to get the data from.
Returns:
the map.