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