Search in sources :

Example 1 with H2TableDescriptor

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

the class IgniteH2Indexing method rebuildIndexesFromHash.

/** {@inheritDoc} */
@SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
@Override
public void rebuildIndexesFromHash(GridCacheContext cctx, String schemaName, String typeName) throws IgniteCheckedException {
    H2TableDescriptor tbl = tableDescriptor(schemaName, typeName);
    if (tbl == null)
        return;
    assert tbl.table() != null;
    assert tbl.table().rebuildFromHashInProgress();
    H2PkHashIndex hashIdx = tbl.primaryKeyHashIndex();
    Cursor cursor = hashIdx.find((Session) null, null, null);
    while (cursor.next()) {
        CacheDataRow dataRow = (CacheDataRow) cursor.get();
        boolean done = false;
        while (!done) {
            GridCacheEntryEx entry = cctx.cache().entryEx(dataRow.key());
            try {
                synchronized (entry) {
                    // TODO : How to correctly get current value and link here?
                    GridH2Row row = tbl.table().rowDescriptor().createRow(entry.key(), entry.partition(), dataRow.value(), entry.version(), entry.expireTime());
                    row.link(dataRow.link());
                    List<Index> indexes = tbl.table().getAllIndexes();
                    for (int i = 2; i < indexes.size(); i++) {
                        Index idx = indexes.get(i);
                        if (idx instanceof H2TreeIndex)
                            ((H2TreeIndex) idx).put(row);
                    }
                    done = true;
                }
            } catch (GridCacheEntryRemovedException e) {
            // No-op
            }
        }
    }
    tbl.table().markRebuildFromHashInProgress(false);
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.database.CacheDataRow) H2TreeIndex(org.apache.ignite.internal.processors.query.h2.database.H2TreeIndex) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) GridH2Row(org.apache.ignite.internal.processors.query.h2.opt.GridH2Row) 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) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) H2PkHashIndex(org.apache.ignite.internal.processors.query.h2.database.H2PkHashIndex) QueryCursor(org.apache.ignite.cache.query.QueryCursor) Cursor(org.h2.index.Cursor) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor)

Example 2 with H2TableDescriptor

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

the class IgniteH2Indexing method queryLocalSql.

/**
 * Executes regular query.
 *
 * @param schemaName Schema name.
 * @param cacheName Cache name.
 * @param qry Query.
 * @param alias Table alias.
 * @param params Query parameters.
 * @param type Query return type.
 * @param filter Cache name and key filter.
 * @param cancel Cancel object.
 * @return Queried rows.
 * @throws IgniteCheckedException If failed.
 */
@SuppressWarnings("unchecked")
<K, V> GridCloseableIterator<IgniteBiTuple<K, V>> queryLocalSql(String schemaName, String cacheName, final String qry, String alias, @Nullable final Collection<Object> params, String type, final IndexingQueryFilter filter, GridQueryCancel cancel) throws IgniteCheckedException {
    final H2TableDescriptor tbl = tableDescriptor(schemaName, cacheName, type);
    if (tbl == null)
        throw new IgniteSQLException("Failed to find SQL table for type: " + type, IgniteQueryErrorCode.TABLE_NOT_FOUND);
    String sql = generateQuery(qry, alias, tbl);
    Connection conn = connectionForThread(tbl.schemaName());
    H2Utils.setupConnection(conn, false, false);
    GridH2QueryContext.set(new GridH2QueryContext(nodeId, nodeId, 0, LOCAL).filter(filter).distributedJoinMode(OFF));
    GridRunningQueryInfo run = new GridRunningQueryInfo(qryIdGen.incrementAndGet(), qry, SQL, schemaName, U.currentTimeMillis(), null, true);
    runs.put(run.id(), run);
    try {
        ResultSet rs = executeSqlQueryWithTimer(conn, sql, params, true, 0, cancel);
        return new H2KeyValueIterator(rs);
    } finally {
        GridH2QueryContext.clearThreadLocal();
        runs.remove(run.id());
    }
}
Also used : IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) Connection(java.sql.Connection) GridRunningQueryInfo(org.apache.ignite.internal.processors.query.GridRunningQueryInfo) ResultSet(java.sql.ResultSet) IgniteSystemProperties.getString(org.apache.ignite.IgniteSystemProperties.getString) GridH2QueryContext(org.apache.ignite.internal.processors.query.h2.opt.GridH2QueryContext)

Example 3 with H2TableDescriptor

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

