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;
}
}
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;
}
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);
}
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();
}
}
}
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);
}
}
Aggregations