Search in sources :

Example 6 with H2TableDescriptor

use of org.apache.ignite.internal.processors.query.h2.H2TableDescriptor in project ignite by apache.

the class SchemaManager method createIndex.

/**
 * Create index dynamically.
 *
 * @param schemaName Schema name.
 * @param tblName Table name.
 * @param idxDesc Index descriptor.
 * @param ifNotExists If-not-exists.
 * @param cacheVisitor Cache visitor.
 * @throws IgniteCheckedException If failed.
 */
public void createIndex(String schemaName, String tblName, QueryIndexDescriptorImpl idxDesc, boolean ifNotExists, SchemaIndexCacheVisitor cacheVisitor) throws IgniteCheckedException {
    // Locate table.
    H2Schema schema = schema(schemaName);
    H2TableDescriptor desc = (schema != null ? schema.tableByName(tblName) : null);
    if (desc == null)
        throw new IgniteCheckedException("Table not found in internal H2 database [schemaName=" + schemaName + ", tblName=" + tblName + ']');
    GridH2Table h2Tbl = desc.table();
    // Create index.
    final GridH2IndexBase h2Idx = desc.createUserIndex(idxDesc, cacheVisitor);
    h2Tbl.proposeUserIndex(h2Idx);
    try {
        // At this point index is in consistent state, promote it through H2 SQL statement, so that cached
        // prepared statements are re-built.
        String sql = H2Utils.indexCreateSql(desc.fullTableName(), h2Idx, ifNotExists);
        connMgr.executeStatement(schemaName, sql);
    } catch (Exception e) {
        // Rollback and re-throw.
        h2Tbl.rollbackUserIndex(h2Idx.getName());
        throw e;
    }
}
Also used : GridH2IndexBase(org.apache.ignite.internal.processors.query.h2.opt.GridH2IndexBase) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException)

Example 7 with H2TableDescriptor

use of org.apache.ignite.internal.processors.query.h2.H2TableDescriptor in project ignite by apache.

the class SchemaManager method createTable.

/**
 * Create db table by using given table descriptor.
 *
 * @param schemaName Schema name.
 * @param schema Schema.
 * @param tbl Table descriptor.
 * @param conn Connection.
 * @throws SQLException If failed to create db table.
 * @throws IgniteCheckedException If failed.
 */
private GridH2Table createTable(String schemaName, H2Schema schema, H2TableDescriptor tbl, H2PooledConnection conn) throws SQLException, IgniteCheckedException {
    assert schema != null;
    assert tbl != null;
    String sql = H2Utils.tableCreateSql(tbl);
    if (log.isDebugEnabled())
        log.debug("Creating DB table with SQL: " + sql);
    GridH2RowDescriptor rowDesc = new GridH2RowDescriptor(tbl, tbl.type());
    GridH2Table h2Tbl = H2TableEngine.createTable(conn.connection(), sql, rowDesc, tbl, ctx.indexProcessor());
    for (GridH2IndexBase usrIdx : tbl.createUserIndexes()) createInitialUserIndex(schemaName, tbl, usrIdx);
    return h2Tbl;
}
Also used : GridH2IndexBase(org.apache.ignite.internal.processors.query.h2.opt.GridH2IndexBase) GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)

Example 8 with H2TableDescriptor

use of org.apache.ignite.internal.processors.query.h2.H2TableDescriptor in project ignite by apache.

the class IgniteH2Indexing method remove.

/**
 * {@inheritDoc}
 */
@Override
public void remove(GridCacheContext cctx, GridQueryTypeDescriptor type, CacheDataRow row) throws IgniteCheckedException {
    if (log.isDebugEnabled()) {
        log.debug("Removing key from cache query index [locId=" + nodeId + ", key=" + row.key() + ", val=" + row.value() + ']');
    }
    String cacheName = cctx.name();
    H2TableDescriptor tbl = schemaMgr.tableForType(schema(cacheName), cacheName, type.name());
    if (tbl == null)
        return;
    if (tbl.luceneIndex() != null)
        tbl.luceneIndex().remove(row.key());
    tbl.table().remove(row);
}
Also used : H2Utils.generateFieldsQueryString(org.apache.ignite.internal.processors.query.h2.H2Utils.generateFieldsQueryString)

Example 9 with H2TableDescriptor

use of org.apache.ignite.internal.processors.query.h2.H2TableDescriptor in project ignite by apache.

the class IgniteH2Indexing method unregisterCache.

