Java Code Examples for org.apache.phoenix.schema.PTable#getIndexes()

The following examples show how to use org.apache.phoenix.schema.PTable#getIndexes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CsvBulkLoadTool.java    From phoenix with Apache License 2.0 6 votes vote down vote up
/**
 * Get the index tables of current data table
 * @throws java.sql.SQLException
 */
private List<TargetTableRef> getIndexTables(Connection conn, String schemaName, String qualifiedTableName)
    throws SQLException {
    PTable table = PhoenixRuntime.getTable(conn, qualifiedTableName);
    List<TargetTableRef> indexTables = new ArrayList<TargetTableRef>();
    for(PTable indexTable : table.getIndexes()){
        if (indexTable.getIndexType() == IndexType.LOCAL) {
            indexTables.add(
                    new TargetTableRef(getQualifiedTableName(schemaName,
                            indexTable.getTableName().getString()),
                            MetaDataUtil.getLocalIndexTableName(qualifiedTableName)));
        } else {
            indexTables.add(new TargetTableRef(getQualifiedTableName(schemaName,
                    indexTable.getTableName().getString())));
        }
    }
    return indexTables;
}
 
Example 2
Source File: BaseQueryPlan.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private void serializeIndexMaintainerIntoScan(Scan scan, PTable dataTable) throws SQLException {
    PName name = context.getCurrentTable().getTable().getName();
    List<PTable> indexes = Lists.newArrayListWithExpectedSize(1);
    for (PTable index : dataTable.getIndexes()) {
        if (index.getName().equals(name) && index.getIndexType() == IndexType.LOCAL) {
            indexes.add(index);
            break;
        }
    }
    ImmutableBytesWritable ptr = new ImmutableBytesWritable();
    IndexMaintainer.serialize(dataTable, ptr, indexes, context.getConnection());
    scan.setAttribute(BaseScannerRegionObserver.LOCAL_INDEX_BUILD_PROTO, ByteUtil.copyKeyBytesIfNecessary(ptr));
    if (dataTable.isTransactional()) {
        scan.setAttribute(BaseScannerRegionObserver.TX_STATE, context.getConnection().getMutationState().encodeTransaction());
    }
}
 
Example 3
Source File: AbstractBulkLoadTool.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * Get the index tables of current data table
 * @throws java.sql.SQLException
 */
private List<TargetTableRef> getIndexTables(Connection conn, String qualifiedTableName)
        throws SQLException {
    PTable table = PhoenixRuntime.getTable(conn, qualifiedTableName);
    List<TargetTableRef> indexTables = new ArrayList<TargetTableRef>();
    for(PTable indexTable : table.getIndexes()){
        indexTables.add(new TargetTableRef(indexTable.getName().getString(), indexTable
                .getPhysicalName().getString()));
    }
    return indexTables;
}
 
Example 4
Source File: PartialIndexRebuilderIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private static boolean hasIndexWithState(PMetaData metaCache, PTableKey key, PIndexState expectedState) throws TableNotFoundException {
    PTable table = metaCache.getTableRef(key).getTable();
    for (PTable index : table.getIndexes()) {
        if (index.getIndexState() == expectedState) {
            return true;
        }
    }
    return false;
}
 
Example 5
Source File: PartialIndexRebuilderIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private static boolean hasInactiveIndex(PMetaData metaCache, PTableKey key) throws TableNotFoundException {
    PTable table = metaCache.getTableRef(key).getTable();
    for (PTable index : table.getIndexes()) {
        if (index.getIndexState() == PIndexState.INACTIVE) {
            return true;
        }
    }
    return false;
}
 
