Skip to content

Renaming performance indexes on upgrade #342

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ public abstract class DatabaseMetaDataProvider implements Schema {

private final Supplier<Map<String, String>> databaseInformation = Suppliers.memoize(this::loadDatabaseInformation);


/**
* @param connection The database connection from which meta data should be provided.
* @param schemaName The name of the schema in which the data is stored. This might be null.
Expand Down Expand Up @@ -677,7 +676,8 @@ protected Table loadTable(AName tableName) {

final Map<AName, Integer> primaryKey = loadTablePrimaryKey(realTableName);
final Supplier<List<Column>> columns = Suppliers.memoize(() -> loadTableColumns(realTableName, primaryKey));
final Supplier<List<Index>> indexes = Suppliers.memoize(() -> loadTableIndexes(realTableName));
final Supplier<List<Index>> indexes = Suppliers.memoize(() -> loadTableIndexes(realTableName, false));
final Supplier<List<Index>> ignoredIndexes = Suppliers.memoize(() -> loadTableIndexes(realTableName, true));

return new Table() {
@Override
Expand All @@ -695,6 +695,11 @@ public List<Index> indexes() {
return indexes.get();
}

@Override
public List<Index> ignoredIndexes() {
return ignoredIndexes.get();
}

@Override
public boolean isTemporary() {
return false;
Expand Down Expand Up @@ -789,10 +794,12 @@ protected static List<Column> createColumnsFrom(Collection<ColumnBuilder> origin
* Loads the indexes for the given table name, except for the primary key index.
*
* @param tableName Name of the table.
* @param returnIgnored if true return ignored indexes, when false return all the non ignored.
* @return List of table indexes.
*/
protected List<Index> loadTableIndexes(RealName tableName) {
protected List<Index> loadTableIndexes(RealName tableName, boolean returnIgnored) {
final Map<RealName, ImmutableList.Builder<RealName>> indexColumns = new HashMap<>();
final Map<RealName, ImmutableList.Builder<RealName>> ignoredIndexColumns = new HashMap<>();
final Map<RealName, Boolean> indexUniqueness = new HashMap<>();

if (log.isTraceEnabled()) log.trace("Reading table indexes for " + tableName);
Expand All @@ -811,10 +818,6 @@ protected List<Index> loadTableIndexes(RealName tableName) {
if (isPrimaryKeyIndex(indexName)) {
continue;
}
if (DatabaseMetaDataProviderUtils.shouldIgnoreIndex(indexName.getDbName())) {
log.info("Ignoring index: ["+indexName.getDbName()+"]");
continue;
}

String dbColumnName = indexResultSet.getString(INDEX_COLUMN_NAME);
String realColumnName = allColumns.get().get(tableName).get(named(dbColumnName)).getName();
Expand All @@ -827,8 +830,7 @@ protected List<Index> loadTableIndexes(RealName tableName) {

indexUniqueness.put(indexName, unique);

indexColumns.computeIfAbsent(indexName, k -> ImmutableList.builder())
.add(columnName);
appendIndexColumn(indexName, columnName, ignoredIndexColumns, indexColumns);
}
catch (SQLException e) {
throw new RuntimeSqlException("Error reading metadata for index ["+indexName+"] on table ["+tableName+"]", e);
Expand All @@ -838,9 +840,15 @@ protected List<Index> loadTableIndexes(RealName tableName) {
long end = System.currentTimeMillis();
if (log.isTraceEnabled()) log.trace(String.format("Read table indexes for %s in %dms; %d indexes; %d unique", tableName, end-start, indexColumns.size(), indexUniqueness.size()));

return indexColumns.entrySet().stream()
if (returnIgnored) {
return ignoredIndexColumns.entrySet().stream()
.map(e -> createIndexFrom(e.getKey(), indexUniqueness.get(e.getKey()), e.getValue().build()))
.collect(Collectors.toList());
} else {
return indexColumns.entrySet().stream()
.map(e -> createIndexFrom(e.getKey(), indexUniqueness.get(e.getKey()), e.getValue().build()))
.collect(Collectors.toList());
}
}
}
catch (SQLException e) {
Expand All @@ -849,6 +857,19 @@ protected List<Index> loadTableIndexes(RealName tableName) {
}


private static void appendIndexColumn(RealName indexName, RealName columnName, Map<RealName,
ImmutableList.Builder<RealName>> ignoredIndexColumns, Map<RealName, ImmutableList.Builder<RealName>> indexColumns)
{
if (DatabaseMetaDataProviderUtils.shouldIgnoreIndex(indexName.getDbName())) {
ignoredIndexColumns.computeIfAbsent(indexName, k -> ImmutableList.builder())
.add(columnName);
} else {
indexColumns.computeIfAbsent(indexName, k -> ImmutableList.builder())
.add(columnName);
}
}


/**
* Retrieves index name from a result set.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,26 @@ public interface TableBuilder extends Table {
public TableBuilder indexes(Iterable<? extends Index> indexes);


/**
* Sets the indexes for the table.
*
* @param ignoredIndexes The ignored indexes to set, probably provided by calls to
* {@link SchemaUtils#index(String)}
* @return this table builder, for method chaining.
*/
public TableBuilder ignoredIndexes(Index... ignoredIndexes);


/**
* Sets the indexes for the table.
*
* @param ignoredIndexes The ignored indexes to set, probably provided by calls to
* {@link SchemaUtils#index(String)}
* @return this table builder, for method chaining.
*/
public TableBuilder ignoredIndexes(Iterable<? extends Index> ignoredIndexes);


/**
* Creates a temporary table.
*
Expand Down Expand Up @@ -653,8 +673,9 @@ private TableBuilderImpl(String name) {
}


private TableBuilderImpl(String name, Iterable<? extends Column> columns, Iterable<? extends Index> indexes, boolean isTemporary) {
super(name, columns, indexes, isTemporary);
private TableBuilderImpl(String name, Iterable<? extends Column> columns, Iterable<? extends Index> indexes,
Iterable<? extends Index> ignoredIndexes, boolean isTemporary) {
super(name, columns, indexes, ignoredIndexes, isTemporary);
}


Expand All @@ -672,7 +693,7 @@ public TableBuilder columns(Column... columns) {
*/
@Override
public TableBuilder columns(Iterable<? extends Column> columns) {
return new TableBuilderImpl(getName(), columns, indexes(), isTemporary());
return new TableBuilderImpl(getName(), columns, indexes(), ignoredIndexes(), isTemporary());
}


Expand All @@ -685,12 +706,30 @@ public TableBuilder indexes(Index... indexes) {
}


/**
* @see org.alfasoftware.morf.metadata.SchemaUtils.TableBuilder#ignoredIndexes(java.lang.Iterable)
*/
@Override
public TableBuilder ignoredIndexes(Iterable<? extends Index> ignoredIndexes) {
return new TableBuilderImpl(getName(), columns(), indexes(), ignoredIndexes, isTemporary());
}


/**
* @see org.alfasoftware.morf.metadata.SchemaUtils.TableBuilder#ignoredIndexes(org.alfasoftware.morf.metadata.Index[])
*/
@Override
public TableBuilder ignoredIndexes(Index... ignoredIndexes) {
return ignoredIndexes(Arrays.asList(ignoredIndexes));
}


/**
* @see org.alfasoftware.morf.metadata.SchemaUtils.TableBuilder#indexes(java.lang.Iterable)
*/
@Override
public TableBuilder indexes(Iterable<? extends Index> indexes) {
return new TableBuilderImpl(getName(), columns(), indexes, isTemporary());
return new TableBuilderImpl(getName(), columns(), indexes, ignoredIndexes(), isTemporary());
}


Expand All @@ -699,7 +738,7 @@ public TableBuilder indexes(Iterable<? extends Index> indexes) {
*/
@Override
public TableBuilder temporary() {
return new TableBuilderImpl(getName(), columns(), indexes(), true);
return new TableBuilderImpl(getName(), columns(), indexes(), ignoredIndexes(), true);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ public default List<Column> primaryKey() {
public List<Index> indexes();


/**
* @return The ignored indexes on this table.
*/
public default List<Index> ignoredIndexes() {
return List.of();
}


/**
* @return Indicates whether the table is temporary
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.util.ArrayList;
import java.util.List;


import com.google.common.collect.Iterables;


Expand All @@ -44,6 +43,11 @@ class TableBean implements Table {
*/
private final List<Index> indexes = new ArrayList<>();

/**
* Stores the ordered list of ignored indexes.
*/
private final List<Index> ignoredIndexes = new ArrayList<>();

/**
* Indicates whether the table is temporary.
*/
Expand Down Expand Up @@ -77,13 +81,16 @@ private TableBean(String tableName, boolean isTemporary) {
* @param tableName Name of the table to represent.
* @param columns Columns for the table
* @param indexes indexes for the table;
* @param ignoredIndexes ignored indexes for the table
* @param isTemporary Whether the table is a temporary table.
*/
TableBean(String tableName, Iterable<? extends Column> columns, Iterable<? extends Index> indexes, boolean isTemporary) {
TableBean(String tableName, Iterable<? extends Column> columns, Iterable<? extends Index> indexes,
Iterable<? extends Index> ignoredIndexes, boolean isTemporary) {
this(tableName, isTemporary);

Iterables.addAll(this.columns, columns);
Iterables.addAll(this.indexes, indexes);
Iterables.addAll(this.ignoredIndexes, ignoredIndexes);
}


Expand All @@ -106,6 +113,10 @@ private TableBean(String tableName, boolean isTemporary) {
for (Index index : toCopy.indexes()) {
indexes.add(new IndexBean(index));
}

for (Index index : toCopy.ignoredIndexes()) {
ignoredIndexes.add(new IndexBean(index));
}
}


Expand Down Expand Up @@ -136,6 +147,15 @@ public List<Index> indexes() {
}


/**
* @see org.alfasoftware.morf.metadata.Table#ignoredIndexes()
*/
@Override
public List<Index> ignoredIndexes() {
return ignoredIndexes;
}


/**
* Finds a column with the specified name
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.alfasoftware.morf.upgrade;

import java.util.Collection;
import java.util.List;

import org.alfasoftware.morf.jdbc.SqlDialect;
import org.alfasoftware.morf.metadata.Index;
import org.alfasoftware.morf.metadata.Schema;
import org.alfasoftware.morf.metadata.SchemaResource;
import org.alfasoftware.morf.metadata.Table;

/**
* Common code between SchemaChangeVisitor implementors
*/
public abstract class AbstractSchemaChangeVisitor implements SchemaChangeVisitor {

protected Schema sourceSchema;
protected SqlDialect sqlDialect;
protected final SchemaResource schemaResource;

protected abstract void writeStatements(Collection<String> statements);


public AbstractSchemaChangeVisitor(Schema sourceSchema, SchemaResource schemaResource, SqlDialect sqlDialect) {
this.sourceSchema = sourceSchema;
this.schemaResource = schemaResource;
this.sqlDialect = sqlDialect;
}


@Override
public void visit(AddIndex addIndex) {
sourceSchema = addIndex.apply(sourceSchema);
Index foundIndex = null;
Table table = schemaResource.getTable(addIndex.getTableName());
if (!table.ignoredIndexes().isEmpty()) {
List<Index> tableIgnoredIndexes = table.ignoredIndexes();
for (Index index : tableIgnoredIndexes) {
if (index.columnNames().equals(addIndex.getNewIndex().columnNames())) {
foundIndex = index;
break;
}
}
}

if (foundIndex != null) {
writeStatements(sqlDialect.renameIndexStatements(sourceSchema.getTable(addIndex.getTableName()), foundIndex.getName(), addIndex.getNewIndex().getName()));
} else {
writeStatements(sqlDialect.addIndexStatements(sourceSchema.getTable(addIndex.getTableName()), addIndex.getNewIndex()));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.alfasoftware.morf.jdbc.ConnectionResources;
import org.alfasoftware.morf.jdbc.SqlDialect;
import org.alfasoftware.morf.metadata.Schema;
import org.alfasoftware.morf.metadata.SchemaResource;
import org.alfasoftware.morf.metadata.Table;
import org.alfasoftware.morf.upgrade.GraphBasedUpgradeSchemaChangeVisitor.GraphBasedUpgradeSchemaChangeVisitorFactory;
import org.alfasoftware.morf.upgrade.GraphBasedUpgradeScriptGenerator.GraphBasedUpgradeScriptGeneratorFactory;
Expand Down Expand Up @@ -97,20 +98,24 @@ public GraphBasedUpgrade prepareGraphBasedUpgrade(List<String> initialisationSql

GraphBasedUpgradeNode root = prepareGraph(nodes);

List<String> preUpgStatements;
List<String> postUpgStatements;
Table idTable = SqlDialect.IdTable.withPrefix(connectionResources.sqlDialect(), "temp_id_", false);
try (SchemaResource schemaResource = connectionResources.openSchemaResource()) {
GraphBasedUpgradeSchemaChangeVisitor visitor = visitorFactory.create(
sourceSchema,
schemaResource,
connectionResources.sqlDialect(),
idTable,
nodes.stream().collect(Collectors.toMap(GraphBasedUpgradeNode::getName, Function.identity())));

GraphBasedUpgradeSchemaChangeVisitor visitor = visitorFactory.create(
sourceSchema,
connectionResources.sqlDialect(),
idTable,
nodes.stream().collect(Collectors.toMap(GraphBasedUpgradeNode::getName, Function.identity())));

GraphBasedUpgradeScriptGenerator scriptGenerator = scriptGeneratorFactory.create(sourceSchema, targetSchema, connectionResources, idTable, viewChanges, initialisationSql);
GraphBasedUpgradeScriptGenerator scriptGenerator = scriptGeneratorFactory.create(sourceSchema, targetSchema, connectionResources, idTable, viewChanges, initialisationSql);

List<String> preUpgStatements = scriptGenerator.generatePreUpgradeStatements();
schemaChangeSequence.applyTo(visitor);
List<String> postUpgStatements = scriptGenerator.generatePostUpgradeStatements();
preUpgStatements = scriptGenerator.generatePreUpgradeStatements();
schemaChangeSequence.applyTo(visitor);
postUpgStatements = scriptGenerator.generatePostUpgradeStatements();

}
if (LOG.isDebugEnabled()) {
logGraph(root);
}
Expand Down
Loading