Search in sources :

Example 11 with H2TableDescriptor

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

the class IgniteH2Indexing method addInitialUserIndex.

/**
 * Add initial user index.
 *
 * @param schemaName Schema name.
 * @param desc Table descriptor.
 * @param h2Idx User index.
 * @throws IgniteCheckedException If failed.
 */
private void addInitialUserIndex(String schemaName, H2TableDescriptor desc, GridH2IndexBase h2Idx) throws IgniteCheckedException {
    GridH2Table h2Tbl = desc.table();
    h2Tbl.proposeUserIndex(h2Idx);
    try {
        String sql = H2Utils.indexCreateSql(desc.fullTableName(), h2Idx, false);
        executeSql(schemaName, sql);
    } catch (Exception e) {
        // Rollback and re-throw.
        h2Tbl.rollbackUserIndex(h2Idx.getName());
        throw e;
    }
}
Also used : GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) IgniteSystemProperties.getString(org.apache.ignite.IgniteSystemProperties.getString) QueryCancelledException(org.apache.ignite.cache.query.QueryCancelledException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) SqlParseException(org.apache.ignite.internal.sql.SqlParseException) SQLException(java.sql.SQLException) IgniteException(org.apache.ignite.IgniteException) CacheException(javax.cache.CacheException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException)

Example 12 with H2TableDescriptor

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

the class IgniteH2Indexing method dynamicIndexCreate.

/**
 * {@inheritDoc}
 */
@Override
public void dynamicIndexCreate(final String schemaName, final String tblName, final QueryIndexDescriptorImpl idxDesc, boolean ifNotExists, SchemaIndexCacheVisitor cacheVisitor) throws IgniteCheckedException {
    // Locate table.
    H2Schema schema = schemas.get(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);
    h2Tbl.proposeUserIndex(h2Idx);
    try {
        // Populate index with existing cache data.
        final GridH2RowDescriptor rowDesc = h2Tbl.rowDescriptor();
        SchemaIndexCacheVisitorClosure clo = new SchemaIndexCacheVisitorClosure() {

            @Override
            public void apply(CacheDataRow row) throws IgniteCheckedException {
                GridH2Row h2Row = rowDesc.createRow(row);
                h2Idx.putx(h2Row);
            }
        };
        cacheVisitor.visit(clo);
        // 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);
        executeSql(schemaName, sql);
    } catch (Exception e) {
        // Rollback and re-throw.
        h2Tbl.rollbackUserIndex(h2Idx.getName());
        throw e;
    }
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) GridH2IndexBase(org.apache.ignite.internal.processors.query.h2.opt.GridH2IndexBase) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) GridH2Row(org.apache.ignite.internal.processors.query.h2.opt.GridH2Row) SchemaIndexCacheVisitorClosure(org.apache.ignite.internal.processors.query.schema.SchemaIndexCacheVisitorClosure) IgniteSystemProperties.getString(org.apache.ignite.IgniteSystemProperties.getString) QueryCancelledException(org.apache.ignite.cache.query.QueryCancelledException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) SqlParseException(org.apache.ignite.internal.sql.SqlParseException) SQLException(java.sql.SQLException) IgniteException(org.apache.ignite.IgniteException) CacheException(javax.cache.CacheException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException)

Example 13 with H2TableDescriptor

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

the class IgniteH2Indexing method generateFieldsQuery.

/**
 * {@inheritDoc}
 */
@SuppressWarnings("deprecation")
@Override
public SqlFieldsQuery generateFieldsQuery(String cacheName, SqlQuery qry) {
    String schemaName = schema(cacheName);
    String type = qry.getType();
    H2TableDescriptor tblDesc = schemaMgr.tableForType(schemaName, cacheName, type);
    if (tblDesc == null)
        throw new IgniteSQLException("Failed to find SQL table for type: " + type, IgniteQueryErrorCode.TABLE_NOT_FOUND);
    String sql;
    try {
        sql = generateFieldsQueryString(qry.getSql(), qry.getAlias(), tblDesc);
    } catch (IgniteCheckedException e) {
        throw new IgniteException(e);
    }
    SqlFieldsQuery res = QueryUtils.withQueryTimeout(new SqlFieldsQuery(sql), qry.getTimeout(), TimeUnit.MILLISECONDS);
    res.setArgs(qry.getArgs());
    res.setDistributedJoins(qry.isDistributedJoins());
    res.setLocal(qry.isLocal());
    res.setPageSize(qry.getPageSize());
    res.setPartitions(qry.getPartitions());
    res.setReplicatedOnly(qry.isReplicatedOnly());
    res.setSchema(schemaName);
    res.setSql(sql);
    return res;
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) H2Utils.generateFieldsQueryString(org.apache.ignite.internal.processors.query.h2.H2Utils.generateFieldsQueryString) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 14 with H2TableDescriptor

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

the class IgniteH2Indexing method store.

/**
 * {@inheritDoc}
 */
@Override
public void store(GridCacheContext cctx, GridQueryTypeDescriptor type, CacheDataRow row, @Nullable CacheDataRow prevRow, boolean prevRowAvailable) throws IgniteCheckedException {
    String cacheName = cctx.name();
    H2TableDescriptor tbl = schemaMgr.tableForType(schema(cacheName), cacheName, type.name());
    if (tbl == null)
        // Type was rejected.
        return;
    if (tbl.luceneIndex() != null) {
        long expireTime = row.expireTime();
        if (expireTime == 0L)
            expireTime = Long.MAX_VALUE;
        tbl.luceneIndex().store(row.key(), row.value(), row.version(), expireTime);
    }
    tbl.table().update(row, prevRow);
}
Also used : H2Utils.generateFieldsQueryString(org.apache.ignite.internal.processors.query.h2.H2Utils.generateFieldsQueryString)

Example 15 with H2TableDescriptor

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

the class SchemaManager method onCacheDestroyed.

/**
 * Handle cache destroy.
 *
 * @param cacheName Cache name.
 * @param rmvIdx Whether to remove indexes.
 */
public void onCacheDestroyed(String cacheName, boolean rmvIdx) {
    String schemaName = schemaName(cacheName);
    H2Schema schema = schemas.get(schemaName);
    // 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.cacheName(), cacheName)) {
            try {
                tbl.table().setRemoveIndexOnDestroy(rmvIdx);
                dropTable(tbl, rmvIdx);
            } catch (Exception e) {
                U.error(log, "Failed to drop table on cache stop (will ignore): " + tbl.fullTableName(), e);
            }
            schema.drop(tbl);
            rmvTbls.add(tbl);
            GridH2Table h2Tbl = tbl.table();
            dataTables.remove(h2Tbl.identifier(), h2Tbl);
        }
    }
    synchronized (schemaMux) {
        if (schema.decrementUsageCount()) {
            schemas.remove(schemaName);
            try {
                dropSchema(schemaName);
            } catch (Exception 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);
    }
}
Also used : GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) Index(org.h2.index.Index) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet)

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