/** {@inheritDoc} */
@Override
public void unregisterCache(String cacheName) {
    String schemaName = schema(cacheName);
    boolean dflt = isDefaultSchema(schemaName);
    H2Schema schema = dflt ? schemas.get(schemaName) : schemas.remove(schemaName);
    if (schema != null) {
        mapQryExec.onCacheStop(cacheName);
        dmlProc.onCacheStop(cacheName);
        // Remove this mapping only after callback to DML proc - it needs that mapping internally
        cacheName2schema.remove(cacheName);
        // Drop tables.
        Collection<H2TableDescriptor> rmvTbls = new HashSet<>();
        for (H2TableDescriptor tbl : schema.tables()) {
            if (F.eq(tbl.cache().name(), cacheName)) {
                try {
                    dropTable(tbl);
                } catch (IgniteCheckedException e) {
                    U.error(log, "Failed to drop table on cache stop (will ignore): " + tbl.fullTableName(), e);
                }
                schema.drop(tbl);
                rmvTbls.add(tbl);
            }
        }
        if (!dflt) {
            try {
                dropSchema(schemaName);
            } catch (IgniteCheckedException e) {
                U.error(log, "Failed to drop schema on cache stop (will ignore): " + cacheName, e);
            }
        }
        for (H2TableDescriptor tbl : rmvTbls) {
            for (Index idx : tbl.table().getIndexes()) idx.close(null);
        }
        int cacheId = CU.cacheId(cacheName);
        for (Iterator<Map.Entry<H2TwoStepCachedQueryKey, H2TwoStepCachedQuery>> it = twoStepCache.entrySet().iterator(); it.hasNext(); ) {
            Map.Entry<H2TwoStepCachedQueryKey, H2TwoStepCachedQuery> e = it.next();
            GridCacheTwoStepQuery qry = e.getValue().query();
            if (!F.isEmpty(qry.cacheIds()) && qry.cacheIds().contains(cacheId))
                it.remove();
        }
    }
}
Also used : GridCacheTwoStepQuery(org.apache.ignite.internal.processors.cache.query.GridCacheTwoStepQuery) Index(org.h2.index.Index) H2TreeIndex(org.apache.ignite.internal.processors.query.h2.database.H2TreeIndex) H2PkHashIndex(org.apache.ignite.internal.processors.query.h2.database.H2PkHashIndex) IgniteSystemProperties.getString(org.apache.ignite.IgniteSystemProperties.getString) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) GridBoundedConcurrentLinkedHashMap(org.apache.ignite.internal.util.GridBoundedConcurrentLinkedHashMap) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Example 10 with H2TableDescriptor

use of org.apache.ignite.internal.processors.query.h2.H2TableDescriptor in project ignite by apache.

the class IgniteH2Indexing method dropTable.

/**
 * Drops table form h2 database and clear all related indexes (h2 text, lucene).
 *
 * @param tbl Table to unregister.
 * @throws IgniteCheckedException If failed to unregister.
 */
private void dropTable(H2TableDescriptor tbl) throws IgniteCheckedException {
    assert tbl != null;
    if (log.isDebugEnabled())
        log.debug("Removing query index table: " + tbl.fullTableName());
    Connection c = connectionForThread(tbl.schemaName());
    Statement stmt = null;
    try {
        stmt = c.createStatement();
        String sql = "DROP TABLE IF EXISTS " + tbl.fullTableName();
        if (log.isDebugEnabled())
            log.debug("Dropping database index table with SQL: " + sql);
        stmt.executeUpdate(sql);
    } catch (SQLException e) {
        onSqlException();
        throw new IgniteSQLException("Failed to drop database index table [type=" + tbl.type().name() + ", table=" + tbl.fullTableName() + "]", IgniteQueryErrorCode.TABLE_DROP_FAILED, e);
    } finally {
        U.close(stmt, log);
    }
}
Also used : SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) PreparedStatement(java.sql.PreparedStatement) JdbcStatement(org.h2.jdbc.JdbcStatement) Statement(java.sql.Statement) GridSqlStatement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlStatement) Connection(java.sql.Connection) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) IgniteSystemProperties.getString(org.apache.ignite.IgniteSystemProperties.getString)

Aggregations

IgniteCheckedException (org.apache.ignite.IgniteCheckedException)9 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)9 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)8 SQLException (java.sql.SQLException)7 IgniteSystemProperties.getString (org.apache.ignite.IgniteSystemProperties.getString)7 IgniteException (org.apache.ignite.IgniteException)6 H2TreeIndex (org.apache.ignite.internal.processors.query.h2.database.H2TreeIndex)4 GridH2IndexBase (org.apache.ignite.internal.processors.query.h2.opt.GridH2IndexBase)4 Index (org.h2.index.Index)4 HashSet (java.util.HashSet)3 LinkedHashSet (java.util.LinkedHashSet)3 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ConcurrentMap (java.util.concurrent.ConcurrentMap)3 H2TableDescriptor (org.apache.ignite.internal.processors.query.h2.H2TableDescriptor)3 H2Utils.generateFieldsQueryString (org.apache.ignite.internal.processors.query.h2.H2Utils.generateFieldsQueryString)3 IgniteH2Indexing (org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing)3 GridH2RowDescriptor (org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor)3 GridBoundedConcurrentLinkedHashMap (org.apache.ignite.internal.util.GridBoundedConcurrentLinkedHashMap)3 Connection (java.sql.Connection)2