Search in sources :

Example 56 with QueryIndex

use of org.apache.ignite.cache.QueryIndex in project ignite by apache.

the class IgniteCacheRandomOperationBenchmark method configureCacheSqlDescriptor.

/**
 * @param cacheName Ignite cache name.
 * @param qryEntity Query entry.
 * @param valCls Class of value.
 * @throws ClassNotFoundException If fail.
 */
private void configureCacheSqlDescriptor(String cacheName, QueryEntity qryEntity, Class valCls) throws ClassNotFoundException {
    List<SqlCacheDescriptor> descs = cacheSqlDescriptors.get(cacheName);
    if (descs == null) {
        descs = new ArrayList<>();
        cacheSqlDescriptors.put(cacheName, descs);
    }
    Map<String, Class> indexedFields = new HashMap<>();
    for (QueryIndex index : qryEntity.getIndexes()) {
        for (String iField : index.getFieldNames()) {
            indexedFields.put(iField, Class.forName(qryEntity.getFields().get(iField)));
        }
    }
    descs.add(new SqlCacheDescriptor(valCls, qryEntity.getFields().keySet(), indexedFields));
}
Also used : HashMap(java.util.HashMap) QueryIndex(org.apache.ignite.cache.QueryIndex)

Example 57 with QueryIndex

use of org.apache.ignite.cache.QueryIndex in project ignite by apache.

the class DatabaseMetadataDialect method index.

/**
 * Create index descriptor.
 *
 * @param idxName Index name.
 * @return New initialized {@code QueryIndex} instance.
 */
protected QueryIndex index(String idxName) {
    QueryIndex idx = new QueryIndex();
    idx.setName(idxName);
    idx.setIndexType(QueryIndexType.SORTED);
    idx.setFields(new LinkedHashMap<String, Boolean>());
    return idx;
}
Also used : VisorQueryIndex(org.apache.ignite.internal.visor.query.VisorQueryIndex) QueryIndex(org.apache.ignite.cache.QueryIndex)

Example 58 with QueryIndex

use of org.apache.ignite.cache.QueryIndex in project ignite by apache.

the class JdbcMetadataDialect method tables.

/**
 * {@inheritDoc}
 */
@Override
public Collection<DbTable> tables(Connection conn, List<String> schemas, boolean tblsOnly) throws SQLException {
    DatabaseMetaData dbMeta = conn.getMetaData();
    Set<String> sys = systemSchemas();
    Collection<String> unsignedTypes = unsignedTypes(dbMeta);
    if (schemas.isEmpty())
        schemas.add(null);
    Collection<DbTable> tbls = new ArrayList<>();
    for (String toSchema : schemas) {
        try (ResultSet tblsRs = dbMeta.getTables(useCatalog() ? toSchema : null, useSchema() ? toSchema : null, "%", tblsOnly ? TABLES_ONLY : TABLES_AND_VIEWS)) {
            while (tblsRs.next()) {
                String tblCatalog = tblsRs.getString(TBL_CATALOG_IDX);
                String tblSchema = tblsRs.getString(TBL_SCHEMA_IDX);
                String tblName = tblsRs.getString(TBL_NAME_IDX);
                // In case of MySql we should use catalog.
                String schema = tblSchema != null ? tblSchema : tblCatalog;
                // Skip system schemas.
                if (sys.contains(schema))
                    continue;
                Set<String> pkCols = new LinkedHashSet<>();
                try (ResultSet pkRs = dbMeta.getPrimaryKeys(tblCatalog, tblSchema, tblName)) {
                    while (pkRs.next()) pkCols.add(pkRs.getString(PK_COL_NAME_IDX));
                }
                Map.Entry<String, Set<String>> uniqueIdxAsPk = null;
                // If PK not found, trying to use first UNIQUE index as key.
                if (pkCols.isEmpty()) {
                    Map<String, Set<String>> uniqueIdxs = new LinkedHashMap<>();
                    try (ResultSet idxRs = dbMeta.getIndexInfo(tblCatalog, tblSchema, tblName, true, true)) {
                        while (idxRs.next()) {
                            String idxName = idxRs.getString(IDX_NAME_IDX);
                            String colName = idxRs.getString(IDX_COL_NAME_IDX);
                            if (idxName == null || colName == null)
                                continue;
                            Set<String> idxCols = uniqueIdxs.get(idxName);
                            if (idxCols == null) {
                                idxCols = new LinkedHashSet<>();
                                uniqueIdxs.put(idxName, idxCols);
                            }
                            idxCols.add(colName);
                        }
                    }
                    uniqueIdxAsPk = uniqueIndexAsPk(uniqueIdxs);
                    if (uniqueIdxAsPk != null)
                        pkCols.addAll(uniqueIdxAsPk.getValue());
                }
                Collection<DbColumn> cols = new ArrayList<>();
                try (ResultSet colsRs = dbMeta.getColumns(tblCatalog, tblSchema, tblName, null)) {
                    while (colsRs.next()) {
                        String colName = colsRs.getString(COL_NAME_IDX);
                        cols.add(new DbColumn(colName, colsRs.getInt(COL_DATA_TYPE_IDX), pkCols.contains(colName), colsRs.getInt(COL_NULLABLE_IDX) == DatabaseMetaData.columnNullable, unsignedTypes.contains(colsRs.getString(COL_TYPE_NAME_IDX))));
                    }
                }
                String uniqueIdxAsPkName = uniqueIdxAsPk != null ? uniqueIdxAsPk.getKey() : null;
                Map<String, QueryIndex> idxs = new LinkedHashMap<>();
                try (ResultSet idxRs = dbMeta.getIndexInfo(tblCatalog, tblSchema, tblName, false, true)) {
                    while (idxRs.next()) {
                        String idxName = idxRs.getString(IDX_NAME_IDX);
                        String colName = idxRs.getString(IDX_COL_NAME_IDX);
                        // Skip {@code null} names and unique index used as PK.
                        if (idxName == null || colName == null || idxName.equals(uniqueIdxAsPkName))
                            continue;
                        QueryIndex idx = idxs.get(idxName);
                        if (idx == null) {
                            idx = index(idxName);
                            idxs.put(idxName, idx);
                        }
                        String askOrDesc = idxRs.getString(IDX_ASC_OR_DESC_IDX);
                        Boolean asc = askOrDesc == null || "A".equals(askOrDesc);
                        idx.getFields().put(colName, asc);
                    }
                }
                // Remove index that is equals to primary key.
                if (!pkCols.isEmpty()) {
                    for (Map.Entry<String, QueryIndex> entry : idxs.entrySet()) {
                        QueryIndex idx = entry.getValue();
                        if (pkCols.equals(idx.getFields().keySet())) {
                            idxs.remove(entry.getKey());
                            break;
                        }
                    }
                }
                tbls.add(table(schema, tblName, cols, idxs.values()));
            }
        }
    }
    return tbls;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DbColumn(org.apache.ignite.console.agent.db.DbColumn) Set(java.util.Set) ResultSet(java.sql.ResultSet) LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) DatabaseMetaData(java.sql.DatabaseMetaData) LinkedHashMap(java.util.LinkedHashMap) ResultSet(java.sql.ResultSet) QueryIndex(org.apache.ignite.cache.QueryIndex) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) DbTable(org.apache.ignite.console.agent.db.DbTable)