Example 6
Source File: UpdateCacheAcrossDifferentClientsIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testTableSentWhenIndexStateChanges() throws Throwable {
    // Create connections 1 and 2
    Properties longRunningProps = new Properties(); // Must update config before starting server
    longRunningProps.put(QueryServices.EXTRA_JDBC_ARGUMENTS_ATTRIB,
        QueryServicesOptions.DEFAULT_EXTRA_JDBC_ARGUMENTS);
    longRunningProps.put(QueryServices.DROP_METADATA_ATTRIB, Boolean.TRUE.toString());
    Connection conn1 = DriverManager.getConnection(url, longRunningProps);
    String url2 = url + PhoenixRuntime.JDBC_PROTOCOL_SEPARATOR + "LongRunningQueries";
    Connection conn2 = DriverManager.getConnection(url2, longRunningProps);
    conn1.setAutoCommit(true);
    conn2.setAutoCommit(true);
    try {
        String schemaName = generateUniqueName();
        String tableName = generateUniqueName();
        String indexName = generateUniqueName();
        final String fullTableName = SchemaUtil.getTableName(schemaName, tableName);
        String fullIndexName = SchemaUtil.getTableName(schemaName, indexName);
        conn1.createStatement().execute("CREATE TABLE " + fullTableName + "(k INTEGER PRIMARY KEY, v1 INTEGER, v2 INTEGER) COLUMN_ENCODED_BYTES = 0, STORE_NULLS=true");
        conn1.createStatement().execute("CREATE INDEX " + indexName + " ON " + fullTableName + " (v1) INCLUDE (v2)");
        Table metaTable = conn2.unwrap(PhoenixConnection.class).getQueryServices().getTable(PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME_BYTES);
        IndexUtil.updateIndexState(fullIndexName, 0, metaTable, PIndexState.DISABLE);
        conn2.createStatement().execute("UPSERT INTO " + fullTableName + " VALUES(1,2,3)");
        conn2.commit();
        conn1.createStatement().execute("UPSERT INTO " + fullTableName + " VALUES(4,5,6)");
        conn1.commit();
        PTableKey key = new PTableKey(null,fullTableName);
        PMetaData metaCache = conn1.unwrap(PhoenixConnection.class).getMetaDataCache();
        PTable table = metaCache.getTableRef(key).getTable();
        for (PTable index : table.getIndexes()) {
            assertEquals(PIndexState.DISABLE, index.getIndexState());
        }
    } finally {
        conn1.close();
        conn2.close();
    }
}
 
Example 7
Source File: UpgradeUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private static void syncUpdateCacheFreqForIndexesOfTable(PTable baseTable,
        PreparedStatement stmt) throws SQLException {
    for (PTable index : baseTable.getIndexes()) {
        if (index.getUpdateCacheFrequency() == baseTable.getUpdateCacheFrequency()) {
            continue;
        }
        stmt.setString(2, index.getSchemaName().getString());
        stmt.setString(3, index.getTableName().getString());
        stmt.setLong(4, baseTable.getUpdateCacheFrequency());
        stmt.addBatch();
    }
}
 
Example 8
Source File: UpgradeUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * Synchronize certain properties across column families of global index tables for a given base table
 * @param cqs CQS object to get table descriptor from PTable
 * @param baseTable base table
 * @param defaultColFam column family to be used for synchronizing properties
 * @param syncedProps Map of properties to be kept in sync as read from the default column family descriptor
 * @param tableDescsToSync set of modified table descriptors
 */
private static void syncGlobalIndexesForTable(ConnectionQueryServices cqs, PTable baseTable, ColumnFamilyDescriptor defaultColFam,
        Map<String, Object> syncedProps, Set<TableDescriptor> tableDescsToSync) throws SQLException {
    for (PTable indexTable: baseTable.getIndexes()) {
        // We already handle local index property synchronization when considering all column families of the base table
        if (indexTable.getIndexType() == IndexType.GLOBAL) {
            addTableDescIfPropsChanged(cqs.getTableDescriptor(indexTable.getPhysicalName().getBytes()),
                    defaultColFam, syncedProps, tableDescsToSync);
        }
    }
}
 
Example 9
Source File: SchemaUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static boolean hasGlobalIndex(PTable table) {
    for (PTable index : table.getIndexes()) {
        if (index.getIndexType() == IndexType.GLOBAL) {
            return true;
        }
    }
    return false;
}
 
