Search in sources :

Example 1 with DataDictionary

use of org.apache.derby.iapi.sql.dictionary.DataDictionary in project derby by apache.

the class SystemProcedures method SYSCS_INPLACE_COMPRESS_TABLE.

/**
 *    Implementation of SYSCS_UTIL.SYSCS_INPLACE_COMPRESS_TABLE().
 *    <p>
 *    Code which implements the following system procedure:
 *
 *    void SYSCS_UTIL.SYSCS_INPLACE_COMPRESS_TABLE(
 *        IN SCHEMANAME        VARCHAR(128),
 *        IN TABLENAME         VARCHAR(128),
 *        IN PURGE_ROWS        SMALLINT,
 *        IN DEFRAGMENT_ROWS   SMALLINT,
 *        IN TRUNCATE_END      SMALLINT)
 *    <p>
 *    Use the SYSCS_UTIL.SYSCS_INPLACE_COMPRESS_TABLE system procedure to reclaim
 *    unused, allocated space in a table and its indexes. Typically, unused allocated
 *    space exists when a large amount of data is deleted from a table, and there
 *    have not been subsequent inserts to use the space freed by the deletes.
 *    By default, Derby does not return unused space to the operating system. For
 *    example, once a page has been allocated to a table or index, it is not
 *    automatically returned to the operating system until the table or index is
 *    destroyed. SYSCS_UTIL.SYSCS_INPLACE_COMPRESS_TABLE allows you to return unused
 *    space to the operating system.
 *    <p>
 *    This system procedure can be used to force 3 levels of in place compression
 *    of a SQL table: PURGE_ROWS, DEFRAGMENT_ROWS, TRUNCATE_END.  Unlike
 *    SYSCS_UTIL.SYSCS_COMPRESS_TABLE() all work is done in place in the existing
 *    table/index.
 *    <p>
 *    Syntax:
 *    SYSCS_UTIL.SYSCS_INPLACE_COMPRESS_TABLE(
 *        IN SCHEMANAME        VARCHAR(128),
 *        IN TABLENAME         VARCHAR(128),
 *        IN PURGE_ROWS        SMALLINT,
 *        IN DEFRAGMENT_ROWS   SMALLINT,
 *        IN TRUNCATE_END      SMALLINT)
 *    <p>
 *    SCHEMANAME:
 *    An input argument of type VARCHAR(128) that specifies the schema of the table. Passing a null will result in an error.
 *    <p>
 *    TABLENAME:
 *    An input argument of type VARCHAR(128) that specifies the table name of the
 *    table. The string must exactly match the case of the table name, and the
 *    argument of "Fred" will be passed to SQL as the delimited identifier 'Fred'.
 *    Passing a null will result in an error.
 *    <p>
 *    PURGE_ROWS:
 *    If PURGE_ROWS is set to non-zero then a single pass is made through the table
 *    which will purge committed deleted rows from the table.  This space is then
 *    available for future inserted rows, but remains allocated to the table.
 *    As this option scans every page of the table, it's performance is linearly
 *    related to the size of the table.
 *    <p>
 *    DEFRAGMENT_ROWS:
 *    If DEFRAGMENT_ROWS is set to non-zero then a single defragment pass is made
 *    which will move existing rows from the end of the table towards the front
 *    of the table.  The goal of the defragment run is to empty a set of pages
 *    at the end of the table which can then be returned to the OS by the
 *    TRUNCATE_END option.  It is recommended to only run DEFRAGMENT_ROWS, if also
 *    specifying the TRUNCATE_END option.  This option scans the whole table and
 *    needs to update index entries for every base table row move, and thus execution
 *    time is linearly related to the size of the table.
 *    <p>
 *    TRUNCATE_END:
 *    If TRUNCATE_END is set to non-zero then all contiguous pages at the end of
 *    the table will be returned to the OS.  Running the PURGE_ROWS and/or
 *    DEFRAGMENT_ROWS passes options may increase the number of pages affected.
 *    This option itself does no scans of the table, so performs on the order of a
 *    few system calls.
 *    <p>
 *    SQL example:
 *    To compress a table called CUSTOMER in a schema called US, using all
 *    available compress options:
 *    call SYSCS_UTIL.SYSCS_INPLACE_COMPRESS_TABLE('US', 'CUSTOMER', 1, 1, 1);
 *
 *    To quickly just return the empty free space at the end of the same table,
 *    this option will run much quicker than running all phases but will likely
 *    return much less space:
 *    call SYSCS_UTIL.SYSCS_INPLACE_COMPRESS_TABLE('US', 'CUSTOMER', 0, 0, 1);
 *
 *    Java example:
 *    To compress a table called CUSTOMER in a schema called US, using all
 *    available compress options:
 *
 *    CallableStatement cs = conn.prepareCall
 *    ("CALL SYSCS_UTIL.SYSCS_COMPRESS_TABLE(?, ?, ?, ?, ?)");
 *    cs.setString(1, "US");
 *    cs.setString(2, "CUSTOMER");
 *    cs.setShort(3, (short) 1);
 *    cs.setShort(4, (short) 1);
 *    cs.setShort(5, (short) 1);
 *    cs.execute();
 *
 *    To quickly just return the empty free space at the end of the same table,
 *    this option will run much quicker than running all phases but will likely
 *    return much less space:
 *
 *    CallableStatement cs = conn.prepareCall
 *    ("CALL SYSCS_UTIL.SYSCS_COMPRESS_TABLE(?, ?, ?, ?, ?)");
 *    cs.setString(1, "US");
 *    cs.setString(2, "CUSTOMER");
 *    cs.setShort(3, (short) 0);
 *    cs.setShort(4, (short) 0);
 *    cs.setShort(5, (short) 1);
 *    cs.execute();
 *
 *    <p>
 *    It is recommended that the SYSCS_UTIL.SYSCS_COMPRESS_TABLE procedure is
 *    issued in auto-commit mode.
 *    Note: This procedure acquires an exclusive table lock on the table being compressed. All statement plans dependent on the table or its indexes are invalidated. For information on identifying unused space, see the Derby Server and Administration Guide.
 *
 *    TODO LIST:
 *    o defragment requires table level lock in nested user transaction, which
 *      will conflict with user lock on same table in user transaction.
 */