Example 59 with QueryIndex

use of org.apache.ignite.cache.QueryIndex in project ignite by apache.

the class OracleMetadataDialect method indexes.

/**
 * Retrieve index columns.
 *
 * @param stmt Prepared SQL statement to execute.
 * @param owner DB owner.
 * @param tbl Table name.
 * @param uniqueIdxAsPk Optional unique index that used as PK.
 * @return Indexes.
 * @throws SQLException If failed to retrieve indexes columns.
 */
private Collection<QueryIndex> indexes(PreparedStatement stmt, String owner, String tbl, String uniqueIdxAsPk) throws SQLException {
    stmt.setString(1, owner);
    stmt.setString(2, tbl);
    Map<String, QueryIndex> idxs = new LinkedHashMap<>();
    try (ResultSet idxsRs = stmt.executeQuery()) {
        while (idxsRs.next()) {
            String idxName = idxsRs.getString(IDX_NAME_IDX);
            // Skip unique index used as PK.
            if (idxName.equals(uniqueIdxAsPk))
                continue;
            QueryIndex idx = idxs.get(idxName);
            if (idx == null) {
                idx = index(idxName);
                idxs.put(idxName, idx);
            }
            String expr = idxsRs.getString(IDX_EXPR_IDX);
            String col = expr == null ? idxsRs.getString(IDX_COL_NAME_IDX) : expr.replaceAll("\"", "");
            idx.getFields().put(col, !"DESC".equals(idxsRs.getString(IDX_COL_DESCEND_IDX)));
        }
    }
    return idxs.values();
}
Also used : ResultSet(java.sql.ResultSet) QueryIndex(org.apache.ignite.cache.QueryIndex) LinkedHashMap(java.util.LinkedHashMap)

Example 60 with QueryIndex

use of org.apache.ignite.cache.QueryIndex in project ignite by apache.

the class AbstractSchemaSelfTest method index.

/**
 * Convenient method for index creation.
 *
 * @param name Name.
 * @param fields Fields.
 * @return Index.
 */
protected static QueryIndex index(String name, IgniteBiTuple<String, Boolean>... fields) {
    QueryIndex idx = new QueryIndex();
    idx.setName(name);
    LinkedHashMap<String, Boolean> fields0 = new LinkedHashMap<>();
    for (IgniteBiTuple<String, Boolean> field : fields) fields0.put(field.getKey(), field.getValue());
    idx.setFields(fields0);
    return idx;
}
Also used : QueryIndex(org.apache.ignite.cache.QueryIndex) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

QueryIndex (org.apache.ignite.cache.QueryIndex)89 QueryEntity (org.apache.ignite.cache.QueryEntity)46 LinkedHashMap (java.util.LinkedHashMap)35 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)26 SchemaOperationException (org.apache.ignite.internal.processors.query.schema.SchemaOperationException)24 ArrayList (java.util.ArrayList)21 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)20 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)14 Ignite (org.apache.ignite.Ignite)13 CacheException (javax.cache.CacheException)10 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)10 IgniteException (org.apache.ignite.IgniteException)9 HashMap (java.util.HashMap)8 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)8 Map (java.util.Map)7 TcpDiscoverySpi (org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi)7 HashSet (java.util.HashSet)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 BinaryObject (org.apache.ignite.binary.BinaryObject)5 CountDownLatch (java.util.concurrent.CountDownLatch)4