@Override @Async public Future<?> createIndexesAsync( ConcurrentLinkedQueue<AnalyticsIndex> indexes ) { taskLoop : while ( true ) { AnalyticsIndex inx = indexes.poll(); if ( inx == null ) { break taskLoop; } final String indexName = inx.getIndexName( getAnalyticsTableType() ); final String indexType = inx.hasType() ? " using " + inx.getType() : ""; final String indexColumns = StringUtils.join( inx.getColumns(), "," ); final String sql = "create index " + indexName + " on " + inx.getTable() + indexType + " (" + indexColumns + ")"; log.debug( "Create index: " + indexName + " SQL: " + sql ); jdbcTemplate.execute( sql ); log.debug( "Created index: " + indexName ); } return null; }
/** * Returns index name for column. Purpose of code suffix is to avoid uniqueness * collision between indexes for temporary and real tables. * * @param tableType the {@link AnalyticsTableType}. */ public String getIndexName( AnalyticsTableType tableType ) { String columnName = StringUtils.join( this.getColumns(), "_" ); return quote( PREFIX_INDEX + removeQuote( columnName ) + "_" + shortenTableName( this.getTable(), tableType ) + "_" + CodeGenerator.generateCode( 5 ) ); }
/** * Creates indexes on the given analytics tables. * * @param tables the list of {@link AnalyticsTable}. */ private void createIndexes( List<AnalyticsTable> tables ) { List<AnalyticsTablePartition> partitions = PartitionUtils.getTablePartitions( tables ); ConcurrentLinkedQueue<AnalyticsIndex> indexes = new ConcurrentLinkedQueue<>(); for ( AnalyticsTablePartition partition : partitions ) { List<AnalyticsTableColumn> columns = partition.getMasterTable().getDimensionColumns(); for ( AnalyticsTableColumn col : columns ) { if ( !col.isSkipIndex() ) { List<String> indexColumns = col.hasIndexColumns() ? col.getIndexColumns() : Lists.newArrayList( col.getName() ); indexes.add( new AnalyticsIndex( partition.getTempTableName(), indexColumns, col.getIndexType() ) ); } } } log.info( "No of analytics table indexes: " + indexes.size() ); List<Future<?>> futures = new ArrayList<>(); for ( int i = 0; i < getProcessNo(); i++ ) { futures.add( tableManager.createIndexesAsync( indexes ) ); } ConcurrentUtils.waitForCompletion( futures ); }