public static void SYSCS_INPLACE_COMPRESS_TABLE(String schema, String tablename, short purgeRows, short defragmentRows, short truncateEnd) throws SQLException {
    // Inplace compress let's the user call compress on VTI but it
    // is really a no-op. In order to avoid having to go throught
    // the ALTER TABLE code just for a no-op, we simply do the check
    // here and return if we are dealing with VTI.
    LanguageConnectionContext lcc = ConnectionUtil.getCurrentLCC();
    TransactionController tc = lcc.getTransactionExecute();
    try {
        DataDictionary data_dictionary = lcc.getDataDictionary();
        SchemaDescriptor sd = data_dictionary.getSchemaDescriptor(schema, tc, true);
        TableDescriptor td = data_dictionary.getTableDescriptor(tablename, sd, tc);
        if (td != null && td.getTableType() == TableDescriptor.VTI_TYPE) {
            return;
        }
    } catch (StandardException se) {
        throw PublicAPI.wrapStandardException(se);
    }
    // Send all the other inplace compress requests to ALTER TABLE
    // machinery
    String escapedSchema = IdUtil.normalToDelimited(schema);
    String escapedTableName = IdUtil.normalToDelimited(tablename);
    String query = "alter table " + escapedSchema + "." + escapedTableName + " compress inplace" + (purgeRows != 0 ? " purge" : "") + (defragmentRows != 0 ? " defragment" : "") + (truncateEnd != 0 ? " truncate_end" : "");
    Connection conn = getDefaultConn();
    PreparedStatement ps = conn.prepareStatement(query);
    ps.executeUpdate();
    ps.close();
    conn.close();
}
Also used : StandardException(org.apache.derby.shared.common.error.StandardException) SchemaDescriptor(org.apache.derby.iapi.sql.dictionary.SchemaDescriptor) LanguageConnectionContext(org.apache.derby.iapi.sql.conn.LanguageConnectionContext) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) TransactionController(org.apache.derby.iapi.store.access.TransactionController) DataDictionary(org.apache.derby.iapi.sql.dictionary.DataDictionary) XPLAINTableDescriptor(org.apache.derby.impl.sql.catalog.XPLAINTableDescriptor) TableDescriptor(org.apache.derby.iapi.sql.dictionary.TableDescriptor)

