protected void createIndexesOnColumns ( EntityMetadata m , String tableName , List < Column > columns , Class columnType ) { Object pooledConnection = null ; try { Cassandra . Client api = null ; pooledConnection = getConnection ( ) ; api = ( org . apache . cassandra . thrift . Cassandra . Client ) getConnection ( pooledConnection ) ; KsDef ksDef = api . describe_keyspace ( m . getSchema ( ) ) ; List < CfDef > cfDefs = ksDef . getCf_defs ( ) ; // Column family definition on which secondary index creation is // required CfDef columnFamilyDefToUpdate = null ; boolean isUpdatable = false ; for ( CfDef cfDef : cfDefs ) { if ( cfDef . getName ( ) . equals ( tableName ) ) { columnFamilyDefToUpdate = cfDef ; break ; } } if ( columnFamilyDefToUpdate == null ) { log . error ( "Join table {} not available." , tableName ) ; throw new PersistenceException ( "table" + tableName + " not found!" ) ; } // create a column family, in case it is not already available. // Get list of indexes already created List < ColumnDef > columnMetadataList = columnFamilyDefToUpdate . getColumn_metadata ( ) ; List < String > indexList = new ArrayList < String > ( ) ; if ( columnMetadataList != null ) { for ( ColumnDef columnDef : columnMetadataList ) { indexList . add ( new StringAccessor ( ) . fromBytes ( String . class , columnDef . getName ( ) ) ) ; } // need to set them to null else it is giving problem on update // column family and trying to add again existing indexes. // columnFamilyDefToUpdate.column_metadata = null; } // Iterate over all columns for creating secondary index on them for ( Column column : columns ) { ColumnDef columnDef = new ColumnDef ( ) ; columnDef . setName ( column . getName ( ) ) ; columnDef . setValidation_class ( CassandraValidationClassMapper . getValidationClass ( columnType , false ) ) ; columnDef . setIndex_type ( IndexType . KEYS ) ; // Add secondary index only if it's not already created // (if already created, it would be there in column family // definition) if ( ! indexList . contains ( new StringAccessor ( ) . fromBytes ( String . class , column . getName ( ) ) ) ) { isUpdatable = true ; columnFamilyDefToUpdate . addToColumn_metadata ( columnDef ) ; } } // Finally, update column family with modified column family // definition if ( isUpdatable ) { columnFamilyDefToUpdate . setKey_validation_class ( CassandraValidationClassMapper . getValidationClass ( m . getIdAttribute ( ) . getJavaType ( ) , isCql3Enabled ( m ) ) ) ; api . system_update_column_family ( columnFamilyDefToUpdate ) ; } } catch ( Exception e ) { log . warn ( "Could not create secondary index on column family {}, Caused by: . " , tableName , e ) ; } finally { releaseConnection ( pooledConnection ) ; } }
Creates secondary indexes on columns if not already created .