HashMultimap<Integer,Index> tableToIndexes = HashMultimap.create();
for(Index stub : stubIndexes) {
// Re-look index up as external API previously only relied on names
Table table = ais.getTable(stub.getIndexName().getFullTableName());
String stubName = stub.getIndexName().getName();
final Index index;
switch(stub.getIndexType()) {
case TABLE: index = table.getIndexIncludingInternal(stubName); break;
case FULL_TEXT: index = table.getFullTextIndex(stubName); break;
case GROUP: index = table.getGroup().getIndex(stubName); break;
default:
throw new IllegalStateException(stub.getIndexType().toString());
}
assert (index != null) : stub;
for(Integer tid : index.getAllTableIDs()) {
tableToIndexes.put(tid, index);
}
}
List<ChangeSet> changeSets = new ArrayList<>();
for(Entry<Integer, Collection<Index>> entry : tableToIndexes.asMap().entrySet()) {
Table table = ais.getTable(entry.getKey());
ChangeSet.Builder builder = ChangeSet.newBuilder();
builder.setChangeLevel(ChangeLevel.INDEX.name());
builder.setTableId(table.getTableId());
builder.setOldSchema(table.getName().getSchemaName());
builder.setOldName(table.getName().getTableName());
builder.setNewSchema(table.getName().getSchemaName());
builder.setNewName(table.getName().getTableName());
for(Index index : entry.getValue()) {
TableChanges.IndexChange.Builder indexChange = TableChanges.IndexChange.newBuilder();
indexChange.setChange(ChangeSetHelper.createAddChange(index.getIndexName().getName()));
indexChange.setIndexType(index.getIndexType().name());
builder.addIndexChange(indexChange);
}
changeSets.add(builder.build());
}
return changeSets;