Example 2 with DataDictionary

use of org.apache.derby.iapi.sql.dictionary.DataDictionary in project derby by apache.

the class SystemProcedures method addUser.

/**
 * Create a new user (this entry is called when bootstrapping the credentials of the DBO
 * at database creation time.
 */
public static void addUser(String userName, String password, TransactionController tc) throws SQLException {
    // 
    try {
        LanguageConnectionContext lcc = ConnectionUtil.getCurrentLCC();
        DataDictionary dd = lcc.getDataDictionary();
        /*
            ** Inform the data dictionary that we are about to write to it.
            ** There are several calls to data dictionary "get" methods here
            ** that might be done in "read" mode in the data dictionary, but
            ** it seemed safer to do this whole operation in "write" mode.
            **
            ** We tell the data dictionary we're done writing at the end of
            ** the transaction.
            */
        dd.startWriting(lcc);
        UserDescriptor userDescriptor = makeUserDescriptor(dd, tc, userName, password);
        dd.addDescriptor(userDescriptor, null, DataDictionary.SYSUSERS_CATALOG_NUM, false, tc);
        // turn on NATIVE::LOCAL authentication
        if (dd.getAuthorizationDatabaseOwner().equals(userName)) {
            tc.setProperty(Property.AUTHENTICATION_PROVIDER_PARAMETER, Property.AUTHENTICATION_PROVIDER_NATIVE_LOCAL, true);
        }
    } catch (StandardException se) {
        throw PublicAPI.wrapStandardException(se);
    }
}
Also used : StandardException(org.apache.derby.shared.common.error.StandardException) LanguageConnectionContext(org.apache.derby.iapi.sql.conn.LanguageConnectionContext) DataDictionary(org.apache.derby.iapi.sql.dictionary.DataDictionary) UserDescriptor(org.apache.derby.iapi.sql.dictionary.UserDescriptor)

Example 3 with DataDictionary

use of org.apache.derby.iapi.sql.dictionary.DataDictionary in project derby by apache.

the class SystemProcedures method resetAuthorizationIDPassword.

/**
 * Reset the password for an already normalized authorization id.
 */
private static void resetAuthorizationIDPassword(String userName, String password) throws SQLException {
    try {
        LanguageConnectionContext lcc = ConnectionUtil.getCurrentLCC();
        DataDictionary dd = lcc.getDataDictionary();
        TransactionController tc = lcc.getTransactionExecute();
        checkLegalUser(dd, userName);
        /*
            ** Inform the data dictionary that we are about to write to it.
            ** There are several calls to data dictionary "get" methods here
            ** that might be done in "read" mode in the data dictionary, but
            ** it seemed safer to do this whole operation in "write" mode.
            **
            ** We tell the data dictionary we're done writing at the end of
            ** the transaction.
            */
        dd.startWriting(lcc);
        UserDescriptor userDescriptor = makeUserDescriptor(dd, tc, userName, password);
        dd.updateUser(userDescriptor, tc);
    } catch (StandardException se) {
        throw PublicAPI.wrapStandardException(se);
    }
}
Also used : StandardException(org.apache.derby.shared.common.error.StandardException) LanguageConnectionContext(org.apache.derby.iapi.sql.conn.LanguageConnectionContext) DataDictionary(org.apache.derby.iapi.sql.dictionary.DataDictionary) TransactionController(org.apache.derby.iapi.store.access.TransactionController) UserDescriptor(org.apache.derby.iapi.sql.dictionary.UserDescriptor)

Example 4 with DataDictionary

use of org.apache.derby.iapi.sql.dictionary.DataDictionary in project derby by apache.

the class DiagUtil method checkAccess.

/**
 * Raise an exception if we are running with SQL authorization turned on
 * but the current user isn't the database owner. This method is used
 * to restrict access to VTIs which disclose sensitive information.
 * See DERBY-5395.
 */