Example 10
Source File: IndexUpgradeTool.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private boolean extractTablesAndIndexes(PhoenixConnection conn) {
    String [] tables = inputTables.trim().split(",");
    PTable dataTable = null;
    try {
        for (String tableName : tables) {
            HashSet<String> physicalIndexes = new HashSet<>();
            dataTable = PhoenixRuntime.getTableNoCache(conn, tableName);
            String physicalTableName = dataTable.getPhysicalName().getString();
            if (!dataTable.isTransactional() && dataTable.getType().equals(PTableType.TABLE)) {
                for (PTable indexTable : dataTable.getIndexes()) {
                    if (indexTable.getIndexType().equals(PTable.IndexType.GLOBAL)) {
                        String physicalIndexName = indexTable.getPhysicalName().getString();
                        physicalIndexes.add(physicalIndexName);
                    }
                }
                if (MetaDataUtil.hasViewIndexTable(conn, dataTable.getPhysicalName())) {
                    String viewIndexPhysicalName = MetaDataUtil
                            .getViewIndexPhysicalName(physicalTableName);
                    physicalIndexes.add(viewIndexPhysicalName);
                }
                //for upgrade or rollback
                tablesAndIndexes.put(physicalTableName, physicalIndexes);
            } else {
                LOGGER.info("Skipping Table " + tableName + " because it is " +
                        (dataTable.isTransactional() ? "transactional" : "not a data table"));
            }
        }
        return true;
    } catch (SQLException e) {
        LOGGER.severe("Failed to find list of indexes "+e);
        if (dataTable == null) {
            LOGGER.severe("Unable to find the provided data table");
        }
        return false;
    }
}
 
Example 11
Source File: DeleteCompiler.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private Set<PTable> getNonDisabledImmutableIndexes(TableRef tableRef) {
    PTable table = tableRef.getTable();
    if (table.isImmutableRows() && !table.getIndexes().isEmpty()) {
        Set<PTable> nonDisabledIndexes = Sets.newHashSetWithExpectedSize(table.getIndexes().size());
        for (PTable index : table.getIndexes()) {
            if (index.getIndexState() != PIndexState.DISABLE) {
                nonDisabledIndexes.add(index);
            }
        }
        return nonDisabledIndexes;
    }
    return Collections.emptySet();
}
 
Example 12
Source File: DeleteCompiler.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private List<PTable> getClientSideMaintainedIndexes(TableRef tableRef) {
    PTable table = tableRef.getTable();
    if (!table.getIndexes().isEmpty()) {
        List<PTable> nonDisabledIndexes = Lists.newArrayListWithExpectedSize(table.getIndexes().size());
        for (PTable index : table.getIndexes()) {
            if (index.getIndexState() != PIndexState.DISABLE && isMaintainedOnClient(index)) {
                nonDisabledIndexes.add(index);
            }
        }
        return nonDisabledIndexes;
    }
    return Collections.emptyList();
}
 
Example 13
Source File: IndexHalfStoreFileReaderGenerator.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * @param env
 * @param store Local Index store 
 * @param scan
 * @param scanType
 * @param earliestPutTs
 * @param request
 * @return StoreScanner for new Local Index data for a passed store and Null if repair is not possible
 * @throws IOException
 */
private InternalScanner getRepairScanner(RegionCoprocessorEnvironment env, Store store) throws IOException {
    //List<KeyValueScanner> scannersForStoreFiles = Lists.newArrayListWithExpectedSize(store.getStorefilesCount());
    Scan scan = new Scan();
    scan.readVersions(store.getColumnFamilyDescriptor().getMaxVersions());
    for (Store s : env.getRegion().getStores()) {
        if (!IndexUtil.isLocalIndexStore(s)) {
            scan.addFamily(s.getColumnFamilyDescriptor().getName());
        }
    }
    try {
        PhoenixConnection conn = QueryUtil.getConnectionOnServer(env.getConfiguration())
                .unwrap(PhoenixConnection.class);
        PTable dataPTable = IndexUtil.getPDataTable(conn, env.getRegion().getTableDescriptor());
        final List<IndexMaintainer> maintainers = Lists
                .newArrayListWithExpectedSize(dataPTable.getIndexes().size());
        for (PTable index : dataPTable.getIndexes()) {
            if (index.getIndexType() == IndexType.LOCAL) {
                maintainers.add(index.getIndexMaintainer(dataPTable, conn));
            }
        }
        return new DataTableLocalIndexRegionScanner(env.getRegion().getScanner(scan), env.getRegion(),
                maintainers, store.getColumnFamilyDescriptor().getName(),env.getConfiguration());
        

    } catch (SQLException e) {
        throw new IOException(e);

    }
}
 