the class IgniteH2Indexing 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 void createTable(String schemaName, H2Schema schema, H2TableDescriptor tbl, Connection conn) throws SQLException, IgniteCheckedException {
    assert schema != null;
    assert tbl != null;
    String keyType = dbTypeFromClass(tbl.type().keyClass());
    String valTypeStr = dbTypeFromClass(tbl.type().valueClass());
    SB sql = new SB();
    String keyValVisibility = tbl.type().fields().isEmpty() ? " VISIBLE" : " INVISIBLE";
    sql.a("CREATE TABLE ").a(tbl.fullTableName()).a(" (").a(KEY_FIELD_NAME).a(' ').a(keyType).a(keyValVisibility).a(" NOT NULL");
    sql.a(',').a(VAL_FIELD_NAME).a(' ').a(valTypeStr).a(keyValVisibility);
    sql.a(',').a(VER_FIELD_NAME).a(" OTHER INVISIBLE");
    for (Map.Entry<String, Class<?>> e : tbl.type().fields().entrySet()) sql.a(',').a(H2Utils.withQuotes(e.getKey())).a(' ').a(dbTypeFromClass(e.getValue())).a(tbl.type().property(e.getKey()).notNull() ? " NOT NULL" : "");
    sql.a(')');
    if (log.isDebugEnabled())
        log.debug("Creating DB table with SQL: " + sql);
    GridH2RowDescriptor rowDesc = new GridH2RowDescriptor(this, tbl, tbl.type());
    H2RowFactory rowFactory = tbl.rowFactory(rowDesc);
    GridH2Table h2Tbl = H2TableEngine.createTable(conn, sql.toString(), rowDesc, rowFactory, tbl);
    for (GridH2IndexBase usrIdx : tbl.createUserIndexes()) addInitialUserIndex(schemaName, tbl, usrIdx);
    if (dataTables.putIfAbsent(h2Tbl.identifier(), h2Tbl) != null)
        throw new IllegalStateException("Table already exists: " + h2Tbl.identifierString());
}
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) H2RowFactory(org.apache.ignite.internal.processors.query.h2.database.H2RowFactory) IgniteSystemProperties.getString(org.apache.ignite.IgniteSystemProperties.getString) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) GridBoundedConcurrentLinkedHashMap(org.apache.ignite.internal.util.GridBoundedConcurrentLinkedHashMap) SB(org.apache.ignite.internal.util.typedef.internal.SB)

Example 4 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(GridCacheContext cctx, boolean rmvIdx) {
    rowCache.onCacheUnregistered(cctx);
    String cacheName = cctx.name();
    String schemaName = schema(cacheName);
    H2Schema schema = schemas.get(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 {
                    tbl.table().setRemoveIndexOnDestroy(rmvIdx);
                    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 (!isDefaultSchema(schemaName)) {
            synchronized (schemaMux) {
                if (schema.decrementUsageCount() == 0) {
                    schemas.remove(schemaName);
                    try {
                        dropSchema(schemaName);
                    } catch (IgniteCheckedException e) {
                        U.error(log, "Failed to drop schema on cache stop (will ignore): " + cacheName, e);
                    }
                }
            }
        }
        stmtCache.clear();
        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) 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 5 with H2TableDescriptor

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

the class SchemaManager method onCacheTypeCreated.

/**
 * Registers new class description.
 *
 * @param cacheInfo Cache info.
 * @param idx Indexing.
 * @param type Type descriptor.
 * @param isSql Whether SQL enabled.
 * @throws IgniteCheckedException If failed.
 */
public void onCacheTypeCreated(GridCacheContextInfo cacheInfo, IgniteH2Indexing idx, GridQueryTypeDescriptor type, boolean isSql) throws IgniteCheckedException {
    String schemaName = schemaName(cacheInfo.name());
    H2TableDescriptor tblDesc = new H2TableDescriptor(idx, schemaName, type, cacheInfo, isSql);
    H2Schema schema = schema(schemaName);
    try (H2PooledConnection conn = connMgr.connection(schema.schemaName())) {
        GridH2Table h2tbl = createTable(schema.schemaName(), schema, tblDesc, conn);
        schema.add(tblDesc);
        if (dataTables.putIfAbsent(h2tbl.identifier(), h2tbl) != null)
            throw new IllegalStateException("Table already exists: " + h2tbl.identifierString());
    } catch (SQLException e) {
        throw new IgniteCheckedException("Failed to register query type: " + tblDesc, e);
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)

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