static void checkAccess() throws StandardException {
    LanguageConnectionContext lcc = (LanguageConnectionContext) getContextOrNull(LanguageConnectionContext.CONTEXT_ID);
    DataDictionary dd = lcc.getDataDictionary();
    if (dd.usesSqlAuthorization()) {
        String databaseOwner = dd.getAuthorizationDatabaseOwner();
        String currentUser = lcc.getStatementContext().getSQLSessionContext().getCurrentUser();
        if (!databaseOwner.equals(currentUser)) {
            throw StandardException.newException(SQLState.DBO_ONLY);
        }
    }
}
Also used : LanguageConnectionContext(org.apache.derby.iapi.sql.conn.LanguageConnectionContext) DataDictionary(org.apache.derby.iapi.sql.dictionary.DataDictionary)

Example 5 with DataDictionary

use of org.apache.derby.iapi.sql.dictionary.DataDictionary in project derby by apache.

the class ConglomInfo method getConglomInfo.

private void getConglomInfo(LanguageConnectionContext lcc) throws StandardException {
    DataDictionary dd = lcc.getDataDictionary();
    if (schemaName == null) {
        schemaName = lcc.getCurrentSchemaName();
    }
    ConglomerateDescriptor[] cds;
    if (tableName != null) {
        // if schemaName is null, it gets the default schema
        SchemaDescriptor sd = dd.getSchemaDescriptor(schemaName, tc, true);
        TableDescriptor td = dd.getTableDescriptor(tableName, sd, tc);
        if (// table does not exist
        td == null) {
            // make empty conglom table
            conglomTable = new ConglomInfo[0];
            return;
        }
        cds = td.getConglomerateDescriptors();
    } else // 0-arg constructor, no table name, get all conglomerates
    {
        cds = dd.getConglomerateDescriptors(null);
    }
    // initialize spaceTable
    conglomTable = new ConglomInfo[cds.length];
    for (int i = 0; i < cds.length; i++) {
        String conglomerateName;
        if (cds[i].isIndex()) {
            conglomerateName = cds[i].getConglomerateName();
        } else if (tableName != null) {
            conglomerateName = tableName;
        } else {
            // 0-arg constructor. need to ask data dictionary for name of table
            conglomerateName = dd.getTableDescriptor(cds[i].getTableID()).getName();
        }
        conglomTable[i] = new ConglomInfo(cds[i].getTableID().toString(), cds[i].getConglomerateNumber(), conglomerateName, cds[i].isIndex());
    }
}
Also used : SchemaDescriptor(org.apache.derby.iapi.sql.dictionary.SchemaDescriptor) DataDictionary(org.apache.derby.iapi.sql.dictionary.DataDictionary) ConglomerateDescriptor(org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor) TableDescriptor(org.apache.derby.iapi.sql.dictionary.TableDescriptor)

Aggregations

DataDictionary (org.apache.derby.iapi.sql.dictionary.DataDictionary)102 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)57 TransactionController (org.apache.derby.iapi.store.access.TransactionController)40 SchemaDescriptor (org.apache.derby.iapi.sql.dictionary.SchemaDescriptor)33 TableDescriptor (org.apache.derby.iapi.sql.dictionary.TableDescriptor)23 DependencyManager (org.apache.derby.iapi.sql.depend.DependencyManager)22 ConglomerateDescriptor (org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor)17 StandardException (org.apache.derby.shared.common.error.StandardException)16 UUID (org.apache.derby.catalog.UUID)15 ConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor)15 DataDescriptorGenerator (org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator)13 CompilerContext (org.apache.derby.iapi.sql.compile.CompilerContext)10 AliasDescriptor (org.apache.derby.iapi.sql.dictionary.AliasDescriptor)9 RoleGrantDescriptor (org.apache.derby.iapi.sql.dictionary.RoleGrantDescriptor)8 FormatableBitSet (org.apache.derby.iapi.services.io.FormatableBitSet)7 ConstraintDescriptorList (org.apache.derby.iapi.sql.dictionary.ConstraintDescriptorList)7 ReferencedKeyConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor)7 ColumnDescriptor (org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)6 ConglomerateController (org.apache.derby.iapi.store.access.ConglomerateController)6 Iterator (java.util.Iterator)5