Example 14
Source File: BaseQueryPlan.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private void serializeIndexMaintainerIntoScan(Scan scan, PTable dataTable) {
    PName name = context.getCurrentTable().getTable().getName();
    List<PTable> indexes = Lists.newArrayListWithExpectedSize(1);
    for (PTable index : dataTable.getIndexes()) {
        if (index.getName().equals(name) && index.getIndexType() == IndexType.LOCAL) {
            indexes.add(index);
            break;
        }
    }
    ImmutableBytesWritable ptr = new ImmutableBytesWritable();
    IndexMaintainer.serialize(dataTable, ptr, indexes, context.getConnection());
    scan.setAttribute(BaseScannerRegionObserver.LOCAL_INDEX_BUILD, ByteUtil.copyKeyBytesIfNecessary(ptr));
}
 
Example 15
Source File: PostLocalIndexDDLCompiler.java    From phoenix with Apache License 2.0 4 votes vote down vote up
public MutationPlan compile(PTable index) throws SQLException {
try (final PhoenixStatement statement = new PhoenixStatement(connection)) {
          String query = "SELECT count(*) FROM " + tableName;
          final QueryPlan plan = statement.compileQuery(query);
          TableRef tableRef = plan.getTableRef();
          Scan scan = plan.getContext().getScan();
          ImmutableBytesWritable ptr = new ImmutableBytesWritable();
          final PTable dataTable = tableRef.getTable();
          List<PTable> indexes = Lists.newArrayListWithExpectedSize(1);
          for (PTable indexTable : dataTable.getIndexes()) {
              if (indexTable.getKey().equals(index.getKey())) {
                  index = indexTable;
                  break;
              }
          }
          // Only build newly created index.
          indexes.add(index);
          IndexMaintainer.serialize(dataTable, ptr, indexes, plan.getContext().getConnection());
          // Set attribute on scan that UngroupedAggregateRegionObserver will switch on.
          // We'll detect that this attribute was set the server-side and write the index
          // rows per region as a result. The value of the attribute will be our persisted
          // index maintainers.
          // Define the LOCAL_INDEX_BUILD as a new static in BaseScannerRegionObserver
          scan.setAttribute(BaseScannerRegionObserver.LOCAL_INDEX_BUILD_PROTO, ByteUtil.copyKeyBytesIfNecessary(ptr));
          // By default, we'd use a FirstKeyOnly filter as nothing else needs to be projected for count(*).
          // However, in this case, we need to project all of the data columns that contribute to the index.
          IndexMaintainer indexMaintainer = index.getIndexMaintainer(dataTable, connection);
          for (ColumnReference columnRef : indexMaintainer.getAllColumns()) {
              if (index.getImmutableStorageScheme() == ImmutableStorageScheme.SINGLE_CELL_ARRAY_WITH_OFFSETS) {
                  scan.addFamily(columnRef.getFamily());
              } else {
                  scan.addColumn(columnRef.getFamily(), columnRef.getQualifier());
              }
          }
          if (dataTable.isTransactional()) {
              scan.setAttribute(BaseScannerRegionObserver.TX_STATE, connection.getMutationState().encodeTransaction());
          }

          // Go through MutationPlan abstraction so that we can create local indexes
          // with a connectionless connection (which makes testing easier).
          return new PostLocalIndexDDLMutationPlan(plan, dataTable);
  }
}
 
Example 16
Source File: IndexMaintainer.java    From phoenix with Apache License 2.0 2 votes vote down vote up
/**
 * For client-side to serialize all IndexMaintainers for a given table
 * @param dataTable data table
 * @param ptr bytes pointer to hold returned serialized value
 */
public static void serialize(PTable dataTable, ImmutableBytesWritable ptr, PhoenixConnection connection) {
    List<PTable> indexes = dataTable.getIndexes();
    serializeServerMaintainedIndexes(dataTable, ptr, indexes, connection);
}
 
Example 17
Source File: IndexMaintainer.java    From phoenix with Apache License 2.0 2 votes vote down vote up
/**
 * For client-side to serialize all IndexMaintainers for a given table
 * @param dataTable data table
 * @param ptr bytes pointer to hold returned serialized value
 */
public static void serialize(PTable dataTable, ImmutableBytesWritable ptr, PhoenixConnection connection) {
    List<PTable> indexes = dataTable.getIndexes();
    serialize(dataTable, ptr, indexes, connection);
}