Changeset 6668


Ignore:
Timestamp:
Dec 18, 2014, 2:49:15 PM (8 years ago)
Author:
Nicklas Nordborg
Message:

References #1742: Upgrade to Hibernate 4.x

Hopefully fixes the third issue. The updateindexes.sh should now detect duplicate indexes and remove all but one of them. It preferes to save the index with the same name as defined in Hibernate.

Location:
branches/3.4-stable/src/core/net/sf/basedb/core
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/3.4-stable/src/core/net/sf/basedb/core/dbengine/TableInfo.java

    r6650 r6668  
    2828import java.sql.Statement;
    2929import java.sql.Types;
     30import java.util.ArrayList;
    3031import java.util.Arrays;
    3132import java.util.Collections;
     
    461462    }
    462463    return null;
     464  }
     465 
     466  /**
     467    Find all indexes with the given set of columns. Used for
     468    getting rid of duplicates.
     469    @param columns A set containing the column names that should be present in
     470      the index
     471    @return A list with the index information or an empty list of no index is found
     472    @since 3.4
     473  */
     474  public List<IndexInfo> findAll(Set<String> columns)
     475  {
     476    List<IndexInfo> all = new ArrayList<IndexInfo>();
     477    for (IndexInfo iiInfo : indexes)
     478    {
     479      if (columns.equals(iiInfo.getColumns())) all.add(iiInfo);
     480    }
     481    return all;
    463482  }
    464483
  • branches/3.4-stable/src/core/net/sf/basedb/core/hibernate/DbIndexWork.java

    r6650 r6668  
    2626import java.sql.SQLException;
    2727import java.sql.Statement;
     28import java.util.HashSet;
    2829import java.util.Iterator;
     30import java.util.List;
     31import java.util.Set;
    2932
    3033import org.hibernate.cfg.Configuration;
     
    242245          }
    243246        }
     247       
     248        List<IndexInfo> existingIndexes = tiDb.findAll(ii.getColumns());
     249        Set<String> duplicateNames = new HashSet<String>();
     250        //String dbName = tiDb.findIndexName(ii.getName(), ii.getColumns());
     251        boolean exists = existingIndexes.size() > 0;
     252        boolean duplicates = existingIndexes.size() > 1;
     253        boolean safeToDrop = exists && tiDb.safeToDrop(ii);
     254 
     255        String dbName = exists ? existingIndexes.get(0).getName() : null;
     256        if (duplicates)
     257        {
     258          // If there are duplicates we find the index with the
     259          // same name as defined by Hibernate or with the shortest name
     260          // and claim that as the 'true' index. The others will be dropped
     261          String shortestName = null;
     262          for (IndexInfo dup : existingIndexes)
     263          {
     264            String name = dup.getName();
     265            duplicateNames.add(name);
     266            if (shortestName == null || name.length() < shortestName.length())
     267            {
     268              shortestName = name;
     269            }
     270          }
    244271         
    245         String dbName = tiDb.findIndexName(ii.getName(), ii.getColumns());
    246         boolean exists = dbName != null;
    247         boolean safeToDrop = exists && tiDb.safeToDrop(ii);
    248  
     272          dbName = duplicateNames.contains(ii.getName()) ? ii.getName() : shortestName;
     273          duplicateNames.remove(dbName);
     274        }
     275       
    249276        String dropSql = exists ?
    250277          dbEngine.getDropIndexSql(catalog, schema,
     
    252279        String createSql =
    253280          dbEngine.getCreateIndexSql(catalog, schema,
    254             table.getName(), table.getName() + "_" + ii.getName(),
     281            table.getName(), ii.getName(),
    255282          ii.getColumns(), ii.isUnique());
    256283        boolean actionDrop = dropIndexes && exists && safeToDrop;
    257284        boolean actionCreate = updateIndexes && (actionDrop || !exists);
     285        boolean actionDropDuplicates = (dropIndexes || updateIndexes) && duplicates;
    258286       
    259287        if (!silent)
    260288        {
    261289          System.out.println("    Exists     : " + exists + (exists ? "(" + dbName + ")" : ""));
     290          System.out.println("    Duplicates : " + duplicates + (duplicates ? "(" + duplicateNames + ")" : ""));
    262291          System.out.println("    Safe drop  : " + safeToDrop);
    263292          System.out.println("    DROP-SQL   : " + dropSql);
    264293          System.out.println("    CREATE-SQL : " + createSql);
    265294          System.out.println("    Actions    : " + (actionDrop ? "DROP " : "") +
    266             (actionCreate ? "CREATE" : ""));
    267         }
    268         if (actionDrop || actionCreate)
     295            (actionCreate ? "CREATE" : "") +
     296            (actionDropDuplicates ? "DROP-DUPLICATES" : ""));
     297        }
     298        if (actionDrop || actionCreate || actionDropDuplicates)
    269299        {
    270300          Statement st = null;
     
    272302          {
    273303            st = connection.createStatement();
     304            if (actionDropDuplicates)
     305            {
     306              for (String dupName : duplicateNames)
     307              {
     308                String dropDupSql = dbEngine.getDropIndexSql(catalog, schema,
     309                    table.getName(), dupName, ii.isUnique());
     310                log.info("Dropping duplicate index: " + dropDupSql);
     311                st.executeUpdate(dropDupSql);
     312              }
     313            }
    274314            if (actionDrop)
    275315            {
     
    308348       
    309349      }
    310      
     350
    311351      if (!silent)
    312352      {
Note: See TracChangeset for help on using the changeset viewer.